Struct OptimizedGoertzel

Source
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

Source

pub fn new() -> Self

Source

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.
Source

pub fn process_sample(&mut self, input: &f32) -> Option<f32>

Process a sample.

§Arguments
  • ‘input’ - The sample to add.
§Returns

Returns the magnitude of the frequency if a block is completed, otherwise None. Magnitude is normalized to [0.0, 1.0].

Trait Implementations§

Source§

impl Default for OptimizedGoertzel

Source§

fn default() -> OptimizedGoertzel

Returns the “default value” for a type. 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, 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<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.