21

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.

  • Is it important that the solution is free? Have you investigated the Matlab C API? –  Aug 21 '11 at 16:21
  • 2
    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). – Trevor Boyd Smith Aug 21 '11 at 16:38
  • A secondary priority would be that the source has a license that is friendly to commercial development (i.e. free, non-GPL (lgpl, apache, etc)). – Trevor Boyd Smith Aug 21 '11 at 16:40
  • 3
    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. – Trevor Boyd Smith Aug 21 '11 at 16:42
  • 1
    @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. – Lorem Ipsum Aug 22 '11 at 15:48
  • 1
    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. – Trevor Boyd Smith Aug 23 '11 at 16:26
  • Please keep in mind that this is my first time actively participating in a new stackexchange site. – Trevor Boyd Smith Aug 23 '11 at 17:00
  • @TrevorBoydSmith [1/2] The reason why you got 4 downvotes is because code/implementation questions are better off on StackOverflow and the only closed question on the site so far was also one that asked for an implementation of code. The reason why yours was downvoted, but not closed is probably because you've phrased your question much better than the other person (which read more like a give me the codez). A brief meta discussion came to the consensus that questions for code are off-topic. – Lorem Ipsum Aug 23 '11 at 18:41
  • @TrevorBoydSmith [2/2] While I'm against blatant gimme-da-codez questions, I'm ambivalent on questions like yours which ask for implementations of a specific algorithm to a focussed group of people. The reason I don't have a stand on this issue is because I've never had to implement something in hardware, so I cannot gauge it's importance (or how good a response you'd get on SO). But the rest of the community seems to disagree with such questions, so there's that. Note that I'm only speculating reasons here (since you haven't had an explanation for the downvotes so far), but I think I'm right. – Lorem Ipsum Aug 23 '11 at 18:45
  • @yoda and others, I consider myself fairly well informed... and i still asked a question that many consider off topic. If this sort of thing ("gimme-the-codez") were mentioned in the FAQ it would be easier for me to not make the mistake in the first place. – Trevor Boyd Smith Aug 25 '11 at 15:12
  • The high level goal in asking this question is to figure out how to write signal processing code that is not hard coded so much for specific filters and sample rates... which IMO adds lots of value to dsp.stackexchange.com. – Trevor Boyd Smith Aug 25 '11 at 15:16
  • @TrevorBoydSmith This site is still in private beta, so what is acceptable/not acceptable is still not defined. Other than the fact that questions must be related to signal processing (which yours is), nothing more can be said in the FAQ at this point. What shapes the FAQ for the future, is what people think of certain types of questions in both the private beta and public beta. Their actions set the tone and guidelines for when the site eventually goes mainstream. From what I've seen with such questions, the community disagrees. – Lorem Ipsum Aug 25 '11 at 15:21
  • @TrevorBoydSmith However, DO NOT let that weigh you down! If you strongly feel such questions are on-topic and add alue to the site, please DO bring it up on meta.dsp. The longer you wait, the harder it gets. – Lorem Ipsum Aug 25 '11 at 15:22
  • Given the many downvotes here for a question I thought was good and the many downvotes I got for some answers I submitted, I am somewhat discouraged in general and I am also a little hesitant to post on meta.dsp. But I will probably post on meta.dsp about this specific topic. – Trevor Boyd Smith Aug 25 '11 at 15:30

7 Answers7

11

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.

Jason R
  • 24,595
  • 2
  • 67
  • 74
  • "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: http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.remez.html – endolith Aug 22 '11 at 14:35
5

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.

Mark Borgerding
  • 3,020
  • 19
  • 26
5

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

Alex I
  • 230
  • 3
  • 12
  • 1
    The C-source code appears to be here: https://github.com/scipy/scipy/blob/master/scipy/signal/sigtoolsmodule.c – Dave C Feb 11 '13 at 05:00
2

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

user5108_Dan
  • 769
  • 2
  • 5
  • 11
1

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.

bone
  • 1,241
  • 11
  • 15
1

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.

David
  • 2,861
  • 11
  • 13
  • 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). – David Jan 23 '14 at 13:42
1

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')
Zhanwen Chen
  • 121
  • 1