Skip to content

Add FIR filter examples to scipy.signal documentation #4368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rgommers opened this issue Jan 4, 2015 · 14 comments
Open

Add FIR filter examples to scipy.signal documentation #4368

rgommers opened this issue Jan 4, 2015 · 14 comments
Labels
Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org good first issue Good topic for first contributor pull requests, with a relatively straightforward solution scipy.signal task A straightforward change, verification or fix.

Comments

@rgommers
Copy link
Member

rgommers commented Jan 4, 2015

Here are a few examples that could easily be used to improve the docstrings and/or tutorial of scipy.signal:

http://www.scipy.org/Cookbook/FIRFilter
http://www.scipy.org/Cookbook/FiltFilt
http://mpastell.com/2010/01/18/fir-with-scipy/ (IIRC @mpastell was OK with using that content, but I can't find an archived email that says that anymore. Matti can you confirm that this is OK?)

signal.filtfilt now has at least one nice example thanks to @WarrenWeckesser, and the tutorial is slowly improving mainly due to @endolith's PRs, but there's a ways to go....

@rgommers rgommers added task A straightforward change, verification or fix. scipy.signal Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org good first issue Good topic for first contributor pull requests, with a relatively straightforward solution labels Jan 4, 2015
@WarrenWeckesser
Copy link
Member

@mpastell's examples are nice, but it is not true (perhaps better to say no longer true) that "SciPy does not have a function for directly designing a high-pass FIR filter". Check out the docstring for scipy.signal.firwin. Admittedly, the examples in that docstring could use a little work; e.g. f is used but it isn't defined.

Also check out firwin2 and remez. Here's a recent question on stackoverflow about designing a high-pass filter with remez: http://stackoverflow.com/questions/27754979/python-scipy-signal-remez-high-pass-filter-design-yields-strange-transfer-functi

@WarrenWeckesser
Copy link
Member

<pedantic>By the way, one wouldn't normally use filtfilt with a FIR filter. A FIR filter is usually designed to have linear phase, and if the filter has linear phase, there is no advantage to applying the filter with filtfilt.</pendantic>

@rgommers
Copy link
Member Author

rgommers commented Jan 4, 2015

</pendantic>

typo :)

@rgommers
Copy link
Member Author

rgommers commented Jan 4, 2015

but yes, thanks for that remark.

@WarrenWeckesser
Copy link
Member

typo :)

Stop being so pendantic.

@mpastell
Copy link

mpastell commented Jan 4, 2015

Yes, feel free to use the examples and modify them in anyway you want. I'm happy if they are useful to someone.
There is slightly more up to date version in Pweave examples: http://mpastell.com/pweave/examples/FIR_design.html (regarding FIR highpass), I should probably update the old blog post too.

@WarrenWeckesser
Copy link
Member

What I should have said first: Yes, this is a great idea!

I put some remez examples here: https://gist.github.com/WarrenWeckesser/67bef61f496080aeb798

I have several other tasks to finish on my scipy to-do list before I'd work on this, so if anyone wants to expand the remez docstring with examples like those, feel free to use whatever you like.

@endolith
Copy link
Member

endolith commented Jan 9, 2015

Here's another example of filtfilt vs lfilter, though very similar to the one you linked to: http://dsp.stackexchange.com/a/19086/29

Could firwin be modified to take a btype argument so you don't need to do it by hand, similar to the IIR filter design functions? Or make another function for that purpose that wraps it?

@WarrenWeckesser
Copy link
Member

FWIW, here's another example that I created for a stackoverflow answer:
http://stackoverflow.com/questions/16301569/bandpass-filter-in-python/16306891#16306891

@larsoner
Copy link
Member

larsoner commented Mar 9, 2018

Maybe this tutorial I wrote for MNE could be adapted:

https://martinos.org/mne/stable/auto_tutorials/plot_background_filtering.html

At least some (non-signal-processing engineer) neuroscientists I've talked to have found it to be a useful intro. Of course for SciPy it could be expanded to deal with other design methods (e.g., remez).

timonmerk pushed a commit to timonmerk/scipy that referenced this issue Apr 16, 2018
Add examples to remez docstring with lowpass, highpass, bandpass and
bandstop
Inspired by https://gist.github.com/WarrenWeckesser/67bef61f496080aeb798
Fixes scipy#4368
Work done together with @pooja-subramaniam and @jja4
@rgommers rgommers modified the milestone: 1.3.0 Mar 1, 2019
@rgommers
Copy link
Member Author

rgommers commented Mar 1, 2019

gh-8744 just improved the remez docstring, there are more functions discussed here - reopening

@rgommers rgommers reopened this Mar 1, 2019
@Adarsh-NP
Copy link

Adarsh-NP commented Feb 6, 2022

Hi @endolith @rgommers , I think firwin2 should have more examples, especially for a custom filter. I want to add this something like this, but wanted to confirm from the maintainers and core members if the examples need to be from some standard reference material.

I would like to know if it is correct or not, I tried replicating one of the examples that I encountered in my course but could not find in scipy.

Screenshot from 2022-02-06 14-38-40

Additionally, I am confused if there is any difference between firwin and firwin2. I could notice that firwin2 accomodates type III and IV filters and I think we can give an arbitrary gain frequency map to it which is not possible in firwin. Kindly clarify if I got this right.

@Codeknightjl
Copy link

@Adarsh-NP I know this response is quite late but can you please provide more details on the example that you tried to replicate?

As for your final question,

Additionally, I am confused if there is any difference between firwin and firwin2

The main difference is in the desired properties of the filter.

  • firwin allows you to design a FIR filter by giving it the desired cutoff frequency.
  • firwin2 allows you to design it by giving it a list of frequency points where the gains must be the corresponding values given by you in a separate list.

You are also correct that type III/IV filters can be designed using firwin2 but not with firwin. This line from the firwin documentary makes it clear.

This function computes the coefficients of a finite impulse response filter. The filter will have linear phase; it will be Type I if numtaps is odd and Type II if numtaps is even.

Please let me know if I have missed anything.

@bdas123
Copy link

bdas123 commented Mar 14, 2024

Is this issue still active?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org good first issue Good topic for first contributor pull requests, with a relatively straightforward solution scipy.signal task A straightforward change, verification or fix.
Projects
None yet
Development

No branches or pull requests

8 participants