Maryam Nahid
Maryam Nahid

Reputation: 59

Low pass filter-Python

I am trying to use low pass filter on my data. My sample data is for every minute and I want to filter it with the time constant of 1440(daily). It is just writing a for loop that computes the filtering and appending the data into the y(filtered data) list.

Following is the code that I have written:

OD_deltaT=df["OD_deltaT"].values.tolist()
#a=1/(timeconstant)+1
a=1/1441
u, y=[0], []
for i, x in enumerate(OD_deltaT,1):
    u.append(x)
    y[1]=u[1]
    y[i]=(1-a)*y[i-1]+(a)*u[i]

But I get this error:

list index out of range.  

Thank you in advance!

Upvotes: 0

Views: 2150

Answers (1)

Matthieu Brucher
Matthieu Brucher

Reputation: 22023

You need to append your data, like your did for u. You can also write the code in a little bit more concise way:

u = [0]
y = [0]
for x in OD_deltaT:
    u.append(x)
    y.append((1-a)*y[-1])+a*x)

Of course, scipy filters would do this faster ;)

Upvotes: 2

Related Questions