augmented_dsp_filters/
state.rs

1// Augmented Audio: Audio libraries and applications
2// Copyright (c) 2022 Pedro Tacla Yamada
3//
4// The MIT License (MIT)
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22// THE SOFTWARE.
23use num::Float;
24
25use crate::coefficients::BiquadCoefficients;
26
27pub trait FilterState {
28    type Sample: Float;
29
30    fn reset(&mut self);
31    fn process1(
32        &mut self,
33        coefficients: &BiquadCoefficients<Self::Sample>,
34        input: Self::Sample,
35        very_small_amount: Self::Sample,
36    ) -> Self::Sample;
37}
38
39/// State for applying a second order section to a sample using Direct Form I
40/// Difference equation:
41///
42/// ```ignore
43/// y[n] = (b0/a0)*x[n] + (b1/a0)*x[n-1] + (b2/a0)*x[n-2]
44///                     - (a1/a0)*y[n-1] - (a2/a0)*y[n-2]
45/// ```
46pub struct DirectFormIState<Sample: Float> {
47    x2: Sample, // x[n - 2]
48    y2: Sample, // y[n - 2]
49    x1: Sample, // x[n - 1]
50    y1: Sample, // y[n - 1]
51}
52
53impl<Sample: Float> Default for DirectFormIState<Sample> {
54    fn default() -> Self {
55        Self::new()
56    }
57}
58
59impl<Sample: Float> DirectFormIState<Sample> {
60    pub fn new() -> Self {
61        DirectFormIState {
62            x2: Sample::zero(),
63            y2: Sample::zero(),
64            x1: Sample::zero(),
65            y1: Sample::zero(),
66        }
67    }
68}
69
70impl<Sample: Float> FilterState for DirectFormIState<Sample> {
71    type Sample = Sample;
72
73    fn reset(&mut self) {
74        self.x1 = Sample::zero();
75        self.x2 = Sample::zero();
76        self.y1 = Sample::zero();
77        self.y2 = Sample::zero();
78    }
79
80    fn process1(
81        &mut self,
82        coefficients: &BiquadCoefficients<Sample>,
83        input: Sample,
84        very_small_amount: Sample,
85    ) -> Sample {
86        let output = {
87            let BiquadCoefficients {
88                b0, b1, b2, a1, a2, ..
89            } = *coefficients;
90            let DirectFormIState { x1, x2, y2, y1 } = *self;
91            b0 * input + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2 + very_small_amount
92        };
93        self.x2 = self.x1;
94        self.y2 = self.y1;
95        self.x1 = input;
96        self.y1 = output;
97        assert!(!self.y1.is_nan());
98        assert!(!self.y2.is_nan());
99
100        output
101    }
102}
103
104/// State for applying a second order section to a sample using Direct Form II
105///
106/// Difference equation:
107///
108/// ```ignore
109/// v[n] =         x[n] - (a1/a0)*v[n-1] - (a2/a0)*v[n-2]
110/// y(n) = (b0/a0)*v[n] + (b1/a0)*v[n-1] + (b2/a0)*v[n-2]
111/// ```
112pub struct DirectFormIIState<Sample: Float> {
113    v1: Sample, // v[-1]
114    v2: Sample, // v[-2]
115}
116
117impl<Sample: Float> FilterState for DirectFormIIState<Sample> {
118    type Sample = Sample;
119
120    fn reset(&mut self) {
121        self.v1 = Sample::zero();
122        self.v2 = Sample::zero();
123    }
124
125    fn process1(
126        &mut self,
127        coefficients: &BiquadCoefficients<Sample>,
128        input: Sample,
129        very_small_amount: Sample,
130    ) -> Sample {
131        let BiquadCoefficients {
132            a1, a2, b0, b1, b2, ..
133        } = *coefficients;
134        let DirectFormIIState { v1, v2 } = *self;
135        let w = input - a1 * v1 - a2 * v2 + very_small_amount;
136        let output = b0 * w + b1 * v1 + b2 * v2;
137
138        self.v2 = v1;
139        self.v1 = w;
140
141        output
142    }
143}