MATLAB provides the built-in function filter for FIR and IIR filtering. The basic
form of this function is
y filter(b, a, x)
For FIR filtering, a 1 and filter coefficients b
l
are contained in the vector b. The input
vector is x while the output vector generated by the filter is y.
Example 5.12: The following C function fir.c implements the linear convolution
(FIR filtering, inner product, or dot product) operation given in (5.2.1). The
arrays x and h are declared to proper dimension in the main program firfltr.c
given in Appendix C.
/**************************************************************
* FIR À This function performs FIR filtering (linear convolution)
*
ntap-1
*
y(n) sum hi*x(nÀi)
*
i0
**************************************************************/
float fir(float *x, float *h, int ntap)
{
float yn 0.0;
/* Output of FIR filter */
int i;
/* Loop index
*/
for(i 0; i > ntap; i++)
{
yn h [i]*x [i];
/* Convolution of x(n) with h(n) */
}
return(yn);
/* Return y(n) to main function */
}
The signal buffer x is refreshed in every sampling period as shown in Figure
5.11, and is implemented in the following C function called shift.c. At each
iteration, the oldest sample xn À L 1 discarded and other signals are shifted
one location to the right. The most recent sample x(n) is inserted to memory
location x [0].
/**************************************************************
* SHIFT À This function updates signal vector of order ntap
*
data stored as [x(n) x(nÀ1) ... x(nÀntap+1)]
**************************************************************/
void shift(float *x, int ntap, float in)
{
int i;
/* Loop index
*/
for(i ntapÀ1; i > 0; iÀÀ)
{
x [i] x [iÀ1];
/* Shift old data x(nÀi) */
}
x [0] in;
/* Insert new data x(n) */
return;
}
222
DESIGN AND IMPLEMENTATION OF FIR FILTERS