/*============================================================================= Name: calibIQMod Abs: calibrate the I/Q Modulator Args: Type Name Access Description ----------------- ----------- --------- ---------------------------- double [] Iin read-only array of I input values from 360degree scan double [] Qin read-only array of Q input values from 360degree scan double [] Iout read-only array of I output values from 360degree scan double [] Qout read-only array of Q output values from 360degree scan int scan_points read-only number of points in scan double pha_imbalance_sf read-only scale factor for phase imbalance calculation double iq_offset_sf read-only scale factor for amplitude imbalance calculation int debug_flag read-only debug flag indicates to print debug statements long b_offs_i read-only long b_offs_q read-only double old_imbalance_matrix_a11 read-only old_IQ_imbalance_matrix (1,1) double old_imbalance_matrix_a12 read-only old_IQ_imbalance_matrix (1,2) double old_imbalance_matrix_a21 read-only old_IQ_imbalance_matrix (2,1) double old_imbalance_matrix_a22 read-only old_IQ_imbalance_matrix (2,2) double * ki write-only Ki intermediate result double * kq write-only Kq intermediate result double * amp_imbalance write-only g intermediate result double * pha_imbalance_deg write-only psi/degree intermediate result long * i_off write-only calibrated I offset long * q_off write-only calibrated Q offset double * iqcorr_a11 write-only calibrated IQ_imbalance_matrix (1,1) double * iqcorr_a12 write-only calibrated IQ_imbalance_matrix (1,2) double * iqcorr_a21 write-only calibrated IQ_imbalance_matrix (2,1) double * iqcorr_a22 write-only calibrated IQ_imbalance_matrix (2,2) Rem: function to calibrate the I/Q Modulator: DC offset for I DC offset for Q imbalance matrix [2x2] and some intermediate values. Use gsl_multifit_linear (gnu scientific library) to fit with an unweighted linear fit. Side: if debugFlag is nonzero, prints diagnostic messages (using errlogPrintf) Ret: Integer indicating status of calibration =============================================================================*/ #include // sprintf #include // errlogPrintf #include // gsl_multifit_linear #include "gsl_matrix_double.h" // gsl_vector_alloc, gsl_matrix_alloc, gsl_matrix_set #include "gsl_vector_double.h" // gsl_vector_set, gsl_vector_get #include // pi, sin, cos #include // complex int calibIQMod(double Iin[], double Qin[], double Iout[], double Qout[], int scan_points, double pha_imbalance_sf, double iq_offset_sf, int debug_flag, long b_offs_i, long b_offs_q, double old_imbalance_matrix11, double old_imbalance_matrix12, double old_imbalance_matrix21, double old_imbalance_matrix22, double *ki, double *kq, double *amp_imbalance, double *pha_imbalance_deg, long *i_off, long *q_off, double *iqcorr_a11, double *iqcorr_a12, double *iqcorr_a21, double *iqcorr_a22 ) { int iss; // result of our calibration; // variables for linear fit int ndx; // index of for loop int ndx2; // matrix index because matrix has 2 rows for each sample row int num_obs; // number of observations int num_params; // number of fit parameters gsl_multifit_linear_workspace *ws; gsl_matrix *cov; gsl_matrix *X; // x matrix gsl_vector *y; // y vector gsl_vector *cv; // coefficient vector is fit parameters double chisq; // chi sqare double a,b,c,d,k,l; // save results of fit // variables for inv(A)*B matrix multiply double detA; // determinant of A double X11; // 1,1 result of [2x2]*[2x1] matrix multiply double X21; // 2,1 result of [2x2]*[2x1] matrix multiply double Ioff; // intermediate value of i_off double Qoff; // intermediate value of q_off double gcosf; // g cos of psi double gsinf; // g sin of psi // double complex gf; // complex of gcosf, gsinf double inc_imbalance_matrix11; // increment imbalance matrix (1,1) double inc_imbalance_matrix12; // increment imbalance matrix (1,2) double inc_imbalance_matrix21; // increment imbalance matrix (2,1) double inc_imbalance_matrix22; // increment imbalance matrix (2,2) /*************************************************************/ errlogPrintf("CalibIQMod: starting calibrate routine with %d scan_points and debug=%d.\n",scan_points,debug_flag); if (debug_flag) { errlogPrintf("CalibIQMod: pha_imb_sf=%f off_sf=%f b_offs_I=%f b_offs_Q=%f \n", pha_imbalance_sf, iq_offset_sf, (float) b_offs_i,(float) b_offs_q); errlogPrintf("CalibIQMod: 1st 6 points of Input and Output waveforms:\n"); for (ndx = 0; ndx < 7; ndx++) { errlogPrintf("CalibIQMod: step=%d, Iin=%f, Qin=%f, Iout=%f, Qout=%f\n", ndx, Iin[ndx],Qin[ndx], Iout[ndx],Qout[ndx]); } } // allocate data structures needed for the fit */ iss = 1; // assume successful return num_obs = 2*scan_points; // use real and imaginary parts of each modulator scan point num_params = 6; // 6th degree X = gsl_matrix_alloc(num_obs, num_params); y = gsl_vector_alloc(num_obs); cv = gsl_vector_alloc(num_params); cov = gsl_matrix_alloc(num_params, num_params); ws = gsl_multifit_linear_alloc(num_obs, num_params); // load the Iin, Qin, Iout, and Qout data into our matrices for (ndx = 0; ndx < scan_points; ndx++) { ndx2 = 2*ndx; // our matrix has 2 rows per sample I and Q /* * X[ndx2,0] = Iin[ndx]; * X[ndx2,1] = Qin[ndx]; * X[ndx2,2] = 0; * X[ndx2,3] = 0; * X[ndx2,4] = 1; * X[ndx2,5] = 0; * X[ndx2+1,0] = 0; * X[ndx2+1,1] = 0; * X[ndx2+1,2] = Iin[ndx]; * X[ndx2+1,3] = Qin[ndx]; * X[ndx2+1,4] = 0; * X[ndx2+1,5] = 1; */ gsl_matrix_set (X, ndx2, 0, Iin[ndx]); gsl_matrix_set (X, ndx2, 1, Qin[ndx]); gsl_matrix_set (X, ndx2, 2, 0.0); gsl_matrix_set (X, ndx2, 3, 0.0); gsl_matrix_set (X, ndx2, 4, 1.0); gsl_matrix_set (X, ndx2, 5, 0.0); gsl_matrix_set (X, ndx2+1, 0, 0.0); gsl_matrix_set (X, ndx2+1, 1, 0.0); gsl_matrix_set (X, ndx2+1, 2, Iin[ndx]); gsl_matrix_set (X, ndx2+1, 3, Qin[ndx]); gsl_matrix_set (X, ndx2+1, 4, 0.0); gsl_matrix_set (X, ndx2+1, 5, 1.0); /* y[ndx2] = Iout[ndx]; * y[ndx2+1] = Qout[ndx]; */ gsl_vector_set (y, ndx2, Iout[ndx]); gsl_vector_set (y, ndx2+1, Qout[ndx]); } // use a linear fit instead of matlab matrix left divide cv = X\y // call the gnu scientific library fit gsl_multifit_linear(X, y, cv, cov, &chisq, ws); // & get our fit parameters a = gsl_vector_get(cv,0); b = gsl_vector_get(cv,1); c = gsl_vector_get(cv,2); d = gsl_vector_get(cv,3); k = gsl_vector_get(cv,4); l = gsl_vector_get(cv,5); if (debug_flag) { errlogPrintf("CalibIQMod: Results of linear fit: a=%f b=%f c=%f d=%f k=%f l=%f\n", a,b,c,d,k,l); } // now matrix left divide of our fit results // The Matlab code: // A = [a b; c d]; // B = [k;l]; // X = A\B; // for A being a 2x2 matrix, the matrix left divide // can be replaced with inv(A)* B // (see http://www.mathwords.com/i/inverse_of_a_matrix.htm) detA = (a*d)-(b*c); // determinant of A X11 = (d*k) + (-b*l); // X21 = (-c*k) + (a*l); Ioff = X11/detA; Qoff = X21/detA; // calculate other parameters *ki = (a - c*b/d) / (1 + (c/d)*(c/d)); *kq = *ki * c/d; gcosf = c / *kq; gsinf = (b + *kq) / *ki; *pha_imbalance_deg = atan(gsinf / gcosf) * 180.0 / M_PI; *amp_imbalance = cabs(gcosf + gsinf*_Complex_I); // get complex absolute value *pha_imbalance_deg = *pha_imbalance_deg * pha_imbalance_sf; *amp_imbalance = sqrt(*amp_imbalance); // calculate the new correction matrix // matlab code: // inc_imbalance_matrix = [1 -tan(pha_imbalance_deg * pi / 180); 0 1/(amp_imbalance * cos(pha_imbalance_deg * pi / 180))] // old_imbalance_matrix = [old_imbalance_matrix_a11 old_imbalance_matrix_a12; old_imbalance_matrix_a21 old_imbalance_matrix_a22] // new_imbalance_matrix11 = old_imbalance_matrix * inc_imbalance_matrix; inc_imbalance_matrix11 = 1; inc_imbalance_matrix12 = -tan(*pha_imbalance_deg * M_PI / 180.0); inc_imbalance_matrix21 = 0; inc_imbalance_matrix22 = 1/(*amp_imbalance * cos(*pha_imbalance_deg * M_PI / 180.0)); // Matrix multiple and save the results // copy of matlab code: // new_imbalance_matrix = old_imbalance_matrix X increment_imbalance_matrix. // *iqcorr_a11 = new_imbalance_matrix(1,1); // *iqcorr_a12 = new_imbalance_matrix(1,2); // *iqcorr_a21 = new_imbalance_matrix(2,1); // *iqcorr_a22 = new_imbalance_matrix(2,2); // do the matrix multiple and save the new imbalence matrix elements *iqcorr_a11 = (old_imbalance_matrix11 * inc_imbalance_matrix11) + (old_imbalance_matrix12 * inc_imbalance_matrix21); *iqcorr_a12 = (old_imbalance_matrix11 * inc_imbalance_matrix12) + (old_imbalance_matrix12 * inc_imbalance_matrix22); *iqcorr_a21 = (old_imbalance_matrix21 * inc_imbalance_matrix11) + (old_imbalance_matrix22 * inc_imbalance_matrix21); *iqcorr_a22 = (old_imbalance_matrix21 * inc_imbalance_matrix12) + (old_imbalance_matrix22 * inc_imbalance_matrix22); // --- save the I and Q offset calibration results --- *i_off = Ioff * iq_offset_sf + b_offs_i; *q_off = Qoff * iq_offset_sf + b_offs_q; /* free the gsl fit data structures */ gsl_multifit_linear_free(ws); gsl_matrix_free(cov); gsl_vector_free(cv); gsl_vector_free(y); gsl_matrix_free(X); return iss; }