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
/*!
| A notch filter is a type of filter used to
| attenuate a narrow band of frequencies around
| a center frequency while allowing other
| frequencies to pass through unaffected. It is
| also called a band-stop filter or reject
| filter. The attenuation at the center
| frequency is typically very high, often around
| 60 dB or more.
|
| The transfer function of a second-order notch
| filter can be written as:
|
| ```
| H(s) = (s^2 + w0^2) / (s^2 + alpha*s + w0^2)
| ```
|
| where `w0` is the center frequency of the
| notch, `alpha` is a damping factor, and `s` is
| the Laplace variable.
|
| In practice, this transfer function is usually
| implemented as a difference equation, or a set
| of coefficients that define a difference
| equation. A second-order notch filter can be
| implemented using a biquad filter, which is
| a type of IIR filter that uses two poles and
| two zeros.
|
| The difference equation for a second-order
| biquad notch filter is:
|
| ```
| y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] - a1*y[n-1] - a2*y[n-2]
| ```
|
| where `x[n]` and `y[n]` are the input and
| output samples at time `n`, respectively, and
| `b0`, `b1`, `b2`, `a1`, and `a2` are the
| filter coefficients.
|
| The coefficients for a biquad notch filter can
| be calculated from the center frequency `w0`
| and damping factor `alpha` using the following
| formulas:
|
| ```
| b0 = 1
| b1 = -2*cos(w0)
| b2 = 1
| a0 = 1 + alpha
| a1 = -2*cos(w0)
| a2 = 1 - alpha
| ```
|
| where `cos(w0)` is the cosine of the center
| frequency `w0` and is given by:
|
| ```
| cos(w0) = (1 - tan^2(w0/2)) / (1 + tan^2(w0/2))
| ```
|
| where `tan(w0/2)` is the tangent of half the center frequency.
|
| In summary, a notch filter is a filter that
| attenuates a narrow band of frequencies around
| a center frequency. It can be implemented as
| a biquad filter using a set of difference
| equations, or as a set of coefficients that
| define the difference equation. The center
| frequency and damping factor of the notch are
| used to calculate the filter coefficients.
|
*/
crateix!;
/// Implements the BiquadCoeffNotch trait for the
/// BiquadFilter struct.
///