A C++ class to implement low-pass, high-pass, and band-pass filters

Mike Perkins

We recently needed a simple C++ class for linear phase FIR filtering, and I figured it might be useful to others, as well. You can download it here.

Here’s how to use this class:

  1. Specify the desired filter type (low-pass, high-pass, or band-pass) in the constructor, along with the other needed parameters: the number of taps, the transition frequencies, and the sampling frequency of the data you’ll be filtering.
  2. Make one call to the class’s filter function, do_sample(), for every value in the data stream you’re filtering. do_sample() returns a filtered value each time it is called.
  3. Use optional class helper functions as desired, for example, to load an array with the filter taps, write the taps to a file, or write the frequency response to a file.

Details on using the class are contained in the file filt.h, and the implementation is in filt.cpp. A sample program demonstrating its use on an audio file is in devel.cpp.

If all you need is code that works, you can stop here. For those who want to know how the taps are computed, keep reading. I assume that you have some exposure to digital filtering and just need a brief refresher.

Reminder one:

An FIR filter can be implemented using the following structure:

FIR structure

The data to be filtered is shifted into the array on the left (each box holds one data sample). Each time a new value is shifted in, the value currently stored in each position is multiplied by its corresponding tap (the “h(k)” value), and the outputs of the multipliers are summed together to yield a single data output value. The computation of each output value requires 4 multiplies and 3 adds in this four tap filter example. (The class function do_sample() performs this computation.)

Reminder two:

The frequency response of an N-tap FIR filter is given by the Discrete Time Fourier Transform (DTFT) of its taps. The DTFT is periodic on the interval  where corresponds to (half the sampling frequency) and is given by:

FIR eq 1

Reminder three:

The expression above for the DTFT is a weighted sum of complex sinusoids; it looks exactly like a Fourier Series.


One way to design the taps of a digital filter is the following:

  1. Choose a desired frequency response on the interval [,π] where π corresponds to Fs / 2. Note that in the class filt.cpp, the desired frequency response is chosen to be that of an ideal filter of the specified type.
  2. Choose a number of taps.
  3. Compute the Fourier Series coefficients of the desired frequency response.
  4. Plot the frequency response to see how well it meets your needs (the class function write_freqres_to_file() will output the frequency response to a file in a form suitable for plotting, for example, by using Excel).

If you are a purist, you may have noticed that the DTFT equation I gave looks like a Fourier Series but with one exception: The sign of the exponent in the complex sinusoids is negative instead of positive. But not to worry—when computing the Fourier Series coefficients, just flip the sign of the exponent in that formula too. In other words, if is your desired frequency response, compute the taps as follows:

FIR eq 2

For the ideal frequency responses of the filters used in filt.cpp, these computations are easy. You can see the resulting formulas for the coefficients by looking at the code.

Categories: Perk, Theory

Cardinal Peak
Learn more about our Audio & Video capabilities.

Dive deeper into our IoT portfolio

Take a look at the clients we have helped.

We’re always looking for top talent, check out our current openings. 

Contact Us

Please fill out the contact form below and our engineering services team will be in touch soon.

We rely on Cardinal Peak for their ability to bolster our patent licensing efforts with in-depth technical guidance. They have deep expertise and they’re easy to work with.
Diego deGarrido Sr. Manager, LSI
Cardinal Peak has a strong technology portfolio that has complemented our own expertise well. They are communicative, drive toward results quickly, and understand the appropriate level of documentation it takes to effectively convey their work. In…
Jason Damori Director of Engineering, Biamp Systems
We asked Cardinal Peak to take ownership for an important subsystem, and they completed a very high quality deliverable on time.
Matt Cowan Chief Scientific Officer, RealD
Cardinal Peak’s personnel worked side-by-side with our own engineers and engineers from other companies on several of our key projects. The Cardinal Peak staff has consistently provided a level of professionalism and technical expertise that we…
Sherisse Hawkins VP Software Development, Time Warner Cable
Cardinal Peak was a natural choice for us. They were able to develop a high-quality product, based in part on open source, and in part on intellectual property they had already developed, all for a very effective price.
Bruce Webber VP Engineering, VBrick
We completely trust Cardinal Peak to advise us on technology strategy, as well as to implement it. They are a dependable partner that ultimately makes us more competitive in the marketplace.
Brian Brown President and CEO, Decatur Electronics
The Cardinal Peak team started quickly and delivered high-quality results, and they worked really well with our own engineering team.
Charles Corbalis VP Engineering, RGB Networks
We found Cardinal Peak’s team to be very knowledgeable about embedded video delivery systems. Their ability to deliver working solutions on time—combined with excellent project management skills—helped bring success not only to the product…
Ralph Schmitt VP, Product Marketing and Engineering, Kustom Signals
Cardinal Peak has provided deep technical insights, and they’ve allowed us to complete some really hard projects quickly. We are big fans of their team.
Scott Garlington VP Engineering, xG Technology
We’ve used Cardinal Peak on several projects. They have a very capable engineering team. They’re a great resource.
Greg Read Senior Program Manager, Symmetricom
Cardinal Peak has proven to be a trusted and flexible partner who has helped Harmonic to deliver reliably on our commitments to our own customers. The team at Cardinal Peak was responsive to our needs and delivered high quality results.
Alex Derecho VP Professional Services, Harmonic
Yonder Music was an excellent collaboration with Cardinal Peak. Combining our experience with the music industry and target music market, with Cardinal Peak’s technical expertise, the product has made the mobile experience of Yonder as powerful as…
Adam Kidron founder and CEO, Yonder Music
The Cardinal Peak team played an invaluable role in helping us get our first Internet of Things product to market quickly. They were up to speed in no time and provided all of the technical expertise we lacked. They interfaced seamlessly with our i…
Kevin Leadford Vice President of Innovation, Acuity Brands Lighting
We asked Cardinal Peak to help us address a number of open items related to programming our systems in production. Their engineers have a wealth of experience in IoT and embedded fields, and they helped us quickly and diligently. I’d definitely…
Ryan Margoles Founder and CTO, notion