📄 dpll.c
字号:
/*****************************************************************
* 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 <stdio.h>
#include <stdlib.h>
#include <math.h>
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; i<median_order; i++)
{
median_buf[i] = 2047; /* data buffer of median filter */
sort_buf[i] = 2047; /* sorting buffer of median filter */
}
alpha = (float)(1.0 / (float)MA_order); /* alpha = 1./MA_order*/
alpha1 = (float)(1.0 - alpha); /* alpha1 = 1 - alpha */
/*****************************************************************
* Start of main program
*****************************************************************/
while( (fscanf(en_in,"%d",&en)) != EOF)
{ /* read in e(n) from data file and processing it */
/*****************************************************************
* Median filter: Order = 5
*****************************************************************/
for (i=median_order-1; i>0; 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<median_order; i++)
{ /* move data from median filter buffer */
sort_buf[i] = median_buf[i]; /* to sorting buffer */
}
/* Sorting data in sort_buf, the smallest one in sort_buf[0]*/
for (i=1; i<median_order; i++)
{ /* assuming sort_buf[0] contains the smallest one */
if (sort_buf[i] < sort_buf[0])
{ /* sort_buf[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<median_order; i++)
{ /* assuming sort_buf[1] contains the second small one */
if (sort_buf[i] < sort_buf[1])
{
temp = sort_buf[1];
sort_buf[1] = sort_buf[i];
sort_buf[i] = temp;
}
} /* sort_buf[1] is the second small one */
for (i=3; i<median_order; i++)
{ /* assuming sort_buf[2] contains the third small one */
if (sort_buf[i] < sort_buf[2])
{
temp = sort_buf[2];
sort_buf[2] = sort_buf[i];
sort_buf[i] = temp;
}
}
epn = sort_buf[2]; /* sort_buf[2] is median data e'(n) */
/*****************************************************************
* MA filter: Order = MA_order
*
* c(n) = (1-alpha)*c(n-1)+alpha*e'(n)
* where
* alpha = 1/MA_order
*****************************************************************/
cn = alpha1*cn + alpha*(float)epn;
fprintf(cn_out,"%d\n",(int)(cn+0.5));
/* rounding c(n) to integer and write it to output file */
}
fcloseall(); /* close all opened files */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -