alexbrandmeyer@609
|
1 //
|
alexbrandmeyer@609
|
2 // ear.h
|
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
|
alexbrandmeyer@609
|
23 #ifndef CARFAC_Open_Source_C__Library_Ear_h
|
alexbrandmeyer@609
|
24 #define CARFAC_Open_Source_C__Library_Ear_h
|
alexbrandmeyer@609
|
25
|
alexbrandmeyer@609
|
26 #include "car_state.h"
|
alexbrandmeyer@609
|
27 #include "ihc_state.h"
|
alexbrandmeyer@609
|
28 #include "agc_state.h"
|
alexbrandmeyer@609
|
29
|
alexbrandmeyer@609
|
30 class Ear {
|
alexbrandmeyer@610
|
31 public:
|
alexbrandmeyer@610
|
32 // This is the primary initialization function that is called for each
|
alexbrandmeyer@610
|
33 // Ear object in the CARFAC 'Design' method.
|
alexbrandmeyer@626
|
34 void InitEar(const int n_ch, const FPType fs, const FloatArray& pole_freqs,
|
alexbrandmeyer@626
|
35 const CARParams& car_params, const IHCParams& ihc_params,
|
alexbrandmeyer@626
|
36 const AGCParams& agc_params);
|
alexbrandmeyer@610
|
37 // These three methods apply the different stages of the model in sequence
|
alexbrandmeyer@610
|
38 // to individual audio samples.
|
alexbrandmeyer@626
|
39 void CARStep(const FPType input, FloatArray* car_out);
|
alexbrandmeyer@626
|
40 void IHCStep(const FloatArray& car_out, FloatArray* ihc_out);
|
alexbrandmeyer@626
|
41 bool AGCStep(const FloatArray& ihc_out);
|
alexbrandmeyer@626
|
42 // These accessor functions return portions of the CAR state for storage in
|
alexbrandmeyer@626
|
43 // the CAROutput structures.
|
alexbrandmeyer@626
|
44 const FloatArray& za_memory() { return car_state_.za_memory_; }
|
alexbrandmeyer@626
|
45 const FloatArray& zb_memory() { return car_state_.zb_memory_; }
|
alexbrandmeyer@626
|
46 const FloatArray& g_memory() { return car_state_.g_memory_; }
|
alexbrandmeyer@626
|
47 const FloatArray& dzb_memory() { return car_state_.dzb_memory_; }
|
alexbrandmeyer@626
|
48 // These accessor functions return CAR coefficients.
|
alexbrandmeyer@626
|
49 const FloatArray& zr_coeffs() { return car_coeffs_.zr_coeffs_; }
|
alexbrandmeyer@626
|
50 // These accessor functions return portions of the AGC state during the cross
|
alexbrandmeyer@610
|
51 // coupling of the ears.
|
alexbrandmeyer@626
|
52 const int agc_nstages() { return agc_coeffs_.size(); }
|
alexbrandmeyer@626
|
53 const int agc_decim_phase(const int stage) {
|
alexbrandmeyer@626
|
54 return agc_state_[stage].decim_phase_; }
|
alexbrandmeyer@626
|
55 const FPType agc_mix_coeff(const int stage) {
|
alexbrandmeyer@626
|
56 return agc_coeffs_[stage].agc_mix_coeffs_; }
|
alexbrandmeyer@626
|
57 const FloatArray& agc_memory(const int stage) {
|
alexbrandmeyer@626
|
58 return agc_state_[stage].agc_memory_; }
|
alexbrandmeyer@626
|
59 const int agc_decimation(const int stage) {
|
alexbrandmeyer@626
|
60 return agc_coeffs_[stage].decimation_; }
|
alexbrandmeyer@610
|
61 // This returns the stage G value during the closing of the AGC loop.
|
alexbrandmeyer@626
|
62 FloatArray StageGValue(const FloatArray& undamping);
|
alexbrandmeyer@610
|
63 // This function sets the AGC memory during the cross coupling stage.
|
alexbrandmeyer@626
|
64 void set_agc_memory(const int stage, const FloatArray& new_values) {
|
alexbrandmeyer@626
|
65 agc_state_[stage].agc_memory_ = new_values; }
|
alexbrandmeyer@626
|
66 // These are the setter functions for the CAR memory states.
|
alexbrandmeyer@626
|
67 void set_dzb_memory(const FloatArray& new_values) {
|
alexbrandmeyer@626
|
68 car_state_.dzb_memory_ = new_values; }
|
alexbrandmeyer@626
|
69 void set_dg_memory(const FloatArray& new_values) {
|
alexbrandmeyer@626
|
70 car_state_.dg_memory_ = new_values; }
|
alexbrandmeyer@626
|
71
|
alexbrandmeyer@610
|
72 private:
|
alexbrandmeyer@610
|
73 // These are the corresponding methods that initialize the model state
|
alexbrandmeyer@610
|
74 // variables before runtime using the model coefficients.
|
alexbrandmeyer@610
|
75 void InitIHCState();
|
alexbrandmeyer@610
|
76 void InitAGCState();
|
alexbrandmeyer@610
|
77 void InitCARState();
|
alexbrandmeyer@610
|
78 // These are the various helper functions called during the model runtime.
|
alexbrandmeyer@626
|
79 void OHCNonlinearFunction(const FloatArray& velocities,
|
alexbrandmeyer@626
|
80 FloatArray* nonlinear_fun);
|
alexbrandmeyer@626
|
81 bool AGCRecurse(const int stage, FloatArray agc_in);
|
alexbrandmeyer@626
|
82 FloatArray AGCSpatialSmooth(const int stage, FloatArray stage_state);
|
alexbrandmeyer@626
|
83 FloatArray AGCSmoothDoubleExponential(FloatArray stage_state,
|
alexbrandmeyer@626
|
84 const FPType pole_z1,
|
alexbrandmeyer@626
|
85 const FPType pole_z2);
|
alexbrandmeyer@610
|
86 // These are the private data members that store the state and coefficient
|
alexbrandmeyer@610
|
87 // information.
|
alexbrandmeyer@609
|
88 CARCoeffs car_coeffs_;
|
alexbrandmeyer@609
|
89 IHCCoeffs ihc_coeffs_;
|
alexbrandmeyer@626
|
90 std::vector<AGCCoeffs> agc_coeffs_;
|
alexbrandmeyer@609
|
91 CARState car_state_;
|
alexbrandmeyer@609
|
92 IHCState ihc_state_;
|
alexbrandmeyer@626
|
93 std::vector<AGCState> agc_state_;
|
alexbrandmeyer@610
|
94 int n_ch_;
|
alexbrandmeyer@609
|
95 };
|
alexbrandmeyer@609
|
96
|
alexbrandmeyer@610
|
97 #endif |