微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python Audio fftpack:过滤噪声信号并绘制图形?

如何解决Python Audio fftpack:过滤噪声信号并绘制图形?

所以我按照教程创建信号并使用 fftpack 过滤噪声。

问题 1:我试图在图表上绘制过滤和未过滤的信号噪声,以便我可以并排看到它们。

我收到此错误

警告(来自警告模块):文件 “C:\python39\lib\site-packages\numpy\core_asarray.py”,第 83 行 return array(a,dtype,copy=False,order=order) ComplexWarning: Casting complex values to real 丢弃虚部

我认为这是导致错误的原因:

y = sig
x = time_vec

问题 2:我不确定如何在同一个窗口中绘制两个图形?

import numpy as np

from scipy import fftpack

time_step = 0.05

# Return evenly spaced time vector (0.5) between [0,10]
time_vec = np.arange(0,10,time_step)

print(time_vec)

period = 5

# create a signal and add some noise:
# input angle 2pi * time vector) in radians and return value ranging from -1 to +1 -- essentially mimicking a sigla wave that is goes in cycles + adding some noise to this bitch
# numpy.random.randn() - return samples from the standard normal distribution of mean 0 and variance 1
sig = (np.sin(2*np.pi*time_vec)/period) + 0.25 * np.random.randn(time_vec.size)

 

# Return discrete Fourier transform of real or complex sequence
sig_fft = fftpack.fft(sig) # tranform the sin function

# Get Amplitude ?
Amplitude = np.abs(sig_fft) # np.abs() - calculate absolute value from a complex number a + ib

Power = Amplitude**2  # create a power spectrum by power of 2 of amplitude

# Get the (angle) base spectrrum of these transform values i.e. sig_fft 
Angle = np.angle(sig_fft)    # Return the angle of the complex argument

# For each Amplitude and Power (of each element in the array?) - there is will be a corresponding difference in xxx

# This is will return the sampling frequecy or corresponding frequency of each of the (magnitude) i.e. Power
sample_freq = fftpack.fftfreq(sig.size,d=time_step)

print(Amplitude)
print(sample_freq)


# Because we would like to remove the noise we are concerned with peak freqence that contains the peak amplitude
Amp_Freq = np.array([Amplitude,sample_freq])


# Now we try to find the peak amplitude - so we try to extract
Amp_position = Amp_Freq[0,:].argmax()

peak_freq = Amp_Freq[1,Amp_position]   # find the positions of max value position (Amplitude)

# print the position of max Amplitude
print("--",Amp_position)
# print the frequecies of those max amplitude
print(peak_freq)


high_freq_fft = sig_fft.copy()
# assign all the value the corresponding frequecies larger than the peak frequence - assign em 0 - cancel!! in the array (elements) (?)
high_freq_fft[np.abs(sample_freq) > peak_freq] = 0

print("yes:",high_freq_fft)


# Return discrete inverse Fourier transform of real or complex sequence
filtered_sig = fftpack.ifft(high_freq_fft)

print("filtered noise: ",filtered_sig)

# Using Fast Fourier Transform and inverse Fast Fourier Transform we can remove the noise from the frequency domain (that would be otherwise impossible to do in Time Domain) - done.

# Plotting the signal with noise (?) and filtered

import matplotlib.pyplot as plt


y = filtered_sig
x = time_vec

plt.plot(x,y)
plt.xlabel('Time')
plt.ylabel('Filtered Amplitude')
plt.show()


y = sig
x = time_vec

plt.plot(x,y)
plt.xlabel('Time')
plt.ylabel('Unfiltered Amplitude')
plt.show()

解决方法

问题 1: 在绘制 filtered_sig 时出现在 matplotlib 中,因为它包含小的虚部。你可以用real_if_close把它们砍掉。

问题 2:不要在第一个图和第二个图之间使用 show

这是一张带有图例的图表中完整的绘图部分:

import matplotlib.pyplot as plt

x = time_vec
y = np.real_if_close(filtered_sig)
plt.plot(x,y,label='Filtered')
plt.xlabel('Time')
plt.ylabel('Amplitude')

y = sig
plt.plot(x,label='Unfiltered')

plt.legend()
plt.show()

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。