Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
typedef unsigned int U;
struct vec32{
U c;
U d;
};
struct vec32 my_struct;
struct vec32 lsts;
struct vec32 vgagain;
struct vec32 dcout1i;
struct vec32 dcout1q;
struct vec32 dcout2i;
struct vec32 dcout2q;
double input_re, input_im;
double adc_input_re, adc_input_im;
void rf_model(double *input_re, double *input_im, struct vec32 *lsts, struct vec32 *vgagain_ptr, struct vec32 *dcout1i, struct vec32 *dcout1q, struct vec32 *dcout2i, struct vec32 *dcout2q , double *adc_input_re, double *adc_input_im);
int main(void)
{
FILE *Earf;
int ch, line=0;
char c[100];
double *input_re_ptr, *input_im_ptr, *adc_input_re_ptr, *adc_input_im_ptr;
struct vec32 *stptr, *lsts_ptr, *vgagain_ptr, *dcout1i_ptr, *dcout1q_ptr, *dcout2i_ptr, *dcout2q_ptr;
input_re_ptr = &input_re;
input_im_ptr = &input_im;
lsts_ptr = &lsts;
vgagain_ptr = &vgagain;
dcout1i_ptr = &dcout1i;
dcout1q_ptr = &dcout1q;
dcout2i_ptr = &dcout2i;
dcout2q_ptr = &dcout2q;
adc_input_re_ptr = &adc_input_re;
adc_input_im_ptr = &adc_input_im;
Earf = fopen("rf_model.dat", "r");
while (fgets(c,sizeof(c),Earf) != NULL){
sscanf(c,"%lf %lf %u %u %u %u %u %u %lf %lf ", &input_re, &input_im, &(lsts).d , &(vgagain).d,\
&(dcout1i).d, &(dcout1q).d, &(dcout2i).d, &(dcout2q).d, &adc_input_re, &adc_input_im );
rf_model(input_re_ptr, input_im_ptr, lsts_ptr, vgagain_ptr, dcout1i_ptr, dcout1q_ptr, dcout2i_ptr, dcout2q_ptr, adc_input_re_ptr, adc_input_im_ptr);
}
return 0;
}
void rf_model(double *input_re, double *input_im, struct vec32 *lsts, struct vec32 *vgagain, struct vec32 *dcout1i, struct vec32 *dcout1q, struct vec32 *dcout2i, struct vec32 *dcout2q, double *adc_input_re, double *adc_input_im){
double lna_gain_lin;
double lna_high_gain_deci = 17.0;
double lna_low_gain_deci = -3.0;
double vga1_gain;
double vga2_gain;
double fvgagain;
double vgagain_lin;
double dcout1_re, dcout1_im;
double dcout2_re, dcout2_im;
/****** translate straight binary format to 2's compensation format ****/
printf("%lf %lf \n", *adc_input_re, *adc_input_im);
dcout1_re = (double)((*dcout1i).d) -128;
dcout1_im = (double)((*dcout1q).d) -128;
dcout2_re = (double)((*dcout2i).d) -128;
dcout2_im = (double)((*dcout2q).d) -128;
/*** add offset ***/
*adc_input_re = *input_re + 512.0;
*adc_input_im = *input_im - 512.0;
/*** LNA control ***/
if((*lsts).d == 1){
lna_gain_lin = pow(10.0,lna_high_gain_deci/20.0);
}
else{
lna_gain_lin = pow(10.0,lna_low_gain_deci/20.0);
}
*adc_input_re = *adc_input_re * lna_gain_lin;
*adc_input_im = *adc_input_im * lna_gain_lin;
/*** VGA control ***/
fvgagain = (double) ((*vgagain).d);
if(fvgagain <= 41 ){
vga1_gain = 0.0;
vga2_gain = fvgagain;
}
else if(fvgagain <= 47){
vga1_gain = 1.0*6.0;
vga2_gain = fvgagain -vga1_gain ;
}
else if(fvgagain <= 53){
vga1_gain = 2.0*6.0;
vga2_gain = fvgagain -vga1_gain ;
}
else if(fvgagain <= 59){
vga1_gain = 3.0*6.0;
vga2_gain = fvgagain -vga1_gain ;
}
else if(fvgagain <= 65){
vga1_gain = 4.0*6.0;
vga2_gain = fvgagain -vga1_gain ;
}
else if(fvgagain <= 71){
vga1_gain = 5.0*6.0;
vga2_gain = fvgagain -vga1_gain ;
}
else{
vga1_gain = 5.0*6.0;
vga2_gain = fvgagain -vga1_gain ;
}
vgagain_lin = pow(10.0,(vga1_gain)/20.0);
*adc_input_re = (*adc_input_re - dcout1_re)*vgagain_lin;
*adc_input_im = (*adc_input_im - dcout1_im)*vgagain_lin;
vgagain_lin= pow(10.0,(vga2_gain)/20.0);
*adc_input_re = (*adc_input_re - dcout2_re)*vgagain_lin* 0.001;
*adc_input_im = (*adc_input_im - dcout2_im)*vgagain_lin* 0.001;
/****FINAL output *******/
printf("%lf %lf\n ", *adc_input_re, *adc_input_im);
}