### Rust Biquad Filter Implementation Example Source: https://github.com/korken89/biquad-rs/blob/master/README.md Demonstrates how to create and use both Direct Form 1 and Direct Form 2 Transposed biquad filters in Rust. It shows the process of defining filter parameters, generating coefficients, initializing filter instances, and processing input data. ```Rust use biquad:: // Cutoff and sampling frequencies let f0 = 10.hz(); let fs = 1.khz(); // Create coefficients for the biquads let coeffs = Coefficients::::from_params(Type::LowPass, fs, f0, Q_BUTTERWORTH_F32).unwrap(); // Create two different biquads let mut biquad1 = DirectForm1::::new(coeffs); let mut biquad2 = DirectForm2Transposed::::new(coeffs); let input_vec = vec![0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]; let mut output_vec1 = Vec::new(); let mut output_vec2 = Vec::new(); // Run for all the inputs for elem in input_vec { output_vec1.push(biquad1.run(elem)); output_vec2.push(biquad2.run(elem)); } ``` -------------------------------- ### Create Audio Processing Chain Source: https://context7.com/korken89/biquad-rs/llms.txt Shows how to construct a multi-stage audio processing chain using high-pass, parametric EQ, and low-pass filters. This example processes a buffer of samples through a sequence of DirectForm2Transposed filters. ```rust use biquad::*; fn process_audio_buffer(input: &[f32], sample_rate: f32) -> Vec { let fs = Hertz::::from_hz(sample_rate).unwrap(); let hp_coeffs = Coefficients::::from_params(Type::HighPass, fs, 80.hz(), Q_BUTTERWORTH_F32).unwrap(); let eq_coeffs = Coefficients::::from_params(Type::PeakingEQ(3.0), fs, 3000.hz(), 1.5).unwrap(); let lp_coeffs = Coefficients::::from_params(Type::LowPass, fs, 16000.hz(), Q_BUTTERWORTH_F32).unwrap(); let mut hp_filter = DirectForm2Transposed::::new(hp_coeffs); let mut eq_filter = DirectForm2Transposed::::new(eq_coeffs); let mut lp_filter = DirectForm2Transposed::::new(lp_coeffs); input.iter().map(|&sample| { let hp_out = hp_filter.run(sample); let eq_out = eq_filter.run(hp_out); lp_filter.run(eq_out) }).collect() } ``` -------------------------------- ### Biquad Coefficients Creation from Parameters in Rust Source: https://context7.com/korken89/biquad-rs/llms.txt Shows how to generate biquad filter coefficients using the `Coefficients::from_params` method in Rust. This method takes the filter type, sampling frequency, cutoff frequency, and Q factor as input. It includes examples for low-pass, high-pass, and band-pass filters, along with error handling for invalid parameters like violating the Nyquist limit or providing a negative Q factor. ```rust use biquad::*; // Create coefficients for a Butterworth low-pass filter let fs = 48.khz(); let f0 = 1.khz(); let q = Q_BUTTERWORTH_F32; let coeffs = Coefficients::::from_params( Type::LowPass, fs, f0, q ).unwrap(); // Access individual coefficients (normalized form) println!("b0={}, b1={}, b2={}", coeffs.b0, coeffs.b1, coeffs.b2); println!("a1={}, a2={}", coeffs.a1, coeffs.a2); // High-pass filter at 500 Hz with high Q (resonant) let hp_coeffs = Coefficients::::from_params( Type::HighPass, 44100.hz(), 500.hz(), 2.0 ).unwrap(); // Band-pass filter with Butterworth Q let bp_coeffs = Coefficients::::from_params( Type::BandPass, 96.khz(), 2.khz(), Q_BUTTERWORTH_F64 ).unwrap(); // Error handling for invalid parameters let invalid_nyquist = Coefficients::::from_params( Type::LowPass, 1.khz(), 600.hz(), Q_BUTTERWORTH_F32 ); assert_eq!(invalid_nyquist.unwrap_err(), Errors::OutsideNyquist); let invalid_q = Coefficients::::from_params( Type::LowPass, 48.khz(), 1.khz(), -1.0 ); assert_eq!(invalid_q.unwrap_err(), Errors::NegativeQ); ``` -------------------------------- ### Generate Filter Coefficients from Normalized Parameters Source: https://context7.com/korken89/biquad-rs/llms.txt Demonstrates creating various filter types (LowPass, Notch, Shelving, PeakingEQ) using normalized frequency parameters. The normalized frequency must be within the range of 0 to 1, representing the ratio of the cutoff frequency to the Nyquist frequency. ```rust use biquad::*; let normalized_f0 = 0.0417; let coeffs = Coefficients::::from_normalized_params(Type::LowPass, normalized_f0, Q_BUTTERWORTH_F32).unwrap(); let notch_coeffs = Coefficients::::from_normalized_params(Type::Notch, 0.25, 5.0).unwrap(); let shelf_coeffs = Coefficients::::from_normalized_params(Type::LowShelf(6.0), 0.1, Q_BUTTERWORTH_F32).unwrap(); let peaking_coeffs = Coefficients::::from_normalized_params(Type::PeakingEQ(3.0), 0.2, 1.5).unwrap(); ``` -------------------------------- ### Implement DirectForm2Transposed Biquad Filter Source: https://context7.com/korken89/biquad-rs/llms.txt Demonstrates how to initialize a DirectForm2Transposed filter, process a stream of samples, and chain multiple filters to create higher-order responses. This topology is recommended for static filters due to its numerical stability. ```rust use biquad::*; let fs = 96.khz(); let f0 = 5.khz(); let coeffs = Coefficients::::from_params(Type::HighPass, fs, f0, Q_BUTTERWORTH_F64).unwrap(); let mut filter = DirectForm2Transposed::::new(coeffs); let input: Vec = vec![0.0, 0.25, 0.5, 0.75, 1.0, 0.75, 0.5, 0.25, 0.0]; let output: Vec = input.iter().map(|&sample| filter.run(sample)).collect(); println!("Internal states: s1={}, s2={}", filter.s1, filter.s2); let mut stage1 = DirectForm2Transposed::::new(Coefficients::from_params(Type::LowPass, 48.khz(), 1.khz(), Q_BUTTERWORTH_F32).unwrap()); let mut stage2 = DirectForm2Transposed::::new(Coefficients::from_params(Type::LowPass, 48.khz(), 1.khz(), Q_BUTTERWORTH_F32).unwrap()); let input_sample: f32 = 1.0; let intermediate = stage1.run(input_sample); let final_output = stage2.run(intermediate); stage1.reset_state(); stage2.reset_state(); ``` -------------------------------- ### DirectForm2Transposed Filter Implementation Source: https://context7.com/korken89/biquad-rs/llms.txt Demonstrates how to initialize and run a DirectForm2Transposed biquad filter for static signal processing. ```APIDOC ## Rust: DirectForm2Transposed Usage ### Description This implementation uses the Direct Form 2 Transposed topology, which is optimized for numerical stability and performance in static filter applications. ### Method Initialization and Sample Processing ### Parameters #### Request Body - **Type** (Enum) - Required - Filter type (HighPass, LowPass, etc.) - **fs** (Hertz) - Required - Sample rate - **f0** (Hertz) - Required - Cutoff frequency - **Q** (f32/f64) - Required - Quality factor ### Request Example let coeffs = Coefficients::::from_params(Type::HighPass, 96.khz(), 5.khz(), Q_BUTTERWORTH_F64).unwrap(); let mut filter = DirectForm2Transposed::::new(coeffs); ### Response #### Success Response (200) - **output** (f64) - The filtered sample value after calling filter.run(sample) ``` -------------------------------- ### Create Band Filters from Cutting Frequencies Source: https://context7.com/korken89/biquad-rs/llms.txt Shows how to generate band-pass or notch filter coefficients by specifying lower and upper normalized cutoff frequencies. This is useful for isolating or rejecting specific frequency ranges in audio signals. ```rust use biquad::*; let bp_coeffs = Coefficients::::band_0db_from_cutting_frequencies(Type::BandPass, 0.1, 0.4).unwrap(); let notch_coeffs = Coefficients::::band_0db_from_cutting_frequencies(Type::Notch, 0.2, 0.3).unwrap(); let audio_bp = Coefficients::::band_0db_from_cutting_frequencies(Type::BandPass, 0.0227, 0.0907).unwrap(); ``` -------------------------------- ### Implement DirectForm1 Biquad Filter Source: https://context7.com/korken89/biquad-rs/llms.txt Illustrates the use of the DirectForm1 struct for real-time audio filtering. It covers initialization, processing individual samples or buffers, updating coefficients dynamically, and resetting filter states. ```rust use biquad::*; let fs = 44100.hz(); let f0 = 1000.hz(); let coeffs = Coefficients::::from_params(Type::LowPass, fs, f0, Q_BUTTERWORTH_F32).unwrap(); let mut filter = DirectForm1::::new(coeffs); let input_sample: f32 = 0.5; let output_sample = filter.run(input_sample); let input_buffer = vec![0.0, 0.1, 0.3, 0.5, 0.7, 0.9, 1.0, 0.9, 0.7, 0.5]; let mut output_buffer = Vec::new(); for sample in input_buffer { output_buffer.push(filter.run(sample)); } let new_f0 = 2000.hz(); let new_coeffs = Coefficients::::from_params(Type::LowPass, fs, new_f0, Q_BUTTERWORTH_F32).unwrap(); filter.update_coefficients(new_coeffs); filter.reset_state(); let even_newer_coeffs = Coefficients::::from_params(Type::HighPass, fs, 500.hz(), Q_BUTTERWORTH_F32).unwrap(); let old_coeffs = filter.replace_coefficients(even_newer_coeffs); ``` -------------------------------- ### Frequency Creation with Hertz Type in Rust Source: https://context7.com/korken89/biquad-rs/llms.txt Illustrates how to use the `Hertz` type for type-safe frequency representation in Rust. It shows creating frequencies using extension methods like `.hz()`, `.khz()`, `.mhz()`, and `.dt()`, as well as error handling for invalid inputs. ```rust use biquad::Hertz; // Creating frequencies using extension methods let freq_10hz: Hertz = 10.hz(); let freq_1khz: Hertz = 1.khz(); let freq_48mhz: Hertz = 48.mhz(); let freq_from_period: Hertz = 0.001.dt(); // Creating frequencies with error handling let f1 = Hertz::::from_hz(440.0).unwrap(); let f2 = Hertz::::from_dt(0.0001).unwrap(); // Extracting the Hz value let hz_value: f32 = freq_10hz.hz(); // Error case: negative frequencies return Err(Errors::NegativeFrequency) let invalid = Hertz::::from_hz(-100.0); assert!(invalid.is_err()); ``` -------------------------------- ### Audio Processing Chain Source: https://context7.com/korken89/biquad-rs/llms.txt Demonstrates chaining multiple biquad filters to create a complex audio processing signal path. ```APIDOC ## Rust: Audio Processing Chain ### Description Shows how to chain multiple filters (High-pass, EQ, Low-pass) to process an audio buffer. ### Method Sequential processing ### Parameters #### Request Body - **input** (&[f32]) - Required - Input audio buffer - **sample_rate** (f32) - Required - Audio sample rate ### Request Example let output = process_audio_buffer(&input, 44100.0); ### Response #### Success Response (200) - **output** (Vec) - The processed audio buffer ``` -------------------------------- ### Coefficients::band_0db_from_cutting_frequencies Source: https://context7.com/korken89/biquad-rs/llms.txt Generates band-pass or notch filter coefficients using lower and upper normalized cutoff frequencies. ```APIDOC ## POST /coefficients/band-cut ### Description Calculates coefficients for band-pass or notch filters defined by a frequency range. ### Method POST ### Parameters #### Request Body - **type** (string) - Required - BandPass or Notch - **lower_f** (float) - Required - Lower normalized cutoff frequency - **upper_f** (float) - Required - Upper normalized cutoff frequency ### Request Example { "type": "BandPass", "lower_f": 0.1, "upper_f": 0.4 } ### Response #### Success Response (200) - **coefficients** (object) - The calculated band-specific coefficients. ``` -------------------------------- ### Biquad Filter Types Enumeration in Rust Source: https://context7.com/korken89/biquad-rs/llms.txt Demonstrates the usage of the `Type` enum to define different biquad filter characteristics like low-pass, high-pass, band-pass, notch, all-pass, and shelving filters. It also shows how to specify gain for shelving and EQ filters. ```rust use biquad::Type; // Available filter types let lowpass = Type::LowPass; let highpass = Type::HighPass; let bandpass = Type::BandPass; let notch = Type::Notch; let allpass = Type::AllPass; let single_pole_lp = Type::SinglePoleLowPass; let single_pole_approx = Type::SinglePoleLowPassApprox; // Shelving and EQ filters with gain in dB let low_shelf: Type = Type::LowShelf(6.0); let high_shelf: Type = Type::HighShelf(-3.0); let peaking_eq: Type = Type::PeakingEQ(12.0); ``` -------------------------------- ### Coefficients::from_normalized_params Source: https://context7.com/korken89/biquad-rs/llms.txt Generates filter coefficients using normalized cutoff frequencies (0.0 to 1.0). ```APIDOC ## POST /coefficients/normalized ### Description Creates filter coefficients based on a normalized frequency (f0 / Nyquist) and a specific filter type. ### Method POST ### Parameters #### Request Body - **type** (string) - Required - The filter type (e.g., LowPass, Notch, LowShelf, PeakingEQ) - **normalized_f0** (float) - Required - Normalized frequency between 0 and 1 - **q** (float) - Required - Quality factor ### Request Example { "type": "LowPass", "normalized_f0": 0.0417, "q": 0.707 } ### Response #### Success Response (200) - **coefficients** (object) - The calculated biquad filter coefficients. ``` -------------------------------- ### DirectForm1 Filter Operations Source: https://context7.com/korken89/biquad-rs/llms.txt Manages the DirectForm1 biquad filter state, processing, and real-time updates. ```APIDOC ## POST /filter/direct-form-1/run ### Description Processes an input sample through the DirectForm1 filter instance. ### Method POST ### Parameters #### Request Body - **sample** (float) - Required - The input audio sample ### Response #### Success Response (200) - **output** (float) - The filtered output sample ## PUT /filter/direct-form-1/coefficients ### Description Updates the filter coefficients in real-time for the existing filter instance. ### Method PUT ### Parameters #### Request Body - **coefficients** (object) - Required - New coefficient set ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.