testing ifft implementation in python does not give the good result

Viewed 105

I am trying to test the np.fft.ifft on a sinus cardinal function, which its Fourier transform is the 'boxcar' function (equal to 1 for |x|<1 and zero elsewhere).

Here is the code :

    import numpy as np
    import matplotlib.pyplot as plt
    zetamax = 50.;
    Nzeta = 1024;
    xf = np.linspace(-zetamax, zetamax, Nzeta)
    yf = np.sinc(xf);
    yfft = np.fft.ifft(yf);
    
    #Analytical solution
    Nx = Nzeta/2;
    xmax = Nzeta/(2*zetamax);
    x = np.linspace(-xmax, xmax, Nx);
    
    def boxcar(x):
        if np.abs(x)<1:
            return 1;
        else:
            return 0;
    y = np.zeros(len(x));
    for i in range(len(y)):
        y[i] = boxcar(x[i]);
    
    plt.plot(xf, yf)
    plt.xlabel('zeta')
    plt.ylabel('sinc')
    plt.figure()
    plt.plot(x, yfft[0:Nx], label='IFFT');
    plt.plot(x, y, label='boxcar')
    plt.legend()
    plt.grid()
    
    plt.show()

This is what I get as a result :

Sinus cardinal: enter image description here

Ifft result versus boxcar function: enter image description here

0 Answers
Related