Skip to main content

NoiseReducer

Struct NoiseReducer 

Source
pub struct NoiseReducer { /* private fields */ }
Expand description

Noise reduction via spectral subtraction with adaptive noise profiling.

Implements the noise reduction specification:

  • STFT-based processing (25ms window, 10ms hop)
  • Adaptive noise profile estimation during VAD-detected silence
  • Magnitude-only spectral subtraction (preserves phase)
  • Achieves ≥6 dB SNR improvement target

§Performance

  • Target: <15ms per 500ms chunk (8000 samples @ 16kHz)
  • Expected: ~7ms (2x headroom)
  • Optimization: Precomputed FFT plans, reused buffers

§Example

use speech_prep::preprocessing::{NoiseReducer, NoiseReductionConfig, VadContext};

let config = NoiseReductionConfig::default();
let mut reducer = NoiseReducer::new(config)?;
let audio_stream = vec![vec![0.0; 8000], vec![0.05; 8080]];

// Process streaming chunks with VAD context
for chunk in audio_stream {
    let vad_ctx = VadContext { is_silence: detect_silence(&chunk) };
    let _denoised = reducer.reduce(&chunk, Some(vad_ctx))?;
}

Implementations§

Source§

impl NoiseReducer

Source

pub fn new(config: NoiseReductionConfig) -> Result<Self>

Create a new noise reducer.

§Arguments
  • config - Configuration parameters (window size, hop, α, β)
§Errors

Returns Error::Configuration if configuration is invalid.

§Example
use speech_prep::preprocessing::{NoiseReducer, NoiseReductionConfig};

let config = NoiseReductionConfig {
    oversubtraction_factor: 2.5, // Aggressive
    ..Default::default()
};
let reducer = NoiseReducer::new(config)?;
Source

pub fn reduce( &mut self, samples: &[f32], vad_context: Option<VadContext>, ) -> Result<Vec<f32>>

Apply noise reduction to audio samples.

§Arguments
  • samples - Input audio samples (typically 500ms chunk = 8000 samples @ 16kHz)
  • vad_context - Optional VAD state for noise profile updates
§Returns

Denoised audio with ≥6 dB SNR improvement on noisy input.

§Performance
  • Expected: ~7ms for 8000 samples (2x better than <15ms target)
  • Complexity: O(n log n) for FFT operations
§Example
use speech_prep::preprocessing::{NoiseReducer, NoiseReductionConfig, VadContext};

let mut reducer = NoiseReducer::new(NoiseReductionConfig::default())?;

// Chunk 1 (silence - initialize noise profile)
let chunk1 = vec![0.001; 8000];
let vad1 = VadContext { is_silence: true };
let output1 = reducer.reduce(&chunk1, Some(vad1))?;

// Chunk 2 (speech - apply noise reduction)
let chunk2 = vec![0.1; 8000];
let vad2 = VadContext { is_silence: false };
let output2 = reducer.reduce(&chunk2, Some(vad2))?;
Source

pub fn reset(&mut self)

Reset noise profile for new audio stream.

Clears noise estimate and overlap-add state. Use this when starting a new, independent audio stream.

Source

pub fn noise_floor(&self) -> f32

Get current average noise floor (for debugging/observability).

Source

pub fn config(&self) -> &NoiseReductionConfig

Get current configuration.

Trait Implementations§

Source§

impl Debug for NoiseReducer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more