2
$\begingroup$

What's the simplest way to implement a FIR filter in C/C++ ?

(I don't want to have to use a third-party software like Matlab or Octave in order to compute the coefficiens because I have to use different filters on thousand of files, with different frequencies for each file.)

Thus, is there a ready-to-use C/C++ DSP library with functions like file.FIR_NotchFilter(frequency,Qfactor) ? or something similar ? (commercial or opensource)

Thanks a lot in advance.

PS1 : I see that there is http://aquila-dsp.org, but there are no binary releases yet, and for older version, it seems difficult to use it

PS2 : A really ready-to-use solution exists for IIR filters : https://github.com/vinniefalco/DSPFilters

// Create a Chebyshev type I Band Stop filter of order 3
Dsp::SimpleFilter <Dsp::ChebyshevI::BandStop <3>, 2> f;
f.setup (3,    // order
     44100,// sample rate
     4000, // center frequency
     880,  // band width
     1);   // ripple dB
f.process (numSamples, arrayOfChannels);
$\endgroup$
  • 1
    $\begingroup$ You've asked a lot of similar questions on DSP.SE and on StackOverflow, without ever really explaining what it is you are trying to achieve. Previously you said you needed linear phase and so couldn't use an IIR filter - is that still true ? Perhaps if you took a step back and explained what it is that you are trying to achieve you might get more useful guidance as to how to get there, rather than iterating through lots of similar but slightly different questions about various different ways to implement notch filters. See: meta.stackexchange.com/questions/66377/what-is-the-xy-problem $\endgroup$ – Paul R Oct 16 '13 at 13:35
  • $\begingroup$ Thanks PaulR for your answer. I explain here : First attempt : try to understand how filters work, etc. (this is the reason for the first questions). I see that the topic is very very very wide. Second attempt (now) : trying to find a ready-to-use library (no time for revinventing the wheel) even if I need to pay for it. I need notch filters for processing thousand of soundfiles : I know that I need notch filters (because I tried with sound editing softwares that already have Notch filters and it works), now I need the fastest way to implement it (a library maybe?) $\endgroup$ – Basj Oct 16 '13 at 13:45
  • 2
    $\begingroup$ I don't think you've given a good answer for why you aren't using something like MATLAB/Octave. I think those would be much better tools to use if you're just looking to gain an understanding for how filtering works. Sure, you will likely pay a performance penalty when compared to a C/C++ implementation, but is that really fatal? $\endgroup$ – Jason R Oct 16 '13 at 13:48
  • 1
    $\begingroup$ Great, but please add some further background, e.g. what kind of data are you filtering? What is the signal that you are trying to filter out? Why do you think you need linear phase? How sharp does the notch filter need to be? What is the ultimate goal of performing this filtering? Much of this might seem irrelevant to you, but by providing the "big picture" you are more likely to get a good solution, possibly something better than your presumed solution. $\endgroup$ – Paul R Oct 16 '13 at 13:49
  • $\begingroup$ The global picture : I need to code a command-line windows tool in C/C++ that can remove some harmonics of constant pitch musical notes (one note played only, with constant pitch). I need it to be linear phase because of further processing. In this topic I really want to see if there exists ready-to-use libraries that can do a FIR notch filter in 5 lines (like the 5-lines solution with IIR filter github.com/vinniefalco/DSPFilters) , rather than enter in filter design. By the way, thanks to all of you for your answers. $\endgroup$ – Basj Oct 16 '13 at 14:26
2
$\begingroup$

If you are filtering in non-realtime (such as when the data is already in a file) and need a linear phase notch, then a suitable IIR filter fed the data both forwards and then backwards will give you both linear phase and a deeper notch.

In C code, you could just reverse the array after each vector IIR function call.

$\endgroup$
  • $\begingroup$ This is interesting. My data is non-realtime. I'm interested, do you have some more details? $\endgroup$ – Basj Oct 16 '13 at 20:54
  • $\begingroup$ hotpaw2, I'm very interested. Can you explain exactly what you mean by feeding the data forwards and backwards ? $\endgroup$ – Basj Oct 17 '13 at 14:52
  • $\begingroup$ Filter forwards in time. Then filter again using the same IIR filter going thru the intermediate result sample vector or file backwards in time (e.g. reverse the intermediate file if needed). $\endgroup$ – hotpaw2 Oct 17 '13 at 16:53
  • $\begingroup$ Do you mean: 1)Open original file / 2)Apply an IIR notch filter / 3) Reverse file in time / 4) Apply the same IIR filter / 5) Reverse file in order to have it in standard time direction? Is it just that or a more subtle thing? $\endgroup$ – Basj Oct 17 '13 at 20:28
  • $\begingroup$ I tried with your reverse technique : the result is AMAZING ! I need to do some subtraction with original signals (that's why I want linear phase)... Without this trick : the unwanted signal that I get is -35 dB. With your technique (linearphase by reversing etc.), the unwanted thing I get after processing is -74dB. Hurrah ! :) I probably won't need any FIR now :) $\endgroup$ – Basj Oct 18 '13 at 13:01
4
$\begingroup$

Have you considered using the Parks McClellan algorithm to generate your FIR filters. The source code for it is available on several sites in Fortran or C.

The original Fortran code is available on Wikipedia.

Here are two sources for C code

You said you were trying to understand filters, so let me explain that the Parks McClellan algorithm is a classic. It was developed in the early 70's, and is still considered to be one of the best FIR design algorithms available.

$\endgroup$
1
$\begingroup$

Since you are interested in designing your own FIR filters, you could start with windowed sinc filters. If you "window" a "sinc", you basically get a lowpass filter's impulse response. And since filtering is linear, you can mix multiple lowpass filters' impolse responses and the unit impulse to do all sorts of things (highpass, bandpass, notch). The filter's impulse response's extent in samples basically controls the transition bandwidts at the corner frequencies: "longer" filters = "sharper cutoffs". The actual window function is another trade off between sharp cutoff and strong rejection. Check out Wikipedia's article on window functions.

See http://www.dspguide.com/ch16.htm

But a low-order IIR filter can be a nice and fast notch for some specific frequency, too. Designing those is trickier, though. One way to attack this is to do the design offline for some arbitrary notch frequency like Nyquist/2 and then to do "frequency warping" at run-time to shift the notch frequency up- or downwards. Frequency warping is actually not that hard if you factor your filter into a sequence of second order sections before. Frequency warping can be applied independently (with the same parameter) to each of the sections.

$\endgroup$
  • $\begingroup$ Even if I know that it would be interesting to understand how it works, I finally need more a ready-to-use library. It exists for IIR (see my answer of my own question), my question is : does it exist for FIR ? $\endgroup$ – Basj Oct 16 '13 at 14:15
0
$\begingroup$

If you do not have to do the calculation FIR coefficients in real time, you can use any off-line tools to calculate coefficients and add it to your program code. Simple function for doing FIR filtering is trivial. Matlab (or its free analog Octave) are almost standart tool for designing FIR coefficients.

If you want to calculate FIR coefficients in real time and/or you want to make data filtering very fast, you can use Intel Integrated Performance Primitives library ("IPP Filtering Functions)".

$\endgroup$
0
$\begingroup$

LiquidDSP is popular with some of my coworkers. Others use FFTW. Both have FIR examples readily available.

$\endgroup$
0
$\begingroup$

You can download a very elegant and easy to use C++ class from Mike Perkins here.

It implements a low pass, band pass and high pass FIR filter.

The advantage over other codes that I have found in internet is that it is one tiny class and there are no magic constants. In the constructor you can define the filter type, the sample rate, the count of taps and the filter frequencies.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.