comparison src/Modules/BMM/ModulePZFC.cc @ 0:582cbe817f2c

- Initial add of support code and modules. Not everything is working yet.
author tomwalters
date Fri, 12 Feb 2010 12:31:23 +0000
parents
children decdac21cfc2
comparison
equal deleted inserted replaced
-1:000000000000 0:582cbe817f2c
1 // Copyright 2008-2010, Thomas Walters
2 //
3 // AIM-C: A C++ implementation of the Auditory Image Model
4 // http://www.acousticscale.org/AIMC
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 /*! \file
20 * \brief Dick Lyon's Pole-Zero Filter Cascade - implemented as an AIM-C
21 * module by Tom Walters from the AIM-MAT module based on Dick Lyon's code
22 */
23
24 /*! \author Thomas Walters <tom@acousticscale.org>
25 * \date created 2008/02/05
26 * \version \$Id: ModulePZFC.cc 4 2010-02-03 18:44:58Z tcw $
27 */
28
29 #include "Support/ERBTools.h"
30
31 #include "Modules/BMM/ModulePZFC.h"
32
33 namespace aimc {
34 ModulePZFC::ModulePZFC(Parameters *parameters) : Module(parameters) {
35 module_identifier_ = "pzfc";
36 module_type_ = "bmm";
37 module_description_ = "Pole-Zero Filter Cascade";
38 module_version_ = "$Id: ModulePZFC.cc 4 2010-02-03 18:44:58Z tcw $";
39
40 // Get parameter values, setting default values where necessary
41 // Each parameter is set here only if it has not already been set elsewhere.
42 cf_max_ = parameters_->DefaultFloat("pzfc.highest_frequency", 6000.0f);
43 cf_min_ = parameters_->DefaultFloat("pzfc.lowest_frequency", 100.0f);
44 pole_damping_ = parameters_->DefaultFloat("pzfc.pole_damping", 0.12f);
45 zero_damping_ = parameters_->DefaultFloat("pzfc.zero_damping", 0.2f);
46 zero_factor_ = parameters_->DefaultFloat("pzfc.zero_factor", 1.4f);
47 step_factor_ = parameters_->DefaultFloat("pzfc.step_factor", 1.0f/3.0f);
48 bandwidth_over_cf_ = parameters_->DefaultFloat("pzfc.bandwidth_over_cf",
49 0.11f);
50 min_bandwidth_hz_ = parameters_->DefaultFloat("pzfc.min_bandwidth_hz",
51 27.0f);
52 agc_factor_ = parameters_->DefaultFloat("pzfc.agc_factor", 12.0f);
53 do_agc_step_ = parameters_->DefaultBool("pzfc.do_agc", true);
54
55 detect_.resize(0);
56 }
57
58 ModulePZFC::~ModulePZFC() {
59 }
60
61 bool ModulePZFC::InitializeInternal(const SignalBank &input) {
62 // Make local convenience copies of some variables
63 sample_rate_ = input.sample_rate();
64 buffer_length_ = input.buffer_length();
65 channel_count_ = 0;
66
67 // Prepare the coefficients and also the output SignalBank
68 if (!SetPZBankCoeffs())
69 return false;
70
71 // The output signal bank should be set up by now.
72 if (!output_.initialized())
73 return false;
74
75 // This initialises all buffers which can be modified by Process()
76 Reset();
77
78 return true;
79 }
80
81 void ModulePZFC::Reset() {
82 // These buffers may be actively modified by the algorithm
83 agc_state_.clear();
84 agc_state_.resize(channel_count_);
85 for (int i = 0; i < channel_count_; ++i) {
86 agc_state_[i].clear();
87 agc_state_[i].resize(agc_stage_count_, 0.0f);
88 }
89
90 state_1_.clear();
91 state_1_.resize(channel_count_, 0.0f);
92
93 state_2_.clear();
94 state_2_.resize(channel_count_, 0.0f);
95
96 previous_out_.clear();
97 previous_out_.resize(channel_count_, 0.0f);
98
99 pole_damps_mod_.clear();
100 pole_damps_mod_.resize(channel_count_, 0.0f);
101
102 inputs_.clear();
103 inputs_.resize(channel_count_, 0.0f);
104
105 // Init AGC
106 AGCDampStep();
107 // pole_damps_mod_ and agc_state_ are now be initialized
108
109 // Modify the pole dampings and AGC state slightly from their values in
110 // silence in case the input is abuptly loud.
111 for (int i = 0; i < channel_count_; ++i) {
112 pole_damps_mod_[i] += 0.05f;
113 for (int j = 0; j < agc_stage_count_; ++j)
114 agc_state_[i][j] += 0.05f;
115 }
116
117 last_input_ = 0.0f;
118 }
119
120 bool ModulePZFC::SetPZBankCoeffsERBFitted() {
121 float parameter_values[3 * 7] = {
122 // Filed, Nfit = 524, 11-3 parameters, PZFC, cwt 0, fit time 9915 sec
123 1.14827, 0.00000, 0.00000, // % SumSqrErr= 10125.41
124 0.53571, -0.70128, 0.63246, // % RMSErr = 2.81586
125 0.76779, 0.00000, 0.00000, // % MeanErr = 0.00000
126 // Inf 0.00000 0.00000 % RMSCost = NaN
127 0.00000, 0.00000, 0.00000,
128 6.00000, 0.00000, 0.00000,
129 1.08869, -0.09470, 0.07844,
130 10.56432, 2.52732, 1.86895
131 // -3.45865 -1.31457 3.91779 % Kv
132 };
133
134 // Precalculate the number of channels required - this method is ugly but it
135 // was the quickest way of converting from MATLAB as the step factor between
136 // channels can vary quadratically with pole frequency...
137
138 // Normalised maximum pole frequency
139 float pole_frequency = cf_max_ / sample_rate_ * (2.0f * M_PI);
140
141 channel_count_ = 0;
142 while ((pole_frequency / (2.0f * M_PI)) * sample_rate_ > cf_min_) {
143 float frequency = pole_frequency / (2.0f * M_PI) * sample_rate_;
144 float f_dep = ERBTools::Freq2ERB(frequency)
145 / ERBTools::Freq2ERB(1000.0f) - 1.0f;
146 float bw = ERBTools::Freq2ERBw(pole_frequency
147 / (2.0f * M_PI) * sample_rate_);
148 float step_factor = 1.0f
149 / (parameter_values[4*3] + parameter_values[4 * 3 + 1]
150 * f_dep + parameter_values[4 * 3 + 2] * f_dep * f_dep); // 1/n2
151 pole_frequency -= step_factor * (bw * (2.0f * M_PI) / sample_rate_);
152 channel_count_++;
153 }
154
155 // Now the number of channels is known, various buffers for the filterbank
156 // coefficients can be initialised
157 pole_dampings_.clear();
158 pole_dampings_.resize(channel_count_, 0.0f);
159 pole_frequencies_.clear();
160 pole_frequencies_.resize(channel_count_, 0.0f);
161
162 // Direct-form coefficients
163 za0_.clear();
164 za0_.resize(channel_count_, 0.0f);
165 za1_.clear();
166 za1_.resize(channel_count_, 0.0f);
167 za2_.clear();
168 za2_.resize(channel_count_, 0.0f);
169
170 // The output signal bank
171 output_.Initialize(channel_count_, buffer_length_, sample_rate_);
172
173 // Reset the pole frequency to maximum
174 pole_frequency = cf_max_ / sample_rate_ * (2.0f * M_PI);
175
176 for (int i = channel_count_ - 1; i > -1; --i) {
177 // Store the normalised pole frequncy
178 pole_frequencies_[i] = pole_frequency;
179
180 // Calculate the real pole frequency from the normalised pole frequency
181 float frequency = pole_frequency / (2.0f * M_PI) * sample_rate_;
182
183 // Store the real pole frequency as the 'centre frequency' of the filterbank
184 // channel
185 output_.set_centre_frequency(i, frequency);
186
187 // From PZFC_Small_Signal_Params.m { From PZFC_Params.m {
188 float DpndF = ERBTools::Freq2ERB(frequency)
189 / ERBTools::Freq2ERB(1000.0f) - 1.0f;
190
191 float p[8]; // Parameters (short name for ease of reading)
192
193 // Use parameter_values to recover the parameter values for this frequency
194 for (int param = 0; param < 7; ++param)
195 p[param] = parameter_values[param * 3]
196 + parameter_values[param * 3 + 1] * DpndF
197 + parameter_values[param * 3 + 2] * DpndF * DpndF;
198
199 // Calculate the final parameter
200 p[7] = p[1] * pow(10.0f, (p[2] / (p[1] * p[4])) * (p[6] - 60.0f) / 20.0f);
201 if (p[7] < 0.2f)
202 p[7] = 0.2f;
203
204 // Nominal bandwidth at this frequency
205 float fERBw = ERBTools::Freq2ERBw(frequency);
206
207 // Pole bandwidth
208 float fPBW = ((p[7] * fERBw * (2 * M_PI) / sample_rate_) / 2)
209 * pow(p[4], 0.5f);
210
211 // Pole damping
212 float pole_damping = fPBW / sqrt(pow(pole_frequency, 2) + pow(fPBW, 2));
213
214 // Store the pole damping
215 pole_dampings_[i] = pole_damping;
216
217 // Zero bandwidth
218 float fZBW = ((p[0] * p[5] * fERBw * (2 * M_PI) / sample_rate_) / 2)
219 * pow(p[4], 0.5f);
220
221 // Zero frequency
222 float zero_frequency = p[5] * pole_frequency;
223
224 if (zero_frequency > M_PI)
225 LOG_ERROR(_T("Warning: Zero frequency is above the Nyquist frequency "
226 "in ModulePZFC(), continuing anyway but results may not "
227 "be accurate."));
228
229 // Zero damping
230 float fZDamp = fZBW / sqrt(pow(zero_frequency, 2) + pow(fZBW, 2));
231
232 // Impulse-invariance mapping
233 float fZTheta = zero_frequency * sqrt(1.0f - pow(fZDamp, 2));
234 float fZRho = exp(-fZDamp * zero_frequency);
235
236 // Direct-form coefficients
237 float fA1 = -2.0f * fZRho * cos(fZTheta);
238 float fA2 = fZRho * fZRho;
239
240 // Normalised to unity gain at DC
241 float fASum = 1.0f + fA1 + fA2;
242 za0_[i] = 1.0f / fASum;
243 za1_[i] = fA1 / fASum;
244 za2_[i] = fA2 / fASum;
245
246 // Subtract step factor (1/n2) times current bandwidth from the pole
247 // frequency
248 pole_frequency -= ((1.0f / p[4])
249 * (fERBw * (2.0f * M_PI) / sample_rate_));
250 }
251 return true;
252 }
253
254 bool ModulePZFC::SetPZBankCoeffs() {
255 /*! \todo Re-implement the alternative parameter settings
256 */
257 if (!SetPZBankCoeffsERBFitted())
258 return false;
259
260 /*! \todo Make fMindamp and fMaxdamp user-settable?
261 */
262 mindamp_ = 0.18f;
263 maxdamp_ = 0.4f;
264
265 rmin_.resize(channel_count_);
266 rmax_.resize(channel_count_);
267 xmin_.resize(channel_count_);
268 xmax_.resize(channel_count_);
269
270 for (int c = 0; c < channel_count_; ++c) {
271 // Calculate maximum and minimum damping options
272 rmin_[c] = exp(-mindamp_ * pole_frequencies_[c]);
273 rmax_[c] = exp(-maxdamp_ * pole_frequencies_[c]);
274
275 xmin_[c] = rmin_[c] * cos(pole_frequencies_[c]
276 * pow((1-pow(mindamp_, 2)), 0.5f));
277 xmax_[c] = rmax_[c] * cos(pole_frequencies_[c]
278 * pow((1-pow(maxdamp_, 2)), 0.5f));
279 }
280
281 // Set up AGC parameters
282 agc_stage_count_ = 4;
283 agc_epsilons_.resize(agc_stage_count_);
284 agc_epsilons_[0] = 0.0064f;
285 agc_epsilons_[1] = 0.0016f;
286 agc_epsilons_[2] = 0.0004f;
287 agc_epsilons_[3] = 0.0001f;
288
289 agc_gains_.resize(agc_stage_count_);
290 agc_gains_[0] = 1.0f;
291 agc_gains_[1] = 1.4f;
292 agc_gains_[2] = 2.0f;
293 agc_gains_[3] = 2.8f;
294
295 float mean_agc_gain = 0.0f;
296 for (int c = 0; c < agc_stage_count_; ++c)
297 mean_agc_gain += agc_gains_[c];
298 mean_agc_gain /= static_cast<float>(agc_stage_count_);
299
300 for (int c = 0; c < agc_stage_count_; ++c)
301 agc_gains_[c] /= mean_agc_gain;
302
303 return true;
304 }
305
306 void ModulePZFC::AGCDampStep() {
307 if (detect_.size() == 0) {
308 // If detect_ is not initialised, it means that the AGC is not set up.
309 // Set up now.
310 /*! \todo Make a separate InitAGC function which does this.
311 */
312 detect_.resize(channel_count_);
313 for (int c = 0; c < channel_count_; ++c)
314 detect_[c] = 1.0f;
315
316 float fDetectZero = DetectFun(0.0f);
317 for (int c = 0; c < channel_count_; c++)
318 detect_[c] *= fDetectZero;
319
320 for (int c = 0; c < channel_count_; c++)
321 for (int st = 0; st < agc_stage_count_; st++)
322 agc_state_[c][st] = (1.2f * detect_[c] * agc_gains_[st]);
323 }
324
325 float fAGCEpsLeft = 0.3f;
326 float fAGCEpsRight = 0.3f;
327
328 for (int c = channel_count_ - 1; c > -1; --c) {
329 for (int st = 0; st < agc_stage_count_; ++st) {
330 // This bounds checking is ugly and wasteful, and in an inner loop.
331 // If this algorithm is slow, this is why!
332 /*! \todo Proper non-ugly bounds checking in AGCDampStep()
333 */
334 float fPrevAGCState;
335 float fCurrAGCState;
336 float fNextAGCState;
337
338 if (c < channel_count_ - 1)
339 fPrevAGCState = agc_state_[c + 1][st];
340 else
341 fPrevAGCState = agc_state_[c][st];
342
343 fCurrAGCState = agc_state_[c][st];
344
345 if (c > 0)
346 fNextAGCState = agc_state_[c - 1][st];
347 else
348 fNextAGCState = agc_state_[c][st];
349
350 // Spatial smoothing
351 /*! \todo Something odd is going on here
352 * I think this line is not quite right.
353 */
354 float agc_avg = fAGCEpsLeft * fPrevAGCState
355 + (1.0f - fAGCEpsLeft - fAGCEpsRight) * fCurrAGCState
356 + fAGCEpsRight * fNextAGCState;
357 // Temporal smoothing
358 agc_state_[c][st] = agc_avg * (1.0f - agc_epsilons_[st])
359 + agc_epsilons_[st] * detect_[c] * agc_gains_[st];
360 }
361 }
362
363 float fOffset = 1.0f - agc_factor_ * DetectFun(0.0f);
364
365 for (int i = 0; i < channel_count_; ++i) {
366 float fAGCStateMean = 0.0f;
367 for (int j = 0; j < agc_stage_count_; ++j)
368 fAGCStateMean += agc_state_[i][j];
369
370 fAGCStateMean /= static_cast<float>(agc_stage_count_);
371
372 pole_damps_mod_[i] = pole_dampings_[i] *
373 (fOffset + agc_factor_ * fAGCStateMean);
374 }
375 }
376
377 float ModulePZFC::DetectFun(float fIN) {
378 if (fIN < 0.0f)
379 fIN = 0.0f;
380 float fDetect = Minimum(1.0f, fIN);
381 float fA = 0.25f;
382 return fA * fIN + (1.0f - fA) * (fDetect - pow(fDetect, 3) / 3.0f);
383 }
384
385 inline float ModulePZFC::Minimum(float a, float b) {
386 if (a < b)
387 return a;
388 else
389 return b;
390 }
391
392 void ModulePZFC::Process(const SignalBank& input) {
393 // Set the start time of the output buffer
394 output_.set_start_time(input.start_time());
395
396 for (int iSample = 0; iSample < input.buffer_length(); ++iSample) {
397 float fInput = input[0][iSample];
398
399 // Lowpass filter the input with a zero at PI
400 fInput = 0.5f * fInput + 0.5f * last_input_;
401 last_input_ = input[0][iSample];
402
403 inputs_[channel_count_ - 1] = fInput;
404 for (int c = 0; c < channel_count_ - 1; ++c)
405 inputs_[c] = previous_out_[c + 1];
406
407 // PZBankStep2
408 // to save a bunch of divides
409 float damp_rate = 1.0f / (maxdamp_ - mindamp_);
410
411 for (int c = channel_count_ - 1; c > -1; --c) {
412 float interp_factor = (pole_damps_mod_[c]
413 - mindamp_) * damp_rate;
414
415 float x = xmin_[c] + (xmax_[c] - xmin_[c]) * interp_factor;
416 float r = rmin_[c] + (rmax_[c] - rmin_[c]) * interp_factor;
417
418 // optional improvement to constellation adds a bit to r
419 float fd = pole_frequencies_[c] * pole_damps_mod_[c];
420 // quadratic for small values, then linear
421 r = r + 0.25f * fd * Minimum(0.05f, fd);
422
423 float zb1 = -2.0f * x;
424 float zb2 = r * r;
425
426 /* canonic poles but with input provided where unity DC gain is assured
427 * (mean value of state is always equal to mean value of input)
428 */
429 float new_state = inputs_[c] - (state_1_[c] - inputs_[c]) * zb1
430 - (state_2_[c] - inputs_[c]) * zb2;
431
432 // canonic zeros part as before:
433 float output = za0_[c] * new_state + za1_[c] * state_1_[c]
434 + za2_[c] * state_2_[c];
435
436 // cubic compression nonlinearity
437 output = output - 0.0001f * pow(output, 3);
438
439 output_.set_sample(c, iSample, output);
440 detect_[c] = DetectFun(output);
441 state_2_[c] = state_1_[c];
442 state_1_[c] = new_state;
443 }
444
445 if (do_agc_step_)
446 AGCDampStep();
447
448 for (int c = 0; c < channel_count_; ++c)
449 previous_out_[c] = output_[c][iSample];
450 }
451 PushOutput();
452 }
453 } // namespace aimc