The purpose of this appendix is to make an algorithm that implements a
-order FIR bandpass filter. The algorithm will be tested in MATLABTM and later implemented in C.
FIR bandpass filter
The FIR filter is designed using windowing. This method is further described in [Oppenheim & Schafer, 1998, page 465 - 477].
The method is to make an ideal filter in the frequency domain, and then translate it into the discrete time domain. However this will give an infinite impulse response. To compensate for this, a window function is multiplied onto the ideal impulse response.
In this appendix a rectangular window is chosen. This is done for simplicity as the window amplitude is either 1 or 0. In FIR filter design the order for the filter is denoted
and it determines the length of the window. The type of FIR filter used here utilizes linear phase i.e. constant group delay.
An example of a FIR bandpass filter is shown in figure M.1. The filter has a center frequency (
and a bandwith of 2 kHz. The order (
) is 20.
20 order FIR bandpass filter.
Type I FIR filter design by windowing utilizes the ideal form of figure jtkr04:fig:fir02. Examining the ideal filter in the frequency domain, the definition of
could be written as a modified equation (7.42) from [Oppenheim & Schafer, 1998, p.466]
Ideal bandpass filter.
Corresponding to the discrete-time notation of
as the approximated impulse response of the filter, and with
as the windowing function
The product of (M.2) is in the frequency domain equal to the convolution
Utilizing equation (7.52) with
and (5.146b) from [Oppenheim & Schafer, 1998, p. 472 and p. 297 respectively] with the amplitude function set to 1,
can be written as
to the time domain yields (by use of Euler rewriting)
with the window
in the time domain, to achieve the definition of
, and transforming to the Z-domain yields
due to the magnitude of
being 1 in
and zero otherwise. This is an approximation of the ideal bandpass filter of equation (M.1) and figure jtkr04:fig:fir02.
Using the basic attribute of
and transforming back to the time domain, will yield the filter output
The above can be readily implemented in an algorithm, with the value of
describing the order of the filter. The values of
are reusable for each
, calculated from the expression in (M.8).
The designed FIR filter is specifically intended for digital implementation, and thereby doesn't have any particular implementation considerations, except that according to [Oppenheim & Schafer, 1998, page 386] at least 14 bits must by used for the filter coefficients.
The MATLABTM implementation relies on the following information to be supplied:
The filter algorithm does the following:
- the sample rate (
), in Hz.
- the center frequency of the passband (
), in Hz.
- the passband width (
), in Hz.
- the order of the filter (
), must be an even number.
- the data to be processed (
Note that the first
samples are using samples before
as zeros. Tested against MATLABTM's internal commands (FIR1), the designed filter produces the same coefficients as MATLABTM.
- Done once:
- Assign memory for
- Loop from 0 to
, decide filter coefficients. One exception is
, where L'Hospitals' rule must be used.
- Determine factor for gain adjustment.
- Done for each data chunk:
- Assign memory for output
- Handle first
- Loop for each sample(i):
- Loop for n = 0 to
, summing coefficients
- Adjust gain
- Move to output
The C-implementation on the selected PC platform imposes the consideration of datatypes and data length.
The system datatype used for handling soundcard samples is set to integers, while the internal workings of the filter relies on float values. The solution is to use float values internally, and export values rounded to integers.
As C contains no built in ``length'' command, the length of the supplied samples must be specified. Furthermore, the data to be processed is supplied in chunks. This results in problems as the filter uses
samples from the past. When crossing the boundary between chunks this must be addressed.
In this appendix an
order FIR bandpass filter has been implemented. The order of the filter can be varied according to the demands of a given situation. However the zeroing of the samples before
must be handled for the actual C-implementation.
Jes Toft Kristensen