Path: news.mathworks.com!not-for-mail
From: "Ken Garrard" <ken_garrardAT@ncsuDOT.edu>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Does Matlab do FFT correctly ?
Date: Fri, 2 May 2008 21:58:03 +0000 (UTC)
Organization: North Carolina State University
Lines: 68
Message-ID: <fvg2pb$73p$1@fred.mathworks.com>
References: <fvfs3j$bnf$1@fred.mathworks.com>
Reply-To: "Ken Garrard" <ken_garrardAT@ncsuDOT.edu>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1209765483 7289 172.30.248.37 (2 May 2008 21:58:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 2 May 2008 21:58:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 216874
Xref: news.mathworks.com comp.soft-sys.matlab:466361


"Chen Sagiv" <chensagivron@gmail.com> wrote in message 
<fvfs3j$bnf$1@fred.mathworks.com>...
> Hi all,
> 
> We all know that the DFT of a square pulse is a sinc. 
> 
> Let's try it:
> 
> % First we define the square pulse
> x=-1:0.01:1;
> f=zeros(size(x));
> f(x>-0.5 & x<0.5)=1;
> figure ; plot(x,f); 
> 
> % Now let's view its Fourier transform:
> 
> figure ; plot(real(fftshift(fft(f))));
> figure ; plot(imag(fftshift(fft(f))));
> 
> % Hey, the imaginary part is NOT zero
> % a remedy: 
> figure ; plot(real(fftshift(fft(ifftshift(f)))));
> figure ; plot(imag(fftshift(fft(ifftshift(f)))));
> 
> So, it seems that Matlab does correct FFT to signals that 
> are not centered about 0, but we have to ifftshift them 
> first. 
> 
> Is that a known "feature" of Matlab ? I will be glad to 
get 
> a clarification ! 
> 
> Best,
> 
> Chen Sagiv
> 

Chen,

Matlab does do the FFT and IFFT correctly.  The problem 
with your example is that your square pulse is not 
perfectly symmetric.  It is symmetric around zero but since 
the total number of data points is odd, there has to be one 
more point at -1 than +1 or the other way around.

Try generating data from -1 to (+1-stepsize) as follows,

---
% First we define the square pulse
x=-1:0.01:(1-0.01);      % NOTE the change.
f=zeros(size(x));
f(x>-0.5 & x<0.5)=1;
figure ; plot(x,f); 

% Now let's view its Fourier transform:

figure ; plot(real(fftshift(fft(f))));
figure ; plot(imag(fftshift(fft(f))));
---

Now the imaginary parts are very close to zero.  The same 
sort of behaviour happens with a sine wave that starts at 
zero and ends at zero; you don't get the expected result 
because the signal has a one point "glitch" if you repeat 
it to + and - infinity.

Ken