Software
This software is free and is distributed under the terms of the GNU General Public License, it is written in ANSI C and should compile with any C compiler. If you use any of this software it would be nice to hear from you: email Stefan (stefan at exstrom dot com) or Richard (richard at exstrom dot com).
IIR Recursive Digital Filter Coefficient Calculation
This is a library of functions for calculating the coefficients of IIR digital filters. Version 1.0 only has functions for calculating Butterworth filter coefficients for lowpass, bandpass, highpass, and bandstop filters. Version 2.0 will include functions for Chebyshev filters.
liir.c - source code, version 1.0 (Nov. 13 2007)
iir.h - header file
iir.txt - documentation
Here are some example programs that use the functions in liir.c.
bwlp.c - Calculates Butterworth lowpass filter coefficients.
bwhp.c - Calculates Butterworth highpass filter coefficients.
bwbp.c - Calculates Butterworth bandpass filter coefficients.
bwbs.c - Calculates Butterworth bandstop filter coefficients.
Makefile - a makefile that will create a library
libiir.a from liir.c and also compile all the above programs. Note that this
only works with the gcc compiler.
The example programs take the order of the filter and the cutoff
frequencies as command line input parameters. Run the programs with no
parameters to get a usage message showing how to list the parameters. The
output is a file that lists the c and d coefficients
of the filter (see the documentation in iir.txt for an explaination of what this
means)
Calculating cutoff frequencies: The cutoff frequencies are
specified as a fraction of π. If fs is the frequency
at which the data is sampled (samples per second) and f is the
cutoff frequency you want, then the parameter you would input to the program
is 2*f/fs. For example if your data is sampled at 5000
samples/sec and you want a bandpass filter from 500 to 1000 Hz the lower
cutoff would be 2*500/5000=0.2 and the upper cutoff would be
2*1000/5000=0.4. Here's a calculator that will calculate this for you.
Other recursive filter programs:
rffr.c - Calculates the frequency response of a recursive
filter using the coefficient file created by one of the above programs.
rdf.c - Filters data from an input file, using a
coefficient file generated by one of the above programs.
FIR Nonrecursive Digital Filters
A general nonrecursive filter is implemented as follows:
y[n] = c0*x[n] + c1*x[n-1] + ... + cM*x[n-M]
The nth output is a linear function of the nth input and the previous M inputs. The ck coefficients are calculated to give the filter a specific frequency response.
The three programs listed below calculate the ck coefficients for implementing a lowpass, bandpass, and highpass filter. The filters are simple (no smoothing) approximations to ideal filters, and have linear phase. The programs take command line input parameters. The parameters are the number of coefficients and the cutoff frequencies. The more coefficients, the sharper the cutoff. The cutoff frequencies are specified as a fraction of Pi. See the discussion under "Recursive Digital Filters" for how to calculate the cutoff frequencies. An additional parameter is the number of zeros to pad on to the end of the coefficient list. This is useful if you want to take the FFT of the coefficients in order to apply the filter in the frequency domain, or if you just want to see what the frequency response of the filter is. You can set this parameter to zero. Running the programs with no parameters will print out a usage message showing the correct order in which the parameters should be given.
tdlpf.c - Calculates lowpass filter coefficients.
tdbpf.c - Calculates bandpass filter coefficients.
tdhpf.c - Calculates highpass filter coefficients.
Spectral Analysis
goertzel.c - A program for doing spectrum calculations using the Goertzel algorithm. It allows spectrum calculation at arbitrary points, whereas the FFT calculates the spectrum at evenly spaced points.
fft.c - FFT program. Takes the FFT of a data (time domain) file, and outputs to file the complex FFT data. The input file needs to be a text (ascii) file. All lines at the top of the input file that start with # are ignored. One data point per line is assumed.
extract.c - Extracts the real or imaginary parts or the magnitude or the phase of an FFT file that was created by the program fft.
Test Data Generation
nsines.c - Produces a noisy sum of sampled sine waves
of the form
fn =
A1sin(n*&omega1+&phi1) +
A2sin(n*&omega2+&phi2) + ... + noise
usage: nsines infile outfile
- infile = input file with the following structure:
np: number of data points to generate.
nmax: maximum amplitude of noise.
seed: unsigned int [0-2147483648] for random number generator.
ns: number of sine waves to use.
Following this are three columns of ns rows:
&omega and &phi ranges are 0...1, interpreted as multiples of π.Amplitude Frequency [rad/s] Phase [rad] A1 &omega1 &phi1 A2 &omega2 &phi2 ... ... ... Ans &omegans &phins
Amplitude range is any real number. - outfile = file where output is written.
Articles and Notes
A Simple Recursive Digital Filter (pdf, 7 pages, 132 KB, Oct 3 2006)
Calculating the Frequency Spectrum of a Signal (pdf, 10 pages, 157 KB)
Fourier Transform of a Sampled Signal (pdf, 5 pages, 93 KB)
Spectrum Magnification: When An FFT Is Not Enough (pdf, 9 pages, 147 KB)
Fourier Series Expansion of Functions in Two or More Dimensions (pdf, 3 pages, 79 KB)
Chebyshev Polynomials (pdf, 6 pages, 154 KB)