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
use super::*;
pub struct BandPass {
central_f: MathT,
quality: MathT,
a0: MathT,
b1: MathT,
b2: MathT,
x1: SampleT,
x2: SampleT,
y1: SampleT,
y2: SampleT,
}
impl BandPass {
pub fn new(f: MathT, q: MathT) -> Self {
let mut bp = BandPass {
central_f: f,
quality: q,
a0: 0.0,
b1: 0.0,
b2: 0.0,
x1: SampleT::default(),
x2: SampleT::default(),
y1: SampleT::default(),
y2: SampleT::default(),
};
bp.reset();
bp
}
pub fn from_corners(f: (MathT,MathT)) -> Self {
let mut bp = BandPass {
central_f: (f.0*f.1).abs().sqrt(),
quality: (f.0*f.1).abs().sqrt()/(f.1-f.0).abs(),
a0: 0.0,
b1: 0.0,
b2: 0.0,
x1: SampleT::default(),
x2: SampleT::default(),
y1: SampleT::default(),
y2: SampleT::default(),
};
bp.reset();
bp
}
pub fn get_central_frequency(&self) -> MathT {
self.central_f
}
pub fn set_central_frequency(&mut self, f: MathT) {
self.central_f = f;
self.reset();
}
pub fn get_quality(&self) -> MathT {
self.quality
}
pub fn set_quality(&mut self, q: MathT) {
self.quality = q;
self.reset();
}
pub fn get_corner_frequencies(&self) -> (MathT,MathT) {
let b = -self.central_f/self.quality;
let (p,n) = quadratic(1.0, b, -self.central_f*self.central_f);
let fl = if p > 0.0 {
p
} else {
n
};
let fh = fl + b;
if fl < fh {
(fl, fh)
} else {
(fh, fl)
}
}
pub fn set_corner_frequencies(&mut self, f: (MathT,MathT)) {
self.central_f = (f.0 * f.1).sqrt();
self.quality = self.central_f/(f.0-f.1).abs();
self.reset();
}
fn reset(&mut self) {
let (fh, fl) = self.get_corner_frequencies();
let theta_l = (std::f64::consts::PI * fl * INV_SAMPLE_RATE).tan();
let theta_h = (std::f64::consts::PI * fh * INV_SAMPLE_RATE).tan();
let al = 1.0 / (1.0+theta_l);
let ah = 1.0 / (1.0+theta_h);
let bl = (1.0-theta_l) / (1.0+theta_l);
let bh = (1.0-theta_h) / (1.0+theta_h);
self.a0 = (1.0-al) * ah;
self.b1 = bl + bh;
self.b2 = bl * bh;
}
}
impl Modifier for BandPass {
fn process(&mut self, x: SampleT) -> SampleT {
let y = (self.a0 * (x - self.x2) as MathT +
self.b1 * self.y1 as MathT -
self.b2 * self.y2 as MathT) as SampleT;
self.y2 = self.y1;
self.y1 = y;
self.x2 = self.x1;
self.x1 = x;
y
}
}
fn quadratic(a: MathT, b: MathT, c: MathT) -> (MathT,MathT) {
(
(-b + (b*b - 4.0*a*c).sqrt())/(2.0*a),
(-b - (b*b - 4.0*a*c).sqrt())/(2.0*a)
)
}
impl Clone for BandPass {
fn clone(&self) -> Self {
BandPass {
central_f: self.central_f,
quality: self.quality,
a0: self.a0,
b1: self.b1,
b2: self.b2,
x1: SampleT::default(),
x2: SampleT::default(),
y1: SampleT::default(),
y2: SampleT::default(),
}
}
}