22 NumberBits = NumberBits - 1;
33 <<
" ==> ( scale: " <<
scale
35 <<
")" << dec << endl;
45 NumberBits = NumberBits - 1;
50 for (
int i=0;
i<w_off_size;
i++)
53 if ( fabs(w_off[
i]) >
max )
max = fabs(w_off[
i]);
62 for (
int i=0;
i<w_off_size;
i++) w_dsp.push_back( (
int) roundf(w_off[
i]*
pow(2.,
scale)) );
67 printf(
"\nAbsolute Max value = %15.8f -> Scale = %3d\n",
max,
scale);
69 for (
int i=0;
i<w_off_size;
i++)
72 printf(
"\n Offline Off*Scale Dsp/scale Dsp Scale \n");
73 printf(
"----------------------------------------------------------------------------------\n");
74 printf(
" %17.10f %17.10f %17.10f 0x%04X %3d \n",
77 printf(
" %17.10f %17.10f %17.10f 0x%04X \n",
81 printf(
" %17.10f %17.10f %17.10f 0x%04X <- SUM\n",
88 bool ConvertOFC(
const vector<vector<vector<vector<double> > > >& w_off,
89 const vector<vector<double> >& calibration,
int calibtype,
int runtype,
90 vector<unsigned int> &OFC,
bool verbose) {
97 float dsp_min_phase = -100.;
98 float dsp_max_phase = -dsp_min_phase;
99 int dsp_phases = (
int) roundf( (dsp_max_phase - dsp_min_phase)*(1./dsp_step) +1. );
103 cout<<
"----- DSP -----"<<endl;
104 cout<<
"Step: "<<dsp_step<<
" ns"<<endl;
105 cout<<
"Min Phase: "<<dsp_min_phase<<
" ns"<<endl;
106 cout<<
"Max Phase: "<<dsp_max_phase<<
" ns"<<endl;
107 cout<<
"Nb phases: "<<dsp_phases<<endl;
111 const float off_step = 0.1;
112 float off_min_phase = -100.;
113 float off_max_phase = -off_min_phase;
114 int off_phases = (
int) roundf( (off_max_phase - off_min_phase)*(1./off_step) + 1. );
117 cout<<
"----- OFFLINE -----"<<endl;
118 cout<<
"Step: "<<off_step<<
" ns"<<endl;
119 cout<<
"Min Phase: "<<off_min_phase<<
" ns"<<endl;
120 cout<<
"Max Phase: "<<off_max_phase<<
" ns"<<endl;
121 cout<<
"Nb phases: "<<off_phases<<endl;
124 int ngains = (
int) w_off.size();
126 int nparams = (
int) w_off[0][0].
size();
129 if ( dsp_step < off_step ){
130 cout<<
"ERROR: DSP phase step smaller than Offline phase step"<<endl;
134 cout<<
"ERROR: Incorrect number of gains"<<endl;
138 cout<<
"ERROR: Incorrect number of phases"<<endl;
141 if (dsp_min_phase < off_min_phase){
142 cout<<
"ERROR: Incorrect minimum phase"<<endl;
145 if (dsp_max_phase > off_max_phase){
146 cout<<
"ERROR: Incorrecto maximum phase"<<endl;
153 }
else if (nparams == 4){
156 cout<<
"ERROR: Incorrect algorithm type"<<endl;
162 int calib_gain = (
int) calibration.size();
163 int calib_size = (
int) calibration[0].
size();
164 if (calib_gain!=ngains){
165 cout<<
"ERROR: Number of gain in calibration vector incorrect"<<endl;
168 vector<double> calibration_lo;
169 vector<double> calibration_hi;
170 for (
int i=0;
i<calib_size;
i++)
173 calibration_lo.push_back( calibration[ig][
i] );
175 calibration_hi.push_back( calibration[ig][
i] );
177 if (
calibtype != 0) calibration_hi[1] = calibration_hi[1]*64.;
181 cout<<
"--- calibration ---"<<endl;
182 cout<<
"[low gain] -> offset: "<<calibration_lo[0]<<
" slope: "<<calibration_lo[1]<<endl;
183 cout<<
"[high gain] -> offset: "<<calibration_hi[0]<<
" slope: "<<calibration_hi[1]<<endl;
184 cout<<
"If calibtype != ADC then slope is multiplied by a factor 64"<<endl;
204 const float inv_off_step = 1. / off_step;
205 for (
int i=0;
i<dsp_phases;
i++){
207 current_phase = dsp_min_phase +
i*dsp_step;
208 index = (
int) roundf((current_phase - off_min_phase)*inv_off_step);
209 if (
verbose) cout<<
"["<<
index<<
"]: Phase "<<current_phase<<
" ns"<<endl;
213 a_lo[is] = w_off[ig][
index][0][is];
214 b_lo[is] = w_off[ig][
index][1][is];
215 g_lo[is] = w_off[ig][
index][nparams-1][is];
219 a_hi[is] = w_off[ig][
index][0][is];
220 b_hi[is] = w_off[ig][
index][1][is];
221 g_hi[is] = w_off[ig][
index][nparams-1][is];
228 if (
verbose) cout<<
"---- LOW GAIN ----"<<endl;
231 if (
verbose) cout<<
"---- HIGH GAIN ----"<<endl;
237 cout <<
"ERROR!!! Format 5 in this call is incomplete (OFC::ConvertOFC)" << endl;
248 calibration_lo.clear();
249 calibration_hi.clear();
271 const std::vector<double>&
b,
272 const std::vector<double>&
c,
273 const std::vector<double>&
g,
274 const std::vector<double>&
h,
275 unsigned int channel_index,
278 std::vector<unsigned int> &OFC,
301 unsigned int slope_scale;
307 int n_2 = (
int) trunc(
a.size()/2.);
310 OFC.push_back( ( (adsp[0] << 16) & 0xFFFF0000) | ( (
ascale-1) & 0xFFFF) );
311 for (
int i=1;
i<=n_2;
i++)
312 OFC.push_back( ( (adsp[2*
i] << 16) & 0xFFFF0000) | ( adsp[2*
i-1] & 0xFFFF) );
315 OFC.push_back( ( (slope << 16) & 0xFFFF0000) | ( (
ascale + slope_scale ) & 0xFFFF ) );
318 OFC.push_back( ( (bdsp[0] << 16) & 0xFFFF0000) | ( (bscale-4) & 0xFFFF) );
319 for (
int i=1;
i<=n_2;
i++)
320 OFC.push_back( ( (bdsp[2*
i] << 16) & 0xFFFF0000) | ( bdsp[2*
i-1] & 0xFFFF) );
323 OFC.push_back( ( (cdsp[0] << 16) & 0xFFFF0000) | ( (cscale) & 0xFFFF) );
324 for (
int i=1;
i<=n_2;
i++)
325 OFC.push_back( ( (cdsp[2*
i] << 16) & 0xFFFF0000) | ( cdsp[2*
i-1] & 0xFFFF) );
328 OFC.push_back( ( (hscale << 16) & 0xFFFF0000) | ( (gscale+1) & 0xFFFF) );
329 for (
size_t i=0;
i<
g.size();
i++)
330 OFC.push_back( ( (hdsp[
i] << 16) & 0xFFFF0000) | ( gdsp[
i] & 0xFFFF) );
333 OFC.push_back( ( (channel_index << 16) & 0xFFFF0000) | (
phase & 0xFFFF) ) ;
335 if (
verbose) cout<<
"size of OFC: "<<OFC.size()<<endl;
360 const vector<double>&
b ,
361 const vector<double>&
c,
362 const vector<double>&
g,
363 const vector<double>&
h,
364 vector<unsigned int> &OFC ,
390 int n_2 = (
int) trunc(
a.size()/2.);
392 for (
int i=0;
i<n_2;
i++)
393 OFC.push_back( ( (adsp[2*
i] << 16 )& 0xFFFF0000) | ( adsp[2*
i+1] & 0x0000FFFF) ) ;
394 OFC.push_back( ( (adsp[2*n_2] << 16 )& 0xFFFF0000) | ( (-asum) & 0x0000FFFF) );
396 for (
int i=0;
i<n_2;
i++)
397 OFC.push_back( ( (bdsp[2*
i] << 16 )& 0xFFFF0000) | ( bdsp[2*
i+1] & 0x0000FFFF) ) ;
398 OFC.push_back( ( (bdsp[2*n_2] << 16 )& 0xFFFF0000) | ( (-bsum) & 0x0000FFFF) );
400 OFC.push_back( ( (bscale << 16 )& 0xFFFF0000) | (
ascale & 0x0000FFFF) );
402 OFC.push_back( ( (cdsp[0] << 16 )& 0xFFFF0000) | ( cscale & 0x0000FFFF) );
403 for (
int i=1;
i<=n_2;
i++)
404 OFC.push_back( ( (cdsp[2*
i] << 16 )& 0xFFFF0000) | ( cdsp[2*
i-1] & 0x0000FFFF) ) ;
406 OFC.push_back( ( (hscale << 16 )& 0xFFFF0000) | ( gscale & 0x0000FFFF) );
407 for (
size_t i=0;
i<
g.size();
i++)
408 OFC.push_back( ( (hdsp[
i] << 16 )& 0xFFFF0000) | ( gdsp[
i] & 0x0000FFFF) );
416 if (
verbose) cout<<
"size of OFC: "<<OFC.size()<<endl;
423 unsigned int offset_scale = 0;
426 unsigned int slope_scale;
431 OFC.push_back( ( (offset_scale << 16) & 0xFFFF0000) | ( slope_scale & 0x0000FFFF) );
432 OFC.push_back( ( (
offset << 16) & 0xFFFF0000) | ( slope & 0x0000FFFF) );
445 cout<<
"ERROR: [FormatInfo] Incorrect number of samples"<<endl;
449 if (calibrationtype > 3 || calibrationtype < 0 ) {
450 cout<<
"ERROR: [FormatInfo] Unknown calibration type"<<endl;
457 cout<<
"ERROR: [FormatInfo] Unknown algorithm type"<<endl;
461 OFC.push_back( ((calibrationtype & 0x3)<<6) | ((runtype & 0x3)<<4) | ((in & 0
x1)<<3) | ((
algorithm & 0
x1) << 2) );
463 if (
verbose) printf(
"OFC info: 0x%08x\n", ((calibrationtype & 0x3)<<6) | ((runtype & 0x3)<<4) | ((in & 0
x1)<<3) | ((
algorithm & 0
x1) << 2) );
472 fin.open(OFCfile, ios::in);
475 cout<<
"Error opening file "<<(OFCfile)<<endl;
483 int nphases = (
int) roundf(-f_phi*2./f_step + 1.);
496 cout<<
"Number of parameters: "<<nparams<<endl;
499 w_off.resize(ngains);
500 for (
int ig=0; ig<ngains; ig++){
503 w_off[ig][
it].resize(nparams);
504 for (
int ip=0;
ip<nparams;
ip++){
512 fin.open(OFCfile, ios::in);
517 for (
int ig=0; ig<ngains; ig++)
523 w_off[ig][
it][
ip][is] = tmp1;
533 cout<<
"Optimal Filtering file: "<<(OFCfile)<<endl;
534 cout<<
"Nb phases: "<<w_off[0].size()<<endl;
535 cout<<
"Nb samples: "<<w_off[0][0][0].size()<<endl;
536 cout<<
"Nb gains: "<<w_off.size()<<endl;
537 if ((
int) w_off[0][0].size() == 3 ) cout<<
"Algorithm: OF1"<<endl;
538 else if ((
int) w_off[0][0].size() == 4 ) cout<<
"Algorithm: OF2"<<endl;