Big Puncho
Big Puncho

Reputation: 301

Transform filter function to Python code

I'm trying to understand how to transform a filter, in this case a Notch(stopband) filter, to Python but I don't know how.

x(n)=-2*x(n)/(-0.9*x(n) -0.9*x(n-1))

Can anyone help me please?

Thanks in advance.

Upvotes: 1

Views: 330

Answers (2)

mgilson
mgilson

Reputation: 310167

If you're using numpy arrays, this should work:

x[1:]=-2*x[1:]/(-0.9*x[1:]-0.9*x[:-1])

this changes your array in place, but you could just as easily assign it to a new array.

y=-2*x[1:]/(-0.9*x[1:]-0.9*x[:-1])

Note that your algorithm isn't really well defined for the 0th element, so my translation leaves x[0] unchanged.

EDIT

To change an iterable to a numpy array:

import numpy as np
x=np.array(iterable)  #pretty easy :) although there could be more efficient ways depending on where "iterable" comes from.

Upvotes: 3

pyInTheSky
pyInTheSky

Reputation: 1479

result = []
#prime your result, that is, add the initial values to handle indexing
lower_bound = #
upper_bound = #
for n in range(lower_bound,upper_bound):
    result.append( 2*result[n]/(-0.9*result[n] -0.9*result[n-1]) )

Upvotes: 0

Related Questions