Skip to content

Commit 184c8f5

Browse files
Only check whether fft is power of two when not using rustfft-backend (#19)
* Only check fft is power of two when not using rustfft-backend * Add some tests for microfft --------- Co-authored-by: David Maseda Neira <david.masedan@gmail.com>
1 parent 17bc87d commit 184c8f5

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl<T: Float + FromPrimitive + fmt::Debug> StftConfig<T> {
157157
window: WindowType,
158158
reconstruction_mode: ReconstructionMode,
159159
) -> Result<Self, ConfigError<T>> {
160-
if fft_size == 0 || !fft_size.is_power_of_two() {
160+
if fft_size == 0 || !(cfg!(feature = "rustfft-backend") || fft_size.is_power_of_two()) {
161161
return Err(ConfigError::InvalidFftSize);
162162
}
163163
if hop_size == 0 || hop_size > fft_size {
@@ -361,21 +361,21 @@ fn generate_window<T: Float + FromPrimitive>(window_type: WindowType, size: usiz
361361
let half = T::from(0.5).unwrap();
362362
let one = T::one();
363363
let i_t = T::from(i).unwrap();
364-
let size_t = T::from(size).unwrap(); // Use N, not N-1 for periodic window
364+
let size_t = T::from(size).unwrap(); // Use N, not N-1 for periodic window
365365
half * (one - (two * pi * i_t / size_t).cos())
366366
})
367367
.collect(),
368368
WindowType::Hamming => (0..size)
369369
.map(|i| {
370370
let i_t = T::from(i).unwrap();
371-
let size_t = T::from(size).unwrap(); // Use N, not N-1 for periodic window
371+
let size_t = T::from(size).unwrap(); // Use N, not N-1 for periodic window
372372
T::from(0.54).unwrap() - T::from(0.46).unwrap() * (two * pi * i_t / size_t).cos()
373373
})
374374
.collect(),
375375
WindowType::Blackman => (0..size)
376376
.map(|i| {
377377
let i_t = T::from(i).unwrap();
378-
let size_t = T::from(size).unwrap(); // Use N, not N-1 for periodic window
378+
let size_t = T::from(size).unwrap(); // Use N, not N-1 for periodic window
379379
let angle = two * pi * i_t / size_t;
380380
T::from(0.42).unwrap() - T::from(0.5).unwrap() * angle.cos()
381381
+ T::from(0.08).unwrap() * (two * angle).cos()

tests/config_tests.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,30 @@ fn test_config_invalid_ola() {
2121
assert!(config.is_err())
2222
}
2323

24+
#[cfg(not(feature = "rustfft-backend"))]
2425
#[test]
2526
#[allow(deprecated)]
2627
fn test_config_invalid_fft_size() {
2728
let config = StftConfig::<f32>::new(4095, 1024, WindowType::Hann, ReconstructionMode::Ola);
28-
assert!(matches!(config, Err(_)));
29+
assert!(config.is_err());
30+
}
31+
32+
#[cfg(feature = "rustfft-backend")]
33+
#[test]
34+
#[allow(deprecated)]
35+
fn test_config_fft_size_not_power_of_two() {
36+
let config = StftConfig::<f32>::new(4095, 1024, WindowType::Hann, ReconstructionMode::Ola);
37+
assert!(config.is_ok());
2938
}
3039

3140
#[test]
3241
#[allow(deprecated)]
3342
fn test_config_invalid_hop_size() {
3443
let config = StftConfig::<f32>::new(4096, 0, WindowType::Hann, ReconstructionMode::Ola);
35-
assert!(matches!(config, Err(_)));
44+
assert!(config.is_err());
3645

3746
let config = StftConfig::<f32>::new(4096, 5000, WindowType::Hann, ReconstructionMode::Ola);
38-
assert!(matches!(config, Err(_)));
47+
assert!(config.is_err());
3948
}
4049

4150
// Builder pattern tests
@@ -75,20 +84,17 @@ fn test_builder_with_reconstruction_mode() {
7584

7685
#[test]
7786
fn test_builder_missing_fft_size() {
78-
let config = StftConfig::<f32>::builder()
79-
.hop_size(1024)
80-
.build();
87+
let config = StftConfig::<f32>::builder().hop_size(1024).build();
8188
assert!(config.is_err());
8289
}
8390

8491
#[test]
8592
fn test_builder_missing_hop_size() {
86-
let config = StftConfig::<f32>::builder()
87-
.fft_size(4096)
88-
.build();
93+
let config = StftConfig::<f32>::builder().fft_size(4096).build();
8994
assert!(config.is_err());
9095
}
9196

97+
#[cfg(not(feature = "rustfft-backend"))]
9298
#[test]
9399
fn test_builder_invalid_fft_size() {
94100
let config = StftConfig::<f32>::builder()
@@ -98,6 +104,16 @@ fn test_builder_invalid_fft_size() {
98104
assert!(config.is_err());
99105
}
100106

107+
#[cfg(feature = "rustfft-backend")]
108+
#[test]
109+
fn test_builder_fft_size_not_power_of_two() {
110+
let config = StftConfig::<f32>::builder()
111+
.fft_size(4095) // Not a power of 2
112+
.hop_size(1024)
113+
.build();
114+
assert!(config.is_ok());
115+
}
116+
101117
#[test]
102118
fn test_builder_invalid_hop_size() {
103119
let config = StftConfig::<f32>::builder()

0 commit comments

Comments
 (0)