20
$\begingroup$

Background: Often times I am doing some sort of signal processing task that requires a unique filter. Usually at this point I go to MATLAB and generate a new unique filter using $\tt firpm()$. The MATLAB firpm() function implements that Parks-McClellan algorithm. Now I have a filter and I put the filter into a hardcoded array. But here's the problem I now have a hardcoded filter that only works for one scenario.

The problem: I can now solve my signal processing problem du-jour... but only for a very SPECIFIC single sample rate or SPECIFIC scenario.

The goal: I want to be able to call $\tt firpm()$ from C code or some other language and make my signal processing code more generic. I can't find an open source implementation of firpm() !

Where can I get an open source implementation of the Parks-McClellan optimal FIR filter design algorithm (aka $\tt firpm()$ in MATLAB)?

  • P.S. I am aware that I can design filters differently using windowing or something else... feel free to mention those in the comments. But the point of this question is not to ask "what are other filter design techniques?" the point is to find an open source implementation of the VERY VERY useful $\tt firpm()$... or something similar.

  • P.P.S. One of the goals of this question is to learn how the Parks-McClellan algorithm works by looking at the code first and then I plan on reading some background theory.

$\endgroup$
14
  • $\begingroup$ Is it important that the solution is free? Have you investigated the Matlab C API? $\endgroup$
    – user42
    Aug 21, 2011 at 16:21
  • 2
    $\begingroup$ The highest priority is I want to see the source code (preferably not fortran so I don't have to stab my eyes out). I won't put the restriction that it must be free (maybe there is some sort of open source but non-free source code). $\endgroup$ Aug 21, 2011 at 16:38
  • 3
    $\begingroup$ I am aware that you can compile Matlab using the Matlab compiler and then distribute using Matlab Runtime... so technically your customer doesn't have to pay for Matlab license. I am also aware of the Matlab Engine (aka C to Matlab API). Both of these are irrelevant because I usually run on an embedded platform where neither are available. $\endgroup$ Aug 21, 2011 at 16:42
  • 1
    $\begingroup$ @TrevorBoydSmith Since you just want to look at the source code, have you tried type firpm.m in MATLAB? That will show you MATLAB's implementation of the function. $\endgroup$ Aug 22, 2011 at 15:48
  • 1
    $\begingroup$ FIR filter design is very useful for signal processing and parks-mcclelan is a non-trivial subject matter. And yet I am being down voted repeatedly for asking about a subject that IMO fits squarely in the dsp.stackexchange charter. Please explain your downvotes. $\endgroup$ Aug 23, 2011 at 16:26

7 Answers 7

5
$\begingroup$

Here's an LGPL version of the Remez exchange algorithm. The octave code seems to be derived from it. It was linked from the wikipedia page Parks McClellan page.
The original Janovetz code might be more easily usable in your project, since it doesn't have the octave calls, but it would be wise to dig through the octave-forge svn changelog for any info about bugfixes or speedups in the remez.cc file.

$\endgroup$
4
  • $\begingroup$ I agree using the Janovetz code in a project would be easier because it is straight C. I also definitely agree that checking the change log of the octave implementation would be very smart. $\endgroup$ Sep 13, 2011 at 16:25
  • $\begingroup$ The Janovetz code IMO is probably a first or second draft... but hasn't been used heavily like the Octave code. $\endgroup$ Sep 13, 2011 at 16:25
  • $\begingroup$ VERY IMPORTANT NOTE: The Janovetz code is LGPL so you can use it in a commercial setting. $\endgroup$ Sep 13, 2011 at 16:26
  • $\begingroup$ The first link from the answer is broken so here is a link to a library where the same implementation is used. $\endgroup$
    – Machta
    Feb 17, 2014 at 16:12
11
$\begingroup$

There is an open-source implementation of Parks-McClellan (also known as the Remez exchange algorithm) in GNU Octave, a free-software implementation of a MATLAB-like environment. The function, called "remez", is contained in the "signal" package, which is hosted at Octave-Forge. If you download the package, you'll find "remez.cc", a C++ implementation of the algorithm.

One nice thing about Octave is that it is almost code-compatible with MATLAB, so you can easily port code over to use there if you like. It's a good way to peek under the hood at implementations of algorithms that are provided in MEX form in MATLAB.

$\endgroup$
1
  • $\begingroup$ "The Parks-McClellan algorithm is a variation of the Remez algorithm or Remez exchange algorithm, with the change that it is specifically designed for FIR filters and has become a standard method for FIR filter design." Also in SciPy: docs.scipy.org/doc/scipy/reference/generated/… $\endgroup$
    – endolith
    Aug 22, 2011 at 14:35
5
$\begingroup$

A convenient version can be found in Python's scipy.signal.remez. Nice if using numpy/scipy.

$\endgroup$
1
2
$\begingroup$

Here is another source for the Parks McClellan algorithm in C. This code is different from the SciPy code mentioned above in that it has 61 of the original 69 goto statements removed (the SciPy code still has about 37 goto's). It also fixes the code in 3 places where divide by zero can occur and it has some additional code that range checks the band edge values.

http://www.iowahills.com/A7ExampleCodePage.html

$\endgroup$
1
$\begingroup$

maybe you already know this, but if you have matlab you can use the matlab coder, and create a simple function that uses the feature you want to examine. Then run it and see the created C code. I tried with with the FFT, and with QR decomposition, and while it is a bit messy, it can be understood just fine.

$\endgroup$
1
$\begingroup$

Here is a paper which does an actual Matlab version of the "core" remez algorithm. “A MATLAB based optimum multiband FIR filters design program following the original idea of the Remez multiple exchange algorithm” -2011 IEEE International Symposium on Circuits and Systems (ISCAS) - Authors (Ahsan, Saramaki)

This paper does a good job at explaining the basic algorithm. The goal of the paper was to avoid using the original Fortran code - which does not explain the algorithm very well and often just gets translated into various other languages directly.

One thing I will comment on. One of the core ideas of the algorithm is to fit a curve and then find the extremal points. Usually a Lagrange Interpolation is used to explain this idea, but Lagrange Interpolation has poor numerical properties. In the original algorithm the use Barycentric Implementation of Lagrange Interpolation - which avoids many of the associated pitfalls of Lagrangian interpolation. So if you are trying to fully understand the code, you may want to look up Barycentric Interpolation.

$\endgroup$
1
  • $\begingroup$ Looking at the paper - it is a modified version of the Parks-McClellan code. It is still based on the Remez exchange algorithm, but it tends to have better performance, and allows you to design filters which are much longer than those you get from the PM algorithm (or Matlab's implementation of it). $\endgroup$
    – David
    Jan 23, 2014 at 13:42
1
$\begingroup$

Beware of the differences between Matlab's firpm and Scipy.signal's remez. For example, these two statements are equivalent:

% Matlab
firpm(10,[.2 .8],[1 1],'Hilbert')
# Python
from scipy.signal import remez

remez(11, [0.1, 0.4], [1], type='hilbert')
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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