📄 adaptive_filter.c
字号:
/*****************************************************************************/
/* */
/* FILENAME */
/* adaptive_filter.c */
/* */
/* DESCRIPTION */
/* Using an adaptive filter to identify a waveform from the analog to */
/* digital converter (ADC) and compare it with a reference waveform */
/* generated from a table. */
/* */
/* REVISION */
/* Revision: 1.00 */
/* Author : Richard Sikora */
/*---------------------------------------------------------------------------*/
/* */
/* HISTORY */
/* Revision 1.00 */
/* 13th November 2002. Created by Richard Sikora from TMS320C5402 code. */
/* Modified so that can it be used a starting point for assembly version. */
/* */
/*****************************************************************************/
/*****************************************************************************/
/* Constants */
/*---------------------------------------------------------------------------*/
/* Here N is number of elements in adaptive filter. */
/* BETA is the amount of the output used for feedback to determine the new */
/* filter weights. 56 represents 56/32767 i.e. 0.002 */
/* We could also approximate 0.002 to 1/512 i.e. divide by 2 ^ 9 */
/*****************************************************************************/
#define N 12
#define BETA 5600
/*****************************************************************************/
/* Variables */
/*---------------------------------------------------------------------------*/
/* Two arrays are required. One to contain the input samples and the other */
/* to contain the weights. */
/* */
/*****************************************************************************/
static signed int x[N]; /* Array to hold input samples */
static signed int w[N]; /* Weights (adaptive filter coefficients)*/
static signed int error; /* Difference between desired and actual. */
/*****************************************************************************/
/* adaptive_filter_initialize() */
/*****************************************************************************/
/* */
/* Start with all inputs and adaptive filter coefficients at zero. */
/* */
/*****************************************************************************/
void adaptive_filter_initialize(void)
{
unsigned int i;
for ( i = 0 ; i < N ; i++)
{
w[i] = 0;
x[i] = 0;
}
error = 0;
}
/*****************************************************************************/
/* adaptive_filter() */
/*---------------------------------------------------------------------------*/
/* */
/* INPUT 1: desired (or reference) waveform. */
/* INPUT 2: input signal (unknown) to be compared with the reference. */
/* */
/* RETURNS: Filter output y in high word. Error in low word */
/* */
/*****************************************************************************/
signed int i;
signed long y; /* Output of adaptive filter */
signed long difference;
signed long adaptive_filter( signed int desired, signed int latest_measurement)
{
/* Shuffle first and update */
for ( i = N-1 ; i > 0 ; i--)
{
x[i] = x[i-1];
}
x[0] = latest_measurement;
difference = error * BETA;
error = (signed int) (difference >> 15); /* Update error */
y = 0; /* Start accumulation at zero */
/* Use error from last time through */
for ( i = N-1 ; i >= 0 ; i--)
{
y += ((long)(x[i] * w[i]) >> 0 ); /* Prevent overflows */
difference = ( (long) error * x[i] ) >> 14;
/* Remove fractional part then update coeffient W[i] */
if ( difference & 0x0001)
{
if ( difference >= 0)
{
difference++ ; /* Round up when above zero */
}
else
{
difference--; /* Round down when below zero */
}
}
w[i] += (signed int) ( difference >> 1);
}
y >>= 15; /* Remove remains of fractional part */
error = desired - (signed int) y;
return( ( y << 16 ) | (long) error ); /* Return y OR error */
}
/*****************************************************************************/
/* End of adaptive_filter.c */
/*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -