/***************************************************************** * dpll.C - C program simulation of DPLL using a nonlinear median * filter and a linear lowpass filter ****************************************************************** * Real-time system configuration: * * * e(n) |---------------| e'(n)|----------------| c(n) * ---->| Median filter |----->| Lowpass filter |-----> * |---------------| |----------------| * * where * e(n) is data read in from UP/DOWN counter after phase detector * Median filter: Order 5, the output e'(n) is the median of 5 * samples in the buffer * Lowpass filter is the moving-averaging filer implemented * as the first-order IIR filter * * NOTE: all data buffers are initialized to 2047 as middle of * real-time data using 12-bit converter ****************************************************************** * System simulation configuration: * e(n) is the data from data file "en.dat" * c(n) is the LPF output data to data file: "cn.dat" *****************************************************************/ #include #include #include void main() { /***************************************************************** * Define variable arrays, define and clear variables *****************************************************************/ int i; /* integers for indexes */ int median_order = 5; /* order of median filter */ int MA_order = 1024; /* order of MA filter */ int en = 0; /* e(n) from en.dat file */ int epn = 0; /* e'(n), output from median filter */ float cn = 2047.0; /* c(n), output from MA filter */ int median_buf[5]; /* buffer for median filter */ int sort_buf[5]; /* sorted buffer for median filter */ int temp = 0; /* temporary storage for sorting */ float alpha = 0.0; /* alpha = 1./MA_order */ float alpha1 = 0.0; /* alpha1 = 1 - alpha */ /***************************************************************** * Declare file pointers *****************************************************************/ FILE *en_in; /* file pointer of e(n) */ FILE *cn_out; /* file pointer of c(n) */ en_in = fopen("en.dat","r"); /* open file for output x(n) */ cn_out = fopen("cn.dat","w"); /* open file for output w(n) */ /***************************************************************** * Clear arrays and initialization *****************************************************************/ for (i=0; i0; i--) { /* update signal buffer of median filter*/ median_buf[i] = median_buf[i-1]; } median_buf[0] = en; /* shift the new sample into buffer */ for (i=0; i sort_buf[0], swap the data */ temp = sort_buf[0]; sort_buf[0] = sort_buf[i]; sort_buf[i] = temp; } } /* now sort_buf[0] is smallest one */ for (i=2; i