0

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 Answer 1

1
  • 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.
Sign up to request clarification or add additional context in comments.

1 Comment

When i plot it, do i plot it x,y or x,yf?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.