Skip to main content

DcHighPassFilter

Struct DcHighPassFilter 

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

DC offset removal and high-pass filtering with streaming state.

Implements the DC offset removal specification:

  • Removes DC offset using exponential moving average (EMA)
  • Applies cascaded Butterworth high-pass filtering (defaults to 4th order @ 80 Hz)
  • Maintains filter state across chunks for streaming continuity
  • Achieves <2ms latency target per 500ms chunk (8000 samples @ 16kHz)

§Performance

  • Target: <2ms per 500ms chunk
  • Expected: ~0.16ms (10x headroom)
  • Optimization: Precomputed coefficients, preallocated buffers

§Example

use speech_prep::preprocessing::{DcHighPassFilter, PreprocessingConfig};

let mut filter = DcHighPassFilter::new(PreprocessingConfig::default())?;
let audio_stream = vec![vec![0.0; 8000], vec![0.1; 8000]];

// Process streaming chunks with state continuity
for chunk in audio_stream {
    let clean = filter.process(&chunk, None)?;
    // No discontinuities at boundaries!
}

Implementations§

Source§

impl DcHighPassFilter

Source

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

Create a new DC offset removal and high-pass filter.

§Arguments
  • config - Configuration parameters (cutoff frequency, sample rate, EMA alpha)
§Errors

Returns Error::Configuration if configuration is invalid.

§Example
use speech_prep::preprocessing::{DcHighPassFilter, PreprocessingConfig};

let config = PreprocessingConfig {
    highpass_cutoff_hz: 100.0, // More aggressive
    ..Default::default()
};
let filter = DcHighPassFilter::new(config)?;
Source

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

Process audio samples with DC removal and high-pass filtering.

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

Processed audio with DC removed and low frequencies attenuated.

§Performance
  • Expected: ~0.16ms for 8000 samples (10x better than <2ms target)
  • Complexity: O(n) where n = samples.len()
§Example
use speech_prep::preprocessing::{DcHighPassFilter, PreprocessingConfig, VadContext};

let mut filter = DcHighPassFilter::new(PreprocessingConfig::default())?;

// Chunk 1
let chunk1 = vec![0.1, 0.2, -0.1, 0.15];
let output1 = filter.process(&chunk1, None)?;

// Chunk 2 (state preserved from chunk1 - no discontinuity!)
let chunk2 = vec![0.2, 0.1, 0.3, 0.0];
let output2 = filter.process(&chunk2, None)?;
Source

pub fn reset(&mut self)

Reset filter state for new audio stream.

Clears filter history (x1, x2, y1, y2) and DC bias estimate. Use this when starting a new, independent audio stream.

§Example
use speech_prep::preprocessing::{DcHighPassFilter, PreprocessingConfig};

let mut filter = DcHighPassFilter::new(PreprocessingConfig::default())?;
let audio_stream_1 = vec![0.0; 8000];
let audio_stream_2 = vec![0.2; 8000];

// Process stream 1
filter.process(&audio_stream_1, None)?;

// Switch to unrelated stream 2 - reset state
filter.reset();
filter.process(&audio_stream_2, None)?;
Source

pub fn dc_bias(&self) -> f32

Get current DC bias estimate.

Useful for debugging or observability.

Source

pub fn config(&self) -> &PreprocessingConfig

Get current configuration.

Trait Implementations§

Source§

impl Clone for DcHighPassFilter

Source§

fn clone(&self) -> DcHighPassFilter

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DcHighPassFilter

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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