Marsyas
0.6.0-alpha
|
00001 /* 00002 ** Copyright (C) 1998-2011 George Tzanetakis <gtzan@cs.uvic.ca> 00003 ** 00004 ** This program is free software; you can redistribute it and/or modify 00005 ** it under the terms of the GNU General Public License as published by 00006 ** the Free Software Foundation; either version 2 of the License, or 00007 ** (at your option) any later version. 00008 ** 00009 ** This program is distributed in the hope that it will be useful, 00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 ** GNU General Public License for more details. 00013 ** 00014 ** You should have received a copy of the GNU General Public License 00015 ** along with this program; if not, write to the Free Software 00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 */ 00018 00019 #ifndef MARSYAS_CARFAC_COEFFS_H 00020 #define MARSYAS_CARFAC_COEFFS_H 00021 00022 #include <marsyas/system/MarSystem.h> 00023 00024 namespace Marsyas 00025 { 00046 class CF_class; 00047 00048 class filter_state_class { 00049 public: 00050 std::vector<double> z1_memory; 00051 std::vector<double> z2_memory; 00052 std::vector<double> zA_memory; 00053 std::vector<double> zB_memory; 00054 std::vector<double> dzB_memory; 00055 std::vector<double> zY_memory; 00056 std::vector<double> detect_accum; 00057 00058 std::vector<double> filterstep_inputs; 00059 std::vector<double> filterstep_zA; 00060 std::vector<double> filterstep_zB; 00061 std::vector<double> filterstep_zY; 00062 std::vector<double> filterstep_r; 00063 std::vector<double> filterstep_z1; 00064 std::vector<double> filterstep_z2; 00065 // std::vector<double> filterstep_detect; 00066 00067 bool init; 00068 00069 public: 00070 filter_state_class(); 00071 ~filter_state_class(); 00072 00073 std::vector<double> FilterStep(CF_class &CF, double input_waves, std::vector<double> &detect); 00074 00075 friend std::ostream& operator<<(std::ostream&, const filter_state_class&); 00076 friend std::ostream& operator<<(std::ostream&, std::vector<std::vector<double> > a); 00077 friend std::ostream& operator<<(std::ostream&, std::vector<double> a); 00078 }; 00079 00080 class AGC_state_class { 00081 public: 00082 std::vector<std::vector<double> > AGC_memory; 00083 std::vector<double> sum_AGC; 00084 00085 00086 public: 00087 AGC_state_class(); 00088 ~AGC_state_class(); 00089 00090 friend std::ostream& operator<<(std::ostream&, const AGC_state_class&); 00091 friend std::ostream& operator<<(std::ostream&, std::vector<double> a); 00092 }; 00093 00094 class strobe_state_class { 00095 public: 00096 std::vector<double> lastdata; 00097 std::vector<double> thresholds; 00098 00099 std::vector<int> trigger_index; 00100 std::vector<int> sai_index; 00101 00102 public: 00103 strobe_state_class(); 00104 ~strobe_state_class(); 00105 00106 friend std::ostream& operator<<(std::ostream&, const strobe_state_class&); 00107 friend std::ostream& operator<<(std::ostream&, std::vector<double> a); 00108 }; 00109 00110 class filter_coeffs_class { 00111 public: 00112 double velocity_scale; 00113 00114 std::vector<double> r_coeffs; 00115 std::vector<double> a_coeffs; 00116 std::vector<double> c_coeffs; 00117 std::vector<double> h_coeffs; 00118 std::vector<double> g_coeffs; 00119 00120 public: 00121 filter_coeffs_class(); 00122 ~filter_coeffs_class(); 00123 00124 void init(double velocity_scale, int n_ch); 00125 00126 friend std::ostream& operator<<(std::ostream&, const filter_coeffs_class&); 00127 }; 00128 00129 class CF_AGC_params_class { 00130 public: 00131 int n_stages; // The number of AGC stages 00132 std::vector<double> time_constants; 00133 int AGC_stage_gain; // Gain from each stage to next slower stage 00134 int decimation; // How often to update the AGC states 00135 std::vector<double> AGC1_scales; // In units of channels 00136 std::vector<double> AGC2_scales; // Spread more toward base 00137 double detect_scale; // Max nap will be 10 to 100 typically 00138 double AGC_mix_coeff; 00139 00140 public: 00141 CF_AGC_params_class(); 00142 CF_AGC_params_class(const CF_AGC_params_class& a); 00143 ~CF_AGC_params_class(); 00144 00145 friend std::ostream& operator<<(std::ostream&, const CF_AGC_params_class&); 00146 }; 00147 00148 class AGC_coeffs_class { 00149 public: 00150 double detect_scale; 00151 double AGC_stage_gain; 00152 double AGC_mix_coeff; 00153 std::vector<double> AGC_epsilon; 00154 std::vector<double> AGC1_polez; 00155 std::vector<double> AGC2_polez; 00156 00157 public: 00158 AGC_coeffs_class(); 00159 ~AGC_coeffs_class(); 00160 }; 00161 00162 00163 class CF_filter_params_class { 00164 public: 00165 double velocity_scale; 00166 double min_zeta; 00167 double first_pole_theta; 00168 double zero_ratio; 00169 double ERB_per_step; 00170 double min_pole_Hz; 00171 00172 public: 00173 CF_filter_params_class(); 00174 ~CF_filter_params_class(); 00175 00176 friend std::ostream& operator<<(std::ostream&, const CF_filter_params_class&); 00177 }; 00178 00179 class CF_class { 00180 public: 00181 int fs; // Sampling frequency 00182 CF_filter_params_class CF_filter_params; // The CARFAC filter parameters 00183 CF_AGC_params_class CF_AGC_params; // The Automatic Gain Control parameters 00184 int n_ch; // Number of channels 00185 std::vector<double> pole_freqs; // The frequencies of each of the poles 00186 filter_coeffs_class filter_coeffs; // The filter coefficients 00187 AGC_coeffs_class AGC_coeffs; // The Automatic Gain Control coefficients 00188 int n_mics; // Number of microphones (input observations) 00189 std::vector<filter_state_class> filter_state; // The current state of the filter 00190 std::vector<AGC_state_class> AGC_state; // The current state of the AGC 00191 std::vector<strobe_state_class> strobe_state; // The current state of the strobes 00192 std::vector<std::vector<std::vector<double> > > nap; // The Neural Activity Pattern, the output of this filter 00193 int cum_k; // Global time step in concatenated segments 00194 00195 double strobe_threshold_start; 00196 00197 public: 00198 CF_class(); 00199 CF_class(const CF_class& a); 00200 ~CF_class(); 00201 00202 void CARFAC_Design(double fs = -1, double ERB_break_freq = -1, double ERB_q = -1); 00203 void CARFAC_DesignFilters(); 00204 void CARFAC_DesignAGC(); 00205 void CARFAC_Init(int n_mics = -1); 00206 00207 double ERB_Hz(double CF_Hz, double ERB_break_freq = -1, double ERB_Q = -1); 00208 00209 bool printstate; 00210 bool printcoeffs; 00211 00212 friend std::ostream& operator<<(std::ostream&, const CF_class&); 00213 }; 00214 00215 00216 }//namespace Marsyas 00217 00218 #endif