[Python] The error of manually computed inverse-FFT increase along x_axis

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

  1. Stack

    Stack Membro Participativo

    I am manually compute the inverse FFT using the numpy FFT components by the below codes, to compare the rebuild signal with the original signal(sin function). As shown in the results, the error(original -rebuilt) is increasing along the x axis. Does anyone know the reasons and how to correct it. If I used the build-in function ifft to rebuild the signal,the error doesn't have this increase tread along x axis.

    import numpy as np
    from scipy import *
    from matplotlib import pyplot as plt
    tempdata = np.loadtxt("sinx_80.dat")


    x_positions = tempdata[:, 0]
    delta_x=x_positions[1] - x_positions[0]
    roughness_data = tempdata[:, 1]


    fft_1d = np.fft.fft(roughness_data)
    num_x = len(fft_1d)

    freq_x = np.fft.fftfreq(num_x, d=delta_x)
    reconstructed_surface=np.zeros(num_x)
    x_grid = (x_positions - x_positions[0])

    for n in range(num_x):
    amplitude = np.abs(fft_1d[n]) / (num_x)
    phase = np.angle(fft_1d[n])
    reconstructed_surface += amplitude * np.cos(2 * np.pi * (freq_x[n] * x_grid) +
    phase)

    difference = roughness_data - reconstructed_surface


    max_error = np.max(np.abs(difference))
    rms_error = np.sqrt(np.mean(difference**2))

    print(f"Max Error: {max_error}")
    print(f"RMS Error: {rms_error}")


    plt.figure(figsize=(12, 8))
    plt.subplot(3, 1, 1)
    plt.plot(x_positions, roughness_data, label="Original Data", color="blue")
    plt.title("Original Data")
    plt.legend()
    plt.grid()


    plt.subplot(3, 1, 2)
    plt.plot(x_positions, reconstructed_surface, label="Reconstructed Data",
    color="orange")
    plt.title("Reconstructed Data")
    plt.legend()
    plt.grid()


    plt.subplot(3, 1, 3)
    plt.plot(x_positions, difference, label="Difference (Original - Reconstructed)",
    color="red")
    plt.title("Difference")
    plt.legend()
    plt.grid()


    plt.show()


    [​IMG]

    Continue reading...

Compartilhe esta Página