pub struct OptimizedGoertzel { /* private fields */ }
Expand description
Optimized Goertzel algorithm.
It only calculates the magnitude.
use goertzel_algorithm::OptimizedGoertzel;
use approx::{assert_relative_eq, assert_relative_ne};
const SAMPLE_RATE: u32 = 48_000u32;
const TARGET_FREQUENCY: f32 = 750.0f32;
const BLOCK_SIZE: u32 = 128u32;
let phase_increment = TARGET_FREQUENCY * std::f32::consts::PI * 2.0f32 * (1.0f32 / SAMPLE_RATE as f32);
let mut phase = 0.0f32;
let mut goertzel = OptimizedGoertzel::new();
goertzel.prepare(SAMPLE_RATE, TARGET_FREQUENCY, BLOCK_SIZE);
for i in 0..BLOCK_SIZE {
let input = phase.sin();//Generate a sine wave same frequency as the target frequency
if let Some(magnitude) = goertzel.process_sample(&input) {
println!("{}: {}", i, magnitude);//127: 1.0
}
phase += phase_increment;
if phase >= std::f32::consts::PI * 2.0f32 {
phase -= std::f32::consts::PI * 2.0f32;
}
}
Implementations§
Source§impl OptimizedGoertzel
impl OptimizedGoertzel
pub fn new() -> Self
Sourcepub fn prepare(
&mut self,
sample_rate: u32,
target_frequency: f32,
block_size: u32,
)
pub fn prepare( &mut self, sample_rate: u32, target_frequency: f32, block_size: u32, )
Prepare the Goertzel algorithm’s coefficients.
§Arguments
- ‘sample_rate’ - The sample rate of the input signal.
- ‘target_frequency’ - The frequency to detect.
- ‘block_size’ - The number of samples to process at a time. It is like FFT size, but does not have to be a power of 2.
Trait Implementations§
Source§impl Default for OptimizedGoertzel
impl Default for OptimizedGoertzel
Source§fn default() -> OptimizedGoertzel
fn default() -> OptimizedGoertzel
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for OptimizedGoertzel
impl RefUnwindSafe for OptimizedGoertzel
impl Send for OptimizedGoertzel
impl Sync for OptimizedGoertzel
impl Unpin for OptimizedGoertzel
impl UnwindSafe for OptimizedGoertzel
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more