矩形脉冲序列FFT频谱仿真异常问题排查求助
矩形脉冲序列FFT频谱仿真异常问题排查求助
我现在需要模拟一组矩形脉冲序列,于是写了一段Python代码,但最终的频谱结果不太理想。从生成的图里能看到,脉冲序列的波形是符合预期的,但频谱部分明显有问题——我试过提高采样率也没能解决这个问题。想请教一下,我的代码里哪里出错了,导致FFT频谱生成异常?
#!/bin/python import numpy as np from numpy.fft import fft import matplotlib.pyplot as plt A = np.random.randint(1, 6) # random amplitude (from 1 to 5) tilda = 0.5 # pulse duration SR = 1000 # sampling rate t = np.linspace(0, 3, SR) # time range from 0 to 2 seconds n_pulses = 3 # number of pulses t_i = 0.5 # interval between pulses pulse1_start = 0.25 # the beginning of the first pulse rect_pulses = np.zeros_like(t) for i in range(n_pulses): pulse_start = pulse1_start + i * (tilda + t_i) pulse_end = pulse_start + tilda rect_pulses = np.where((t >= pulse_start) & (t <= pulse_end), A, rect_pulses) FFT = fft(rect_pulses) # magnitude of FFT FFT_n = FFT / len(FFT) # normalization f_ax = np.linspace(0, (SR), len(FFT)) # frequency axis half_fft = len(FFT) // 2 # half the length of the FFT f_half = f_ax[:half_fft] # frequencies up to f_ax / 2 FFT_half = np.abs(FFT_n[:half_fft]) # FFT module up to f_ax/2 FFT_half *= 2 # doubling the magnitude plt.figure(figsize=(10, 10)) plt.subplot(2, 1, 1) plt.plot(t, rect_pulses, linewidth=1.7) plt.title('Rect. Pulse', fontsize=21) plt.xlabel('t', fontsize=21, fontweight='bold') plt.ylabel('x(t)', fontsize=21, fontweight='bold') plt.grid() plt.subplot(2, 1, 2) plt.plot(f_half, FFT_half, 'r-', linewidth=1.7) plt.title('FFT of Rect. Pulse', fontsize=21) plt.xlabel('$f$', fontsize=21, fontweight='bold') plt.ylabel('S($f$)', fontsize=21, fontweight='bold') plt.xlim([0,40]) plt.grid() plt.tight_layout() plt.show()

备注:内容来源于stack exchange,提问作者Varga
相关产品推荐
相关产品推荐

