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