I am trying to recreate a function in Matlab that claimed to "use FFT to create an approximation to the continuous time Fourier transform" and was purportedly accurate for arbitrary input signals. The challenge is that I haven't been able to get it to work consistently for zero centered rectangular pulses or sinc functions--or so far anything but a Gaussian pulse. The central issue seems to be the fact that the FFT assumes the very first sample exists at time t=0. I am aware of the approximation formulas given in resources like A tutorial on THz pulse analysis: accurate retrieval of pulse arrival times, spectral energy density and absolute spectral phase and elsewhere that have two components:
- Scaling the FFT by deltaT (the sampling period in time)
- Multiplying by a complex exponential with a phase of 2pifreq*t(1)
I would greatly appreciate any insight, as all the resources I've found that either use the two step approach above or talk about ifftshift-ing the time-domain input before applying the FFT just don't seem to produce the right result. I believe I don't have any toolbox-specific functions in this script, but I can update it if so.
For the rectangular pulse, I'm expecting the phase spectrum to be like a pulse train with zero phase in the center as long as the input isn't shifted. For the sinc function, I think the phase ought to just be flat at zero for the width of the tophat in the magnitude spectrum but may have floating point junk outside. For the Gaussian, that seems to behave nicely and have flat phase of zero where the magnitude is high. Code below:
clc clearvars close all tSamp = 0.01; fSamp = 1/tSamp; t = -2:tSamp:2 - tSamp; % t = -2:tSamp:2; pick = 1; % 1 for rect, 2 for sinc, 3 for gaussian phaseCorrect = true; timeShift = 0; padFlag = 0; % 1 for end only, 2 for symmetric nSamp = length(t); % number*2=even % Want this to be able to handle signals with arbitrary delay, but not % getting expected results for zero-centered signals % x = rectpuls(t, 1); x = (abs((t-timeShift)) <= 0.5); y = 3*sinc(3*(t-timeShift)); sigma = 0.025; z = exp((-(t-timeShift).^2)/sigma^2); sigs = [x; y; z]; figure plot(t, sigs(pick, :)) xlabel('Time (s)') ylabel('Amplitude') if padFlag == 1 nSamp = 2*length(t); % number*2=even padSize = nSamp - length(t); pad = [sigs(pick, :), zeros(1, padSize)]; elseif padFlag == 2 nSamp = 2*length(t); % number*2=even padSize = nSamp - length(t); pad = [zeros(1, padSize/2), sigs(pick, :), zeros(1, padSize/2)]; else nSamp = length(t); pad = sigs(pick, :); end discSpec = fft((pad)); freq = (0:nSamp-1)/tSamp/nSamp; figure plot(abs(discSpec)) xlabel('Index') ylabel('Amplitude') title('Uncorrected Spectrum') % Dividing by fSamp=multiplying by tSamp to go from summation to % (approximate) integration % Multiplying by complex exponential to correct for the fact that the DFT % assumes the first sample is at t=0 and these start at t=-2. freq2 = -fSamp/2:fSamp/(nSamp):fSamp/2 - fSamp/(nSamp); tShift = t(1); shifter = exp(-j*2*pi*tShift*freq2); if phaseCorrect approxSpec = shifter.*fftshift(discSpec)/fSamp; else approxSpec = fftshift(discSpec)/fSamp; end figure freq2 = -1/(2*tSamp):1/(nSamp*tSamp):1/(2*tSamp) - 1/(nSamp*tSamp); plot(freq2, (abs(approxSpec))) xlabel('Frequency (Hz)') ylabel('Amplitude') title('Scaled Amplitude Spectrum') figure plot(freq2, (unwrap(angle(approxSpec)))) xlabel('Frequency (Hz)') ylabel('Phase') title('Unwrapped Phase Spectrum') figure plot(freq2, ((angle(approxSpec)))) % plot(freq2, atan2(imag(approxSpec), real(approxSpec))) xlabel('Frequency (Hz)') ylabel('Phase') title('Phase Spectrum') figure % plot(freq2, unwrap((angle(exp(j*2*pi*2*freq2))))) plot(freq2, unwrap(atan2(imag(shifter), real(shifter)))) EDIT 1: The primary test case I am trying to replicate is to get an accurate spectrum for $x(t)=2*sinc(3.2(t-1.5))$ where the sample spacing is 0.01 and $ -1\leq t \leq 4 $.
I think I follow the interpolation idea for a case where there are no restrictions placed, but perhaps my expectations for the result are out of order. For this case, the phase spectrum should have a negative slope of about $2\pi(1.5)$ and be zero outside the region, right?
