码迷,mamicode.com
首页 > 其他好文 > 详细

FIR滤波器的实现方法(转)

时间:2015-03-28 11:26:28      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

源:http://blog.sina.com.cn/s/blog_493520900102uy26.html

 

内容来自于上篇博文,第七章,FIR滤波器

http://blog.sina.com.cn/s/blog_493520900102uxzu.html

 
使用 FDATool工具,构建一个Kaiser窗低通滤波器,采样频率8kHz,生成滤波器系数:
 
1)例程1
 
文件 fir_fltcoeff.h,以浮点形式表示的量化后的滤波器系数
//define the   FIR  filter  length
#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 ISR start/stop postings
 
///////////////////////////////////////////////////////////////////////
// Filename: ISRs_fir_float.c
//
// Synopsis: Interrupt service routine for codec data transmit/receive
//           floating point FIR filtering with coefficients in *.h file
//
///////////////////////////////////////////////////////////////////////
 
#include "DSP_Config.h"
#include "fir_fltcoeff.h"   //coefficients in float format
 
// Function Prototypes
long int rand_int(void);
  
// Data is received as 2 16-bit words (left/right) packed into one
// 32-bit word.  The union allows the data to be accessed as a single 
// entity when transferring to and from the serial port, but still be 
// able to manipulate the left and right channels independently.
 
#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 interrupt service routine  
//
// Input:     None
//
// Returns:   Nothing
//
// Calls:     CheckForOverrun, ReadCodecData, WriteCodecData
//
// Notes:     None
///////////////////////////////////////////////////////////////////////
{                    
WriteDigitalOutputs(1); // Write to GPIO J15, pin 6; begin ISR timing pulse
int i;
float result = 0; //initialize the accumulator
 
 
  if(CheckForOverrun()) // overrun error occurred (i.e. halted DSP)
return; // so serial port is reset to recover
 
  CodecDataIn.UINT = ReadCodecData(); // get input data samples
 
//Work with Left ADC sample
//x_buffer[0] = 0.25 * CodecDataIn.Channel[ LEFT];
//Use the next line to noise test the filter
x_buffer[0] = 0.125*((short) rand_int());//scale input by 1/8
 
//Filtering using a 32-bit accumulator
for(i=0; i< N_FIR; i++)
{
result += x_buffer[i] * h[i];
}
//Update filter history
for(i=N_FIR-1; i>0; i--)
{
x_buffer[i] = x_buffer[i-1];
}
 
//Return 16-bit sample to DAC
CodecDataOut.Channel[ LEFT] = (short) result;
// Copy Right input directly to Right output with no filtering
CodecDataOut.Channel[RIGHT] = CodecDataIn.Channel[ RIGHT];
WriteCodecData(CodecDataOut.UINT); // send output data to  port
WriteDigitalOutputs(0); // Write to GPIO J15, pin 6; end ISR timing pulse
}
 
//White noise generator for filter noise testing
long 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

 

FIR滤波器的实现方法(转)

标签:

原文地址:http://www.cnblogs.com/LittleTiger/p/4373688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!