alexbrandmeyer@609
|
1 //
|
alexbrandmeyer@609
|
2 // ear.cc
|
alexbrandmeyer@609
|
3 // CARFAC Open Source C++ Library
|
alexbrandmeyer@609
|
4 //
|
alexbrandmeyer@609
|
5 // Created by Alex Brandmeyer on 5/10/13.
|
alexbrandmeyer@609
|
6 //
|
alexbrandmeyer@609
|
7 // This C++ file is part of an implementation of Lyon's cochlear model:
|
alexbrandmeyer@609
|
8 // "Cascade of Asymmetric Resonators with Fast-Acting Compression"
|
alexbrandmeyer@609
|
9 // to supplement Lyon's upcoming book "Human and Machine Hearing"
|
alexbrandmeyer@609
|
10 //
|
alexbrandmeyer@609
|
11 // Licensed under the Apache License, Version 2.0 (the "License");
|
alexbrandmeyer@609
|
12 // you may not use this file except in compliance with the License.
|
alexbrandmeyer@609
|
13 // You may obtain a copy of the License at
|
alexbrandmeyer@609
|
14 //
|
alexbrandmeyer@609
|
15 // http://www.apache.org/licenses/LICENSE-2.0
|
alexbrandmeyer@609
|
16 //
|
alexbrandmeyer@609
|
17 // Unless required by applicable law or agreed to in writing, software
|
alexbrandmeyer@609
|
18 // distributed under the License is distributed on an "AS IS" BASIS,
|
alexbrandmeyer@609
|
19 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
alexbrandmeyer@609
|
20 // See the License for the specific language governing permissions and
|
alexbrandmeyer@609
|
21 // limitations under the License.
|
alexbrandmeyer@609
|
22
|
ronw@628
|
23 #include <assert.h>
|
ronw@628
|
24
|
alexbrandmeyer@609
|
25 #include "ear.h"
|
alexbrandmeyer@609
|
26
|
alexbrandmeyer@610
|
27 // The 'InitEar' function takes a set of model parameters and initializes the
|
alexbrandmeyer@610
|
28 // design coefficients and model state variables needed for running the model
|
alexbrandmeyer@626
|
29 // on a single audio channel.
|
alexbrandmeyer@626
|
30 void Ear::InitEar(const int n_ch, const FPType fs,
|
alexbrandmeyer@626
|
31 const FloatArray& pole_freqs, const CARParams& car_params,
|
alexbrandmeyer@626
|
32 const IHCParams& ihc_params, const AGCParams& agc_params) {
|
alexbrandmeyer@610
|
33 // The first section of code determines the number of channels that will be
|
alexbrandmeyer@610
|
34 // used in the model on the basis of the sample rate and the CAR parameters
|
alexbrandmeyer@610
|
35 // that have been passed to this function.
|
alexbrandmeyer@610
|
36 n_ch_ = n_ch;
|
alexbrandmeyer@610
|
37 // These functions use the parameters that have been passed to design the
|
alexbrandmeyer@626
|
38 // coefficients for the first two stages of the model.
|
alexbrandmeyer@626
|
39 car_coeffs_.Design(car_params, fs, pole_freqs);
|
alexbrandmeyer@626
|
40 ihc_coeffs_.Design(ihc_params, fs);
|
alexbrandmeyer@626
|
41 // This code initializes the coefficients for each of the AGC stages.
|
alexbrandmeyer@626
|
42 agc_coeffs_.resize(agc_params.n_stages_);
|
alexbrandmeyer@626
|
43 FPType previous_stage_gain = 0.0;
|
alexbrandmeyer@626
|
44 FPType decim = 1.0;
|
alexbrandmeyer@626
|
45 for (int stage = 0; stage < agc_params.n_stages_; ++stage) {
|
alexbrandmeyer@626
|
46 agc_coeffs_[stage].Design(agc_params, stage, fs, previous_stage_gain,
|
alexbrandmeyer@626
|
47 decim);
|
alexbrandmeyer@626
|
48 // Two variables store the decimation and gain levels for use in the design
|
alexbrandmeyer@626
|
49 // of the subsequent stages.
|
alexbrandmeyer@626
|
50 previous_stage_gain = agc_coeffs_[stage].agc_gain_;
|
alexbrandmeyer@626
|
51 decim = agc_coeffs_[stage].decim_;
|
alexbrandmeyer@626
|
52 }
|
alexbrandmeyer@610
|
53 // Once the coefficients have been determined, we can initialize the state
|
alexbrandmeyer@610
|
54 // variables that will be used during runtime.
|
alexbrandmeyer@610
|
55 InitCARState();
|
alexbrandmeyer@610
|
56 InitIHCState();
|
alexbrandmeyer@610
|
57 InitAGCState();
|
alexbrandmeyer@609
|
58 }
|
alexbrandmeyer@609
|
59
|
alexbrandmeyer@610
|
60 void Ear::InitCARState() {
|
alexbrandmeyer@610
|
61 car_state_.z1_memory_ = FloatArray::Zero(n_ch_);
|
alexbrandmeyer@610
|
62 car_state_.z2_memory_ = FloatArray::Zero(n_ch_);
|
alexbrandmeyer@610
|
63 car_state_.za_memory_ = FloatArray::Zero(n_ch_);
|
alexbrandmeyer@610
|
64 car_state_.zb_memory_ = car_coeffs_.zr_coeffs_;
|
alexbrandmeyer@610
|
65 car_state_.dzb_memory_ = FloatArray::Zero(n_ch_);
|
alexbrandmeyer@610
|
66 car_state_.zy_memory_ = FloatArray::Zero(n_ch_);
|
alexbrandmeyer@610
|
67 car_state_.g_memory_ = car_coeffs_.g0_coeffs_;
|
alexbrandmeyer@610
|
68 car_state_.dg_memory_ = FloatArray::Zero(n_ch_);
|
alexbrandmeyer@610
|
69 }
|
alexbrandmeyer@610
|
70
|
alexbrandmeyer@610
|
71 void Ear::InitIHCState() {
|
alexbrandmeyer@610
|
72 ihc_state_.ihc_accum_ = FloatArray::Zero(n_ch_);
|
alexbrandmeyer@610
|
73 if (! ihc_coeffs_.just_hwr_) {
|
alexbrandmeyer@610
|
74 ihc_state_.ac_coupler_ = FloatArray::Zero(n_ch_);
|
alexbrandmeyer@610
|
75 ihc_state_.lpf1_state_ = ihc_coeffs_.rest_output_ * FloatArray::Ones(n_ch_);
|
alexbrandmeyer@610
|
76 ihc_state_.lpf2_state_ = ihc_coeffs_.rest_output_ * FloatArray::Ones(n_ch_);
|
alexbrandmeyer@610
|
77 if (ihc_coeffs_.one_cap_) {
|
alexbrandmeyer@610
|
78 ihc_state_.cap1_voltage_ = ihc_coeffs_.rest_cap1_ *
|
alexbrandmeyer@626
|
79 FloatArray::Ones(n_ch_);
|
alexbrandmeyer@610
|
80 } else {
|
alexbrandmeyer@610
|
81 ihc_state_.cap1_voltage_ = ihc_coeffs_.rest_cap1_ *
|
alexbrandmeyer@626
|
82 FloatArray::Ones(n_ch_);
|
alexbrandmeyer@610
|
83 ihc_state_.cap2_voltage_ = ihc_coeffs_.rest_cap2_ *
|
alexbrandmeyer@626
|
84 FloatArray::Ones(n_ch_);
|
alexbrandmeyer@610
|
85 }
|
alexbrandmeyer@610
|
86 }
|
alexbrandmeyer@610
|
87 }
|
alexbrandmeyer@610
|
88
|
alexbrandmeyer@610
|
89 void Ear::InitAGCState() {
|
alexbrandmeyer@626
|
90 int n_agc_stages = agc_coeffs_.size();
|
alexbrandmeyer@626
|
91 agc_state_.resize(n_agc_stages);
|
alexbrandmeyer@626
|
92 for (int i = 0; i < n_agc_stages; ++i) {
|
alexbrandmeyer@626
|
93 agc_state_[i].decim_phase_ = 0;
|
alexbrandmeyer@626
|
94 agc_state_[i].agc_memory_ = FloatArray::Zero(n_ch_);
|
alexbrandmeyer@626
|
95 agc_state_[i].input_accum_ = FloatArray::Zero(n_ch_);
|
alexbrandmeyer@610
|
96 }
|
alexbrandmeyer@610
|
97 }
|
alexbrandmeyer@610
|
98
|
alexbrandmeyer@626
|
99 void Ear::CARStep(const FPType input, FloatArray* car_out) {
|
alexbrandmeyer@626
|
100 // This interpolates g.
|
alexbrandmeyer@626
|
101 car_state_.g_memory_ = car_state_.g_memory_ + car_state_.dg_memory_;
|
alexbrandmeyer@610
|
102 // This calculates the AGC interpolation state.
|
alexbrandmeyer@626
|
103 car_state_.zb_memory_ = car_state_.zb_memory_ + car_state_.dzb_memory_;
|
alexbrandmeyer@610
|
104 // This updates the nonlinear function of 'velocity' along with zA, which is
|
alexbrandmeyer@610
|
105 // a delay of z2.
|
alexbrandmeyer@626
|
106 FloatArray nonlinear_fun(n_ch_);
|
alexbrandmeyer@626
|
107 FloatArray velocities = car_state_.z2_memory_ - car_state_.za_memory_;
|
alexbrandmeyer@626
|
108 OHCNonlinearFunction(velocities, &nonlinear_fun);
|
alexbrandmeyer@626
|
109 // Here, zb_memory_ * nonlinear_fun is "undamping" delta r.
|
alexbrandmeyer@626
|
110 FloatArray r = car_coeffs_.r1_coeffs_ + (car_state_.zb_memory_ *
|
alexbrandmeyer@626
|
111 nonlinear_fun);
|
alexbrandmeyer@626
|
112 car_state_.za_memory_ = car_state_.z2_memory_;
|
alexbrandmeyer@610
|
113 // Here we reduce the CAR state by r and rotate with the fixed cos/sin coeffs.
|
alexbrandmeyer@626
|
114 FloatArray z1 = r * ((car_coeffs_.a0_coeffs_ * car_state_.z1_memory_) -
|
alexbrandmeyer@626
|
115 (car_coeffs_.c0_coeffs_ * car_state_.z2_memory_));
|
alexbrandmeyer@626
|
116 car_state_.z2_memory_ = r *
|
alexbrandmeyer@626
|
117 ((car_coeffs_.c0_coeffs_ * car_state_.z1_memory_) +
|
alexbrandmeyer@626
|
118 (car_coeffs_.a0_coeffs_ * car_state_.z2_memory_));
|
alexbrandmeyer@626
|
119 car_state_.zy_memory_ = car_coeffs_.h_coeffs_ * car_state_.z2_memory_;
|
alexbrandmeyer@610
|
120 // This section ripples the input-output path, to avoid delay...
|
alexbrandmeyer@610
|
121 // It's the only part that doesn't get computed "in parallel":
|
alexbrandmeyer@626
|
122 FPType in_out = input;
|
alexbrandmeyer@610
|
123 for (int ch = 0; ch < n_ch_; ch++) {
|
alexbrandmeyer@609
|
124 z1(ch) = z1(ch) + in_out;
|
alexbrandmeyer@610
|
125 // This performs the ripple, and saves the final channel outputs in zy.
|
alexbrandmeyer@626
|
126 in_out = car_state_.g_memory_(ch) * (in_out + car_state_.zy_memory_(ch));
|
alexbrandmeyer@626
|
127 car_state_.zy_memory_(ch) = in_out;
|
alexbrandmeyer@609
|
128 }
|
alexbrandmeyer@609
|
129 car_state_.z1_memory_ = z1;
|
alexbrandmeyer@626
|
130 *car_out = car_state_.zy_memory_;
|
alexbrandmeyer@609
|
131 }
|
alexbrandmeyer@609
|
132
|
alexbrandmeyer@610
|
133 // We start with a quadratic nonlinear function, and limit it via a
|
alexbrandmeyer@610
|
134 // rational function. This makes the result go to zero at high
|
alexbrandmeyer@609
|
135 // absolute velocities, so it will do nothing there.
|
alexbrandmeyer@626
|
136 void Ear::OHCNonlinearFunction(const FloatArray& velocities,
|
alexbrandmeyer@626
|
137 FloatArray* nonlinear_fun) {
|
alexbrandmeyer@626
|
138 *nonlinear_fun = (1 + ((velocities * car_coeffs_.velocity_scale_) +
|
alexbrandmeyer@626
|
139 car_coeffs_.v_offset_).square()).inverse();
|
alexbrandmeyer@609
|
140 }
|
alexbrandmeyer@609
|
141
|
alexbrandmeyer@610
|
142 // This step is a one sample-time update of the inner-hair-cell (IHC) model,
|
alexbrandmeyer@610
|
143 // including the detection nonlinearity and either one or two capacitor state
|
alexbrandmeyer@610
|
144 // variables.
|
alexbrandmeyer@626
|
145 void Ear::IHCStep(const FloatArray& car_out, FloatArray* ihc_out) {
|
alexbrandmeyer@626
|
146 FloatArray ac_diff = car_out - ihc_state_.ac_coupler_;
|
alexbrandmeyer@609
|
147 ihc_state_.ac_coupler_ = ihc_state_.ac_coupler_ +
|
alexbrandmeyer@626
|
148 (ihc_coeffs_.ac_coeff_ * ac_diff);
|
alexbrandmeyer@609
|
149 if (ihc_coeffs_.just_hwr_) {
|
alexbrandmeyer@626
|
150 FloatArray output(n_ch_);
|
alexbrandmeyer@626
|
151 for (int ch = 0; ch < n_ch_; ++ch) {
|
alexbrandmeyer@626
|
152 FPType a = (ac_diff(ch) > 0.0) ? ac_diff(ch) : 0.0;
|
alexbrandmeyer@626
|
153 output(ch) = (a < 2) ? a : 2;
|
alexbrandmeyer@609
|
154 }
|
alexbrandmeyer@626
|
155 *ihc_out = output;
|
alexbrandmeyer@609
|
156 } else {
|
alexbrandmeyer@626
|
157 FloatArray conductance = CARFACDetect(ac_diff);
|
alexbrandmeyer@609
|
158 if (ihc_coeffs_.one_cap_) {
|
alexbrandmeyer@626
|
159 *ihc_out = conductance * ihc_state_.cap1_voltage_;
|
alexbrandmeyer@609
|
160 ihc_state_.cap1_voltage_ = ihc_state_.cap1_voltage_ -
|
alexbrandmeyer@626
|
161 (*ihc_out * ihc_coeffs_.out1_rate_) +
|
alexbrandmeyer@626
|
162 ((1 - ihc_state_.cap1_voltage_)
|
alexbrandmeyer@626
|
163 * ihc_coeffs_.in1_rate_);
|
alexbrandmeyer@609
|
164 } else {
|
alexbrandmeyer@626
|
165 *ihc_out = conductance * ihc_state_.cap2_voltage_;
|
alexbrandmeyer@609
|
166 ihc_state_.cap1_voltage_ = ihc_state_.cap1_voltage_ -
|
alexbrandmeyer@626
|
167 ((ihc_state_.cap1_voltage_ - ihc_state_.cap2_voltage_)
|
alexbrandmeyer@626
|
168 * ihc_coeffs_.out1_rate_) +
|
alexbrandmeyer@626
|
169 ((1 - ihc_state_.cap1_voltage_) * ihc_coeffs_.in1_rate_);
|
alexbrandmeyer@609
|
170 ihc_state_.cap2_voltage_ = ihc_state_.cap2_voltage_ -
|
alexbrandmeyer@626
|
171 (*ihc_out * ihc_coeffs_.out2_rate_) +
|
alexbrandmeyer@626
|
172 ((ihc_state_.cap1_voltage_ - ihc_state_.cap2_voltage_)
|
alexbrandmeyer@626
|
173 * ihc_coeffs_.in2_rate_);
|
alexbrandmeyer@609
|
174 }
|
alexbrandmeyer@610
|
175 // Here we smooth the output twice using a LPF.
|
alexbrandmeyer@626
|
176 *ihc_out *= ihc_coeffs_.output_gain_;
|
alexbrandmeyer@609
|
177 ihc_state_.lpf1_state_ = ihc_state_.lpf1_state_ +
|
alexbrandmeyer@626
|
178 (ihc_coeffs_.lpf_coeff_ *
|
alexbrandmeyer@626
|
179 (*ihc_out - ihc_state_.lpf1_state_));
|
alexbrandmeyer@609
|
180 ihc_state_.lpf2_state_ = ihc_state_.lpf2_state_ +
|
alexbrandmeyer@626
|
181 (ihc_coeffs_.lpf_coeff_ *
|
alexbrandmeyer@626
|
182 (ihc_state_.lpf1_state_ - ihc_state_.lpf2_state_));
|
alexbrandmeyer@626
|
183 *ihc_out = ihc_state_.lpf2_state_ - ihc_coeffs_.rest_output_;
|
alexbrandmeyer@609
|
184 }
|
alexbrandmeyer@626
|
185 ihc_state_.ihc_accum_ += *ihc_out;
|
alexbrandmeyer@609
|
186 }
|
alexbrandmeyer@609
|
187
|
alexbrandmeyer@626
|
188 bool Ear::AGCStep(const FloatArray& ihc_out) {
|
alexbrandmeyer@609
|
189 int stage = 0;
|
alexbrandmeyer@626
|
190 int n_stages = agc_coeffs_[0].n_agc_stages_;
|
alexbrandmeyer@626
|
191 FPType detect_scale = agc_coeffs_[n_stages - 1].detect_scale_;
|
alexbrandmeyer@626
|
192 bool updated = AGCRecurse(stage, detect_scale * ihc_out);
|
alexbrandmeyer@609
|
193 return updated;
|
alexbrandmeyer@609
|
194 }
|
alexbrandmeyer@609
|
195
|
alexbrandmeyer@626
|
196 bool Ear::AGCRecurse(const int stage, FloatArray agc_in) {
|
alexbrandmeyer@609
|
197 bool updated = true;
|
alexbrandmeyer@610
|
198 // This is the decim factor for this stage, relative to input or prev. stage:
|
alexbrandmeyer@626
|
199 int decim = agc_coeffs_[stage].decimation_;
|
alexbrandmeyer@610
|
200 // This is the decim phase of this stage (do work on phase 0 only):
|
alexbrandmeyer@626
|
201 int decim_phase = agc_state_[stage].decim_phase_ + 1;
|
alexbrandmeyer@609
|
202 decim_phase = decim_phase % decim;
|
alexbrandmeyer@626
|
203 agc_state_[stage].decim_phase_ = decim_phase;
|
alexbrandmeyer@610
|
204 // Here we accumulate input for this stage from the previous stage:
|
alexbrandmeyer@626
|
205 agc_state_[stage].input_accum_ += agc_in;
|
alexbrandmeyer@610
|
206 // We don't do anything if it's not the right decim_phase.
|
alexbrandmeyer@610
|
207 if (decim_phase == 0) {
|
alexbrandmeyer@610
|
208 // Now we do lots of work, at the decimated rate.
|
alexbrandmeyer@610
|
209 // These are the decimated inputs for this stage, which will be further
|
alexbrandmeyer@610
|
210 // decimated at the next stage.
|
alexbrandmeyer@626
|
211 agc_in = agc_state_[stage].input_accum_ / decim;
|
alexbrandmeyer@610
|
212 // This resets the accumulator.
|
alexbrandmeyer@626
|
213 agc_state_[stage].input_accum_ = FloatArray::Zero(n_ch_);
|
alexbrandmeyer@626
|
214 if (stage < (agc_coeffs_.size() - 1)) {
|
alexbrandmeyer@610
|
215 // Now we recurse to evaluate the next stage(s).
|
alexbrandmeyer@626
|
216 // TODO (alexbrandmeyer): the Matlab version of AGCRecurse can return a
|
alexbrandmeyer@626
|
217 // value for updated, but isn't used in that version. Check with Dick to
|
alexbrandmeyer@626
|
218 // see if that is needed.
|
alexbrandmeyer@610
|
219 updated = AGCRecurse(stage + 1, agc_in);
|
alexbrandmeyer@610
|
220 // Afterwards we add its output to this stage input, whether it updated or
|
alexbrandmeyer@610
|
221 // not.
|
alexbrandmeyer@626
|
222 agc_in += agc_coeffs_[stage].agc_stage_gain_ *
|
alexbrandmeyer@626
|
223 agc_state_[stage + 1].agc_memory_;
|
alexbrandmeyer@609
|
224 }
|
alexbrandmeyer@626
|
225 FloatArray agc_stage_state = agc_state_[stage].agc_memory_;
|
alexbrandmeyer@610
|
226 // This performs a first-order recursive smoothing filter update, in time.
|
alexbrandmeyer@626
|
227 agc_stage_state += agc_coeffs_[stage].agc_epsilon_ *
|
alexbrandmeyer@626
|
228 (agc_in - agc_stage_state);
|
alexbrandmeyer@609
|
229 agc_stage_state = AGCSpatialSmooth(stage, agc_stage_state);
|
alexbrandmeyer@626
|
230 agc_state_[stage].agc_memory_ = agc_stage_state;
|
alexbrandmeyer@626
|
231 updated = true;
|
alexbrandmeyer@609
|
232 } else {
|
alexbrandmeyer@609
|
233 updated = false;
|
alexbrandmeyer@609
|
234 }
|
alexbrandmeyer@609
|
235 return updated;
|
alexbrandmeyer@609
|
236 }
|
alexbrandmeyer@609
|
237
|
alexbrandmeyer@626
|
238 // TODO (alexbrandmeyer): figure out how to operate directly on stage_state.
|
alexbrandmeyer@626
|
239 // Using a pointer breaks the () indexing of the Eigen arrays, but there must
|
alexbrandmeyer@626
|
240 // be a way around this.
|
alexbrandmeyer@626
|
241 FloatArray Ear::AGCSpatialSmooth(const int stage, FloatArray stage_state) {
|
alexbrandmeyer@626
|
242 int n_iterations = agc_coeffs_[stage].agc_spatial_iterations_;
|
alexbrandmeyer@609
|
243 bool use_fir;
|
alexbrandmeyer@610
|
244 use_fir = (n_iterations < 4) ? true : false;
|
alexbrandmeyer@609
|
245 if (use_fir) {
|
alexbrandmeyer@626
|
246 std::vector<FPType> fir_coeffs = agc_coeffs_[stage].agc_spatial_fir_;
|
alexbrandmeyer@609
|
247 FloatArray ss_tap1(n_ch_);
|
alexbrandmeyer@609
|
248 FloatArray ss_tap2(n_ch_);
|
alexbrandmeyer@609
|
249 FloatArray ss_tap3(n_ch_);
|
alexbrandmeyer@609
|
250 FloatArray ss_tap4(n_ch_);
|
alexbrandmeyer@626
|
251 int n_taps = agc_coeffs_[stage].agc_spatial_n_taps_;
|
alexbrandmeyer@610
|
252 // This initializes the first two taps of stage state, which are used for
|
alexbrandmeyer@610
|
253 // both possible cases.
|
alexbrandmeyer@609
|
254 ss_tap1(0) = stage_state(0);
|
alexbrandmeyer@610
|
255 ss_tap1.block(1, 0, n_ch_ - 1, 1) = stage_state.block(0, 0, n_ch_ - 1, 1);
|
alexbrandmeyer@610
|
256 ss_tap2(n_ch_ - 1) = stage_state(n_ch_ - 1);
|
alexbrandmeyer@610
|
257 ss_tap2.block(0, 0, n_ch_ - 1, 1) = stage_state.block(1, 0, n_ch_ - 1, 1);
|
alexbrandmeyer@609
|
258 switch (n_taps) {
|
alexbrandmeyer@609
|
259 case 3:
|
alexbrandmeyer@626
|
260 stage_state = (fir_coeffs[0] * ss_tap1) +
|
alexbrandmeyer@626
|
261 (fir_coeffs[1] * stage_state) +
|
alexbrandmeyer@626
|
262 (fir_coeffs[2] * ss_tap2);
|
alexbrandmeyer@609
|
263 break;
|
alexbrandmeyer@609
|
264 case 5:
|
alexbrandmeyer@610
|
265 // Now we initialize last two taps of stage state, which are only used
|
alexbrandmeyer@610
|
266 // for the 5-tap case.
|
alexbrandmeyer@609
|
267 ss_tap3(0) = stage_state(0);
|
alexbrandmeyer@609
|
268 ss_tap3(1) = stage_state(1);
|
alexbrandmeyer@610
|
269 ss_tap3.block(2, 0, n_ch_ - 2, 1) =
|
alexbrandmeyer@626
|
270 stage_state.block(0, 0, n_ch_ - 2, 1);
|
alexbrandmeyer@610
|
271 ss_tap4(n_ch_ - 2) = stage_state(n_ch_ - 1);
|
alexbrandmeyer@610
|
272 ss_tap4(n_ch_ - 1) = stage_state(n_ch_ - 2);
|
alexbrandmeyer@610
|
273 ss_tap4.block(0, 0, n_ch_ - 2, 1) =
|
alexbrandmeyer@626
|
274 stage_state.block(2, 0, n_ch_ - 2, 1);
|
alexbrandmeyer@626
|
275 stage_state = (fir_coeffs[0] * (ss_tap3 + ss_tap1)) +
|
alexbrandmeyer@626
|
276 (fir_coeffs[1] * stage_state) +
|
alexbrandmeyer@626
|
277 (fir_coeffs[2] * (ss_tap2 + ss_tap4));
|
alexbrandmeyer@609
|
278 break;
|
alexbrandmeyer@609
|
279 default:
|
ronw@628
|
280 assert(true && "Bad n_taps in AGCSpatialSmooth; should be 3 or 5.");
|
alexbrandmeyer@611
|
281 break;
|
alexbrandmeyer@609
|
282 }
|
alexbrandmeyer@609
|
283 } else {
|
alexbrandmeyer@610
|
284 stage_state = AGCSmoothDoubleExponential(stage_state,
|
alexbrandmeyer@626
|
285 agc_coeffs_[stage].agc_pole_z1_,
|
alexbrandmeyer@626
|
286 agc_coeffs_[stage].agc_pole_z2_);
|
alexbrandmeyer@609
|
287 }
|
alexbrandmeyer@609
|
288 return stage_state;
|
alexbrandmeyer@609
|
289 }
|
alexbrandmeyer@609
|
290
|
alexbrandmeyer@626
|
291 // TODO (alexbrandmeyer): figure out how to operate directly on stage_state.
|
alexbrandmeyer@626
|
292 // Same point as above for AGCSpatialSmooth.
|
alexbrandmeyer@610
|
293 FloatArray Ear::AGCSmoothDoubleExponential(FloatArray stage_state,
|
alexbrandmeyer@626
|
294 const FPType pole_z1,
|
alexbrandmeyer@626
|
295 const FPType pole_z2) {
|
alexbrandmeyer@611
|
296 int32_t n_pts = stage_state.size();
|
alexbrandmeyer@610
|
297 FPType input;
|
alexbrandmeyer@611
|
298 FPType state = 0.0;
|
alexbrandmeyer@626
|
299 // TODO (alexbrandmeyer): I'm assuming one dimensional input for now, but this
|
alexbrandmeyer@610
|
300 // should be verified with Dick for the final version
|
alexbrandmeyer@626
|
301 for (int i = n_pts - 11; i < n_pts; ++i){
|
alexbrandmeyer@610
|
302 input = stage_state(i);
|
alexbrandmeyer@610
|
303 state = state + (1 - pole_z1) * (input - state);
|
alexbrandmeyer@610
|
304 }
|
alexbrandmeyer@626
|
305 for (int i = n_pts - 1; i > -1; --i){
|
alexbrandmeyer@610
|
306 input = stage_state(i);
|
alexbrandmeyer@610
|
307 state = state + (1 - pole_z2) * (input - state);
|
alexbrandmeyer@610
|
308 }
|
alexbrandmeyer@626
|
309 for (int i = 0; i < n_pts; ++i){
|
alexbrandmeyer@610
|
310 input = stage_state(i);
|
alexbrandmeyer@610
|
311 state = state + (1 - pole_z1) * (input - state);
|
alexbrandmeyer@610
|
312 stage_state(i) = state;
|
alexbrandmeyer@610
|
313 }
|
alexbrandmeyer@609
|
314 return stage_state;
|
alexbrandmeyer@609
|
315 }
|
alexbrandmeyer@610
|
316
|
alexbrandmeyer@626
|
317 FloatArray Ear::StageGValue(const FloatArray& undamping) {
|
alexbrandmeyer@626
|
318 FloatArray r = car_coeffs_.r1_coeffs_ + car_coeffs_.zr_coeffs_ * undamping;
|
alexbrandmeyer@626
|
319 return (1 - 2 * r * car_coeffs_.a0_coeffs_ + (r * r)) /
|
alexbrandmeyer@626
|
320 (1 - 2 * r * car_coeffs_.a0_coeffs_ +
|
alexbrandmeyer@626
|
321 car_coeffs_.h_coeffs_ * r * car_coeffs_.c0_coeffs_ + (r * r));
|
ronw@628
|
322 }
|