flatmax@674
|
1
|
flatmax@598
|
2 // Author Matt Flax <flatmax@>
|
flatmax@597
|
3 //
|
flatmax@597
|
4 // This C++ file is part of an implementation of Lyon's cochlear model:
|
flatmax@597
|
5 // "Cascade of Asymmetric Resonators with Fast-Acting Compression"
|
flatmax@597
|
6 // to supplement Lyon's upcoming book "Human and Machine Hearing"
|
flatmax@597
|
7 //
|
flatmax@597
|
8 // Licensed under the Apache License, Version 2.0 (the "License");
|
flatmax@597
|
9 // you may not use this file except in compliance with the License.
|
flatmax@597
|
10 // You may obtain a copy of the License at
|
flatmax@597
|
11 //
|
flatmax@597
|
12 // http://www.apache.org/licenses/LICENSE-2.0
|
flatmax@597
|
13 //
|
flatmax@597
|
14 // Unless required by applicable law or agreed to in writing, software
|
flatmax@597
|
15 // distributed under the License is distributed on an "AS IS" BASIS,
|
flatmax@597
|
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
flatmax@597
|
17 // See the License for the specific language governing permissions and
|
flatmax@597
|
18 // limitations under the License.
|
flatmax@597
|
19 /**
|
flatmax@597
|
20 \author {Matt Flax <flatmax\@>}
|
flatmax@597
|
21 \date 2013.02.08
|
flatmax@597
|
22 */
|
flatmax@597
|
23
|
flatmax@597
|
24 #include "CAR.H"
|
flatmax@597
|
25
|
flatmax@597
|
26 CAR::CAR() {
|
flatmax@597
|
27 //ctor
|
flatmax@597
|
28 }
|
flatmax@597
|
29
|
flatmax@597
|
30 CAR::~CAR() {
|
flatmax@597
|
31 //dtor
|
flatmax@597
|
32 }
|
flatmax@598
|
33
|
flatmax@598
|
34 void CAR::designFilters(FP_TYPE fs, int n_ch) {
|
flatmax@598
|
35 // don't really need these zero arrays, but it's a clue to what fields
|
flatmax@598
|
36 // and types are need in ohter language implementations:
|
flatmax@600
|
37 coeff.r1_coeffs=Matrix<FP_TYPE, Dynamic, 1>::Zero(n_ch,1); // resize and zero
|
flatmax@600
|
38 coeff.a0_coeffs=Matrix<FP_TYPE, Dynamic, 1>::Zero(n_ch,1);
|
flatmax@600
|
39 coeff.c0_coeffs=Matrix<FP_TYPE, Dynamic, 1>::Zero(n_ch,1);
|
flatmax@600
|
40 coeff.h_coeffs=Matrix<FP_TYPE, Dynamic, 1>::Zero(n_ch,1);
|
flatmax@600
|
41 coeff.g0_coeffs=Matrix<FP_TYPE, Dynamic, 1>::Zero(n_ch,1);
|
flatmax@598
|
42 // zr_coeffs is not zeroed ... perhaps it should be ?
|
flatmax@598
|
43
|
flatmax@598
|
44 // zero_ratio comes in via h. In book's circuit D, zero_ratio is 1/sqrt(a),
|
flatmax@598
|
45 // and that a is here 1 / (1+f) where h = f*c.
|
flatmax@598
|
46 // solve for f: 1/zero_ratio^2 = 1 / (1+f)
|
flatmax@598
|
47 // zero_ratio^2 = 1+f => f = zero_ratio^2 - 1
|
flatmax@598
|
48 FP_TYPE f = pow(param.zero_ratio,2.) - 1.; // nominally 1 for half-octave
|
flatmax@598
|
49
|
flatmax@598
|
50 // Make pole positions, s and c coeffs, h and g coeffs, etc.,
|
flatmax@598
|
51 // which mostly depend on the pole angle theta:
|
flatmax@598
|
52 Array<FP_TYPE, Dynamic, 1> theta = pole_freqs * (2. * M_PI / fs);
|
flatmax@598
|
53
|
flatmax@598
|
54
|
flatmax@598
|
55 // undamped coupled-form coefficients:
|
flatmax@598
|
56 coeff.c0_coeffs = theta.sin();
|
flatmax@598
|
57 coeff.a0_coeffs = theta.cos();
|
flatmax@598
|
58
|
flatmax@598
|
59 // different possible interpretations for min-damping r:
|
flatmax@598
|
60 // r = exp(-theta * CF_CAR_params.min_zeta).
|
flatmax@598
|
61 // Compress theta to give somewhat higher Q at highest thetas:
|
flatmax@598
|
62 FP_TYPE ff = param.high_f_damping_compression; // 0 to 1 typ. 0.5
|
flatmax@598
|
63 Array<FP_TYPE, Dynamic,1> x = theta/M_PI;
|
flatmax@598
|
64
|
flatmax@598
|
65 coeff.zr_coeffs = M_PI * (x - ff * x.pow(3.)); // when ff is 0, this is just theta,
|
flatmax@598
|
66 // and when ff is 1 it goes to zero at theta = pi.
|
flatmax@598
|
67 coeff.r1_coeffs = (1. - coeff.zr_coeffs.array() * param.max_zeta); // "r1" for the max-damping condition
|
flatmax@598
|
68
|
flatmax@598
|
69 // Increase the min damping where channels are spaced out more, by pulling
|
flatmax@598
|
70 // 25% of the way toward ERB_Hz/pole_freqs (close to 0.1 at high f)
|
flatmax@598
|
71 Array<FP_TYPE, Dynamic, 1> min_zetas = param.min_zeta + 0.25*(PsychoAcoustics::Hz2ERB(pole_freqs, param.ERB_break_freq, param.ERB_Q).array() / pole_freqs - param.min_zeta);
|
flatmax@598
|
72 coeff.zr_coeffs = coeff.zr_coeffs.array() * (param.max_zeta - min_zetas); // how r relates to undamping
|
flatmax@598
|
73
|
flatmax@598
|
74 // the zeros follow via the h_coeffs
|
flatmax@598
|
75 coeff.h_coeffs = coeff.c0_coeffs * f;
|
flatmax@598
|
76
|
flatmax@598
|
77 // for unity gain at min damping, radius r; only used in CARFAC_Init:
|
flatmax@598
|
78 Array<FP_TYPE, Dynamic,1> relative_undamping(n_ch, 1);
|
flatmax@600
|
79 relative_undamping=Array<FP_TYPE, Dynamic, 1>::Zero(n_ch, 1).cos();
|
flatmax@598
|
80
|
flatmax@598
|
81 // this function needs to take CAR_coeffs even if we haven't finished
|
flatmax@598
|
82 // constucting it by putting in the g0_coeffs:
|
flatmax@598
|
83 coeff.g0_coeffs = stageG(relative_undamping);
|
flatmax@598
|
84 }
|
flatmax@598
|
85
|
flatmax@598
|
86 Array<FP_TYPE, Dynamic, 1> CAR::stageG(Array<FP_TYPE, Dynamic, 1> &relative_undamping) {
|
flatmax@598
|
87 // at max damping
|
flatmax@598
|
88 Array<FP_TYPE, Dynamic, 1> r = coeff.r1_coeffs.array() + coeff.zr_coeffs.array() * relative_undamping;
|
flatmax@598
|
89 return (1. - 2.*r*coeff.a0_coeffs.array() + r.pow(2.)) / (1. - 2.*r*coeff.a0_coeffs.array() + coeff.h_coeffs.array()*r*coeff.c0_coeffs.array() + pow(r,2.));
|
flatmax@598
|
90 }
|