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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//! Finite-impulse response (FIR) convolution with static tap coefficients.
//!
//! ## Example
//!
//! The following example shows typical API usage:
//!
//! ```rust
//! #[macro_use]
//! extern crate static_fir;
//!
//! use static_fir::FirFilter;
//!
//! impl_fir!(LowpassFir, f32, 21, [
//!     -0.0022183273232,
//!     -0.00364708336518,
//!     -0.0058179856702,
//!     -0.00616633506547,
//!     2.60007787671e-18,
//!     0.0172901503422,
//!     0.0472883481821,
//!     0.0864914386425,
//!     0.126465151635,
//!     0.156489628279,
//!     0.167650028687,
//!     0.156489628279,
//!     0.126465151635,
//!     0.0864914386425,
//!     0.0472883481821,
//!     0.0172901503422,
//!     2.60007787671e-18,
//!     -0.00616633506547,
//!     -0.0058179856702,
//!     -0.00364708336518,
//!     -0.0022183273232,
//! ]);
//!
//! fn main() {
//!     let mut filt = FirFilter::<LowpassFir>::new();
//!
//!     // Run filter over a couple samples.
//!     assert_eq!(filt.feed(1.0), -0.0022183273232);
//!     assert_eq!(filt.feed(2.0), -0.008083738011580001);
//!
//!     // Pad out rest of history.
//!     for _ in 0..19 {
//!         filt.feed(0.0);
//!     }
//!
//!     // Iterate over history in order.
//!     let mut hist = filt.history();
//!     assert_eq!(hist.next().unwrap(), &1.0);
//!     assert_eq!(hist.next().unwrap(), &2.0);
//!     assert_eq!(hist.next().unwrap(), &0.0);
//!
//!     // Compute energy of stored samples.
//!     assert_eq!(filt.history_unordered().fold(0.0, |s, x| {
//!         s + x.powi(2)
//!     }), 5.0);
//! }
//! ```

#![feature(conservative_impl_trait)]

use std::ops::{Add, Mul, Deref, DerefMut};

/// Provides a sequence of coefficients and storage for sample history.
pub trait FirCoefs: Default + Deref<Target = [<Self as FirCoefs>::Sample]> + DerefMut {
    /// Type of sample stored in the history.
    type Sample: Copy + Clone + Default + Add<Output = Self::Sample> +
        Mul<f32, Output = Self::Sample>;

    /// Number of coefficients/stored samples.
    fn size() -> usize;
    /// Sequence of coefficients.
    fn coefs() -> &'static [f32];

    /// Verify the requirement that the filter coefficients are symmetric around the
    /// center (either even or odd length.)
    fn verify_symmetry() {
        for i in 0..Self::size() / 2 {
            assert_eq!(Self::coefs()[i], Self::coefs()[Self::size() - i - 1]);
        }
    }
}

/// Implement `FirCoefs` for a history buffer with the given name, input sample type,
/// storage size, and sequence of coefficients.
#[macro_export]
macro_rules! impl_fir {
    ($name:ident, $sample:ty, $size:expr, $coefs:expr) => {
        pub struct $name([$sample; $size]);

        impl $crate::FirCoefs for $name {
            type Sample = $sample;
            fn size() -> usize { $size }
            fn coefs() -> &'static [f32] {
                static COEFS: [f32; $size] = $coefs;
                &COEFS[..]
            }
        }

        impl Default for $name {
            fn default() -> Self {
                $name([<Self as $crate::FirCoefs>::Sample::default(); $size])
            }
        }

        impl std::ops::Deref for $name {
            type Target = [$sample];
            fn deref(&self) -> &Self::Target { &self.0[..] }
        }

        impl std::ops::DerefMut for $name {
            fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0[..] }
        }
    };
}

/// A FIR filter for convolving with a series of samples.
pub struct FirFilter<C: FirCoefs> {
    /// Coefficients and history storage.
    inner: C,
    /// The index of the most-recently added sample.
    idx: usize,
}

impl<C: FirCoefs> FirFilter<C> {
    /// Create a new `FirFilter` with empty history.
    pub fn new() -> FirFilter<C> {
        FirFilter {
            inner: C::default(),
            idx: 0,
        }
    }

    /// Add a sample to the current history and calculate the convolution.
    pub fn feed(&mut self, sample: C::Sample) -> C::Sample {
        // Store the given sample in the current history slot.
        self.inner[self.idx] = sample;

        // Move to the next slot and wrap around.
        self.idx += 1;
        self.idx %= C::size();

        self.calc()
    }

    /// Calculate the convolution of saved samples with coefficients, where the given
    /// index gives the position of the most recent sample in the history ring buffer.
    fn calc(&self) -> C::Sample {
        let (hleft, hright) = self.inner.split_at(self.idx);
        let (cleft, cright) = C::coefs().split_at(C::size() - self.idx);

        cleft.iter().zip(hright)
            .fold(C::Sample::default(), |s, (&c, &x)| s + x * c) +
        cright.iter().zip(hleft)
            .fold(C::Sample::default(), |s, (&c, &x)| s + x * c)
    }

    /// Create an iterator over the history of stored samples, with the oldest sample as
    /// the first item yielded and the newest as the last.
    #[inline]
    pub fn history<'a>(&'a self) -> impl Iterator<Item = &'a C::Sample> {
        let (left, right) = self.inner.split_at(self.idx);
        right.iter().chain(left.iter())
    }

    /// Create an iterator over the history of stored samples, where the samples are
    /// yielded in the order they're stored in the underlying ring buffer.
    ///
    /// This may be more efficient than `history` if the order of the samples is
    /// insignificant.
    pub fn history_unordered<'a>(&'a self) -> impl Iterator<Item = &'a C::Sample> {
        self.inner.iter()
    }
}

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

    impl_fir!(TestFIR, f32, 4, [
        1.0,
        0.0,
        2.0,
        0.0,
    ]);

    impl_fir!(SymmetricOddFIR, f32, 5, [
        0.2,
        0.4,
        1.0,
        0.4,
        0.2,
    ]);

    impl_fir!(SymmetricEvenFIR, f32, 6, [
        0.2,
        0.4,
        1.0,
        1.0,
        0.4,
        0.2,
    ]);

    impl_fir!(NonSymmetricOddFIR, f32, 5, [
        0.2,
        0.4,
        1.0,
        0.5,
        0.2,
    ]);

    impl_fir!(NonSymmetricEvenFIR, f32, 6, [
        0.2,
        0.4,
        1.0,
        1.0,
        0.5,
        0.2,
    ]);

    #[test]
    fn test_fir() {
        let mut f = FirFilter::<TestFIR>::new();

        assert!(f.feed(100.0) == 0.0);
        assert!(f.feed(200.0) == 200.0);
        assert!(f.feed(300.0) == 400.0);
        assert!(f.feed(400.0) == 700.0);
        assert!(f.feed(0.0) == 1000.0);
        assert!(f.feed(0.0) == 300.0);
        assert!(f.feed(0.0) == 400.0);
        assert!(f.feed(0.0) == 0.0);
        assert!(f.feed(0.0) == 0.0);
        assert!(f.feed(100.0) == 0.0);
        assert!(f.feed(200.0) == 200.0);
        assert!(f.feed(300.0) == 400.0);
        assert!(f.feed(400.0) == 700.0);

        let mut iter = f.history();

        assert_eq!(iter.next().unwrap(), &100.0);
        assert_eq!(iter.next().unwrap(), &200.0);
        assert_eq!(iter.next().unwrap(), &300.0);
        assert_eq!(iter.next().unwrap(), &400.0);

        let mut iter = f.history_unordered();

        assert_eq!(iter.next().unwrap(), &400.0);
        assert_eq!(iter.next().unwrap(), &100.0);
        assert_eq!(iter.next().unwrap(), &200.0);
        assert_eq!(iter.next().unwrap(), &300.0);
    }

    #[test]
    fn test_verify_symmetry() {
        SymmetricOddFIR::verify_symmetry();
        SymmetricEvenFIR::verify_symmetry();
    }

    #[test]
    #[should_panic]
    fn test_verify_nonsymmetry_odd() {
        NonSymmetricOddFIR::verify_symmetry();
    }

    #[test]
    #[should_panic]
    fn test_verify_nonsymmetry_even() {
        NonSymmetricEvenFIR::verify_symmetry();
    }
}