我正在尝试使用matplotlib.pyplot.stem()
函数查看由50,000点组成的信号。但是,matplotlib运行时间太长。我已经在MATLAB中完成了相同的任务,而没有任何明显的延迟,我希望Python有一种遵循套件的方法。对加快流程有什么建议吗?预先感谢。
我已经按照here和here的方法进行抽选,但是由于信号非常复杂,我丢失了太多信息。如果我大大降低了fs=1e6
的采样频率(甚至fs=5e5
也会明显降低输出质量),也会发生同样的情况。
我正在使用以下代码:(anaconda发行版)Python 3.6.6,Anaconda 5.3.1,matplotlib 3.0.2,macOS Mojave(10.14.1)。
这是我的代码(我为多次导入而道歉-这些是来自单独的文件):
# --- Create the Noisy Signal --- #
from numpy import sin, cos, pi, arange
from numpy.random import randint
import matplotlib.pyplot as plt
import pandas as pd
fs = 1e6
Ts = 1/fs
t_end = 50e-3
t = arange(0,t_end-Ts,Ts)
print(t.size)
f1 = 1.8e3
f2 = 1.9e3
f3 = 2e3
f4 = 1.85e3
f5 = 1.87e3
f6 = 1.94e3
f7 = 1.92e3
info_signal = 2.5*cos(2*pi*f1*t) + 1.75*cos(2*pi*f2*t) + 2*cos(2*pi*f3*t) + 2*cos(2*pi*f4*t) + 1*cos(2*pi*f5*t) + 1*cos(2*pi*f6*t) + 1.5*cos(2*pi*f7*t)
N = 25
my_sum = 0
for i in range(N+1):
noise_amp = 0.075*randint(-10,10,size=(1,1))
noise_freq = randint(-1e6,1e6,size=(1,1))
noise_signal = my_sum + noise_amp * cos(2*pi*noise_freq*t)
my_sum = noise_signal
f6 = 50e3 #50kHz
f7 = 49.9e3
f8 = 51e3
pwr_supply_noise = 1.5*sin(2*pi*f6*t) + 1.25*sin(2*pi*f7*t) + 1*sin(2*pi*f8*t)
f9 = 60
low_freq_noise = 1.5*sin(2*pi*f9*t)
total_signal = info_signal + noise_signal + pwr_supply_noise + low_freq_noise
total_signal = total_signal.reshape(total_signal.size)
df = pd.DataFrame({'0':t,
'1':total_signal})
df.to_csv('NoisySignal.csv')
# --- Import the Noisy Signal --- #
import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as sig
import pandas as pd
import control
from scipy.fftpack import fft, fftshift
# Load Input Signal
df = pd.read_csv('NoisySignal.csv')
t = df['0'].values
sensor_sig = df['1'].values
# --- Define Fast Fourier Transform Routine --- #
def fftBean(x,fs):
from numpy import angle,arange
from scipy.fftpack import fft, fftshift
N = len(x) # Find length of signal
print('N =',N)
X_fft = fft(x) # Perform the fft
X_fft_shifted = fftshift(X_fft) # Shift zero frequency component to center of spectrum
freq = arange(-N/2,N/2) * fs/N # Compute the frequencies for the output signal
X_mag = abs(X_fft_shifted) / N # Compute the magnitudes of the signal
X_phi = angle(X_fft_shifted) # Compute the phases of the signal
for i in range(len(X_phi)):
if abs(X_mag[i]) < 1e-10:
X_phi[i] = 0
return freq, X_mag, X_phi
# --- Call FFT Routine + Plot Results --- #
freq,sig_mag,sig_phi = fftBean(sensor_sig,fs)
plt.stem(freq,sig_mag)
plt.show()