Title: Does Python/Scipy have a firls( ) replacement (i.e. a weighted, least squares, FIR filter design)?
Tags: python;algorithm;math;matlab;digital-filter
Question: I am porting code from Matlab to Python and am having trouble finding a replacement for the firls( ) routine.  It is used for, least-squares linear-phase Finite Impulse Response (FIR) filter design.

I gjones@example.org and nothing there looked like it would do the trick.  Of course I was able to replace my remez and freqz algorithsm, so that's good.

On one blog I found an algorithm that implemented this filter without weighting, but I need one with weights.

Thanks, David



Here is another answer: Obviously, this post is somewhat dated, but maybe it is still interesting for some:

I think there are two near-equivalents to firls in Python:


You can try the firwin function with window='boxcar'. This is similar to Matlab where fir1 with a boxcar window delivers the same (? or at least very similar results) as firls.
You could also try the firwin2 method (frequency sampling method, similar to fir2 in Matlab), again using window='boxcar'


I did try one example from the Matlab firls reference and achieved near-identical results for:

Matlab:

```F = [0 0.3  0.4 0.6  0.7 0.9];
A = [0  1   0  0  0.5 0.5];
b = firls(24,F,A,'hilbert');
```

Python:

```F = [0, 0.3,  0.4, 0.6,  0.7, 0.9, 1]
A = [0,  1,   0,  0,  0.5, 0.5, 0]
bb = sig.firwin2( 25, F,A, window='boxcar', antisymmetric=True )
```

I had to increase the order to N = 25 and I also had to add another data point (F = 1, A = 0) which Python insisted upon; the option antisymmetric = True is only necessary for this special case (Hilbert filter)

Comment for this answer: Please see additional responses especially from @Pev Hall. These are not best practices for digital filter implementations and would have comparatively poor performance (as suggested, comparisons should be done on a log scale).

Here is another answer: This blog post contains code detailing how to use ```scipy.signal``` to implement FIR filters. 

Comment for this answer: Well, that was intereseting blog post, but not exactly what I was looking for.  I saw the firwin( ) function, but it does not have have the ability to express the frequency response the way I need it for Magnetic resonance imaging... 

I was hoping not to reinvent the wheel, but it's looking more likely that I will need to do that.

Thanks,

Here is another answer: This post is really in response to 

```
  You can try the firwin function with window='boxcar'...
```

Do don't use boxcar it means no window at all (it is ideal but only works "ideally" with an infinite number of multipliers - sinc in time). The whole perpose of using a window is the reduce the number of multipliers required to get good stop band attenuation. See Window function

When comparing filters please use dB/log scale.

Scipy not having firls (FIR least squares filter) function is a large limitation (as it generates the optimum filter in many situations).

REMEZ has it's place but the flat roll off is a real killer when your trying to get the best results (and not just meeting some managers spec). ( warning scipy remez implementation can give amplification in stop band - see plot at bottom)

If you are using python (or need to use some window) I recommend using the kasiar window which gets very good results and can easily be tweaked for your attenuation vs transition vs multipliers requirement(attenuation (in dB) = 2.285 * (multipliers - 1) * pi * width + 7.95). It performance is not quite as good as firls but it has the benefit of being fast and easy to calculate (great if you don't store the coefficients).


Here is another answer: I found a firls() implementation attached here in SciPy ticket 648

Minor changes to get it working:


Swap the following two lines:
```
bands, desired, weight = array(bands), array(desired), array(weight)
if weight==None : weight = ones(len(bands)/2)
```
import roots from numpy instead of scipy.signal



Here is another answer: The firls equivalent in python now appears to be implemented as part of the signal package:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.firls.html#scipy.signal.firls

Also I agree with everything that @pev hall stated above especially how firls is optimum in many situations (such as when overall signal to noise is being optimized for a given number of taps), and to not use the boxcar window as he stated, they are not equivalent at all! firls generally outperforms all window and frequency sampling approaches to filter design when designing traditional FIR filters.


Here is another answer: Since version 0.18 in July, 2016 scipy includes an implementation of firls, as scipy.signal.firls.


Here is another answer: It seems unlikely that you'll find exactly what you seek already written in Python, but perhaps the Matlab function's help page gives or references a description of the algorithm?


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 7, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2010:00:04/02/10', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<python><algorithm><math><matlab><digital-filter>', 'Title': 'Does Python/Scipy have a firls( ) replacement (i.e. a weighted, least squares, FIR filter design)?', 'ViewCount': 4460, 'id': 2568707, 'user_AboutMe': '<p>An experienced software engineer with a background in science and a flare for find foods.</p>\n', 'user_AccountId': 117591.0, 'user_DisplayName': 'delicasso', 'user_Reputation': 187.0, 'user_Views': 34.0, 'user_id': 307886.0}}