1个不稳定版本
0.1.0 | 2023年9月21日 |
---|
#1388 在 嵌入式开发
14KB
232 行
Goertzel算法
在分析特定频率的幅度或相位时很有用。
与FFT的区别
当仅分析几个特定频率时,可能比FFT更高效。
与FFT不同,即使在块大小不是2的幂时,计算成本也保持不变。
no_std
默认与no_std兼容。
lib.rs
:
Goertzel算法
此crate提供了一个Goertzel算法的实现。
示例
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;
}
}
依赖项
~410KB