annotate branches/carfac_cpp/src/AGC.cpp @ 564:9c4c3675c3f8

* Added class Ear, and moved the CARFAC members AGC CAR IHC into Ear. CARFAC now holds an array of Ear. TBD what is best. * Moved the files around, and introduced a makefile that builds unittests using GTest. (Note, GTest path is configured in makefile atm.). - two moronic tests implemented. :)
author Ulf.Hammarqvist@gmail.com
date Sun, 20 May 2012 22:36:47 +0000
parents 8ca6eb401a03
children f3dde307f4b8
rev   line source
Ulf@538 1 #include "AGC.h"
pfh1976@554 2 #include <cmath>
pfh1976@554 3 #include <stdlib.h>
pfh1976@554 4 #include <stdio.h>
Ulf@538 5
Ulf@564 6 AGC_coefficients::AGC_coefficients(AGC_parameters* AGC_params_p,
Ulf@545 7 float fs, int n_ch){
pfh1976@554 8 float decim = 1.0;
pfh1976@554 9 float total_DC_gain = 0.0;
pfh1976@554 10 float tau, ntimes, delay, spread_sq, u, p, dp;
pfh1976@554 11 int n_taps = 0, n_iterations = 1;
Ulf@557 12
pfh1976@554 13 n_ch_ = n_ch;
Ulf@564 14 n_agc_stages_ = AGC_params_p->n_stages_;
Ulf@564 15 agc_stage_gain_ = AGC_params_p->agc_stage_gain_;
pfh1976@554 16
pfh1976@554 17 // FloatArray initialization using assign method - dont know if this is good enough
pfh1976@554 18 agc_epsilon_.assign(n_agc_stages_, 0.0); //the 1/(tau*fs) roughly
pfh1976@554 19 agc_polez1_ = agc_epsilon_;
pfh1976@554 20 agc_polez2_ = agc_epsilon_;
pfh1976@554 21 agc_spatial_iterations_ = agc_epsilon_;
pfh1976@554 22 agc_spatial_n_taps_ = agc_epsilon_;
pfh1976@554 23 agc_mix_coeffs_ = agc_epsilon_;
Ulf@564 24 FloatArray agc1_scales = AGC_params_p->agc1_scales_;
Ulf@564 25 FloatArray agc2_scales = AGC_params_p->agc2_scales_;
pfh1976@554 26 FloatArray agc_spatial_FIR;
Ulf@564 27 decimation_ = AGC_params_p->decimation_;
pfh1976@554 28
pfh1976@554 29 for(int stage=0; stage < n_agc_stages_; stage++){
Ulf@564 30 tau = AGC_params_p->time_constants_[stage];
Ulf@564 31 decim *= AGC_params_p->decimation_[stage];
pfh1976@554 32 agc_epsilon_[stage] = 1.0 - exp(-decim/(tau*fs));
pfh1976@554 33 ntimes = tau * (fs/decim);
pfh1976@554 34 delay = (agc2_scales[stage]-agc1_scales[stage])/ntimes;
pfh1976@554 35 spread_sq = (agc1_scales[stage]*agc1_scales[stage] + agc2_scales[stage]*agc2_scales[stage])/ntimes;
pfh1976@554 36
pfh1976@554 37 u = 1.0 + 1.0/spread_sq;
pfh1976@554 38 p = u - sqrt(u*u-1);
pfh1976@554 39 dp = delay*(1 - 2*p + p*p)*0.5;
pfh1976@554 40 agc_polez1_[stage] = p - dp;
pfh1976@554 41 agc_polez2_[stage] = p + dp;
Ulf@557 42
Ulf@557 43 agc_spatial_FIR = Build_FIR_coeffs(spread_sq, delay, &n_iterations, &n_taps);
Ulf@557 44
pfh1976@554 45 agc_spatial_iterations_[stage] = (float) n_iterations;
pfh1976@554 46 agc_spatial_n_taps_[stage] = (float) n_taps;
pfh1976@554 47 agc_spatial_fir_.push_back(FloatArray());
pfh1976@554 48
pfh1976@554 49 for(int i =0; i < 3; i++)
pfh1976@554 50 agc_spatial_fir_[stage].push_back(agc_spatial_FIR[i]);
pfh1976@554 51
Ulf@564 52 total_DC_gain += pow(AGC_params_p->agc_stage_gain_,stage);
pfh1976@554 53
pfh1976@554 54 if(stage == 0)
pfh1976@554 55 agc_mix_coeffs_[stage] = 0.0;
pfh1976@554 56 else
Ulf@564 57 agc_mix_coeffs_[stage] = AGC_params_p->agc_mix_coeff_/(tau * (fs/decim));
pfh1976@554 58 }
pfh1976@554 59 agc_gain_ = total_DC_gain;
Ulf@564 60 detect_scale_ = AGC_params_p->detect_scale_/total_DC_gain;
Ulf@538 61 }
Ulf@545 62 AGC_coefficients::~AGC_coefficients(){
Ulf@544 63 // TODO Auto-generated destructor stub
Ulf@538 64 }
pfh1976@554 65
Ulf@557 66 FloatArray AGC_coefficients::Build_FIR_coeffs(float var, float mn, int* ptr_iters, int* ptr_taps){
pfh1976@554 67 float a, b;
pfh1976@554 68 FloatArray FIR(3);
Ulf@557 69
Ulf@557 70 // Try with 3 FIR taps
Ulf@557 71 a = (var + mn*mn - mn)/2;
Ulf@557 72 b = (var + mn*mn + mn)/2;
Ulf@557 73 FIR[0] = a;
Ulf@557 74 FIR[1] = 1.0 - a - b;
Ulf@557 75 FIR[2] = b;
Ulf@557 76
Ulf@557 77 if(FIR[1] >= 0.2){
Ulf@557 78 *ptr_taps = 3;
Ulf@557 79 return FIR;
pfh1976@554 80 }
Ulf@557 81 else //Try with 5 FIR taps
Ulf@557 82 {
Ulf@557 83 for(int n_iter = 1; n_iter<16; n_iter++){
Ulf@557 84 mn /= n_iter;
Ulf@557 85 var /= n_iter;
Ulf@557 86
Ulf@557 87 a = ((var + mn*mn)*2/5 - mn*2/3)/2;
Ulf@557 88 b = ((var + mn*mn)*2/5 + mn*2/3)/2;
Ulf@557 89 FIR[0] = a/2;
Ulf@557 90 FIR[1] = 1.0 - a - b;
Ulf@557 91 FIR[2] = b;
Ulf@557 92
Ulf@557 93 *ptr_iters = n_iter;
Ulf@557 94
Ulf@557 95 if(FIR[1] >= 0.1){
Ulf@557 96 *ptr_taps = 5;
Ulf@557 97 return FIR;
Ulf@557 98 }
Ulf@557 99 }
Ulf@557 100 //TODO: discuss how we handle errors
Ulf@557 101 printf("Too many iterations in FIR_coeffs\n");
Ulf@557 102 FIR = {0, 0, 0};
Ulf@557 103 return FIR;
Ulf@557 104 }
pfh1976@554 105 }