[SciPy-user] Lowpass Filter

Ariel Rokem arokem at berkeley.edu
Thu Feb 5 12:34:48 EST 2009


Hi - I don't know if this what you want (I don't know how to use lp2lp or
scipy.signal), but one strategy that I have used is to convolve your signal
with a box-car function of a length equal to the inverse of your cut-off.
This is most definitely not the best filter known to man, but fwiw here is
the code.

For example (here I do a lowpass and then subtract the low-passed signal
from the original, effectively doing a quick-and-ugly highpass) :

box_car = np.ones(np.ceil(1.0/(f_c/TR))) #TR is the inverse of the sampling
frequency in the fMRI signal I am analyzing, f_c is the cutoff
box_car = box_car/(float(len(box_car)))

    print('Normalizing and detrending time series')
    for i in range(len(tSeries)):
        #Detrending
        #Start by applying a low-pass to the signal:
        #Pad the signal on each side with the initial and terminal signal
value:
        pad_s = np.append(np.ones(len(box_car)) * tSeries[i][0],
tSeries[i][:])
        pad_s = np.append(pad_s, np.ones(len(box_car)) * tSeries[i][-1])
        #Filter operation is a convolution with the box-car:
        conv_s = np.convolve(pad_s,box_car)
        #Extract the low pass signal (by excising the central len(tSeries)
points:
        s_lp=
conv_s[len(conv_s)/2-np.ceil(len(tSeries[i][:])/2.0):len(conv_s)/2+len(tSeries[i][:])/2]
#ceil(/2.0) for cases where the tSeries has an odd number of points
        #Extract the high pass signal simply by subtracting the high pass
signal
        #from the original signal:
        tSeries[i] = tSeries[i][:] - s_lp + np.mean(s_lp) #add mean to make
sure that there are no negative values
        #Normalization
        tSeries[i] = tSeries[i]/np.mean(tSeries[i])-1


On Thu, Feb 5, 2009 at 9:17 AM, Marco <gaedol at gmail.com> wrote:

> Hi list!
>
> Let's suppose a to be a 1D array with N elements.
> Basically, it's a signal of some sort.
>
> How do I apply a low pass filter (with selected frequency and width)
> to this signal?
> How to store the resulting, filtered, signal, in a new array?
>
> I had a look at lp2lp() in scipy.signal, but it returns, if I am
> right, a filter object, which then I dunno how to use to filter my
> data.
>
> Any ideas or pointers?
>
> TIA,
>
> marco
>
>
>
> --
>
> Quando sei una human pignata
> e la pazzo jacket si è accorciata
> e non ti puoi liberare
> dai colpi di legno e di bastone
> dai petardi sul groppone
>
> Vinicio Capossela
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20090205/8ab5d185/attachment.html>


More information about the SciPy-User mailing list