is there any prepared function in python to apply a filter (for example Butterworth filter) to a given signal?
I looking for such a function in ‘scipy.signal’ but I haven’t find any useful functions more than filter design ones.
actually I want this function to convolve a filter with the signal.
is there any prepared function in python to apply a filter (for example Butterworth
Share
Yes! There are two:
There are also methods for convolution (
convolveandfftconvolve), but these are probably not appropriate for your application because it involves IIR filters.Full code sample:
You can read more about the arguments and usage in the documentation. One gotcha is that
Wnis a fraction of the Nyquist frequency (half the sampling frequency). So if the sampling rate is 1000Hz and you want a cutoff of 250Hz, you should useWn=0.5.By the way, I highly recommend the use of
filtfiltoverlfilter(which is called justfilterin Matlab) for most applications. As the documentation states:What this means is that each value of the output is a function of both “past” and “future” points in the input equally. Therefore it will not lag the input.
In contrast,
lfilteruses only “past” values of the input. This inevitably introduces a time lag, which will be frequency-dependent. There are of course a few applications for which this is desirable (notably real-time filtering), but most users are far better off withfiltfilt.