Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl<T: Float + FromPrimitive + fmt::Debug> StftConfig<T> {
window: WindowType,
reconstruction_mode: ReconstructionMode,
) -> Result<Self, ConfigError<T>> {
if fft_size == 0 || !fft_size.is_power_of_two() {
if fft_size == 0 || !(cfg!(feature = "rustfft-backend") || fft_size.is_power_of_two()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While logically correct, this condition can be made more readable. By applying De Morgan's law, you can express the logic without negating a grouped expression, which is often easier to reason about. The rewritten form !cfg!(feature = "rustfft-backend") && !fft_size.is_power_of_two() makes it immediately clear that the power-of-two constraint applies only when not using the rustfft-backend.

Suggested change
if fft_size == 0 || !(cfg!(feature = "rustfft-backend") || fft_size.is_power_of_two()) {
if fft_size == 0 || !cfg!(feature = "rustfft-backend") && !fft_size.is_power_of_two() {

return Err(ConfigError::InvalidFftSize);
}
if hop_size == 0 || hop_size > fft_size {
Expand Down Expand Up @@ -361,21 +361,21 @@ fn generate_window<T: Float + FromPrimitive>(window_type: WindowType, size: usiz
let half = T::from(0.5).unwrap();
let one = T::one();
let i_t = T::from(i).unwrap();
let size_t = T::from(size).unwrap(); // Use N, not N-1 for periodic window
let size_t = T::from(size).unwrap(); // Use N, not N-1 for periodic window
half * (one - (two * pi * i_t / size_t).cos())
})
.collect(),
WindowType::Hamming => (0..size)
.map(|i| {
let i_t = T::from(i).unwrap();
let size_t = T::from(size).unwrap(); // Use N, not N-1 for periodic window
let size_t = T::from(size).unwrap(); // Use N, not N-1 for periodic window
T::from(0.54).unwrap() - T::from(0.46).unwrap() * (two * pi * i_t / size_t).cos()
})
.collect(),
WindowType::Blackman => (0..size)
.map(|i| {
let i_t = T::from(i).unwrap();
let size_t = T::from(size).unwrap(); // Use N, not N-1 for periodic window
let size_t = T::from(size).unwrap(); // Use N, not N-1 for periodic window
let angle = two * pi * i_t / size_t;
T::from(0.42).unwrap() - T::from(0.5).unwrap() * angle.cos()
+ T::from(0.08).unwrap() * (two * angle).cos()
Expand Down
34 changes: 25 additions & 9 deletions tests/config_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,30 @@ fn test_config_invalid_ola() {
assert!(config.is_err())
}

#[cfg(not(feature = "rustfft-backend"))]
#[test]
#[allow(deprecated)]
fn test_config_invalid_fft_size() {
let config = StftConfig::<f32>::new(4095, 1024, WindowType::Hann, ReconstructionMode::Ola);
assert!(matches!(config, Err(_)));
assert!(config.is_err());
}

#[cfg(feature = "rustfft-backend")]
#[test]
#[allow(deprecated)]
fn test_config_fft_size_not_power_of_two() {
let config = StftConfig::<f32>::new(4095, 1024, WindowType::Hann, ReconstructionMode::Ola);
assert!(config.is_ok());
}

#[test]
#[allow(deprecated)]
fn test_config_invalid_hop_size() {
let config = StftConfig::<f32>::new(4096, 0, WindowType::Hann, ReconstructionMode::Ola);
assert!(matches!(config, Err(_)));
assert!(config.is_err());

let config = StftConfig::<f32>::new(4096, 5000, WindowType::Hann, ReconstructionMode::Ola);
assert!(matches!(config, Err(_)));
assert!(config.is_err());
}

// Builder pattern tests
Expand Down Expand Up @@ -75,20 +84,17 @@ fn test_builder_with_reconstruction_mode() {

#[test]
fn test_builder_missing_fft_size() {
let config = StftConfig::<f32>::builder()
.hop_size(1024)
.build();
let config = StftConfig::<f32>::builder().hop_size(1024).build();
assert!(config.is_err());
}

#[test]
fn test_builder_missing_hop_size() {
let config = StftConfig::<f32>::builder()
.fft_size(4096)
.build();
let config = StftConfig::<f32>::builder().fft_size(4096).build();
assert!(config.is_err());
}

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

#[cfg(feature = "rustfft-backend")]
#[test]
fn test_builder_fft_size_not_power_of_two() {
let config = StftConfig::<f32>::builder()
.fft_size(4095) // Not a power of 2
.hop_size(1024)
.build();
assert!(config.is_ok());
}

#[test]
fn test_builder_invalid_hop_size() {
let config = StftConfig::<f32>::builder()
Expand Down