sfilip / firpm

A scalable C++ implementation of the Parks-McClellan algorithm for designing FIR filters

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

firpm fix-refactor patch for various crashes

jlwehle opened this issue · comments

In trying a particular filter specification with your firpm fix-refactor
I was able to trigger various crashes. The platform I'm using is FreeBSD 11.3
with the OS supplied compiler (clang version 8.0.1).

Issues fixed by the enclosed patch:

  1. There are lambdas of the form:

    fbands[i].weight = [i, w](space_t, double x) ...
    fbands[i].amplitude = [i, a, fbands](space_t space, double x) ...

    Unfortunately capturing arrays by value causes the process to consume
    excessive memory before finally dying.

  2. uniform can be called with an omega array that's smaller than the
    band array. This results in avgDistance being negative so latter
    extremas calculations wrap to a large number causing a crash.

  3. If firpmRS calls exchange which fails, then output.x will be empty.
    This causes referenceScaling to crash.

Notes:

a) These same changes probably should be propagated to both firpm_ld
and firpm_mp.

-- John
PatchJLW01-fr.txt

Thanks for the feedback! Changed 1 as you suggested. For 2 and 3 I went with other fixes.
Can you please re-check to see if you are now able to run your filter specifications with firpm in the fix-refactor branch?

  • On the up side ...

No longer runs out of memory or crashes on the couple of
specifications I tried.

  • On the down side ...

The resulting filter doesn't have much attenuation in the
stop band compared to what GNU Octave fir2 produces. It
does look okay in the pass band.

It may be that I'm doing something wrong. I've uploaded
a zip file containing various inputs and outputs:

firpm.cxx

cic-comp-bands-200k-freq.txt
cic-comp-bands-200k-mag.txt

I build firpm like so:

clang++ -I/usr/local/include -O2 -march=amdfam10 -o firpm firpm.cxx -L/usr/local/lib -lfirpm

and run it like so:

./firpm cic-comp-bands-200k

it produces:

cic-comp-bands-200k-coeff.txt

I've include:

cic-comp-bands-200k.png

which is a graph of the firpm solution. The FIR response
is in red. The blue is the combined CIC / FIR response.

I've also included:

cic-comp-bands-200k-coeff-octave.txt
cic-comp-bands-200k-octave.png

which are the coefficients and graph of the Octave solution.

firpm-octave-200k.zip

I'm afraid I can't make this particular example work with firpm. The problem is that there are some discontinuous jumps in the frequency response for your specification (e.g., the normalized frequency 2.233333333333333e-01 maps to both 9.731654898434647e-01 and 0.000000000000000e+00 in magnitude); they can't be handled using minimax filter design procedures like firpm. Matlab's firpm, for instance, will give an error if a specification with discontinuities is given.

fir2 produces interpolation filters that are able to handle such discontinuities.

I'll add a check to detect these discontinuities in the specification and output an error message in case any are found.

I've run through a few corrected filter specifications and firpm_d seems to be
producing the desired results.

Thanks.