idsp 0.22.1

DSP algorithms for embedded, mostly integer math
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! Statically sized linear Kalman filters with scalar measurements.
//!
//! The implementation uses raw values for the estimate, covariance, and noise,
//! and coefficient values for `F`, `H`, and the Kalman gain.
//! Consequently fixed-point operation follows the same widened dot-product
//! pattern as [`crate::iir::Biquad`]: `coefficient * raw` products accumulate
//! wide and quantize once per result.
//!
//! [`RandomWalk`] is the smallest complete filter and an ordinary scalar DSP
//! stage:
//!
//! ```
//! use dsp_process::{Process, Split};
//! use idsp::kalman::{Estimate, RandomWalk};
//!
//! let config = RandomWalk::<f32>::new(0.01, 1.0);
//! let mut filter = Split::new(config, Estimate::new([0.0], [[10.0]]));
//! let filtered = filter.process(2.0);
//! assert!(filtered > 0.0 && filtered < 2.0);
//! ```
//!
//! Use [`ConstantVelocity`] with [`Direct`] for the common position sensor;
//! use [`Transition`] and [`Observation`] only when the matrices are dense.

use core::{
    iter::Sum,
    marker::PhantomData,
    ops::{Add, Mul, Sub},
};

use dsp_fixedpoint::FromRatio;
use dsp_process::{Map, SplitInplace, SplitProcess};
use num_traits::{AsPrimitive, ConstOne, ConstZero};

/// State estimate and its error covariance.
///
/// Fixed-point models must normalize all state components and measurements to
/// compatible numerical ranges. `covariance`, process noise, and measurement
/// noise must use one common raw covariance scale.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Estimate<T, const N: usize> {
    /// Estimated state vector.
    pub state: [T; N],
    /// Symmetric state-error covariance matrix.
    ///
    /// The transition reads the complete matrix. Prediction and correction
    /// restore exact symmetry by computing the upper triangle and mirroring it.
    pub covariance: [[T; N]; N],
}

impl<T, const N: usize> Estimate<T, N> {
    /// Construct an estimate from a state and covariance.
    #[must_use]
    pub const fn new(state: [T; N], covariance: [[T; N]; N]) -> Self {
        Self { state, covariance }
    }
}

/// Linear state transition model.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Transition<C, const N: usize, T = C> {
    /// State transition matrix, conventionally `F`.
    pub matrix: [[C; N]; N],
    /// Symmetric process-noise covariance, conventionally `Q`.
    ///
    /// Only the upper triangle is used.
    pub noise: [[T; N]; N],
}

impl<C, const N: usize, T> Transition<C, N, T> {
    /// Construct a transition model.
    #[must_use]
    pub const fn new(matrix: [[C; N]; N], noise: [[T; N]; N]) -> Self {
        Self { matrix, noise }
    }

    /// Advance an estimate through this transition model.
    pub fn predict<A>(&self, estimate: &mut Estimate<T, N>)
    where
        T: 'static + Copy,
        C: Copy + ConstOne + Mul<T, Output = A>,
        A: Add<Output = A> + Sum + AsPrimitive<T>,
    {
        const { assert!(N > 0, "a Kalman filter needs at least one state") }

        let state = estimate.state;
        for (row, output) in self.matrix.iter().zip(&mut estimate.state) {
            let sum: A = row.iter().zip(&state).map(|(&a, &b)| a * b).sum();
            *output = sum.as_();
        }

        // Store F*P. This is the only covariance-sized scratch matrix.
        let mut fp = [[estimate.covariance[0][0]; N]; N];
        for (i, row) in fp.iter_mut().enumerate() {
            for (j, output) in row.iter_mut().enumerate() {
                let sum: A = self.matrix[i]
                    .iter()
                    .zip(&estimate.covariance)
                    .map(|(&a, row)| a * row[j])
                    .sum();
                *output = sum.as_();
            }
        }

        // F*P*F' + Q is symmetric. Compute one triangle and mirror it.
        for (i, fp) in fp.iter().enumerate() {
            for j in i..N {
                let sum: A = self.matrix[j].iter().zip(fp).map(|(&a, &b)| a * b).sum();
                let covariance = (sum + C::ONE * self.noise[i][j]).as_();
                estimate.covariance[i][j] = covariance;
                estimate.covariance[j][i] = covariance;
            }
        }
    }
}

impl<C, const N: usize, T> Transition<C, N, T>
where
    C: Copy + ConstOne + ConstZero,
    T: Copy,
{
    /// Construct an identity transition with the supplied process noise.
    #[must_use]
    pub fn identity(noise: [[T; N]; N]) -> Self {
        let mut matrix = [[C::ZERO; N]; N];
        for (i, row) in matrix.iter_mut().enumerate() {
            row[i] = C::ONE;
        }
        Self::new(matrix, noise)
    }
}

/// Linear scalar observation model.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Observation<C, const N: usize, T = C> {
    /// Scalar measurement matrix row, conventionally `H`.
    pub matrix: [C; N],
    /// Scalar measurement-noise variance, conventionally `R`.
    pub noise: T,
}

impl<C, const N: usize, T> Observation<C, N, T> {
    /// Construct a scalar observation model.
    #[must_use]
    pub const fn new(matrix: [C; N], noise: T) -> Self {
        Self { matrix, noise }
    }

    /// Project the state estimate into measurement space.
    #[must_use]
    pub fn project<A>(&self, estimate: &Estimate<T, N>) -> T
    where
        T: 'static + Copy,
        C: Copy + Mul<T, Output = A>,
        A: Sum + AsPrimitive<T>,
    {
        const { assert!(N > 0, "a Kalman filter needs at least one state") }
        let sum: A = self
            .matrix
            .iter()
            .zip(&estimate.state)
            .map(|(&a, &b)| a * b)
            .sum();
        sum.as_()
    }

    /// Correct an estimate with one scalar measurement.
    ///
    /// Returns the posterior measurement estimate. The innovation covariance
    /// must be positive and the calculated gain must fit `C`.
    pub fn correct<A>(&self, estimate: &mut Estimate<T, N>, measurement: T) -> T
    where
        T: 'static + Copy,
        C: Copy + ConstOne + FromRatio<T> + Mul<T, Output = A>,
        A: Add<Output = A> + Sub<Output = A> + Sum + AsPrimitive<T>,
    {
        const { assert!(N > 0, "a Kalman filter needs at least one state") }

        let expected: A = self
            .matrix
            .iter()
            .zip(&estimate.state)
            .map(|(&a, &b)| a * b)
            .sum();
        let innovation = (C::ONE * measurement - expected).as_();

        // u = P*H'
        let mut cross = [estimate.covariance[0][0]; N];
        for (row, output) in estimate.covariance.iter().zip(&mut cross) {
            let sum: A = self.matrix.iter().zip(row).map(|(&a, &b)| a * b).sum();
            *output = sum.as_();
        }

        // s = H*u + R
        let variance: A = self.matrix.iter().zip(&cross).map(|(&a, &b)| a * b).sum();
        let variance = (variance + C::ONE * self.noise).as_();
        finish_correction::<C, _, _, _, N>(
            estimate,
            innovation,
            cross,
            variance,
            self.noise,
            |values| {
                let sum: A = self.matrix.iter().zip(values).map(|(&a, &b)| a * b).sum();
                sum.as_()
            },
        )
    }
}

/// Direct observation of state component `I`.
///
/// Unlike a dense [`Observation`], this carries no zero coefficients and lets
/// correction reduce `H*x`, `P*H'`, and `H*P*H'` to indexing.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Direct<C, const I: usize, T = C> {
    /// Scalar measurement-noise variance, conventionally `R`.
    pub noise: T,
    coefficient: PhantomData<fn() -> C>,
}

impl<C, const I: usize, T> Direct<C, I, T> {
    /// Construct a direct observation.
    #[must_use]
    pub const fn new(noise: T) -> Self {
        Self {
            noise,
            coefficient: PhantomData,
        }
    }

    /// Project the state estimate into measurement space.
    #[must_use]
    pub fn project<const N: usize>(&self, estimate: &Estimate<T, N>) -> T
    where
        T: Copy,
    {
        const { assert!(I < N, "state index out of range") }
        estimate.state[I]
    }

    /// Correct an estimate with one scalar measurement.
    ///
    /// Returns the posterior measurement estimate. The innovation covariance
    /// must be positive and the calculated gain must fit `C`.
    pub fn correct<A, const N: usize>(&self, estimate: &mut Estimate<T, N>, measurement: T) -> T
    where
        T: 'static + Copy,
        C: Copy + ConstOne + FromRatio<T> + Mul<T, Output = A>,
        A: Add<Output = A> + Sub<Output = A> + AsPrimitive<T>,
    {
        const { assert!(I < N, "state index out of range") }

        let innovation = (C::ONE * measurement - C::ONE * estimate.state[I]).as_();
        let mut cross = [estimate.covariance[0][I]; N];
        for (row, output) in estimate.covariance.iter().zip(&mut cross) {
            *output = row[I];
        }
        let variance = (C::ONE * cross[I] + C::ONE * self.noise).as_();
        finish_correction::<C, _, _, _, N>(
            estimate,
            innovation,
            cross,
            variance,
            self.noise,
            |values| values[I],
        )
    }
}

/// Constant-velocity motion model for `[position, velocity]`.
///
/// Its transition matrix is `[[1, interval], [0, 1]]`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ConstantVelocity<C, T = C> {
    /// Sample interval.
    pub interval: C,
    /// Symmetric process-noise covariance. Only the upper triangle is used.
    pub noise: [[T; 2]; 2],
}

impl<C, T> ConstantVelocity<C, T> {
    /// Construct a constant-velocity transition.
    #[must_use]
    pub const fn new(interval: C, noise: [[T; 2]; 2]) -> Self {
        Self { interval, noise }
    }

    /// Advance a position-velocity estimate.
    pub fn predict<A>(&self, estimate: &mut Estimate<T, 2>)
    where
        T: 'static + Copy,
        C: Copy + ConstOne + Mul<T, Output = A>,
        A: Add<Output = A> + AsPrimitive<T>,
    {
        let [[p00, p01], [p10, p11]] = estimate.covariance;
        estimate.state[0] = (C::ONE * estimate.state[0] + self.interval * estimate.state[1]).as_();

        let fp00 = (C::ONE * p00 + self.interval * p10).as_();
        let fp01 = (C::ONE * p01 + self.interval * p11).as_();
        let p00 = (C::ONE * fp00 + self.interval * fp01 + C::ONE * self.noise[0][0]).as_();
        let p01 = (C::ONE * fp01 + C::ONE * self.noise[0][1]).as_();
        let p11 = (C::ONE * p11 + C::ONE * self.noise[1][1]).as_();
        estimate.covariance = [[p00, p01], [p01, p11]];
    }
}

/// One prediction phase followed by one measurement-update phase.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Kalman<P, U> {
    /// Prediction phase.
    pub predict: P,
    /// Measurement-update phase.
    pub update: U,
}

impl<P, U> Kalman<P, U> {
    /// Compose prediction and measurement-update phases over shared state.
    #[must_use]
    pub const fn new(predict: P, update: U) -> Self {
        Self { predict, update }
    }

    /// Run prediction but skip the measurement update when input is absent.
    #[must_use]
    pub fn optional(self) -> Kalman<P, Map<U>> {
        Kalman::new(self.predict, Map(self.update))
    }
}

/// Dense linear transition and scalar measurement model.
pub type DenseKalman<C, const N: usize, T = C> = Kalman<Transition<C, N, T>, Observation<C, N, T>>;

/// Complete scalar random-walk filter with `F = H = 1`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RandomWalk<C, T = C> {
    /// Scalar process-noise variance.
    pub process_noise: T,
    /// Scalar measurement-noise variance.
    pub measurement_noise: T,
    coefficient: PhantomData<fn() -> C>,
}

impl<C, T> RandomWalk<C, T> {
    /// Construct a scalar random-walk filter.
    #[must_use]
    pub const fn new(process_noise: T, measurement_noise: T) -> Self {
        Self {
            process_noise,
            measurement_noise,
            coefficient: PhantomData,
        }
    }
}

impl<C, const N: usize, T, A> SplitProcess<(), (), Estimate<T, N>> for Transition<C, N, T>
where
    T: 'static + Copy,
    C: Copy + ConstOne + Mul<T, Output = A>,
    A: Add<Output = A> + Sum + AsPrimitive<T>,
{
    fn process(&self, estimate: &mut Estimate<T, N>, (): ()) {
        self.predict(estimate);
    }
}

impl<C, const N: usize, T, A> SplitProcess<T, T, Estimate<T, N>> for Observation<C, N, T>
where
    T: 'static + Copy,
    C: Copy + ConstOne + FromRatio<T> + Mul<T, Output = A>,
    A: Add<Output = A> + Sub<Output = A> + Sum + AsPrimitive<T>,
{
    fn process(&self, estimate: &mut Estimate<T, N>, measurement: T) -> T {
        self.correct(estimate, measurement)
    }
}

impl<C, const N: usize, T> SplitInplace<T, Estimate<T, N>> for Observation<C, N, T>
where
    T: Copy,
    Self: SplitProcess<T, T, Estimate<T, N>>,
{
}

impl<C, const I: usize, const N: usize, T, A> SplitProcess<T, T, Estimate<T, N>> for Direct<C, I, T>
where
    T: 'static + Copy,
    C: Copy + ConstOne + FromRatio<T> + Mul<T, Output = A>,
    A: Add<Output = A> + Sub<Output = A> + AsPrimitive<T>,
{
    fn process(&self, estimate: &mut Estimate<T, N>, measurement: T) -> T {
        self.correct(estimate, measurement)
    }
}

impl<C, const I: usize, const N: usize, T> SplitInplace<T, Estimate<T, N>> for Direct<C, I, T>
where
    T: Copy,
    Self: SplitProcess<T, T, Estimate<T, N>>,
{
}

impl<C, T, A> SplitProcess<(), (), Estimate<T, 2>> for ConstantVelocity<C, T>
where
    T: 'static + Copy,
    C: Copy + ConstOne + Mul<T, Output = A>,
    A: Add<Output = A> + AsPrimitive<T>,
{
    fn process(&self, estimate: &mut Estimate<T, 2>, (): ()) {
        self.predict(estimate);
    }
}

impl<X: Copy, Y, S, P, U> SplitProcess<X, Y, S> for Kalman<P, U>
where
    P: SplitProcess<(), (), S>,
    U: SplitProcess<X, Y, S>,
{
    fn process(&self, state: &mut S, input: X) -> Y {
        self.predict.process(state, ());
        self.update.process(state, input)
    }
}

impl<X: Copy, S, P, U> SplitInplace<X, S> for Kalman<P, U> where Self: SplitProcess<X, X, S> {}

impl<C, T, A> SplitProcess<T, T, Estimate<T, 1>> for RandomWalk<C, T>
where
    T: 'static + Copy,
    C: Copy + ConstOne + FromRatio<T> + Mul<T, Output = A>,
    A: Add<Output = A> + Sub<Output = A> + AsPrimitive<T>,
{
    fn process(&self, estimate: &mut Estimate<T, 1>, measurement: T) -> T {
        estimate.covariance[0][0] =
            (C::ONE * estimate.covariance[0][0] + C::ONE * self.process_noise).as_();
        Direct::<C, 0, T>::new(self.measurement_noise).correct(estimate, measurement)
    }
}

impl<C, T> SplitInplace<T, Estimate<T, 1>> for RandomWalk<C, T>
where
    T: Copy,
    Self: SplitProcess<T, T, Estimate<T, 1>>,
{
}

fn finish_correction<C, T, A, F, const N: usize>(
    estimate: &mut Estimate<T, N>,
    innovation: T,
    cross: [T; N],
    variance: T,
    noise: T,
    project: F,
) -> T
where
    T: 'static + Copy,
    C: Copy + ConstOne + FromRatio<T> + Mul<T, Output = A>,
    A: Add<Output = A> + Sub<Output = A> + AsPrimitive<T>,
    F: Fn(&[T; N]) -> T,
{
    let gain = C::from_ratios(cross, variance);

    for (state, gain) in estimate.state.iter_mut().zip(gain.iter().copied()) {
        *state = (C::ONE * *state + gain * innovation).as_();
    }

    // Scalar Joseph update. P1 = P - K*(P*H')'.
    for (i, gain) in gain.iter().copied().enumerate() {
        for (j, cross) in cross.iter().copied().enumerate() {
            estimate.covariance[i][j] = (C::ONE * estimate.covariance[i][j] - gain * cross).as_();
        }
    }

    // v = P1*H'.
    let mut projected = [project(&estimate.covariance[0]); N];
    for (row, output) in estimate.covariance.iter().zip(&mut projected) {
        *output = project(row);
    }

    // P+ = P1 - v*K' + (K*R)*K'. Compute one triangle and mirror it.
    for (i, gain_i) in gain.iter().copied().enumerate() {
        let weighted_noise = (gain_i * noise).as_();
        for (j, gain_j) in gain.iter().copied().enumerate().skip(i) {
            let covariance = (C::ONE * estimate.covariance[i][j] - gain_j * projected[i]
                + gain_j * weighted_noise)
                .as_();
            estimate.covariance[i][j] = covariance;
            estimate.covariance[j][i] = covariance;
        }
    }

    project(&estimate.state)
}

#[cfg(test)]
mod tests {
    use super::*;
    use dsp_fixedpoint::Q32;
    use dsp_process::{Process, Split};

    const EPS: f64 = 1e-12;

    fn assert_close(actual: f64, expected: f64) {
        assert!((actual - expected).abs() < EPS, "{actual} != {expected}");
    }

    #[test]
    fn scalar_reference() {
        let mut filter = Split::new(
            RandomWalk::<f64>::new(0.25, 4.0),
            Estimate::new([2.0], [[9.0]]),
        );

        let output = filter.process(5.0);
        let prior_covariance = 9.25;
        let gain = prior_covariance / (prior_covariance + 4.0);
        let expected = 2.0 + gain * (5.0 - 2.0);
        let expected_covariance = (1.0 - gain) * prior_covariance;

        assert_close(output, expected);
        assert_close(filter.state.state[0], expected);
        assert_close(filter.state.covariance[0][0], expected_covariance);
    }

    #[test]
    fn transition_and_observation_match_combined_process() {
        let transition = ConstantVelocity::new(1.0, [[0.1, 0.0], [0.0, 0.1]]);
        let observation = Direct::<f64, 0>::new(0.5);
        let config = Kalman::new(transition, observation);
        let estimate = Estimate::new([0.0, 1.0], [[2.0, 0.0], [0.0, 1.0]]);
        let mut separate = estimate;
        let mut combined = Split::new(config, estimate);

        transition.predict(&mut separate);
        assert_eq!(separate.state, [1.0, 1.0]);
        assert_eq!(separate.covariance, [[3.1, 1.0], [1.0, 1.1]]);
        let expected = observation.correct(&mut separate, 1.25);
        let actual = combined.process(1.25);

        assert_eq!(actual, expected);
        assert_eq!(combined.state, separate);
        assert_eq!(
            combined.state.covariance[0][1],
            combined.state.covariance[1][0]
        );
    }

    #[test]
    fn direct_matches_dense() {
        let estimate = Estimate::new([2.0, -1.0], [[4.0, 1.5], [1.5, 3.0]]);
        let mut direct = estimate;
        let mut dense = estimate;

        let direct_output = Direct::<f64, 0>::new(0.75).correct(&mut direct, 3.5);
        let dense_output = Observation::new([1.0, 0.0], 0.75).correct(&mut dense, 3.5);

        assert_eq!(direct_output, dense_output);
        assert_eq!(direct, dense);
    }

    #[test]
    fn joseph_matches_closed_form() {
        let observation = Observation::new([1.25, -0.5], 0.75);
        let prior = Estimate::new([2.0, -1.0], [[4.0, 1.5], [1.5, 3.0]]);
        let mut actual = prior;

        let output = observation.correct(&mut actual, 3.5);

        let h = observation.matrix;
        let u = [
            prior.covariance[0][0] * h[0] + prior.covariance[0][1] * h[1],
            prior.covariance[1][0] * h[0] + prior.covariance[1][1] * h[1],
        ];
        let variance = h[0] * u[0] + h[1] * u[1] + observation.noise;
        let gain = [u[0] / variance, u[1] / variance];
        let innovation = 3.5 - h[0] * prior.state[0] - h[1] * prior.state[1];
        let state = [
            prior.state[0] + gain[0] * innovation,
            prior.state[1] + gain[1] * innovation,
        ];
        for (actual, expected) in actual.state.iter().zip(state) {
            assert_close(*actual, expected);
        }
        for (i, (actual, prior)) in actual.covariance.iter().zip(prior.covariance).enumerate() {
            for (j, (actual, prior)) in actual.iter().zip(prior).enumerate() {
                assert_close(*actual, prior - gain[i] * u[j]);
            }
        }
        assert_close(output, h[0] * state[0] + h[1] * state[1]);
    }

    #[test]
    fn optional_measurement_still_predicts() {
        let config = Kalman::new(
            ConstantVelocity::new(1.0, [[0.0; 2]; 2]),
            Direct::<f32, 0>::new(1.0),
        )
        .optional();
        let mut filter = Split::new(config, Estimate::new([2.0, 3.0], [[1.0, 0.0], [0.0, 1.0]]));

        let output = filter.process(None);

        assert_eq!(output, None);
        assert_eq!(filter.state.state, [5.0, 3.0]);
    }

    #[test]
    fn fixed_point_tracks_float() {
        type C = Q32<28>;

        let transition = ConstantVelocity::new(C::from_f32(0.25), [[16, 0], [0, 4]]);
        let observation = Direct::<C, 0, i32>::new(64);
        let mut fixed = Split::new(
            Kalman::new(transition, observation),
            Estimate::new([0, 40], [[256, 0], [0, 64]]),
        );
        let float_transition = ConstantVelocity::new(0.25, [[16.0, 0.0], [0.0, 4.0]]);
        let float_observation = Direct::<f64, 0>::new(64.0);
        let mut float = Split::new(
            Kalman::new(float_transition, float_observation),
            Estimate::new([0.0, 40.0], [[256.0, 0.0], [0.0, 64.0]]),
        );

        for measurement in [12, 24, 37, 49, 63, 74] {
            let fixed_output = fixed.process(measurement);
            let float_output = float.process(measurement as f64);
            assert!((fixed_output as f64 - float_output).abs() < 3.0);
        }

        assert_eq!(fixed.state.covariance[0][1], fixed.state.covariance[1][0]);
    }
}