Python Audio fftpack:过滤噪声信号并绘制图形吗? [英] Python Audio fftpack: Filter noise signal and graph it?

查看:70
本文介绍了Python Audio fftpack:过滤噪声信号并绘制图形吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我要遵循一个教程,在该教程中,我们将使用fftpack创建信号并过滤噪声.

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

我收到此错误:

警告(来自警告模块):文件"C:\ Python39 \ lib \ site-packages \ numpy \ core_asarray.py",第83行返回array(a,dtype,copy = False,order = order)ComplexWarning:将复数值转换为实数会丢弃虚部

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

  y =信号x = time_vec 

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

 将numpy导入为np从scipy导入fftpacktime_step = 0.05#返回[0,10]之间的等距时间向量(0.5)time_vec = np.arange(0,10,time_step)打印(time_vec)期间= 5#创建一个信号并添加一些噪声:#输入角度2pi *时间向量),以弧度表示,返回值的范围是-1至+1-本质上是模仿sigla波,它以周期传播+给此母狗增加了一些噪声#numpy.random.randn()-从均值0和方差1的标准正态分布返回样本sig =(np.sin(2 * np.pi * time_vec)/周期)+ 0.25 * np.random.randn(time_vec.size)#返回实数或复数序列的离散傅立叶变换sig_fft = fftpack.fft(sig)#转换sin函数#获得振幅?振幅= np.abs(sig_fft)#np.abs()-根据复数a + ib计算绝对值功率=振幅** 2#通过振幅2的幂创建功率谱#获取这些变换值(即sig_fft)的(角度)基本光谱Angle = np.angle(sig_fft)#返回复杂参数的角度#对于每个振幅和功率(数组中的每个元素?)-xxx中会有相应的差异#这将返回采样频率或每个(幅度)的相应频率,即功率sample_freq = fftpack.fftfreq(sig.size,d = time_step)打印(振幅)打印(sample_freq)#因为我们想消除噪声,所以我们关心的是包含峰值幅度的峰值频率Amp_Freq = np.array([Amplitude,sample_freq])#现在我们尝试找到峰值幅度-因此我们尝试提取Amp_position = Amp_Freq [0,:].argmax()peak_freq = Amp_Freq [1,Amp_position]#查找最大值位置的位置(振幅)#打印最大振幅的位置print(-",Amp_position)#打印最大振幅的频率打印(peak_freq)high_freq_fft = sig_fft.copy()#为所有值分配大于峰值频率的对应频率-分配em 0-取消!在数组中(元素)(?)high_freq_fft [np.abs(sample_freq)>peak_freq] = 0print(是:",high_freq_fft)#返回实数或复数序列的离散逆傅立叶逆变换filter_sig = fftpack.ifft(high_freq_fft)打印(过滤后的噪声:",filtered_sig)#使用快速傅立叶变换和快速傅立叶逆变换,我们可以从频域中消除噪声(否则在时域中是不可能做到).#用噪声(?)绘制信号并进行滤波导入matplotlib.pyplot作为plty =过滤信号x = time_vecplt.plot(x,y)plt.xlabel('时间')plt.ylabel('滤波幅度')plt.show()y =信号x = time_vecplt.plot(x,y)plt.xlabel('时间')plt.ylabel('未过滤的振幅')plt.show() 

解决方案

问题1:在您绘制 filtered_sig 时会在matplotlib中出现,因为它包含小的虚部.您可以通过

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.

I'm getting this 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 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()

解决方案

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

这篇关于Python Audio fftpack:过滤噪声信号并绘制图形吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆