Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • stephen/goertzel-nostd
1 result
Show changes
Commits on Source (2)
[package] [package]
edition = "2021" edition = "2021"
name = "goertzel-nostd" name = "goertzel-nostd"
version = "0.2.1" version = "0.3.0"
authors = ["Stephen D"] authors = ["Stephen D"]
repository = "https://gitlab.scd31.com/stephen/goertzel-nostd" repository = "https://gitlab.scd31.com/stephen/goertzel-nostd"
keywords = ["dsp", "goertzel", "fft", "fourier"] keywords = ["dsp", "goertzel", "fft", "fourier"]
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
// allow std in tests // allow std in tests
#![cfg_attr(not(test), no_std)] #![cfg_attr(not(test), no_std)]
use core::f32::consts::PI; use core::{borrow::Borrow, f32::consts::PI};
/// Set up parameters (and some precomputed values for those). /// Set up parameters (and some precomputed values for those).
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
...@@ -46,23 +46,24 @@ impl Parameters { ...@@ -46,23 +46,24 @@ impl Parameters {
} }
} }
pub fn mag(self, samples: &[f32]) -> f32 { pub fn mag<V: Borrow<f32>, I: Iterator<Item = V>>(self, samples: I) -> f32 {
self.start().add_samples(samples).finish_mag() self.start().add_samples(samples).finish_mag()
} }
pub fn mag_squared(self, samples: &[f32]) -> f32 { pub fn mag_squared<V: Borrow<f32>, I: Iterator<Item = V>>(self, samples: I) -> f32 {
self.start().add_samples(samples).finish_mag_squared() self.start().add_samples(samples).finish_mag_squared()
} }
} }
impl Partial { impl Partial {
pub fn add_samples(mut self, samples: &[f32]) -> Self { pub fn add_samples<V: Borrow<f32>, I: Iterator<Item = V>>(mut self, samples: I) -> Self {
for &sample in samples { for sample in samples {
let this = self.params.term_coefficient * self.prev - self.prevprev + sample; let this = self.params.term_coefficient * self.prev - self.prevprev + sample.borrow();
self.prevprev = self.prev; self.prevprev = self.prev;
self.prev = this; self.prev = this;
self.count += 1;
} }
self.count += samples.len();
self self
} }
pub fn finish(self) -> (f32, f32) { pub fn finish(self) -> (f32, f32) {
...@@ -85,11 +86,11 @@ impl Partial { ...@@ -85,11 +86,11 @@ impl Partial {
#[test] #[test]
fn zero_data() { fn zero_data() {
let p = Parameters::new(1800., 8000, 256); let p = Parameters::new(1800., 8000, 256);
assert!(p.start().add_samples(&[0.0; 256]).finish_mag() == 0.); assert!(p.start().add_samples([0.0; 256].iter()).finish_mag() == 0.);
assert!( assert!(
p.start() p.start()
.add_samples(&[0.0; 128]) .add_samples([0.0; 128].iter())
.add_samples(&[0.0; 128]) .add_samples([0.0; 128].iter())
.finish_mag() .finish_mag()
== 0. == 0.
); );
...@@ -107,13 +108,13 @@ fn sine() { ...@@ -107,13 +108,13 @@ fn sine() {
} }
let p = Parameters::new(freq, 8000, 8000); let p = Parameters::new(freq, 8000, 8000);
let mag = p.start().add_samples(&buf[..]).finish_mag(); let mag = p.start().add_samples(buf[..].iter()).finish_mag();
for testfreq in (0..30).map(|x| (x * 100) as f32) { for testfreq in (0..30).map(|x| (x * 100) as f32) {
let p = Parameters::new(testfreq, 8000, 8000); let p = Parameters::new(testfreq, 8000, 8000);
let testmag = p.mag(&buf[..]); let testmag = p.mag(&buf[..].iter());
println!("{:4}: {:12.3}", testfreq, testmag); println!("{testfreq:4}: {testmag:12.3}");
if (freq - testfreq).abs() > 100. { if (freq - testfreq).abs() > 100. {
println!("{} > 10*{}", mag, testmag); println!("{mag} > 10*{testmag}");
assert!(mag > 10. * testmag); assert!(mag > 10. * testmag);
} }
} }
......