Python 音频 fftpack:过滤噪声信号并绘制图表?

Python Audio fftpack: Filter noise signal and graph it?

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

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

我收到这个错误:

Warning (from warnings module): File "C:\Python39\lib\site-packages\numpy\core_asarray.py", line 83 return array(a, dtype, copy=False, order=order) ComplexWarning: Casting complex values to real discards the imaginary part

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

y = sig
x = time_vec

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

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, y, label='Unfiltered')

plt.legend()
plt.show()