comparison branches/carfac_cpp/src/AGC.cpp @ 557:0fde611fcd10

* SUGGESTED re-factor of the FIR_coeffs calls (old code remains in commented out form for easy comparison). * Minor changes to CARFAC and IHC corresponding to MATLAB refactoring
author Ulf.Hammarqvist@gmail.com
date Mon, 09 Apr 2012 09:43:02 +0000
parents d18ff10b5559
children 8ca6eb401a03
comparison
equal deleted inserted replaced
556:910efa18d8f5 557:0fde611fcd10
7 float fs, int n_ch){ 7 float fs, int n_ch){
8 float decim = 1.0; 8 float decim = 1.0;
9 float total_DC_gain = 0.0; 9 float total_DC_gain = 0.0;
10 float tau, ntimes, delay, spread_sq, u, p, dp; 10 float tau, ntimes, delay, spread_sq, u, p, dp;
11 int n_taps = 0, n_iterations = 1; 11 int n_taps = 0, n_iterations = 1;
12 bool FIR_OK = false; 12 // bool FIR_OK = false;
13 13
14 n_ch_ = n_ch; 14 n_ch_ = n_ch;
15 n_agc_stages_ = AGC_params->n_stages_; 15 n_agc_stages_ = AGC_params->n_stages_;
16 agc_stage_gain_ = AGC_params->agc_stage_gain_; 16 agc_stage_gain_ = AGC_params->agc_stage_gain_;
17 17
18 // FloatArray initialization using assign method - dont know if this is good enough 18 // FloatArray initialization using assign method - dont know if this is good enough
38 u = 1.0 + 1.0/spread_sq; 38 u = 1.0 + 1.0/spread_sq;
39 p = u - sqrt(u*u-1); 39 p = u - sqrt(u*u-1);
40 dp = delay*(1 - 2*p + p*p)*0.5; 40 dp = delay*(1 - 2*p + p*p)*0.5;
41 agc_polez1_[stage] = p - dp; 41 agc_polez1_[stage] = p - dp;
42 agc_polez2_[stage] = p + dp; 42 agc_polez2_[stage] = p + dp;
43 43
44 while(!FIR_OK){ 44 // while(!FIR_OK){
45 switch(n_taps){ 45 // switch(n_taps){
46 case 0: 46 // case 0:
47 n_taps = 3; 47 // n_taps = 3;
48 break; 48 // break;
49 case 3: 49 // case 3:
50 n_taps = 5; 50 // n_taps = 5;
51 break; 51 // break;
52 case 5: 52 // case 5:
53 n_iterations++; 53 // n_iterations++;
54 if(n_iterations > 16){ 54 // if(n_iterations > 16){
55 printf("Too many n_iterations in CARFAC_DesignAGC\n"); 55 // printf("Too many n_iterations in CARFAC_DesignAGC\n");
56 exit(1); 56 // exit(1);
57 } 57 // }
58 break; 58 // break;
59 default: 59 // default:
60 printf("Bad n_taps in CARFAC_DesignAGC\n"); 60 // printf("Bad n_taps in CARFAC_DesignAGC\n");
61 exit(1); 61 // exit(1);
62 } 62 // }
63 agc_spatial_FIR = FIR_coeffs(n_taps, spread_sq, delay, n_iterations, &FIR_OK); 63 // agc_spatial_FIR = FIR_coeffs(n_taps, spread_sq, delay, n_iterations, &FIR_OK);
64 } 64 // }
65
66 agc_spatial_FIR = Build_FIR_coeffs(spread_sq, delay, &n_iterations, &n_taps);
67
65 agc_spatial_iterations_[stage] = (float) n_iterations; 68 agc_spatial_iterations_[stage] = (float) n_iterations;
66 agc_spatial_n_taps_[stage] = (float) n_taps; 69 agc_spatial_n_taps_[stage] = (float) n_taps;
67 agc_spatial_fir_.push_back(FloatArray()); 70 agc_spatial_fir_.push_back(FloatArray());
68 71
69 for(int i =0; i < 3; i++) 72 for(int i =0; i < 3; i++)
82 } 85 }
83 AGC_coefficients::~AGC_coefficients(){ 86 AGC_coefficients::~AGC_coefficients(){
84 // TODO Auto-generated destructor stub 87 // TODO Auto-generated destructor stub
85 } 88 }
86 89
87 FloatArray AGC_coefficients::FIR_coeffs(int n_taps, float var, float mn, int n_iter, bool* ptr_FIR_OK) 90 //FloatArray AGC_coefficients::FIR_coeffs(int n_taps, float var, float mn, int n_iter, bool* ptr_FIR_OK)
88 { 91 //{
92 // float a, b;
93 // FloatArray FIR(3);
94 // mn /= n_iter;
95 // var /= n_iter;
96 //
97 // switch(n_taps){
98 // case 3:
99 // a = (var + mn*mn - mn)/2;
100 // b = (var + mn*mn + mn)/2;
101 // FIR[0] = a;
102 // FIR[1] = 1.0 - a - b;
103 // FIR[2] = b;
104 // if(FIR[1] >= 0.2)
105 // *ptr_FIR_OK = true;
106 // break;
107 // case 5:
108 // a = ((var + mn*mn)*2/5 - mn*2/3)/2;
109 // b = ((var + mn*mn)*2/5 + mn*2/3)/2;
110 // FIR[0] = a/2;
111 // FIR[1] = 1.0 - a - b;
112 // FIR[2] = b;
113 // if(FIR[1] >= 0.1)
114 // *ptr_FIR_OK = true;
115 // break;
116 // default:
117 // printf("Bad n_taps in AGC_spatial_FIR\n");
118 // exit(1);
119 // }
120 //
121 // return FIR;
122 //}
123
124 FloatArray AGC_coefficients::Build_FIR_coeffs(float var, float mn, int* ptr_iters, int* ptr_taps){
89 float a, b; 125 float a, b;
90 FloatArray FIR(3); 126 FloatArray FIR(3);
91 mn /= n_iter; 127
92 var /= n_iter; 128 // Try with 3 FIR taps
93 129 a = (var + mn*mn - mn)/2;
94 switch(n_taps){ 130 b = (var + mn*mn + mn)/2;
95 case 3: 131 FIR[0] = a;
96 a = (var + mn*mn - mn)/2; 132 FIR[1] = 1.0 - a - b;
97 b = (var + mn*mn + mn)/2; 133 FIR[2] = b;
98 FIR[0] = a; 134
99 FIR[1] = 1.0 - a - b; 135 if(FIR[1] >= 0.2){
100 FIR[2] = b; 136 *ptr_taps = 3;
101 if(FIR[1] >= 0.2) 137 return FIR;
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 } 138 }
117 139 else //Try with 5 FIR taps
118 return FIR; 140 {
141 for(int n_iter = 1; n_iter<16; n_iter++){
142 mn /= n_iter;
143 var /= n_iter;
144
145 a = ((var + mn*mn)*2/5 - mn*2/3)/2;
146 b = ((var + mn*mn)*2/5 + mn*2/3)/2;
147 FIR[0] = a/2;
148 FIR[1] = 1.0 - a - b;
149 FIR[2] = b;
150
151 *ptr_iters = n_iter;
152
153 if(FIR[1] >= 0.1){
154 *ptr_taps = 5;
155 return FIR;
156 }
157 }
158 //TODO: discuss how we handle errors
159 printf("Too many iterations in FIR_coeffs\n");
160 FIR = {0, 0, 0};
161 return FIR;
162 }
119 } 163 }