Signal filtering in Python

781 Views Asked by At

Trying to create a filter for a signal i made, but can't seem to get. I keep getting error about '/' whenever i call my filter function.

import numpy as np
from numpy import sqrt
import matplotlib.pyplot as plt
import scipy as sp
import math

def sinyal(N,c,f,p):
    y=np.zeros(N)
    t=np.linspace(0,2*np.pi,N)
    Nf=len(c)
    for i in range(Nf):
        y+=c[i]*np.sin(f[i]*t)
    return y;

def filter (w,wn):
    yf=1.0/sqrt(1.0+(w/wn)**(2.0*n))
    return yf;

c=[2,5,10]
f=[50, 150, 300]
p=[0,0]
N=2000
w=[50,150,300]
x=np.linspace(0,2.0*math.pi,N)
y=sinyal(N,c,f,p)

yf=filter(w,200)

plt.plot(x[:100],y[:100])

plt.show()    
1

There are 1 best solutions below

1
On
  • w is a list, wn is an int, so w/wn raises

    TypeError: unsupported operand type(s) for /: 'list' and 'int'
    

    You could fix the error by making w a NumPy array:

    w = np.array([50, 150, 300])
    
  • You'll also need to define n.
  • filter is called but the result is never used.
  • Tip: note that filter is the name of a Python builtin. It's best not to define a function or variable the same name as a builtin, since it makes it harder to access the builtin and may be confusing to others.
  • Tip: Python does not need semicolons at the end of statements.