As part of my ongoing research I have been developing a receiver structure. I moved it to C++ for speed after initial experiments using Matlab. Part of the receiver structure includes lowpass and bandpass signal filters that were still using coefficient values printed from a Matlab designed filter using the fir1 command.

Frequency Response

I now need to tweak the filter parameters (order and cut frequency) at run-time based on command-line arguments as part of performance analysis experiments. The filter types I am using are fairly straightforward Lowpass and Bandpass Windowed-Sinc filters. So it seems reasonable to be able to reproduce the filter coefficients generator in C++.

TL;DR For the eager, the code for doing just that can be found at the bottom of this post.

For an overview of Sinc Filter and Windows have a look at the wikipedia pages.

However, there is a great web page that provides an accessible and comprehensive explanation of Sinc Filters and Windows at labbookpages.co.uk. This is where the equations and approach for the sourcecode below was derived.

Once the filter weights were calculated they required normalising to give unity gain at the desired frequency. This same challenge was faced and answered on stack exchange. However, a more in-depth overview of FIR properties can be viewed at dpsguru.com.

The output of the example program below produces the filter coefficients for a Lowpass 4th order 4kHz filter (with sampling frequency of 48kHz). It also produces filter coefficients for a Bandpass 8th order 8kHz to 16kHz filter (with sampling frequency of 48kHz).

If the code below is useful please do drop me a line and let me know. Likewise if you discover any bugs. 🙂

PS. The namespace enum technique comes from this stackexchange answer.

$ ./test.exe
FilterFactory example usage

Lowpass Filter: order=4 fSampling=48000Hz fCut=4000Hz
coefficients=[0.0305778, 0.23833, 0.462183, 0.23833, 0.0305778]

Bandpass Filter: order=8 fSampling=48000Hz fCutLow=8000Hz fCutHigh=16000Hz
coefficients=[0.0168833, -1.28162e-017, -0.227925, 0, 0.510383, 0, -0.227925, -1.28162e-017, 0.0168833]

Sourcecode for the library and example is hosted at: https://github.com/bensherlock/sherlock-code/blob/master/include/MirroredFifo.h