[Python] Incorrect modeling of the spectrum of a sequence of pulse signals

Discussão em 'Python' iniciado por Stack, Março 18, 2025.

  1. Stack

    Stack Membro Participativo

    I have a task to simulate a sequence of rectangular pulses. As a result, I wrote Python code, but the end results are not encouraging.

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


    [​IMG]

    As you can see in the picture, the sequence turned out to be acceptable. But the spectrum didn't work out. Increasing the sampling rate doesn't help me at all. What is wrong with my code that causes such an incorrect FFT to be built?

    Continue reading...

Compartilhe esta Página