#dsp #goertzel

no-std goertzel_algorithm

Goertzel algorithm implementation

1 unstable release

0.1.0 Sep 21, 2023

#1331 in Embedded development

MIT/Apache

14KB
232 lines

Goertzel algorithm

Cargo Documentation

Useful when analyzing the amplitude or phase of a specific frequency.

Difference from FFT

When analyzing only a few specific frequencies, it may be more efficient than an FFT.
Different from the FFT, the computational cost remains the same even when the block size is not a power of 2.

no_std

Works with no_std by default.


lib.rs:

Goertzel algorithm

This crate provides a Goertzel algorithm implementation.

Example

use goertzel_algorithm::Goertzel;
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 = Goertzel::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(mag_phase) = goertzel.process_sample(&input) {
        println!("{}: {}", i, mag_phase.magnitude);//127: 1.0
    }

    phase += phase_increment;
    if phase >= std::f32::consts::PI * 2.0f32 {
        phase -= std::f32::consts::PI * 2.0f32;
    }
}            

Dependencies

~395KB