![](/img/trans.png)
[英]module 'matplotlib.pyplot' has no attribute 'patches'
[英]Problem 'module 'matplotlib.pyplot' has no attribute 'plot_filterz'
我多次尝试修复这段代码,但它仍然存在同样的问题。 这是我收到的 Python 语言的错误代码:
#----------------------------------------------------
#FIR filter
n=100 #Design filter
b=signal.firwin(n, cutoff= 0.2, window = "hamming")
#Plot properties
plt .plot_filterz(b)
x = np.sin(np . linspace(0,500,1024))+0.5*np. cos(np. linspace(0,750,1024))+ np.random.randn(1024)*0.2 +0.2*np. cos(np . linspace(0, 10000, 1024))
xfiltered = signal.lfilter(b, 1, x)
plt.plot(x[:200], label = "Original signal")
plt.plot(xfiltered[:200], "--k", label = "Filtered signal")
plt.legend()
plt.show()
我想不通为什么我一直收到“模块‘matplotlib.pyplot’没有属性‘plot_filterz’object 没有属性‘firwin’? 这是我的完整代码(已编辑): https://docs.google.com/document/d/1VPjlRgssIMfPp4xEP5wV3vG0ROLxXoos4oMMTjMoyVs/edit
我用 sample.wav 文件运行你的代码,发现你的代码中有很多错误。 我已经重构了您的代码,这些是现在从重构代码生成的图。
错误亮点:
重构代码:
import numpy as np
import matplotlib.pyplot as plt
import wave
from scipy import signal
def plot_freqz(c, a=1):
w, h = signal.freqz(c, a)
h_dB = 20 * np.log10(abs(h))
plt.plot(w / np.pi, h_dB)
plt.ylim([max(min(h_dB), -100), 5])
plt.ylabel('Magnitude (db)')
plt.xlabel(r'Normalized Frequency (x$\pi$rad/sample)')
plt.title(r'Amplitude response')
def plot_phasez(c, a=1):
w, h = signal.freqz(c, a)
h_Phase = np.unwrap(np.arctan(np.imag(h)))
plt.plot(w / np.pi, h_Phase)
plt.ylabel('Phase(radians)')
plt.xlabel(r'Normalized Frequency (x$\pi$rad/sample)')
plt.title(r'Phase response')
def plot_impz(c, a=1):
if type(a) == int: # FIR
factor = 10000 # using this factor to speed up the plotting. change as per your need
l = int(len(c) / factor)
else: # IIR
l = 100
impulse = np.repeat(0., l)
impulse[0] = 1
x = np.linspace(0, 1, l)
response = signal.lfilter(c, a, impulse)
plt.stem(x, response, linefmt='b-', basefmt='b-', markerfmt='bo')
plt.ylabel('Amplitude')
plt.xlabel(r'n(samples)')
plt.title(r'Impulse response')
def plot_stepz(c, a=1):
if type(a) == int: # FIR
factor = 10000 # using this factor to speed up the plotting. change as per your need
l = int(len(c)/factor)
else: # IIR
l = 100
impulse = np.repeat(0., l)
impulse[0] = 1
x = np.arange(0, l)
response = signal.lfilter(c, a, impulse)
plt.plot(x, np.cumsum(response))
plt.ylabel(' Amplitude')
plt.xlabel(r'n(samples)')
plt.title(r'Step response')
def plot_filterz(c, a=1):
print('plotting freqz plot')
plt.subplot(221)
plot_freqz(c, a)
print('plotting phasez plot')
plt.subplot(222)
plot_phasez(c, a)
print('plotting impz plot')
plt.subplot(223)
plot_impz(c, a)
print('plotting stepz plot')
plt.subplot(224)
plot_stepz(c, a)
plt.subplots_adjust(hspace=0.5, wspace=0.3)
plt.savefig('filter_properties.png')
plt.show()
print('done plotting properties')
def ma(x, n=5):
b = np.ones(n) / n
xf = signal.lfilter(b, 1, x)
return xf
# ----------------------------------------------------
# FIR filter
if __name__ == '__main__':
spf = wave.open('BabyElephantWalk60.wav', 'r') #sample .wav file, replace with your .wav file
# Extract Original Audio from Wav File
c = spf.readframes(-1)
# c = np.fromstring(c, np.int16)
c= np.frombuffer(c, dtype=np.int16)
n = 100 # Design filter
b = signal.firwin(n, cutoff=0.2, window="hamming")
# Plot properties
plot_filterz(c)
x = np.sin(np.linspace(0, 500, 1024)) + 0.5 * np.cos(np.linspace(0, 750, 1024)) + np.random.randn(
1024) * 0.2 + 0.2 * np.cos(np.linspace(0, 10000, 1024))
print('calculating lfilter of x')
xfiltered = signal.lfilter(b, 1, x)
print('plotting original signal and filtered signal')
plt.title('Original signal v/s filtered signal')
plt.plot(x[:200], label="Original signal")
plt.plot(xfiltered[:200], "--k", label="Filtered signal")
plt.legend()
plt.savefig('orignal_vs_filtered_signal.png')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.