comparison branches/carfac_cpp/src/AGC.cpp @ 554:d18ff10b5559

Adding code to AGC_coefficients(AGC_params*, float, int) Function FIR_coeffs as private function in class AGC_coefficients. FIR_coeffs returns a FloatArray and modify a boolean variable.
author pfh1976@gmail.com
date Sun, 08 Apr 2012 14:28:55 +0000
parents e63fbe19b255
children 0fde611fcd10
comparison
equal deleted inserted replaced
553:335cbd90cc10 554:d18ff10b5559
1 #include "AGC.h" 1 #include "AGC.h"
2 #include <cmath>
3 #include <stdlib.h>
4 #include <stdio.h>
2 5
3 AGC_coefficients::AGC_coefficients(AGC_parameters* AGC_params, 6 AGC_coefficients::AGC_coefficients(AGC_parameters* AGC_params,
4 float fs, int n_ch){ 7 float fs, int n_ch){
5 // TODO stuff goes here 8 float decim = 1.0;
9 float total_DC_gain = 0.0;
10 float tau, ntimes, delay, spread_sq, u, p, dp;
11 int n_taps = 0, n_iterations = 1;
12 bool FIR_OK = false;
13
14 n_ch_ = n_ch;
15 n_agc_stages_ = AGC_params->n_stages_;
16 agc_stage_gain_ = AGC_params->agc_stage_gain_;
17
18 // FloatArray initialization using assign method - dont know if this is good enough
19 agc_epsilon_.assign(n_agc_stages_, 0.0); //the 1/(tau*fs) roughly
20 agc_polez1_ = agc_epsilon_;
21 agc_polez2_ = agc_epsilon_;
22 agc_spatial_iterations_ = agc_epsilon_;
23 agc_spatial_n_taps_ = agc_epsilon_;
24 agc_mix_coeffs_ = agc_epsilon_;
25 FloatArray agc1_scales = AGC_params->agc1_scales_;
26 FloatArray agc2_scales = AGC_params->agc2_scales_;
27 FloatArray agc_spatial_FIR;
28 decimation_ = AGC_params->decimation_;
29
30 for(int stage=0; stage < n_agc_stages_; stage++){
31 tau = AGC_params->time_constants_[stage];
32 decim *= AGC_params->decimation_[stage];
33 agc_epsilon_[stage] = 1.0 - exp(-decim/(tau*fs));
34 ntimes = tau * (fs/decim);
35 delay = (agc2_scales[stage]-agc1_scales[stage])/ntimes;
36 spread_sq = (agc1_scales[stage]*agc1_scales[stage] + agc2_scales[stage]*agc2_scales[stage])/ntimes;
37
38 u = 1.0 + 1.0/spread_sq;
39 p = u - sqrt(u*u-1);
40 dp = delay*(1 - 2*p + p*p)*0.5;
41 agc_polez1_[stage] = p - dp;
42 agc_polez2_[stage] = p + dp;
43
44 while(!FIR_OK){
45 switch(n_taps){
46 case 0:
47 n_taps = 3;
48 break;
49 case 3:
50 n_taps = 5;
51 break;
52 case 5:
53 n_iterations++;
54 if(n_iterations > 16){
55 printf("Too many n_iterations in CARFAC_DesignAGC\n");
56 exit(1);
57 }
58 break;
59 default:
60 printf("Bad n_taps in CARFAC_DesignAGC\n");
61 exit(1);
62 }
63 agc_spatial_FIR = FIR_coeffs(n_taps, spread_sq, delay, n_iterations, &FIR_OK);
64 }
65 agc_spatial_iterations_[stage] = (float) n_iterations;
66 agc_spatial_n_taps_[stage] = (float) n_taps;
67 agc_spatial_fir_.push_back(FloatArray());
68
69 for(int i =0; i < 3; i++)
70 agc_spatial_fir_[stage].push_back(agc_spatial_FIR[i]);
71
72 total_DC_gain += pow(AGC_params->agc_stage_gain_,stage);
73
74 if(stage == 0)
75 agc_mix_coeffs_[stage] = 0.0;
76 else
77 agc_mix_coeffs_[stage] = AGC_params->agc_mix_coeff_/(tau * (fs/decim));
78 }
79 agc_gain_ = total_DC_gain;
80
81 // TODO Computation of the detect_scale_ member
6 } 82 }
7
8 AGC_coefficients::~AGC_coefficients(){ 83 AGC_coefficients::~AGC_coefficients(){
9 // TODO Auto-generated destructor stub 84 // TODO Auto-generated destructor stub
10 } 85 }
86
87 FloatArray AGC_coefficients::FIR_coeffs(int n_taps, float var, float mn, int n_iter, bool* ptr_FIR_OK)
88 {
89 float a, b;
90 FloatArray FIR(3);
91 mn /= n_iter;
92 var /= n_iter;
93
94 switch(n_taps){
95 case 3:
96 a = (var + mn*mn - mn)/2;
97 b = (var + mn*mn + mn)/2;
98 FIR[0] = a;
99 FIR[1] = 1.0 - a - b;
100 FIR[2] = b;
101 if(FIR[1] >= 0.2)
102 *ptr_FIR_OK = true;
103 break;
104 case 5:
105 a = ((var + mn*mn)*2/5 - mn*2/3)/2;
106 b = ((var + mn*mn)*2/5 + mn*2/3)/2;
107 FIR[0] = a/2;
108 FIR[1] = 1.0 - a - b;
109 FIR[2] = b;
110 if(FIR[1] >= 0.1)
111 *ptr_FIR_OK = true;
112 break;
113 default:
114 printf("Bad n_taps in AGC_spatial_FIR\n");
115 exit(1);
116 }
117
118 return FIR;
119 }