Language: Python
Processor: Not Relevant
Submitted by Christopher Felton on Feb 24 2012
Licensed under a Creative Commons Attribution 3.0 Unported License
The following code snippet is an example how to design a half band filter. Half band filters are interesting because every even coefficient, except 0, is 0. Those multiplications do not need to be performed, additional calculation reduction can be gained by implementing a folded FIR (coeffecients are symmetric). Only 1/4 the multiplies are required for a straight-forward implementation. Also, polyphase implementations [1] reduce the required number of multiplications even further.
This code snippet is an example how to design a half band filter using the remez function in the Python scipy.signal package.
A half band filter can be designed using the Parks-McCellen equilripple design methods by having equal offsets of the pass-band and stop-band (from filter specification) and equal weights of the pass-band and stop-band [2].
Filter Specification:
The code snippet below is an iPython session. The numpy/scipy packages are required. The "remez" function is part of the scipy.signal package. The following plot is generated from the code snippet output and shows the filter response, in dB, for the filter length in the example.

A commentor, @kaz, pointed out that the firwin design produces lower attenuation in the stopband and less ripple in the passband. But the firwin design has a slower transition band. In multi-rate systems which use the halfband filter and decimate by 2 more signal beyond Fs/4 will be aliased. This is a consideration for the designer. The windowed frequency sampling design (scipy.signal firwin, Matlab fir1) produces the same strucutre, every other coefficient 0 so it can be used in an optimized implementation.

The coefficients from this design are :
remez firwin
------------------------------------
tap 0 0.000000 0.000000
tap 1 -0.010233 -0.001888
tap 2 0.000000 0.000000
tap 3 0.010668 0.003862
tap 4 0.000000 0.000000
tap 5 -0.016324 -0.008242
tap 6 0.000000 0.000000
tap 7 0.024377 0.015947
tap 8 0.000000 0.000000
tap 9 -0.036482 -0.028677
tap 10 0.000000 0.000000
tap 11 0.056990 0.050719
tap 12 0.000000 0.000000
tap 13 -0.101993 -0.098016
tap 14 0.000000 0.000000
tap 15 0.316926 0.315942
tap 16 0.500009 0.500706
tap 17 0.316926 0.315942
tap 18 0.000000 0.000000
tap 19 -0.101993 -0.098016
tap 20 0.000000 0.000000
tap 21 0.056990 0.050719
tap 22 0.000000 0.000000
tap 23 -0.036482 -0.028677
tap 24 0.000000 0.000000
tap 25 0.024377 0.015947
tap 26 0.000000 0.000000
tap 27 -0.016324 -0.008242
tap 28 0.000000 0.000000
tap 29 0.010668 0.003862
tap 30 0.000000 0.000000
tap 31 -0.010233 -0.001888
tap 32 0.000000 0.000000
Every other coeffecient is zero as expected.
[1] Lyons, Rick, "Understanding Digital Signal Processing", 3rd Ed, Pearson 2011
[2] Harris, Fred, "Multirate Signal Processing", Pearson 2004
[3] McClellan, Parks, "A Personal History of the Parks-McClellan Algorithm", IEEE Signal Processing Magazine 2005.
posted by Christopher Felton