sameold 0.6.0

A SAME/EAS digital receiver library
Documentation
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//! # FIR linear filters
//!
//! The [`FilterCoeff`] implements the
//! multiply-accumulate operation of a Finite Impulse Response
//! filter. FIR filters convolve the input with an impulse
//! response `h` to obtain the output. Convolution consists of
//! two operations:
//!
//! 1. Multiply-accumulate: A window which contains the previous
//!    `h.len()` input samples is multiplied element-wise with `h`.
//!    The sum of all the elements is the output.
//!
//! 2. Sliding window: to advance time to the next output sample,
//!    a new input sample is shifted onto the window. The oldest
//!    output sample is aged off.
//!
//! ## Multiply-Accumulate
//!
//! [`FilterCoeff`] implements only (1). To
//! use, let the input window be a slice like
//!
//! ```txt
//! // the sample L is the youngest sample, and O is the oldest
//! // [ O | N | M | L ]
//! ```
//!
//! to obtain the output for sample `Q`, run
//!
//! ```ignore
//! let coeff = FilterCoeff::from_slice(&[1.0f32]);  // this is the identity filter
//! let history = &[1.0f32, 2.0f32, 3.0f32, 4.0f32]; // sample history slice
//! let out = coeff.filter(history); // out is 4.0f32
//! ```
//!
//! Then shift the slice like this,
//!
//! ```txt
//! // the sample K is now the youngest sample
//! // [ N | M | L | K ]
//! ```
//!
//! and repeat to obtain the output for sample `K`.
//!
//! The input window *should* be the same length as the filter
//! coefficients, but this is not strictly enforced.
//!
//! * If it is shorter, the missing samples are treated as zeros.
//! * If it is longer, the excess samples are ignored.
//!
//! ## Sliding window
//!
//! The [`Window`] class implements a sliding
//! window. New samples are pushed onto the window, and old samples
//! are allowed to age off. The `Window` has a fixed size at
//! construction time.
//!
//! To use `Window` for FIR filtering, create it with the same
//! length as `FilterCoeff`.
//!
//! ```ignore
//! let wind : Window<f32> = Window::new(4);
//! wind.push(&[1.0f32, 2.0f32, 3.0f32, 4.0f32]);
//! ```

use std::collections::VecDeque;
use std::convert::AsRef;

use nalgebra::base::Scalar;
use nalgebra::DVector;
use num_traits::{One, Zero};

/// FIR filter coefficients
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq)]
pub struct FilterCoeff<T>(DVector<T>)
where
    T: Copy + Scalar + One + Zero;

#[allow(dead_code)]
impl<T> FilterCoeff<T>
where
    T: Copy + Scalar + One + Zero,
{
    /// Create from slice
    ///
    /// Creates FIR filter coefficients with the specified impulse
    /// response `h`. The coefficients `h` use the same representation
    /// as GNU Octave's `filter()` function.
    pub fn from_slice<S>(h: S) -> Self
    where
        S: AsRef<[T]>,
    {
        let inp = h.as_ref();
        FilterCoeff(DVector::from_iterator(inp.len(), inp.iter().copied()))
    }

    /// Create an identity filter
    ///
    /// The identity filter is a "no-op" impulse response
    pub fn from_identity(len: usize) -> Self {
        let mut out = FilterCoeff(DVector::from_iterator(
            len,
            std::iter::repeat(T::zero()).take(len),
        ));
        out.0[0] = T::one();
        out
    }

    /// Number of filter coefficients
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Perform FIR filtering with the given sample history
    ///
    /// Computes the current output sample of the filter assuming
    /// the given `history`. `history` must be a
    /// `DoubledEndedIterator` which outputs the oldest sample
    /// first and the newest sample last. The newest sample is
    /// used for feedforward lag 0. `history` SHOULD contain at
    /// least `self.len()` samples, but no error is raised if it
    /// is shorter.
    ///
    /// The caller is encouraged to use a deque for `history`,
    /// but this is not enforced.
    pub fn filter<W, In, Out>(&self, history: W) -> Out
    where
        W: IntoIterator<Item = In>,
        W::IntoIter: DoubleEndedIterator,
        In: Copy + Scalar + std::ops::Mul<T, Output = Out>,
        Out: Copy + Scalar + Zero + std::ops::AddAssign,
    {
        multiply_accumulate(history, self.as_ref())
    }

    /// Reset to identity filter
    ///
    /// The filter coefficients are reset to a "no-op" identity
    /// filter.
    pub fn identity(&mut self) {
        for coeff in self.0.iter_mut() {
            *coeff = T::zero();
        }
        self.0[0] = T::one();
    }

    /// Return filter coefficients as slice
    #[inline]
    pub fn as_slice(&self) -> &[T] {
        self.0.as_slice()
    }

    /// Return filter coefficients as mutable slice
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        self.0.as_mut_slice()
    }

    /// Obtain filter coefficients
    #[inline]
    pub fn inner(&self) -> &DVector<T> {
        &self.0
    }

    /// Obtain filter coefficients (mutable)
    #[inline]
    pub fn inner_mut(&mut self) -> &mut DVector<T> {
        &mut self.0
    }
}

impl<T> AsRef<[T]> for FilterCoeff<T>
where
    T: Copy + Scalar + One + Zero,
{
    #[inline]
    fn as_ref(&self) -> &[T] {
        self.as_slice()
    }
}

impl<T> AsMut<[T]> for FilterCoeff<T>
where
    T: Copy + Scalar + One + Zero,
{
    #[inline]
    fn as_mut(&mut self) -> &mut [T] {
        self.as_mut_slice()
    }
}

impl<T> std::ops::Index<usize> for FilterCoeff<T>
where
    T: Copy + Scalar + One + Zero,
{
    type Output = T;

    #[inline]
    fn index(&self, ind: usize) -> &T {
        self.0.index(ind)
    }
}

impl<T> std::ops::IndexMut<usize> for FilterCoeff<T>
where
    T: Copy + Scalar + One + Zero,
{
    #[inline]
    fn index_mut(&mut self, ind: usize) -> &mut T {
        self.0.index_mut(ind)
    }
}

/// Filter window
///
/// Implements a fixed-size lookback window for FIR filters
/// or other purposes.
#[derive(Clone, Debug)]
pub struct Window<T>(VecDeque<T>)
where
    T: Copy + Scalar + Zero;

#[allow(dead_code)]
impl<T> Window<T>
where
    T: Copy + Scalar + Zero,
{
    /// Create empty window, filling it with zeros
    ///
    /// Creates a new `Window` with the given `len`gth
    pub fn new(len: usize) -> Self {
        let mut q = VecDeque::with_capacity(len);
        q.resize(len, T::zero());
        Self(q)
    }

    /// Reset to zero initial conditions
    ///
    /// Clear the window, filling it with zeros
    pub fn reset(&mut self) {
        for s in &mut self.0 {
            *s = T::zero()
        }
    }

    /// Window length
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Append to sample window
    ///
    /// Appends the `input` slice to the right side of the Window.
    /// The last sample of `input` becomes the right-most / most recent
    /// sample of the Window.
    ///
    /// If the length of `input` exceeds the length of the Window,
    /// then the right-most chunk of `input` will be taken.
    pub fn push<S>(&mut self, input: S)
    where
        S: AsRef<[T]>,
    {
        let input = input.as_ref();
        let input = if input.len() > self.0.len() {
            let start = input.len() - self.0.len();
            &input[start..]
        } else {
            input
        };

        // age off the size of input
        std::mem::drop(self.0.drain(0..input.len()));

        // add new
        self.0.extend(input.as_ref());
    }

    /// Append a scalar to the sample window
    ///
    /// Appends the `input` scalar to the right side of the Window.
    /// It becomes the last / most recent sample of Window.
    /// Returns the sample that was formerly the oldest
    /// sample in the Window.
    #[inline]
    pub fn push_scalar(&mut self, input: T) -> T {
        let out = self.0.pop_front().unwrap_or(T::zero());
        self.0.push_back(input);
        out
    }

    /// Iterator over window contents
    ///
    /// The iterator outputs the least recent sample first.
    /// The most recent sample is output last.
    pub fn iter(&self) -> <&Window<T> as IntoIterator>::IntoIter {
        self.into_iter()
    }

    /// Convert window contents to a vector
    ///
    /// Copy the current contents of the window to a freshly-allocated
    /// vector. Vector elements are ordered from least recent sample
    /// to most recent sample.
    pub fn to_vec(&self) -> Vec<T> {
        self.iter().collect()
    }

    /// Obtain the inner SliceRingBuffer
    pub fn inner(&self) -> &VecDeque<T> {
        &self.0
    }

    /// Most recent element pushed into the Window
    #[inline]
    pub fn back(&self) -> T {
        *self.0.back().unwrap()
    }

    /// Least recent element pushed into the Window
    #[inline]
    pub fn front(&self) -> T {
        *self.0.front().unwrap()
    }
}

impl<'a, T> IntoIterator for &'a Window<T>
where
    T: Copy + Scalar + Zero,
{
    type Item = T;

    type IntoIter = std::iter::Copied<std::collections::vec_deque::Iter<'a, T>>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.iter().copied()
    }
}

// Multiply-accumulate operation
//
// Calculates the sum of the element-wise multiplication,
//
// ```txt
// out = Σ history[i] * coeff[i]
// ```
//
// This is the core operation of FIR filtering. In an FIR filter,
// `history` contains the sample history. The most recent sample
// is stored in `history[N-1]`, and the least recent sample in the
// history is stored in `history[0]`. The filter coefficients are
// stored in `coeff`, with `coeff[0]` being the zeroth filter
// coefficient.
//
// The two inputs need not be the same length. If `history` is shorter
// than `coeff`, then the sample history is assumed to be zero
// outside of its range.
//
// To perform FIR filtering, new samples are shifted onto the end of
// history, and a `multiply_accumulate()` operation is performed for
// each output sample.
//
// The output value is returned. Any compatible arithmetic types may
// be used, including complex numbers.
fn multiply_accumulate<W, In, Coeff, Out>(history: W, coeff: &[Coeff]) -> Out
where
    W: IntoIterator<Item = In>,
    W::IntoIter: DoubleEndedIterator,
    In: Copy + Scalar + std::ops::Mul<Coeff, Output = Out>,
    Coeff: Copy + Scalar,
    Out: Copy + Scalar + Zero + std::ops::AddAssign,
{
    let history = history.into_iter();
    let mut out = Out::zero();
    for (hi, co) in history.rev().zip(coeff.iter()) {
        out += hi * *co;
    }
    out
}

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

    use assert_approx_eq::assert_approx_eq;

    use num_complex::Complex;

    #[test]
    fn test_multiply_accumulate() {
        // trivial MAC is zero
        let out = multiply_accumulate(&[0.0f32; 0], &[0.0f32; 0]);
        assert_eq!(0.0f32, out);

        // simple multiplies; we clip to the end
        let out = multiply_accumulate(&[20.0f32, 1.0f32], &[1.0f32]);
        assert_eq!(1.0f32, out);
        let out = multiply_accumulate(&[1.0f32], &[1.0f32, 20.0f32]);
        assert_eq!(1.0f32, out);

        // more complicated multiply
        let out = multiply_accumulate(&[20.0f32, 20.0f32], &[1.0f32, -1.0f32]);
        assert_approx_eq!(0.0f32, out);
    }

    #[test]
    fn test_filter_cplx() {
        const INPUT: &[Complex<f32>] = &[Complex {
            re: 0.5f32,
            im: 0.5f32,
        }];

        let filter = FilterCoeff::from_slice(&[2.0f32, 0.0f32, 0.0f32]);

        let out = filter.filter(INPUT);
        assert_approx_eq!(out.re, 1.0f32);
        assert_approx_eq!(out.im, 1.0f32);
    }

    #[test]
    fn test_filter_identity() {
        const EXPECT: &[f32] = &[1.0f32, 0.0f32, 0.0f32, 0.0f32];

        let mut filter = FilterCoeff::<f32>::from_identity(4);
        assert_eq!(EXPECT, filter.as_ref());
        assert_eq!(10.0f32, filter.filter(&[10.0f32]));

        filter[3] = 5.0f32;
        filter.identity();
        assert_eq!(EXPECT, filter.as_ref());
    }

    #[test]
    fn test_window() {
        let mut wind: Window<f32> = Window::new(4);
        assert_eq!(4, wind.len());
        assert_eq!(vec![0.0f32, 0.0f32, 0.0f32, 0.0f32], wind.to_vec());
        wind.push(&[1.0f32]);
        assert_eq!(vec![0.0f32, 0.0f32, 0.0f32, 1.0f32], wind.to_vec());
        wind.push(&[]);
        assert_eq!(vec![0.0f32, 0.0f32, 0.0f32, 1.0f32], wind.to_vec());

        wind.push(&[2.0f32]);
        assert_eq!(vec![0.0f32, 0.0f32, 1.0f32, 2.0f32], wind.to_vec());

        wind.push(&[-1.0f32, -2.0f32, 1.0f32, 2.0f32, 3.0f32, 4.0f32]);
        assert_eq!(vec![1.0f32, 2.0f32, 3.0f32, 4.0f32], wind.to_vec());
        assert_eq!(4.0f32, wind.back());
        assert_eq!(1.0f32, wind.front());
        assert_eq!(4, wind.len());

        // push individual samples works too
        assert_eq!(1.0f32, wind.push_scalar(10.0f32));
        assert_eq!(4, wind.len());
        assert_eq!(vec![2.0f32, 3.0f32, 4.0f32, 10.0f32], wind.to_vec());

        // exactly enough to fill
        wind.push(&[5.0f32, 4.0f32, 3.0f32, 2.0f32]);
        assert_eq!(vec![5.0f32, 4.0f32, 3.0f32, 2.0f32], wind.to_vec());

        wind.reset();
        assert_eq!(4, wind.len());
        assert_eq!(vec![0.0f32, 0.0f32, 0.0f32, 0.0f32], wind.to_vec());
    }
}