Question
IN PYTHON: The scipy library of matplotlib in Python 3 contains a function scipy.signal.freqz which computes the frequency response of a digital filter. I am
IN PYTHON:
The scipy library of matplotlib in Python 3 contains a function "scipy.signal.freqz" which computes the frequency response of a digital filter. I am trying to write my own version of this function, to relay the same output as this imbedded function. I plan to use the inputs: freqz(b, a = 1, worN=None, whole=None): [b=numerator of linear filter; a=denominator of linear filter; worN=at none, compute at 512 frequencies equally spaced around the unit circle; whole=at none, frequency computed around upper half of unit circle, from 0 to pi]. This function should output two outputs: w and h
After implementing the function, it should pass all the tests provided in the last part of code. Need to implement scipy.signal.freqz using different ways other than source code and FFT.
DON'T USE FFT FUNCTION
I am going to provide the code that I am given. Supposed to translate the equation into code.
Code:
import numpy as np import matplotlib.pyplot as plt import scipy.signal as sig import matplotlib as mpl mpl.rcParams['figure.figsize'] = (5,3) %matplotlib inline
# Implementation of freqz using different way.
Please use the same variables
def my_freqz(b, a = 1, worN=None, whole=None): #replace with your answer pass
Testing code:
from collections import namedtuple Test = namedtuple('Test',['b','a','worN','whole']) tests = [ Test([1,0.5,0.1], [1,1], 256, None), Test(1,1,None, True), Test(1,1,12, False), Test([1,2],1,[0,1,2,3], None) ]
b,a = sig.iirdesign(0.2, 0.3, 1, 20) tests.append(Test(b,a,None,None))
b = sig.firwin(63, 0.25) tests.append(Test(b,1,1024,None))
for test in tests: w, h = sig.freqz(test.b, test.a, test.worN, test.whole) my_w, my_h = my_freqz(test.b, test.a, test.worN, test.whole) print(np.allclose(w, my_w), np.allclose(h, my_h))
Tests should output TRUE for all.
Output should look like
True True
True True
True True
True True
This is what I have for the implementation:
def my_freqz(b, a = 1, worN=None, whole=None): #replace with your answer #Initialize a a = np.atleast_1d(a)
#Initialize b b = np.atleast_1d(b) number = 0 B = len(b) A = len(a) l = 0 k = 0 N = np.exp(-1j*number) D = np.exp(-1j*number) answerB = 0 while number
Output: (Incorrect)
(1.6+0j) True False (1+0j) True True (1+0j) True True (3+0j) True False (0.124266867176+0j) True False (1+0j) True False
Looks like it is just working for integer values but not arrays, help please.
jll jw jwM h(e )= -= jw jwN jll A(eala[1]e + a[N]e jll jw jwM h(e )= -= jw jwN jll A(eala[1]e + a[N]eStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started