使用FDATool工具,构建一个Kaiser窗低通滤波器,采样频率8kHz,生成滤波器系数:
1)例程1
文件 fir_fltcoeff.h,以浮点形式表示的量化后的滤波器系数//define the FIR filterlength#define N_FIR 40
float h[N_FIR] ={ ‐ 0.004314029307,‐0.013091321622,‐0.016515087727, ‐0.006430584433, 0.009817876267,0.010801880238, ‐0.006567413713,‐ 0.016804829623, 0.000653253913, 0.022471280087,0.010147131468,‐0.025657740989, ‐0.026558960619, 0.023048392854,0.050385290390, ‐0.009291203588,‐ 0.087918503442,‐0.033770330014, 0.187334796517, 0.401505729848,0.401505729848, 0.187334796517,‐0.033770330014,‐0.087918503442, ‐0.009291203588, 0.050385290390,0.023048392854, ‐0.026558960619,‐ 0.025657740989, 0.010147131468, 0.022471280087,0.000653253913,‐0.016804829623, ‐0.006567413713, 0.010801880238,0.009817876267, ‐0.006430584433,‐ 0.016515087727,‐0.013091321622, ‐0.004314029307 };
实际电路用AIC3106,每次采样中断来临后读入一组数据,分别为左右声道的数据,各16位。中断服务程序将左声道的每个数据送入滤波器,然后从DAC电路输出滤波后的结果。右声道数据不变。
文件 ISRs_Plus.c ,中断服务程序:// Welch, Wright, & Morrow,// Real-time Digital Signal Processing, 2011// Modified by Mark Wickert February 2012 to include GPIO ISRstart/stop postings
///////////////////////////////////////////////////////////////////////// Filename: ISRs_fir_float.c//// Synopsis: Interrupt service routine for codec datatransmit/receive// floating point FIR filteringwith coefficients in *.h file/////////////////////////////////////////////////////////////////////////
#include "DSP_Config.h"#include "fir_fltcoeff.h" //coefficients infloat format
// Function Prototypeslong int rand_int(void);// Data is received as 2 16-bit words (left/right) packed intoone// 32-bit word. The union allows the data tobe accessed as a single// entity when transferring to and from the serial port, butstill be// able to manipulate the left and right channelsindependently.
#define LEFT 0#define RIGHT 1
volatile union {Uint32 UINT;Int16 Channel[2];} CodecDataIn, CodecDataOut;
float x_buffer[N_FIR]; //buffer for delay samples
interrupt void Codec_ISR()///////////////////////////////////////////////////////////////////////// Purpose: Codec interface interruptservice routine //// Input: None//// Returns: Nothing//// Calls: CheckForOverrun, ReadCodecData, WriteCodecData//// Notes: None///////////////////////////////////////////////////////////////////////{ WriteDigitalOutputs(1); // Write to GPIO J15, pin 6; begin ISRtiming pulseint i;float result = 0; //initialize the accumulator
if(CheckForOverrun()) // overrun erroroccurred (i.e. halted DSP)return; // so serial port is reset to recover
CodecDataIn.UINT = ReadCodecData(); // getinput data samples
//Work with Left ADC sample//x_buffer[0] = 0.25 * CodecDataIn.Channel[ LEFT];//Use the next line to noise test the filterx_buffer[0] = 0.125*((short) rand_int());//scale input by1/8
//Filtering using a 32-bit accumulatorfor(i=0; i< N_FIR; i++){result += x_buffer[i] * h[i];}//Update filter historyfor(i=N_FIR-1; i>0; i--){x_buffer[i] = x_buffer[i-1];}
//Return 16-bit sample to DACCodecDataOut.Channel[ LEFT] = (short) result;// Copy Right input directly to Right output with nofilteringCodecDataOut.Channel[RIGHT] = CodecDataIn.Channel[RIGHT];WriteCodecData(CodecDataOut.UINT); // send output data toportWriteDigitalOutputs(0); // Write to GPIO J15, pin 6; end ISRtiming pulse}
//White noise generator for filter noise testinglong int rand_int(void){static long int a = 100001;
a = (a*125) % 2796203;return a;}
可以看出,中断服务程序是逐点响应的,每次来一个数据,都要做一次滤波器运算,并将原有的数据前移一位,将新数据追加在缓冲区末尾。float x_buffer[N_FIR];//用于保存现在的新值和过去的数值,长度与滤波器系数相同x_buffer[0] // 用于保存当前值x_buffer[1] // 保存过去前一点的值,以此类推h[0...39] // 滤波器系数result += x_buffer[i] * h[i]; // 当前点滤波器输出结果,循环从0到39
