FIR低通滤波器C语言实现
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
int ii; SAMPLE accum;
/* store input at the beginning of the delay line */ z[0] = input;
第2页
文文: C:\Documents and Settings\user\桌桌\FirAlgs.c 2009-3-27 27, 15:53:51
int ii; for (ii = 0; ii < ntaps; ii++) {
z[ii] = 0; } }
/************************************************************* *************** * fir_basic: Does the basic FIR algorithm: store input sample, calculate * output sample, move delay line ************************************************************** ***************/ SAMPLE fir_basic(SAMPLE input, int ntaps, const SAMPLE h[], SAMPLE z[]) {
/* calc FIR and shift data */
accum = 0;
for (ii = ntaps - 1; ii >= 0; ii--) {
accum += h[ii] * z[state];
if (++state >= ntaps) {
/* incr state and check
for wrap */
第3页
文文: C:\Documents and Settings\user\桌桌\FirAlgs.c 2009-3-27 27, 15:53:51
z[state] = input; if (++state >= ntaps) { for wrap */
state = 0; }
/* incr state and check
int *p_state) {
int ii, state; SAMPLE accum;
state = *p_state; to a local */
/* copy the filter's state
/* store input at the beginning of the delay line */
implement FIR filters
* in C.
*
* Copyright 2000 by Grant R. Griffin
*
* Thanks go to contributors of comp.dsp for teaching me some
of these
* techniques, and to Jim Thomas for his review and great
int ii; SAMPLE accum;
/* store input at the beginning of the delay line */ z[0] = input;
/* calc FIR and shift data */ accum = h[ntaps - 1] * z[ntaps - 1]; for (ii = ntaps - 2; ii >= 0; ii--) {
suggestions.
*wenku.baidu.com
*
The Wide Open License (WOL)
*
* Permission to use, copy, modify, distribute and sell this
software and its
* documentation for any purpose is hereby granted without fee,
return accum; }
/************************************************************* *************** * fir_circular: This function illustrates the use of "circular" buffers * in FIR implementations. The advantage of circular buffers is that they * alleviate the need to move data samples around in the delay line (as * was done in all the functions above). Most DSP microprocessors implement * circular buffers in hardware, which allows a single FIR tap can be * calculated in a single instruction. That works fine when programming in * assembly, but since C doesn't have any constructs to represent circular * buffers, you need to "fake" them in C by adding an extra "if" statement * inside the FIR calculation, even if the DSP processor provides hardware to * implement them without overhead. ************************************************************** ***************/ SAMPLE fir_circular(SAMPLE input, int ntaps, const SAMPLE h[], SAMPLE z[],
#include <stdio.h> #ifdef WIN32
#include <conio.h> #endif
#define SAMPLE double samples */
/* define the type used for data
/************************************************************* ***************/ /* clear: zeroize a FIR delay line */ /************************************************************* ***************/ void clear(int ntaps, SAMPLE z[]) {
provided that
* the above copyright notice and this license appear in all
第1页
文文: C:\Documents and Settings\user\桌桌\FirAlgs.c 2009-3-27 27, 15:53:51
source copies. * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF * ANY KIND. See http://www.dspguru.com/wol.htm for more information. * ************************************************************** ***************/
文文: C:\Documents and Settings\user\桌桌\FirAlgs.c 2009-3-27 27, 15:53:51
/*************************************************************
***************
*
* Name: FirAlgs.c
not very
* efficient in C, and are provided here mainly to illustrate
FIR
* algorithmic concepts. However, the functions fir_split,
fir_double_z
* and fir_double_h are all fairly efficient ways to
state = 0;
}
}
*p_state = state; caller */
/* return new state to
return accum; }
/************************************************************* *************** * fir_shuffle: This is like fir_basic, except that data is shuffled by * moving it _inside_ the calculation loop. This is similar to the MACD * instruction on TI's fixed-point processors ************************************************************** ***************/ SAMPLE fir_shuffle(SAMPLE input, int ntaps, const SAMPLE h[], SAMPLE z[]) {
accum += h[ii] * z[ii]; z[ii + 1] = z[ii]; }
return accum;
第4页
文文: C:\Documents and Settings\user\桌桌\FirAlgs.c 2009-3-27 27, 15:53:51
}
/************************************************************* *************** * fir_split: This splits the calculation into two parts so the circular * buffer logic doesn't have to be done inside the calculation loop ************************************************************** ***************/ SAMPLE fir_split(SAMPLE input, int ntaps, const SAMPLE h[], SAMPLE z[],
*
* Synopsis: FIR filter algorithms for use in C
*
* Description:
*
* This module provides functions to implement Finite Impulse
Response (FIR)
* filters using a variety of algorithms.
*
z[]
- the FIR delay line array
*
*p_state - the "state" of the filter; initialize this
to 0 before
*
the first call
*
* The functions fir_basic, fir_shuffle, and fir_circular are
*
* These functions use most or all of the following input
parameters:
*
*
input - the input sample data
*
ntaps - the number of taps in the filter
*
h[]
- the FIR coefficient array
/* calc FIR */ accum = 0; for (ii = 0; ii < ntaps; ii++) {
accum += h[ii] * z[ii]; }
/* shift delay line */ for (ii = ntaps - 2; ii >= 0; ii--) {
z[ii + 1] = z[ii]; }
/* store input at the beginning of the delay line */ z[0] = input;
第2页
文文: C:\Documents and Settings\user\桌桌\FirAlgs.c 2009-3-27 27, 15:53:51
int ii; for (ii = 0; ii < ntaps; ii++) {
z[ii] = 0; } }
/************************************************************* *************** * fir_basic: Does the basic FIR algorithm: store input sample, calculate * output sample, move delay line ************************************************************** ***************/ SAMPLE fir_basic(SAMPLE input, int ntaps, const SAMPLE h[], SAMPLE z[]) {
/* calc FIR and shift data */
accum = 0;
for (ii = ntaps - 1; ii >= 0; ii--) {
accum += h[ii] * z[state];
if (++state >= ntaps) {
/* incr state and check
for wrap */
第3页
文文: C:\Documents and Settings\user\桌桌\FirAlgs.c 2009-3-27 27, 15:53:51
z[state] = input; if (++state >= ntaps) { for wrap */
state = 0; }
/* incr state and check
int *p_state) {
int ii, state; SAMPLE accum;
state = *p_state; to a local */
/* copy the filter's state
/* store input at the beginning of the delay line */
implement FIR filters
* in C.
*
* Copyright 2000 by Grant R. Griffin
*
* Thanks go to contributors of comp.dsp for teaching me some
of these
* techniques, and to Jim Thomas for his review and great
int ii; SAMPLE accum;
/* store input at the beginning of the delay line */ z[0] = input;
/* calc FIR and shift data */ accum = h[ntaps - 1] * z[ntaps - 1]; for (ii = ntaps - 2; ii >= 0; ii--) {
suggestions.
*wenku.baidu.com
*
The Wide Open License (WOL)
*
* Permission to use, copy, modify, distribute and sell this
software and its
* documentation for any purpose is hereby granted without fee,
return accum; }
/************************************************************* *************** * fir_circular: This function illustrates the use of "circular" buffers * in FIR implementations. The advantage of circular buffers is that they * alleviate the need to move data samples around in the delay line (as * was done in all the functions above). Most DSP microprocessors implement * circular buffers in hardware, which allows a single FIR tap can be * calculated in a single instruction. That works fine when programming in * assembly, but since C doesn't have any constructs to represent circular * buffers, you need to "fake" them in C by adding an extra "if" statement * inside the FIR calculation, even if the DSP processor provides hardware to * implement them without overhead. ************************************************************** ***************/ SAMPLE fir_circular(SAMPLE input, int ntaps, const SAMPLE h[], SAMPLE z[],
#include <stdio.h> #ifdef WIN32
#include <conio.h> #endif
#define SAMPLE double samples */
/* define the type used for data
/************************************************************* ***************/ /* clear: zeroize a FIR delay line */ /************************************************************* ***************/ void clear(int ntaps, SAMPLE z[]) {
provided that
* the above copyright notice and this license appear in all
第1页
文文: C:\Documents and Settings\user\桌桌\FirAlgs.c 2009-3-27 27, 15:53:51
source copies. * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF * ANY KIND. See http://www.dspguru.com/wol.htm for more information. * ************************************************************** ***************/
文文: C:\Documents and Settings\user\桌桌\FirAlgs.c 2009-3-27 27, 15:53:51
/*************************************************************
***************
*
* Name: FirAlgs.c
not very
* efficient in C, and are provided here mainly to illustrate
FIR
* algorithmic concepts. However, the functions fir_split,
fir_double_z
* and fir_double_h are all fairly efficient ways to
state = 0;
}
}
*p_state = state; caller */
/* return new state to
return accum; }
/************************************************************* *************** * fir_shuffle: This is like fir_basic, except that data is shuffled by * moving it _inside_ the calculation loop. This is similar to the MACD * instruction on TI's fixed-point processors ************************************************************** ***************/ SAMPLE fir_shuffle(SAMPLE input, int ntaps, const SAMPLE h[], SAMPLE z[]) {
accum += h[ii] * z[ii]; z[ii + 1] = z[ii]; }
return accum;
第4页
文文: C:\Documents and Settings\user\桌桌\FirAlgs.c 2009-3-27 27, 15:53:51
}
/************************************************************* *************** * fir_split: This splits the calculation into two parts so the circular * buffer logic doesn't have to be done inside the calculation loop ************************************************************** ***************/ SAMPLE fir_split(SAMPLE input, int ntaps, const SAMPLE h[], SAMPLE z[],
*
* Synopsis: FIR filter algorithms for use in C
*
* Description:
*
* This module provides functions to implement Finite Impulse
Response (FIR)
* filters using a variety of algorithms.
*
z[]
- the FIR delay line array
*
*p_state - the "state" of the filter; initialize this
to 0 before
*
the first call
*
* The functions fir_basic, fir_shuffle, and fir_circular are
*
* These functions use most or all of the following input
parameters:
*
*
input - the input sample data
*
ntaps - the number of taps in the filter
*
h[]
- the FIR coefficient array
/* calc FIR */ accum = 0; for (ii = 0; ii < ntaps; ii++) {
accum += h[ii] * z[ii]; }
/* shift delay line */ for (ii = ntaps - 2; ii >= 0; ii--) {
z[ii + 1] = z[ii]; }