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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#![warn(clippy::std_instead_of_core, clippy::std_instead_of_alloc)]
#![no_std]
use core::slice::{Iter, IterMut};
use libm::sinf;

/// Simple fixed size ringbuffer with fast averaging and smoothing.
/// Do not use if the order of retrieval of each element matters.

pub struct SmoothBuffer<const CAP: usize> {
    data: [f32; CAP],
    head: usize,
    sum: Option<f32>,
    max: Option<f32>,
    min: Option<f32>,
    filled_len: usize,
}

impl<const CAP: usize> Default for SmoothBuffer<CAP> {
    fn default() -> Self {
        Self::new()
    }
}

impl<const CAP: usize> SmoothBuffer<CAP> {
    pub fn new() -> Self {
        SmoothBuffer {
            data: [0.0; CAP],
            head: 0,
            sum: None,
            max: None,
            min: None,
            filled_len: 0,
        }
    }

    // Fast! Sum is always kept up to date on push. No need iterate.
    pub fn average(&self) -> f32 {
        if self.filled_len > 0 {
            return self.sum.unwrap_or(0.0) / self.filled_len as f32;
        }
        0.0
    }

    pub fn clear(&mut self) {
        // for n in 0..self.data.len(){
        //     self.data[n] = 0.0;
        // }
        self.sum = None;
        self.max = None;
        self.min = None;
        self.filled_len = 0;
        self.head = 0;
    }

    pub fn is_empty(&self) -> bool {
        self.filled_len == 0
    }

    pub fn max(&self) -> f32 {
        self.max.unwrap_or(0.0)
    }

    pub fn min(&self) -> f32 {
        self.min.unwrap_or(0.0)
    }

    pub fn capacity(&self) -> usize {
        CAP
    }

    pub fn len(&self) -> usize {
        self.filled_len
    }

    pub fn push(&mut self, value: f32) {
        match self.max {
            None => self.max = Some(value),
            Some(max) => self.max = Some(f32::max(max, value)),
        }
        match self.min {
            None => self.min = Some(value),
            Some(min) => self.min = Some(f32::min(min, value)),
        }
        match self.sum {
            None => self.sum = Some(value),
            Some(sum) => self.sum = Some(sum - self.data[self.head] + value),
        }

        // Push data into storage
        self.data[self.head] = value;
        self.head += 1;
        if self.head == CAP {
            self.head = 0
        }
        if self.filled_len < CAP {
            self.filled_len += 1;
        }
    }

    pub fn iter(&self) -> Iter<f32> {
        self.data[0..self.filled_len].iter()
    }

    pub fn iter_mut(&mut self) -> IterMut<f32> {
        self.data[0..self.filled_len].iter_mut()
    }

    pub fn smooth(&self) -> f32 {
        let len = self.filled_len;
        let mut sum = 0.0;
        match len {
            0 => {}
            1 => sum = self.data[0],
            2 => sum = (self.data[0] + self.data[1]) * 0.5,
            3 => {
                let weights = [0.025, 0.95, 0.025];
                weights
                    .iter()
                    .enumerate()
                    .for_each(|(i, w)| sum += self.data[i] * w);
            }
            4 => {
                let weights = [0.015, 0.485, 0.485, 0.015];
                weights
                    .iter()
                    .enumerate()
                    .for_each(|(i, w)| sum += self.data[i] * w);
            }
            5 => {
                let weights = [0.016667, 0.0333, 0.900066, 0.0333, 0.016667];
                weights
                    .iter()
                    .enumerate()
                    .for_each(|(i, w)| sum += self.data[i] * w);
            }
            _ => {
                use core::f32::consts::TAU;
                for i in 0..self.len() {
                    let x = i as f32 / self.filled_len as f32;
                    let y = (sinf((x - 0.25) * TAU) / 2.0) + 0.5;
                    sum += self.data[i] * y;
                }
                sum /= len as f32;
                sum *= 2.0;
            }
        }
        sum
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn create_and_push() {
        const CAP: usize = 10;
        let mut buf = SmoothBuffer::<CAP>::new();
        for _ in 0..5 {
            buf.push(10.0);
        }

        assert_eq!(buf.capacity(), CAP);
        assert_eq!(buf.len(), 5);
        assert_eq!(buf.average(), 10.0);

        for _ in 0..10 {
            buf.push(5.0);
        }
        assert_eq!(buf.len(), CAP);
        assert_eq!(buf.average(), 5.0);
    }

    #[test]
    fn clearing() {
        let mut buf = SmoothBuffer::<10>::new();
        for n in 0..buf.capacity() {
            buf.push(n as f32);
        }
        buf.clear();
        assert_eq!(buf.capacity(), 10);
        assert_eq!(buf.len(), 0);
        assert_eq!(buf.average(), 0.0);
        assert_eq!(buf.iter().next(), None);
    }

    #[test]
    fn iteration() {
        let mut buf = SmoothBuffer::<10>::new();
        let len = 7;
        for n in 0..len {
            buf.push(n as f32);
        }

        for (i, value) in buf.iter().enumerate() {
            assert_eq!(i as f32, *value);
        }

        assert!(buf.iter().len() == len);
    }

    #[test]
    fn smoothing() {
        let mut buf = SmoothBuffer::<10>::new();
        let values = [
            3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0,
        ];
        for v in values {
            buf.push(v);
        }
        // Smoothed value won't be exactly the same! Will be correct to a few decimal places though
        assert!(buf.smooth() - 3.0 < 0.000001);
    }
}