Python Audio fftpack: Filter noise signal and graph it?

强颜欢笑 提交于 2021-01-29 03:57:40

问题


So I'm follow a tutorial where we create a signal and filter the noise using fftpack.

Problem 1: I'm trying to plot the filtered and unfiltered signal noise on a graph so that I can see them side by side. Getting error:

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

I think this is causing the error:

y = sig
x = time_vec

Problem 2: I'm not sure how to plot the two graphs in the same 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 arguement

# 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:


Problem 1: arises within matplotlib when you plot filtered_sig as it includes small imaginary parts. You can chop them off by real_if_close.

Problem 2: just don't use show between the first and the second plot

Here is the complete working plotting part in one chart with a legend:

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()



来源:https://stackoverflow.com/questions/65404314/python-audio-fftpack-filter-noise-signal-and-graph-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!