1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// This (implicitly/naively) uses a rectangular window.  Some way to set up a window function
// will be needed probably -- if mutating your samples before calling this isn't sufficient.
use std::f32;
use std::f32::consts::PI;

/// Set up parameters (and some precomputed values for those).
#[derive(Clone, Copy)]
pub struct Parameters {
    // Parameters we're working with
    window_size: usize,
    // Precomputed value:
    sine: f32,
    cosine: f32,
    term_coefficient: f32,
}

pub struct Partial {
    params: Parameters,
    count: usize,
    prev: f32,
    prevprev: f32,
}

impl Parameters {
    pub fn new(target_freq: f32, sample_rate: u32, window_size: usize) -> Self {
        let k = target_freq * (window_size as f32) / (sample_rate as f32);
        let omega = (f32::consts::PI * 2. * k) / (window_size as f32);
        let cosine = omega.cos();
        Parameters {
            window_size: window_size,
            sine: omega.sin(),
            cosine: cosine,
            term_coefficient: 2. * cosine,
        }
    }

    pub fn start(self) -> Partial {
        Partial{ params: self, count: 0, prev: 0., prevprev: 0. }
    }
}

impl Partial {
    pub fn add(mut self, samples: &[i16]) -> Self {
        for &sample in samples {
            let this = self.params.term_coefficient * self.prev - self.prevprev + (sample as f32);
            self.prevprev = self.prev;
            self.prev = this;
        }
        self.count += samples.len();
        self
    }
    pub fn finish(self) -> (f32, f32) {
        assert_eq!(self.count, self.params.window_size);
        let real = self.prev - self.prevprev * self.params.cosine;
        let imag = self.prevprev * self.params.sine;
        (real, imag)
    }

    pub fn finish_mag(self) -> f32 {
        let (real, imag) = self.finish();
        (real*real + imag*imag).sqrt()
    }
}

#[test]
fn zero_data() {
    let p = Parameters::new(1800., 8000, 256);
    assert!(p.start().add(&[0; 256]).finish_mag() == 0.);
    assert!(p.start().add(&[0; 128]).add(&[0;128]).finish_mag() == 0.);
}

#[test]
fn sine() {
    let mut buf = [0; 8000];
    for &freq in [697., 1200., 1800., 1633.].iter() {
        // Generate a 1 second sine wave at freq hz
        let step = 1. / 8000.;
        for sample in (0 .. 8000) {
            let time = sample as f32 * step;
            buf[sample] = ((time * freq * 2. * PI).sin()*std::i16::MAX as f32) as i16;
        }

        let p = Parameters::new(freq, 8000, 8000);
        let mag = p.start().add(&buf[..]).finish_mag();
        for testfreq in (0 .. 30).map(|x| (x * 100) as f32) {
            let p = Parameters::new(testfreq, 8000, 8000);
            let testmag = p.start().add(&buf[..]).finish_mag();
            println!("{:4}: {:12.3}", testfreq, testmag);
            if (freq-testfreq).abs() > 100. {
                println!("{} > 10*{}", mag, testmag);
                assert!(mag > 10.*testmag);
            }
        }
    }
}