Reconstructed Pulse Out of Phase

2 views (last 30 days)
Doug on 6 Sep 2022
Commented: Paul on 6 Sep 2022
In my research I'm having a problem reconstructing some pulses, I've replicated the problem I'm having with a simple gaussian pulse. Essentially when I take the IFFT of the pulse, the reconstructed time domain signal is out of phase. I've included my code with the plots, I think the problem is within how I'm reconstructing the time space. Any help is appreciated, thank you!
clear variables
%% FFT Test on Gaussian Pulse
Fs = 100; %Sampling Frequency
t = -0.5:1/Fs:0.5; %Time Vector
L = length(t);
x = 1/(4*sqrt(2*pi*0.01))*(exp(-t.^2/(2*0.01))); %Gaussian pulse
plot(t,x) %plotting pulse
title('Gaussian Pulse')
xlabel('time(t)')
ylabel('Pulse')
n = 2^nextpow2(L);
Y = fft(x,n); %plotting frequency spectrum
P = abs(Y);
f = Fs*(0:(n/2))/n;
plot(f,P(1:n/2+1)
%% Reconstructing Pulse
recon = ifft(Y);
t2 = linspace(-0.5,0.5,128); %Reconstructing Time Space
figure()
plot(t,x)
hold on
plot(t2,recon,'--')
hold off
legend('Original','Reconstruction')

Paul on 6 Sep 2022
Hi Doug,
I think the short answer is, don't use nextpow2.
Fs = 100; %Sampling Frequency
t = -0.5:1/Fs:0.5; %Time Vector
L = length(t);
x = 1/(4*sqrt(2*pi*0.01))*(exp(-t.^2/(2*0.01))); %Gaussian pulse
Y = fft(x);
xr = ifft(Y);
figure;
plot(t,x,t,xr,'o')
However, that answer seems too obvious.
If desired to use more points in the fft for some reason, then we just need to account for what that means on the time vector
N = 2^nextpow2(numel(x));
tr = (0:(numel(x)-1))/Fs + t(1);
xr = ifft(fft(x,N));
xr = xr(1:numel(tr));
In the plot below, I'm also including the what is effectively t2 and recon from the Question. We see that t2 isn't really the correct time vector becasue it effectively redefined the sampling frequency.
figure;
plot(t,x,tr,xr,'o',.1+linspace(-0.5,0.5,128),ifft(fft(x,128)))
legend('Original','Reconstructed','recon from Question')
Paul on 6 Sep 2022
Good luck. Keep in mind that
fft(x)
Only give the correct magnitude, but not the correct phase, because the first element of x corresponds to a negative time. In this case, since we're going to frequency and back to time, it doesn't matter, but keep it in mind.

Cris LaPierre on 6 Sep 2022
I'm not an FFT expert, but it appears to be related to your value of n. At least the shift goes away when I use L instead (seethis example)
%% FFT Test on Gaussian Pulse
Fs = 100; %Sampling Frequency
t = -0.5:1/Fs:0.5; %Time Vector
L = length(t);
x = 1/(4*sqrt(2*pi*0.01))*(exp(-t.^2/(2*0.01))); %Gaussian pulse
plot(t,x) %plotting pulse
title('Gaussian Pulse')
xlabel('time(t)')
ylabel('Pulse')
% n = 2^nextpow2(L);
Y = fft(x); %plotting frequency spectrum
P = abs(Y/L);
f = Fs*(0:(L/2))/L;
plot(f,P(1:L/2+1))
Warning: Integer operands are required for colon operator when used as index.
%% Reconstructing Pulse
recon = ifft(Y);
t2 = linspace(-0.5,0.5,L); %Reconstructing Time Space
figure()
plot(t,x)
hold on
plot(t2,recon,'--')
hold off
legend('Original','Reconstruction')
Doug on 6 Sep 2022
I've noticed this as well, I think the problem lies in the fact that when reconstructing the pulse using n, the reconsctructed points will necessarily be out of phase due to n=/=L. In my original problem however, the ifft needs to be consctructed out of n due to some properties of the data. I'll have to look a bit deeper into this but thank you, it gives me some ideas where to look.