tomwalters@268
|
1 // Copyright 2008-2010, Thomas Walters
|
tomwalters@268
|
2 //
|
tomwalters@268
|
3 // AIM-C: A C++ implementation of the Auditory Image Model
|
tomwalters@268
|
4 // http://www.acousticscale.org/AIMC
|
tomwalters@268
|
5 //
|
tomwalters@318
|
6 // Licensed under the Apache License, Version 2.0 (the "License");
|
tomwalters@318
|
7 // you may not use this file except in compliance with the License.
|
tomwalters@318
|
8 // You may obtain a copy of the License at
|
tomwalters@268
|
9 //
|
tomwalters@318
|
10 // http://www.apache.org/licenses/LICENSE-2.0
|
tomwalters@268
|
11 //
|
tomwalters@318
|
12 // Unless required by applicable law or agreed to in writing, software
|
tomwalters@318
|
13 // distributed under the License is distributed on an "AS IS" BASIS,
|
tomwalters@318
|
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
tomwalters@318
|
15 // See the License for the specific language governing permissions and
|
tomwalters@318
|
16 // limitations under the License.
|
tomwalters@268
|
17
|
tomwalters@268
|
18 /*! \file
|
tomwalters@268
|
19 * \brief Dick Lyon's Pole-Zero Filter Cascade - implemented as an AIM-C
|
tomwalters@268
|
20 * module by Tom Walters from the AIM-MAT module based on Dick Lyon's code
|
tomwalters@268
|
21 */
|
tomwalters@268
|
22
|
tomwalters@268
|
23 /*! \author Thomas Walters <tom@acousticscale.org>
|
tomwalters@268
|
24 * \date created 2008/02/05
|
tomwalters@296
|
25 * \version \$Id$
|
tomwalters@268
|
26 */
|
tomwalters@268
|
27
|
tomwalters@268
|
28 #include "Support/ERBTools.h"
|
tomwalters@268
|
29
|
tomwalters@268
|
30 #include "Modules/BMM/ModulePZFC.h"
|
tomwalters@268
|
31
|
tomwalters@268
|
32 namespace aimc {
|
tomwalters@268
|
33 ModulePZFC::ModulePZFC(Parameters *parameters) : Module(parameters) {
|
tomwalters@268
|
34 module_identifier_ = "pzfc";
|
tomwalters@268
|
35 module_type_ = "bmm";
|
tomwalters@268
|
36 module_description_ = "Pole-Zero Filter Cascade";
|
tomwalters@296
|
37 module_version_ = "$Id$";
|
tomwalters@268
|
38
|
tomwalters@268
|
39 // Get parameter values, setting default values where necessary
|
tomwalters@268
|
40 // Each parameter is set here only if it has not already been set elsewhere.
|
tomwalters@268
|
41 cf_max_ = parameters_->DefaultFloat("pzfc.highest_frequency", 6000.0f);
|
tomwalters@268
|
42 cf_min_ = parameters_->DefaultFloat("pzfc.lowest_frequency", 100.0f);
|
tomwalters@268
|
43 pole_damping_ = parameters_->DefaultFloat("pzfc.pole_damping", 0.12f);
|
tomwalters@268
|
44 zero_damping_ = parameters_->DefaultFloat("pzfc.zero_damping", 0.2f);
|
tomwalters@268
|
45 zero_factor_ = parameters_->DefaultFloat("pzfc.zero_factor", 1.4f);
|
tomwalters@268
|
46 step_factor_ = parameters_->DefaultFloat("pzfc.step_factor", 1.0f/3.0f);
|
tomwalters@268
|
47 bandwidth_over_cf_ = parameters_->DefaultFloat("pzfc.bandwidth_over_cf",
|
tomwalters@268
|
48 0.11f);
|
tomwalters@268
|
49 min_bandwidth_hz_ = parameters_->DefaultFloat("pzfc.min_bandwidth_hz",
|
tomwalters@268
|
50 27.0f);
|
tomwalters@268
|
51 agc_factor_ = parameters_->DefaultFloat("pzfc.agc_factor", 12.0f);
|
tomwalters@268
|
52 do_agc_step_ = parameters_->DefaultBool("pzfc.do_agc", true);
|
tomwalters@321
|
53 use_fitted_parameters_ = parameters_->DefaultBool("pzfc.use_fit", false);
|
tomwalters@268
|
54
|
tomwalters@268
|
55 detect_.resize(0);
|
tomwalters@268
|
56 }
|
tomwalters@268
|
57
|
tomwalters@268
|
58 ModulePZFC::~ModulePZFC() {
|
tomwalters@268
|
59 }
|
tomwalters@268
|
60
|
tomwalters@268
|
61 bool ModulePZFC::InitializeInternal(const SignalBank &input) {
|
tomwalters@268
|
62 // Make local convenience copies of some variables
|
tomwalters@268
|
63 sample_rate_ = input.sample_rate();
|
tomwalters@268
|
64 buffer_length_ = input.buffer_length();
|
tomwalters@268
|
65 channel_count_ = 0;
|
tomwalters@268
|
66
|
tomwalters@268
|
67 // Prepare the coefficients and also the output SignalBank
|
tomwalters@268
|
68 if (!SetPZBankCoeffs())
|
tomwalters@268
|
69 return false;
|
tomwalters@268
|
70
|
tomwalters@268
|
71 // The output signal bank should be set up by now.
|
tomwalters@268
|
72 if (!output_.initialized())
|
tomwalters@268
|
73 return false;
|
tomwalters@268
|
74
|
tomwalters@268
|
75 // This initialises all buffers which can be modified by Process()
|
tomwalters@275
|
76 ResetInternal();
|
tomwalters@268
|
77
|
tomwalters@268
|
78 return true;
|
tomwalters@268
|
79 }
|
tomwalters@268
|
80
|
tomwalters@275
|
81 void ModulePZFC::ResetInternal() {
|
tomwalters@268
|
82 // These buffers may be actively modified by the algorithm
|
tomwalters@268
|
83 agc_state_.clear();
|
tomwalters@268
|
84 agc_state_.resize(channel_count_);
|
tomwalters@268
|
85 for (int i = 0; i < channel_count_; ++i) {
|
tomwalters@268
|
86 agc_state_[i].clear();
|
tomwalters@268
|
87 agc_state_[i].resize(agc_stage_count_, 0.0f);
|
tomwalters@268
|
88 }
|
tomwalters@268
|
89
|
tomwalters@268
|
90 state_1_.clear();
|
tomwalters@268
|
91 state_1_.resize(channel_count_, 0.0f);
|
tomwalters@268
|
92
|
tomwalters@268
|
93 state_2_.clear();
|
tomwalters@268
|
94 state_2_.resize(channel_count_, 0.0f);
|
tomwalters@268
|
95
|
tomwalters@268
|
96 previous_out_.clear();
|
tomwalters@268
|
97 previous_out_.resize(channel_count_, 0.0f);
|
tomwalters@268
|
98
|
tomwalters@268
|
99 pole_damps_mod_.clear();
|
tomwalters@268
|
100 pole_damps_mod_.resize(channel_count_, 0.0f);
|
tomwalters@268
|
101
|
tomwalters@268
|
102 inputs_.clear();
|
tomwalters@268
|
103 inputs_.resize(channel_count_, 0.0f);
|
tomwalters@268
|
104
|
tomwalters@268
|
105 // Init AGC
|
tomwalters@268
|
106 AGCDampStep();
|
tomwalters@268
|
107 // pole_damps_mod_ and agc_state_ are now be initialized
|
tomwalters@268
|
108
|
tomwalters@268
|
109 // Modify the pole dampings and AGC state slightly from their values in
|
tomwalters@268
|
110 // silence in case the input is abuptly loud.
|
tomwalters@268
|
111 for (int i = 0; i < channel_count_; ++i) {
|
tomwalters@268
|
112 pole_damps_mod_[i] += 0.05f;
|
tomwalters@268
|
113 for (int j = 0; j < agc_stage_count_; ++j)
|
tomwalters@268
|
114 agc_state_[i][j] += 0.05f;
|
tomwalters@268
|
115 }
|
tomwalters@268
|
116
|
tomwalters@268
|
117 last_input_ = 0.0f;
|
tomwalters@268
|
118 }
|
tomwalters@268
|
119
|
tomwalters@321
|
120 bool ModulePZFC::SetPZBankCoeffsOrig() {
|
tomwalters@321
|
121 // This function sets the following variables:
|
tomwalters@321
|
122 // channel_count_
|
tomwalters@321
|
123 // pole_dampings_
|
tomwalters@321
|
124 // pole_frequencies_
|
tomwalters@321
|
125 // za0_, za1_, za2
|
tomwalters@321
|
126 // output_
|
tomwalters@321
|
127
|
tomwalters@321
|
128 // TODO(tomwalters): There's significant code-duplication between this function
|
tomwalters@321
|
129 // and SetPZBankCoeffsERBFitted, and SetPZBankCoeffs
|
tomwalters@321
|
130
|
tomwalters@321
|
131 // Normalised maximum pole frequency
|
tomwalters@321
|
132 float pole_frequency = cf_max_ / sample_rate_ * (2.0f * M_PI);
|
tomwalters@321
|
133 channel_count_ = 0;
|
tomwalters@321
|
134 while ((pole_frequency / (2.0f * M_PI)) * sample_rate_ > cf_min_) {
|
tomwalters@398
|
135 float bw = bandwidth_over_cf_ * pole_frequency + 2 * M_PI * min_bandwidth_hz_ / sample_rate_;
|
tomwalters@321
|
136 pole_frequency -= step_factor_ * bw;
|
tomwalters@321
|
137 channel_count_++;
|
tomwalters@321
|
138 }
|
tomwalters@321
|
139
|
tomwalters@321
|
140 // Now the number of channels is known, various buffers for the filterbank
|
tomwalters@321
|
141 // coefficients can be initialised
|
tomwalters@321
|
142 pole_dampings_.clear();
|
tomwalters@321
|
143 pole_dampings_.resize(channel_count_, pole_damping_);
|
tomwalters@321
|
144 pole_frequencies_.clear();
|
tomwalters@321
|
145 pole_frequencies_.resize(channel_count_, 0.0f);
|
tomwalters@321
|
146
|
tomwalters@321
|
147 // Direct-form coefficients
|
tomwalters@321
|
148 za0_.clear();
|
tomwalters@321
|
149 za0_.resize(channel_count_, 0.0f);
|
tomwalters@321
|
150 za1_.clear();
|
tomwalters@321
|
151 za1_.resize(channel_count_, 0.0f);
|
tomwalters@321
|
152 za2_.clear();
|
tomwalters@321
|
153 za2_.resize(channel_count_, 0.0f);
|
tomwalters@321
|
154
|
tomwalters@321
|
155 // The output signal bank
|
tomwalters@321
|
156 output_.Initialize(channel_count_, buffer_length_, sample_rate_);
|
tomwalters@321
|
157
|
tomwalters@321
|
158 // Reset the pole frequency to maximum
|
tomwalters@321
|
159 pole_frequency = cf_max_ / sample_rate_ * (2.0f * M_PI);
|
tomwalters@321
|
160
|
tomwalters@321
|
161 for (int i = channel_count_ - 1; i > -1; --i) {
|
tomwalters@321
|
162 // Store the normalised pole frequncy
|
tomwalters@321
|
163 pole_frequencies_[i] = pole_frequency;
|
tomwalters@321
|
164
|
tomwalters@321
|
165 // Calculate the real pole frequency from the normalised pole frequency
|
tomwalters@321
|
166 float frequency = pole_frequency / (2.0f * M_PI) * sample_rate_;
|
tomwalters@321
|
167
|
tomwalters@321
|
168 // Store the real pole frequency as the 'centre frequency' of the filterbank
|
tomwalters@321
|
169 // channel
|
tomwalters@321
|
170 output_.set_centre_frequency(i, frequency);
|
tomwalters@321
|
171
|
tom@440
|
172 float zero_frequency = Minimum(M_PI, zero_factor_ * pole_frequency);
|
tomwalters@321
|
173
|
tomwalters@321
|
174 // Impulse-invariance mapping
|
tomwalters@321
|
175 float z_plane_theta = zero_frequency * sqrt(1.0f - pow(zero_damping_, 2));
|
tomwalters@321
|
176 float z_plane_rho = exp(-zero_damping_ * zero_frequency);
|
tomwalters@321
|
177
|
tomwalters@321
|
178 // Direct-form coefficients from z-plane rho and theta
|
tomwalters@321
|
179 float a1 = -2.0f * z_plane_rho * cos(z_plane_theta);
|
tomwalters@321
|
180 float a2 = z_plane_rho * z_plane_rho;
|
tomwalters@321
|
181
|
tomwalters@321
|
182 // Normalised to unity gain at DC
|
tomwalters@321
|
183 float a_sum = 1.0f + a1 + a2;
|
tomwalters@321
|
184 za0_[i] = 1.0f / a_sum;
|
tomwalters@321
|
185 za1_[i] = a1 / a_sum;
|
tomwalters@321
|
186 za2_[i] = a2 / a_sum;
|
tomwalters@321
|
187
|
tomwalters@321
|
188 // Subtract step factor (1/n2) times current bandwidth from the pole
|
tomwalters@321
|
189 // frequency
|
tomwalters@398
|
190 float bw = bandwidth_over_cf_ * pole_frequency + 2 * M_PI * min_bandwidth_hz_ / sample_rate_;
|
tomwalters@321
|
191 pole_frequency -= step_factor_ * bw;
|
tomwalters@321
|
192 }
|
tomwalters@321
|
193 return true;
|
tomwalters@321
|
194 }
|
tomwalters@321
|
195
|
tomwalters@321
|
196
|
tomwalters@321
|
197 bool ModulePZFC::SetPZBankCoeffsERB() {
|
tomwalters@321
|
198 // This function sets the following variables:
|
tomwalters@321
|
199 // channel_count_
|
tomwalters@321
|
200 // pole_dampings_
|
tomwalters@321
|
201 // pole_frequencies_
|
tomwalters@321
|
202 // za0_, za1_, za2
|
tomwalters@321
|
203 // output_
|
tomwalters@321
|
204
|
tomwalters@321
|
205 // TODO(tomwalters): There's significant code-duplication between here,
|
tomwalters@321
|
206 // SetPZBankCoeffsERBFitted, and SetPZBankCoeffs
|
tomwalters@321
|
207
|
tomwalters@321
|
208 // Normalised maximum pole frequency
|
tomwalters@321
|
209 float pole_frequency = cf_max_ / sample_rate_ * (2.0f * M_PI);
|
tomwalters@321
|
210 channel_count_ = 0;
|
tomwalters@321
|
211 while ((pole_frequency / (2.0f * M_PI)) * sample_rate_ > cf_min_) {
|
tomwalters@321
|
212 float bw = ERBTools::Freq2ERBw(pole_frequency
|
tomwalters@321
|
213 / (2.0f * M_PI) * sample_rate_);
|
tomwalters@321
|
214 pole_frequency -= step_factor_ * (bw * (2.0f * M_PI) / sample_rate_);
|
tomwalters@321
|
215 channel_count_++;
|
tomwalters@321
|
216 }
|
tomwalters@321
|
217
|
tomwalters@321
|
218 // Now the number of channels is known, various buffers for the filterbank
|
tomwalters@321
|
219 // coefficients can be initialised
|
tomwalters@321
|
220 pole_dampings_.clear();
|
tomwalters@321
|
221 pole_dampings_.resize(channel_count_, pole_damping_);
|
tomwalters@321
|
222 pole_frequencies_.clear();
|
tomwalters@321
|
223 pole_frequencies_.resize(channel_count_, 0.0f);
|
tomwalters@321
|
224
|
tomwalters@321
|
225 // Direct-form coefficients
|
tomwalters@321
|
226 za0_.clear();
|
tomwalters@321
|
227 za0_.resize(channel_count_, 0.0f);
|
tomwalters@321
|
228 za1_.clear();
|
tomwalters@321
|
229 za1_.resize(channel_count_, 0.0f);
|
tomwalters@321
|
230 za2_.clear();
|
tomwalters@321
|
231 za2_.resize(channel_count_, 0.0f);
|
tomwalters@321
|
232
|
tomwalters@321
|
233 // The output signal bank
|
tomwalters@321
|
234 output_.Initialize(channel_count_, buffer_length_, sample_rate_);
|
tomwalters@321
|
235
|
tomwalters@321
|
236 // Reset the pole frequency to maximum
|
tomwalters@321
|
237 pole_frequency = cf_max_ / sample_rate_ * (2.0f * M_PI);
|
tomwalters@321
|
238
|
tomwalters@321
|
239 for (int i = channel_count_ - 1; i > -1; --i) {
|
tomwalters@321
|
240 // Store the normalised pole frequncy
|
tomwalters@321
|
241 pole_frequencies_[i] = pole_frequency;
|
tomwalters@321
|
242
|
tomwalters@321
|
243 // Calculate the real pole frequency from the normalised pole frequency
|
tomwalters@321
|
244 float frequency = pole_frequency / (2.0f * M_PI) * sample_rate_;
|
tomwalters@321
|
245
|
tomwalters@321
|
246 // Store the real pole frequency as the 'centre frequency' of the filterbank
|
tomwalters@321
|
247 // channel
|
tomwalters@321
|
248 output_.set_centre_frequency(i, frequency);
|
tomwalters@321
|
249
|
tomwalters@398
|
250 float zero_frequency = Minimum(M_PI, zero_factor_ * pole_frequency);
|
tomwalters@321
|
251
|
tomwalters@321
|
252 // Impulse-invariance mapping
|
tomwalters@321
|
253 float z_plane_theta = zero_frequency * sqrt(1.0f - pow(zero_damping_, 2));
|
tomwalters@321
|
254 float z_plane_rho = exp(-zero_damping_ * zero_frequency);
|
tomwalters@321
|
255
|
tomwalters@321
|
256 // Direct-form coefficients from z-plane rho and theta
|
tomwalters@321
|
257 float a1 = -2.0f * z_plane_rho * cos(z_plane_theta);
|
tomwalters@321
|
258 float a2 = z_plane_rho * z_plane_rho;
|
tomwalters@321
|
259
|
tomwalters@321
|
260 // Normalised to unity gain at DC
|
tomwalters@321
|
261 float a_sum = 1.0f + a1 + a2;
|
tomwalters@321
|
262 za0_[i] = 1.0f / a_sum;
|
tomwalters@321
|
263 za1_[i] = a1 / a_sum;
|
tomwalters@321
|
264 za2_[i] = a2 / a_sum;
|
tomwalters@321
|
265
|
tomwalters@321
|
266 float bw = ERBTools::Freq2ERBw(pole_frequency
|
tomwalters@321
|
267 / (2.0f * M_PI) * sample_rate_);
|
tomwalters@321
|
268 pole_frequency -= step_factor_ * (bw * (2.0f * M_PI) / sample_rate_);
|
tomwalters@321
|
269 }
|
tomwalters@321
|
270 return true;
|
tomwalters@321
|
271 }
|
tomwalters@321
|
272
|
tomwalters@268
|
273 bool ModulePZFC::SetPZBankCoeffsERBFitted() {
|
tomwalters@321
|
274 //float parameter_values[3 * 7] = {
|
tomwalters@321
|
275 //// Filed, Nfit = 524, 11-3 parameters, PZFC, cwt 0, fit time 9915 sec
|
tomwalters@321
|
276 //1.14827, 0.00000, 0.00000, // % SumSqrErr= 10125.41
|
tomwalters@321
|
277 //0.53571, -0.70128, 0.63246, // % RMSErr = 2.81586
|
tomwalters@321
|
278 //0.76779, 0.00000, 0.00000, // % MeanErr = 0.00000
|
tomwalters@321
|
279 //// Inf 0.00000 0.00000 % RMSCost = NaN
|
tomwalters@321
|
280 //0.00000, 0.00000, 0.00000,
|
tomwalters@321
|
281 //6.00000, 0.00000, 0.00000,
|
tomwalters@321
|
282 //1.08869, -0.09470, 0.07844,
|
tomwalters@321
|
283 //10.56432, 2.52732, 1.86895
|
tomwalters@321
|
284 //// -3.45865 -1.31457 3.91779 % Kv
|
tomwalters@321
|
285 //};
|
tomwalters@321
|
286
|
tomwalters@268
|
287 float parameter_values[3 * 7] = {
|
tomwalters@321
|
288 // Fit 515 from Dick
|
tomwalters@321
|
289 // Final, Nfit = 515, 9-3 parameters, PZFC, cwt 0
|
tomwalters@321
|
290 1.72861, 0.00000, 0.00000, // SumSqrErr = 13622.24
|
tomwalters@321
|
291 0.56657, -0.93911, 0.89163, // RMSErr = 3.26610
|
tomwalters@321
|
292 0.39469, 0.00000, 0.00000, // MeanErr = 0.00000
|
tomwalters@321
|
293 // Inf, 0.00000, 0.00000, // RMSCost = NaN - would set coefc to infinity, but this isn't passed on
|
tomwalters@321
|
294 0.00000, 0.00000, 0.00000,
|
tomwalters@321
|
295 2.00000, 0.00000, 0.00000, //
|
tomwalters@321
|
296 1.27393, 0.00000, 0.00000,
|
tomwalters@321
|
297 11.46247, 5.46894, 0.11800
|
tomwalters@321
|
298 // -4.15525, 1.54874, 2.99858 // Kv
|
tomwalters@268
|
299 };
|
tomwalters@268
|
300
|
tomwalters@268
|
301 // Precalculate the number of channels required - this method is ugly but it
|
tomwalters@268
|
302 // was the quickest way of converting from MATLAB as the step factor between
|
tomwalters@268
|
303 // channels can vary quadratically with pole frequency...
|
tomwalters@268
|
304
|
tomwalters@268
|
305 // Normalised maximum pole frequency
|
tomwalters@268
|
306 float pole_frequency = cf_max_ / sample_rate_ * (2.0f * M_PI);
|
tomwalters@268
|
307
|
tomwalters@268
|
308 channel_count_ = 0;
|
tomwalters@268
|
309 while ((pole_frequency / (2.0f * M_PI)) * sample_rate_ > cf_min_) {
|
tomwalters@268
|
310 float frequency = pole_frequency / (2.0f * M_PI) * sample_rate_;
|
tomwalters@268
|
311 float f_dep = ERBTools::Freq2ERB(frequency)
|
tomwalters@268
|
312 / ERBTools::Freq2ERB(1000.0f) - 1.0f;
|
tomwalters@268
|
313 float bw = ERBTools::Freq2ERBw(pole_frequency
|
tomwalters@268
|
314 / (2.0f * M_PI) * sample_rate_);
|
tomwalters@268
|
315 float step_factor = 1.0f
|
tomwalters@268
|
316 / (parameter_values[4*3] + parameter_values[4 * 3 + 1]
|
tomwalters@268
|
317 * f_dep + parameter_values[4 * 3 + 2] * f_dep * f_dep); // 1/n2
|
tomwalters@268
|
318 pole_frequency -= step_factor * (bw * (2.0f * M_PI) / sample_rate_);
|
tomwalters@268
|
319 channel_count_++;
|
tomwalters@268
|
320 }
|
tomwalters@268
|
321
|
tomwalters@268
|
322 // Now the number of channels is known, various buffers for the filterbank
|
tomwalters@268
|
323 // coefficients can be initialised
|
tomwalters@268
|
324 pole_dampings_.clear();
|
tomwalters@268
|
325 pole_dampings_.resize(channel_count_, 0.0f);
|
tomwalters@268
|
326 pole_frequencies_.clear();
|
tomwalters@268
|
327 pole_frequencies_.resize(channel_count_, 0.0f);
|
tomwalters@268
|
328
|
tomwalters@268
|
329 // Direct-form coefficients
|
tomwalters@268
|
330 za0_.clear();
|
tomwalters@268
|
331 za0_.resize(channel_count_, 0.0f);
|
tomwalters@268
|
332 za1_.clear();
|
tomwalters@268
|
333 za1_.resize(channel_count_, 0.0f);
|
tomwalters@268
|
334 za2_.clear();
|
tomwalters@268
|
335 za2_.resize(channel_count_, 0.0f);
|
tomwalters@268
|
336
|
tomwalters@268
|
337 // The output signal bank
|
tomwalters@268
|
338 output_.Initialize(channel_count_, buffer_length_, sample_rate_);
|
tomwalters@268
|
339
|
tomwalters@268
|
340 // Reset the pole frequency to maximum
|
tomwalters@268
|
341 pole_frequency = cf_max_ / sample_rate_ * (2.0f * M_PI);
|
tomwalters@268
|
342
|
tomwalters@268
|
343 for (int i = channel_count_ - 1; i > -1; --i) {
|
tomwalters@268
|
344 // Store the normalised pole frequncy
|
tomwalters@268
|
345 pole_frequencies_[i] = pole_frequency;
|
tomwalters@268
|
346
|
tomwalters@268
|
347 // Calculate the real pole frequency from the normalised pole frequency
|
tomwalters@268
|
348 float frequency = pole_frequency / (2.0f * M_PI) * sample_rate_;
|
tomwalters@268
|
349
|
tomwalters@268
|
350 // Store the real pole frequency as the 'centre frequency' of the filterbank
|
tomwalters@268
|
351 // channel
|
tomwalters@268
|
352 output_.set_centre_frequency(i, frequency);
|
tomwalters@268
|
353
|
tomwalters@268
|
354 // From PZFC_Small_Signal_Params.m { From PZFC_Params.m {
|
tomwalters@268
|
355 float DpndF = ERBTools::Freq2ERB(frequency)
|
tomwalters@268
|
356 / ERBTools::Freq2ERB(1000.0f) - 1.0f;
|
tomwalters@268
|
357
|
tomwalters@268
|
358 float p[8]; // Parameters (short name for ease of reading)
|
tomwalters@268
|
359
|
tomwalters@268
|
360 // Use parameter_values to recover the parameter values for this frequency
|
tomwalters@268
|
361 for (int param = 0; param < 7; ++param)
|
tomwalters@268
|
362 p[param] = parameter_values[param * 3]
|
tomwalters@268
|
363 + parameter_values[param * 3 + 1] * DpndF
|
tomwalters@268
|
364 + parameter_values[param * 3 + 2] * DpndF * DpndF;
|
tomwalters@268
|
365
|
tomwalters@268
|
366 // Calculate the final parameter
|
tomwalters@268
|
367 p[7] = p[1] * pow(10.0f, (p[2] / (p[1] * p[4])) * (p[6] - 60.0f) / 20.0f);
|
tomwalters@268
|
368 if (p[7] < 0.2f)
|
tomwalters@268
|
369 p[7] = 0.2f;
|
tomwalters@268
|
370
|
tomwalters@268
|
371 // Nominal bandwidth at this frequency
|
tomwalters@268
|
372 float fERBw = ERBTools::Freq2ERBw(frequency);
|
tomwalters@268
|
373
|
tomwalters@268
|
374 // Pole bandwidth
|
tomwalters@268
|
375 float fPBW = ((p[7] * fERBw * (2 * M_PI) / sample_rate_) / 2)
|
tomwalters@268
|
376 * pow(p[4], 0.5f);
|
tomwalters@268
|
377
|
tomwalters@268
|
378 // Pole damping
|
tomwalters@268
|
379 float pole_damping = fPBW / sqrt(pow(pole_frequency, 2) + pow(fPBW, 2));
|
tomwalters@268
|
380
|
tomwalters@268
|
381 // Store the pole damping
|
tomwalters@268
|
382 pole_dampings_[i] = pole_damping;
|
tomwalters@268
|
383
|
tomwalters@268
|
384 // Zero bandwidth
|
tomwalters@268
|
385 float fZBW = ((p[0] * p[5] * fERBw * (2 * M_PI) / sample_rate_) / 2)
|
tomwalters@268
|
386 * pow(p[4], 0.5f);
|
tomwalters@268
|
387
|
tomwalters@268
|
388 // Zero frequency
|
tomwalters@268
|
389 float zero_frequency = p[5] * pole_frequency;
|
tomwalters@268
|
390
|
tomwalters@268
|
391 if (zero_frequency > M_PI)
|
tomwalters@268
|
392 LOG_ERROR(_T("Warning: Zero frequency is above the Nyquist frequency "
|
tomwalters@268
|
393 "in ModulePZFC(), continuing anyway but results may not "
|
tomwalters@268
|
394 "be accurate."));
|
tomwalters@268
|
395
|
tomwalters@268
|
396 // Zero damping
|
tomwalters@268
|
397 float fZDamp = fZBW / sqrt(pow(zero_frequency, 2) + pow(fZBW, 2));
|
tomwalters@268
|
398
|
tomwalters@268
|
399 // Impulse-invariance mapping
|
tomwalters@268
|
400 float fZTheta = zero_frequency * sqrt(1.0f - pow(fZDamp, 2));
|
tomwalters@268
|
401 float fZRho = exp(-fZDamp * zero_frequency);
|
tomwalters@268
|
402
|
tomwalters@268
|
403 // Direct-form coefficients
|
tomwalters@268
|
404 float fA1 = -2.0f * fZRho * cos(fZTheta);
|
tomwalters@268
|
405 float fA2 = fZRho * fZRho;
|
tomwalters@268
|
406
|
tomwalters@268
|
407 // Normalised to unity gain at DC
|
tomwalters@268
|
408 float fASum = 1.0f + fA1 + fA2;
|
tomwalters@268
|
409 za0_[i] = 1.0f / fASum;
|
tomwalters@268
|
410 za1_[i] = fA1 / fASum;
|
tomwalters@268
|
411 za2_[i] = fA2 / fASum;
|
tomwalters@268
|
412
|
tomwalters@268
|
413 // Subtract step factor (1/n2) times current bandwidth from the pole
|
tomwalters@268
|
414 // frequency
|
tomwalters@268
|
415 pole_frequency -= ((1.0f / p[4])
|
tomwalters@268
|
416 * (fERBw * (2.0f * M_PI) / sample_rate_));
|
tomwalters@268
|
417 }
|
tomwalters@268
|
418 return true;
|
tomwalters@268
|
419 }
|
tomwalters@268
|
420
|
tomwalters@268
|
421 bool ModulePZFC::SetPZBankCoeffs() {
|
tomwalters@268
|
422 /*! \todo Re-implement the alternative parameter settings
|
tomwalters@268
|
423 */
|
tomwalters@321
|
424 if (use_fitted_parameters_) {
|
tomwalters@321
|
425 if (!SetPZBankCoeffsERBFitted())
|
tomwalters@321
|
426 return false;
|
tomwalters@321
|
427 } else {
|
tomwalters@321
|
428 if (!SetPZBankCoeffsOrig())
|
tomwalters@398
|
429 return false;
|
tomwalters@321
|
430 }
|
tomwalters@268
|
431
|
tomwalters@268
|
432 /*! \todo Make fMindamp and fMaxdamp user-settable?
|
tomwalters@268
|
433 */
|
tomwalters@268
|
434 mindamp_ = 0.18f;
|
tomwalters@268
|
435 maxdamp_ = 0.4f;
|
tomwalters@268
|
436
|
tomwalters@268
|
437 rmin_.resize(channel_count_);
|
tomwalters@268
|
438 rmax_.resize(channel_count_);
|
tomwalters@268
|
439 xmin_.resize(channel_count_);
|
tomwalters@268
|
440 xmax_.resize(channel_count_);
|
tomwalters@268
|
441
|
tomwalters@268
|
442 for (int c = 0; c < channel_count_; ++c) {
|
tomwalters@268
|
443 // Calculate maximum and minimum damping options
|
tomwalters@268
|
444 rmin_[c] = exp(-mindamp_ * pole_frequencies_[c]);
|
tomwalters@268
|
445 rmax_[c] = exp(-maxdamp_ * pole_frequencies_[c]);
|
tomwalters@268
|
446
|
tomwalters@268
|
447 xmin_[c] = rmin_[c] * cos(pole_frequencies_[c]
|
tomwalters@268
|
448 * pow((1-pow(mindamp_, 2)), 0.5f));
|
tomwalters@268
|
449 xmax_[c] = rmax_[c] * cos(pole_frequencies_[c]
|
tomwalters@268
|
450 * pow((1-pow(maxdamp_, 2)), 0.5f));
|
tomwalters@268
|
451 }
|
tomwalters@268
|
452
|
tomwalters@268
|
453 // Set up AGC parameters
|
tomwalters@268
|
454 agc_stage_count_ = 4;
|
tomwalters@268
|
455 agc_epsilons_.resize(agc_stage_count_);
|
tomwalters@268
|
456 agc_epsilons_[0] = 0.0064f;
|
tomwalters@268
|
457 agc_epsilons_[1] = 0.0016f;
|
tomwalters@268
|
458 agc_epsilons_[2] = 0.0004f;
|
tomwalters@268
|
459 agc_epsilons_[3] = 0.0001f;
|
tomwalters@268
|
460
|
tomwalters@268
|
461 agc_gains_.resize(agc_stage_count_);
|
tomwalters@268
|
462 agc_gains_[0] = 1.0f;
|
tomwalters@268
|
463 agc_gains_[1] = 1.4f;
|
tomwalters@268
|
464 agc_gains_[2] = 2.0f;
|
tomwalters@268
|
465 agc_gains_[3] = 2.8f;
|
tomwalters@268
|
466
|
tomwalters@268
|
467 float mean_agc_gain = 0.0f;
|
tomwalters@268
|
468 for (int c = 0; c < agc_stage_count_; ++c)
|
tomwalters@268
|
469 mean_agc_gain += agc_gains_[c];
|
tomwalters@268
|
470 mean_agc_gain /= static_cast<float>(agc_stage_count_);
|
tomwalters@268
|
471
|
tomwalters@268
|
472 for (int c = 0; c < agc_stage_count_; ++c)
|
tomwalters@268
|
473 agc_gains_[c] /= mean_agc_gain;
|
tomwalters@268
|
474
|
tomwalters@268
|
475 return true;
|
tomwalters@268
|
476 }
|
tomwalters@268
|
477
|
tomwalters@268
|
478 void ModulePZFC::AGCDampStep() {
|
tomwalters@268
|
479 if (detect_.size() == 0) {
|
tomwalters@268
|
480 // If detect_ is not initialised, it means that the AGC is not set up.
|
tomwalters@268
|
481 // Set up now.
|
tomwalters@268
|
482 /*! \todo Make a separate InitAGC function which does this.
|
tomwalters@268
|
483 */
|
tomwalters@317
|
484 detect_.clear();
|
tomwalters@317
|
485 float detect_zero = DetectFun(0.0f);
|
tomwalters@317
|
486 detect_.resize(channel_count_, detect_zero);
|
tomwalters@268
|
487
|
tomwalters@268
|
488 for (int c = 0; c < channel_count_; c++)
|
tomwalters@268
|
489 for (int st = 0; st < agc_stage_count_; st++)
|
tomwalters@268
|
490 agc_state_[c][st] = (1.2f * detect_[c] * agc_gains_[st]);
|
tomwalters@268
|
491 }
|
tomwalters@268
|
492
|
tomwalters@268
|
493 float fAGCEpsLeft = 0.3f;
|
tomwalters@268
|
494 float fAGCEpsRight = 0.3f;
|
tomwalters@268
|
495
|
tomwalters@268
|
496 for (int c = channel_count_ - 1; c > -1; --c) {
|
tomwalters@268
|
497 for (int st = 0; st < agc_stage_count_; ++st) {
|
tomwalters@268
|
498 // This bounds checking is ugly and wasteful, and in an inner loop.
|
tomwalters@268
|
499 // If this algorithm is slow, this is why!
|
tomwalters@268
|
500 /*! \todo Proper non-ugly bounds checking in AGCDampStep()
|
tomwalters@268
|
501 */
|
tomwalters@268
|
502 float fPrevAGCState;
|
tomwalters@268
|
503 float fCurrAGCState;
|
tomwalters@268
|
504 float fNextAGCState;
|
tomwalters@268
|
505
|
tomwalters@268
|
506 if (c < channel_count_ - 1)
|
tomwalters@268
|
507 fPrevAGCState = agc_state_[c + 1][st];
|
tomwalters@268
|
508 else
|
tomwalters@268
|
509 fPrevAGCState = agc_state_[c][st];
|
tomwalters@268
|
510
|
tomwalters@268
|
511 fCurrAGCState = agc_state_[c][st];
|
tomwalters@268
|
512
|
tomwalters@268
|
513 if (c > 0)
|
tomwalters@268
|
514 fNextAGCState = agc_state_[c - 1][st];
|
tomwalters@268
|
515 else
|
tomwalters@268
|
516 fNextAGCState = agc_state_[c][st];
|
tomwalters@268
|
517
|
tomwalters@268
|
518 // Spatial smoothing
|
tomwalters@268
|
519 /*! \todo Something odd is going on here
|
tomwalters@268
|
520 * I think this line is not quite right.
|
tomwalters@268
|
521 */
|
tomwalters@268
|
522 float agc_avg = fAGCEpsLeft * fPrevAGCState
|
tomwalters@268
|
523 + (1.0f - fAGCEpsLeft - fAGCEpsRight) * fCurrAGCState
|
tomwalters@268
|
524 + fAGCEpsRight * fNextAGCState;
|
tomwalters@268
|
525 // Temporal smoothing
|
tomwalters@268
|
526 agc_state_[c][st] = agc_avg * (1.0f - agc_epsilons_[st])
|
tomwalters@268
|
527 + agc_epsilons_[st] * detect_[c] * agc_gains_[st];
|
tomwalters@268
|
528 }
|
tomwalters@268
|
529 }
|
tomwalters@268
|
530
|
tomwalters@317
|
531 float offset = 1.0f - agc_factor_ * DetectFun(0.0f);
|
tomwalters@268
|
532
|
tomwalters@268
|
533 for (int i = 0; i < channel_count_; ++i) {
|
tomwalters@268
|
534 float fAGCStateMean = 0.0f;
|
tomwalters@268
|
535 for (int j = 0; j < agc_stage_count_; ++j)
|
tomwalters@268
|
536 fAGCStateMean += agc_state_[i][j];
|
tomwalters@268
|
537
|
tomwalters@268
|
538 fAGCStateMean /= static_cast<float>(agc_stage_count_);
|
tomwalters@268
|
539
|
tomwalters@268
|
540 pole_damps_mod_[i] = pole_dampings_[i] *
|
tomwalters@317
|
541 (offset + agc_factor_ * fAGCStateMean);
|
tomwalters@268
|
542 }
|
tomwalters@268
|
543 }
|
tomwalters@268
|
544
|
tomwalters@268
|
545 float ModulePZFC::DetectFun(float fIN) {
|
tomwalters@268
|
546 if (fIN < 0.0f)
|
tomwalters@268
|
547 fIN = 0.0f;
|
tomwalters@268
|
548 float fDetect = Minimum(1.0f, fIN);
|
tomwalters@268
|
549 float fA = 0.25f;
|
tomwalters@268
|
550 return fA * fIN + (1.0f - fA) * (fDetect - pow(fDetect, 3) / 3.0f);
|
tomwalters@268
|
551 }
|
tomwalters@268
|
552
|
tomwalters@268
|
553 inline float ModulePZFC::Minimum(float a, float b) {
|
tomwalters@268
|
554 if (a < b)
|
tomwalters@268
|
555 return a;
|
tomwalters@268
|
556 else
|
tomwalters@268
|
557 return b;
|
tomwalters@268
|
558 }
|
tomwalters@268
|
559
|
tomwalters@268
|
560 void ModulePZFC::Process(const SignalBank& input) {
|
tomwalters@268
|
561 // Set the start time of the output buffer
|
tomwalters@268
|
562 output_.set_start_time(input.start_time());
|
tomwalters@268
|
563
|
tomwalters@317
|
564 for (int s = 0; s < input.buffer_length(); ++s) {
|
tomwalters@317
|
565 float input_sample = input.sample(0, s);
|
tomwalters@268
|
566
|
tomwalters@268
|
567 // Lowpass filter the input with a zero at PI
|
tomwalters@317
|
568 input_sample = 0.5f * input_sample + 0.5f * last_input_;
|
tomwalters@317
|
569 last_input_ = input.sample(0, s);
|
tomwalters@268
|
570
|
tomwalters@317
|
571 inputs_[channel_count_ - 1] = input_sample;
|
tomwalters@268
|
572 for (int c = 0; c < channel_count_ - 1; ++c)
|
tomwalters@268
|
573 inputs_[c] = previous_out_[c + 1];
|
tomwalters@268
|
574
|
tomwalters@268
|
575 // PZBankStep2
|
tomwalters@268
|
576 // to save a bunch of divides
|
tomwalters@268
|
577 float damp_rate = 1.0f / (maxdamp_ - mindamp_);
|
tomwalters@268
|
578
|
tomwalters@268
|
579 for (int c = channel_count_ - 1; c > -1; --c) {
|
tomwalters@317
|
580 float interp_factor = (pole_damps_mod_[c] - mindamp_) * damp_rate;
|
tomwalters@268
|
581
|
tomwalters@268
|
582 float x = xmin_[c] + (xmax_[c] - xmin_[c]) * interp_factor;
|
tomwalters@268
|
583 float r = rmin_[c] + (rmax_[c] - rmin_[c]) * interp_factor;
|
tomwalters@268
|
584
|
tomwalters@268
|
585 // optional improvement to constellation adds a bit to r
|
tomwalters@268
|
586 float fd = pole_frequencies_[c] * pole_damps_mod_[c];
|
tomwalters@268
|
587 // quadratic for small values, then linear
|
tomwalters@268
|
588 r = r + 0.25f * fd * Minimum(0.05f, fd);
|
tomwalters@268
|
589
|
tomwalters@268
|
590 float zb1 = -2.0f * x;
|
tomwalters@268
|
591 float zb2 = r * r;
|
tomwalters@268
|
592
|
tomwalters@268
|
593 /* canonic poles but with input provided where unity DC gain is assured
|
tomwalters@268
|
594 * (mean value of state is always equal to mean value of input)
|
tomwalters@268
|
595 */
|
tomwalters@268
|
596 float new_state = inputs_[c] - (state_1_[c] - inputs_[c]) * zb1
|
tomwalters@268
|
597 - (state_2_[c] - inputs_[c]) * zb2;
|
tomwalters@268
|
598
|
tomwalters@268
|
599 // canonic zeros part as before:
|
tomwalters@268
|
600 float output = za0_[c] * new_state + za1_[c] * state_1_[c]
|
tomwalters@268
|
601 + za2_[c] * state_2_[c];
|
tomwalters@268
|
602
|
tomwalters@268
|
603 // cubic compression nonlinearity
|
tomwalters@317
|
604 output -= 0.0001f * pow(output, 3);
|
tomwalters@268
|
605
|
tomwalters@317
|
606 output_.set_sample(c, s, output);
|
tomwalters@268
|
607 detect_[c] = DetectFun(output);
|
tomwalters@268
|
608 state_2_[c] = state_1_[c];
|
tomwalters@268
|
609 state_1_[c] = new_state;
|
tomwalters@268
|
610 }
|
tomwalters@268
|
611
|
tomwalters@268
|
612 if (do_agc_step_)
|
tomwalters@268
|
613 AGCDampStep();
|
tomwalters@268
|
614
|
tomwalters@268
|
615 for (int c = 0; c < channel_count_; ++c)
|
tomwalters@317
|
616 previous_out_[c] = output_[c][s];
|
tomwalters@268
|
617 }
|
tomwalters@268
|
618 PushOutput();
|
tomwalters@268
|
619 }
|
tomwalters@268
|
620 } // namespace aimc
|