Search code examples
matlabfftifftphase

fft matlab time to freq and back


I'm trying a simple (Fast Fourier Transform) fft and convert it back, but it doesn't work. I need to start with this in order to proceed with adding phase to each component. Can you please take a look and see where I go wrong with this?

fun_cos=@(t) cos(1.5e12*t)
nttf=2^17;
t=linspace(-3*t_signal_pulse/2,3*t_signal_pulse/2,nttf);
dni_ni=(1/(t(2)-t(1)));
ni=-dni_ni/2:dni_ni/(nttf):dni_ni/2-dni_ni/(nttf);
w=ni.*2*pi;
figure(1)
plot(t,fun_cos(t))
FFt_cos=fftshift(fft(fun_cos(t),nttf))/length(t);
figure(2);
plot(w,abs(FFt_cos))
fft_back=ifft(ifftshift(FFt_cos));
figure(1)
hold on
plot(t,abs(fft_back),'.r')

Freq domain. you can see here two freq even though I only need one

Final result. The blue is the original cosine and the red is the one that I would expected to be the same

Also, if I want to add phase to both the time domain and frequency domain individually (please note that I only know one side of the freq. domain phases and not both so don't know how to proceed with this yet)


Solution

  • You forgot to rescale with 'length(t)':

    fft_back=ifft(ifftshift(FFt_cos*length(t)));
    
    MaxError=max(abs(fun_cos(t)-fft_back)) %reconstruction error