<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168642</link>
    <title>MATLAB Central Newsreader - Does Matlab do FFT correctly ?</title>
    <description>Feed for thread: Does Matlab do FFT correctly ?</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2008 by The MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>The MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Fri, 02 May 2008 20:04:03 -0400</pubDate>
      <title>Does Matlab do FFT correctly ?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168642#429998</link>
      <author>Chen Sagiv</author>
      <description>Hi all,&lt;br&gt;
&lt;br&gt;
We all know that the DFT of a square pulse is a sinc. &lt;br&gt;
&lt;br&gt;
Let's try it:&lt;br&gt;
&lt;br&gt;
% First we define the square pulse&lt;br&gt;
x=-1:0.01:1;&lt;br&gt;
f=zeros(size(x));&lt;br&gt;
f(x&amp;gt;-0.5 &amp; x&amp;lt;0.5)=1;&lt;br&gt;
figure ; plot(x,f); &lt;br&gt;
&lt;br&gt;
% Now let's view its Fourier transform:&lt;br&gt;
&lt;br&gt;
figure ; plot(real(fftshift(fft(f))));&lt;br&gt;
figure ; plot(imag(fftshift(fft(f))));&lt;br&gt;
&lt;br&gt;
% Hey, the imaginary part is NOT zero&lt;br&gt;
% a remedy: &lt;br&gt;
figure ; plot(real(fftshift(fft(ifftshift(f)))));&lt;br&gt;
figure ; plot(imag(fftshift(fft(ifftshift(f)))));&lt;br&gt;
&lt;br&gt;
So, it seems that Matlab does correct FFT to signals that &lt;br&gt;
are not centered about 0, but we have to ifftshift them &lt;br&gt;
first. &lt;br&gt;
&lt;br&gt;
Is that a known "feature" of Matlab ? I will be glad to get &lt;br&gt;
a clarification ! &lt;br&gt;
&lt;br&gt;
Best,&lt;br&gt;
&lt;br&gt;
Chen Sagiv&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 02 May 2008 20:48:07 -0400</pubDate>
      <title>Re: Does Matlab do FFT correctly ?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168642#430000</link>
      <author>Steven G. Johnson</author>
      <description>On May 2, 4:04 pm, "Chen Sagiv" &amp;lt;chensagiv...@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; We all know that the DFT of a square pulse is a sinc.&lt;br&gt;
&lt;br&gt;
Actually, it's not.  The *Fourier* transform of a square pulse is a&lt;br&gt;
sinc function.  The *discrete* Fourier transform of a square pulse is&lt;br&gt;
something that is close to a sinc, but is not quite the same; it is&lt;br&gt;
the ratio of two sines.  (Just plug a square window into the DFT&lt;br&gt;
formula and sum the geometric series.)&lt;br&gt;
&lt;br&gt;
Your confusion is based on the fact that you don't know precisely what&lt;br&gt;
you are computing.&lt;br&gt;
&lt;br&gt;
&amp;gt; Let's try it:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; % First we define the square pulse&lt;br&gt;
&amp;gt; x=-1:0.01:1;&lt;br&gt;
&amp;gt; f=zeros(size(x));&lt;br&gt;
&amp;gt; f(x&amp;gt;-0.5 &amp; x&amp;lt;0.5)=1;&lt;br&gt;
&amp;gt; figure ; plot(x,f);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; [...]&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; So, it seems that Matlab does correct FFT to signals that&lt;br&gt;
&amp;gt; are not centered about 0, but we have to ifftshift them&lt;br&gt;
&amp;gt; first.&lt;br&gt;
&lt;br&gt;
This has nothing to do with Matlab, and is a consequence of the&lt;br&gt;
definition of the DFT.  The origin of a DFT is at 1st element of the&lt;br&gt;
input (i.e. the "left" end of the array), with periodic boundaries.&lt;br&gt;
This is totally standard and more-or-less universal among&lt;br&gt;
implementations of the DFT and FFT algorithms, and is not a Matlab&lt;br&gt;
quirk.&lt;br&gt;
&lt;br&gt;
The fftshift function shifts the origin to the center of the array,&lt;br&gt;
which is where many people expect it (and which is often more&lt;br&gt;
convenient for plotting).&lt;br&gt;
&lt;br&gt;
Regards,&lt;br&gt;
Steven G. Johnson&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 02 May 2008 21:26:03 -0400</pubDate>
      <title>Re: Does Matlab do FFT correctly ?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168642#430008</link>
      <author>Chen Sagiv</author>
      <description>Dear Steven,&lt;br&gt;
&lt;br&gt;
I appreciate your answer, but I am afraid that my question &lt;br&gt;
is still open. When you run my code, you get an imaginary &lt;br&gt;
part for the transform that is not supposed to be there !!! &lt;br&gt;
I still have to check your remark regarding the DFT of a &lt;br&gt;
square pulse being the ratio of two sines. Still, I do not &lt;br&gt;
think that one may expect getting complex value for the &lt;br&gt;
Fourier transform. &lt;br&gt;
&lt;br&gt;
Best,&lt;br&gt;
&lt;br&gt;
Chen &lt;br&gt;
&lt;br&gt;
"Steven G. Johnson" &amp;lt;stevenj@alum.mit.edu&amp;gt; wrote in message &lt;br&gt;
&amp;lt;80a6a285-8758-488c-9b98-&lt;br&gt;
beed0d03d19e@d45g2000hsc.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; On May 2, 4:04 pm, "Chen Sagiv" &amp;lt;chensagiv...@gmail.com&amp;gt; &lt;br&gt;
wrote:&lt;br&gt;
&amp;gt; &amp;gt; We all know that the DFT of a square pulse is a sinc.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Actually, it's not.  The *Fourier* transform of a square &lt;br&gt;
pulse is a&lt;br&gt;
&amp;gt; sinc function.  The *discrete* Fourier transform of a &lt;br&gt;
square pulse is&lt;br&gt;
&amp;gt; something that is close to a sinc, but is not quite the &lt;br&gt;
same; it is&lt;br&gt;
&amp;gt; the ratio of two sines.  (Just plug a square window into &lt;br&gt;
the DFT&lt;br&gt;
&amp;gt; formula and sum the geometric series.)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Your confusion is based on the fact that you don't know &lt;br&gt;
precisely what&lt;br&gt;
&amp;gt; you are computing.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Let's try it:&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; % First we define the square pulse&lt;br&gt;
&amp;gt; &amp;gt; x=-1:0.01:1;&lt;br&gt;
&amp;gt; &amp;gt; f=zeros(size(x));&lt;br&gt;
&amp;gt; &amp;gt; f(x&amp;gt;-0.5 &amp; x&amp;lt;0.5)=1;&lt;br&gt;
&amp;gt; &amp;gt; figure ; plot(x,f);&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; [...]&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; So, it seems that Matlab does correct FFT to signals &lt;br&gt;
that&lt;br&gt;
&amp;gt; &amp;gt; are not centered about 0, but we have to ifftshift them&lt;br&gt;
&amp;gt; &amp;gt; first.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This has nothing to do with Matlab, and is a consequence &lt;br&gt;
of the&lt;br&gt;
&amp;gt; definition of the DFT.  The origin of a DFT is at 1st &lt;br&gt;
element of the&lt;br&gt;
&amp;gt; input (i.e. the "left" end of the array), with periodic &lt;br&gt;
boundaries.&lt;br&gt;
&amp;gt; This is totally standard and more-or-less universal among&lt;br&gt;
&amp;gt; implementations of the DFT and FFT algorithms, and is not &lt;br&gt;
a Matlab&lt;br&gt;
&amp;gt; quirk.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The fftshift function shifts the origin to the center of &lt;br&gt;
the array,&lt;br&gt;
&amp;gt; which is where many people expect it (and which is often &lt;br&gt;
more&lt;br&gt;
&amp;gt; convenient for plotting).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Regards,&lt;br&gt;
&amp;gt; Steven G. Johnson&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 02 May 2008 21:47:01 -0400</pubDate>
      <title>Re: Does Matlab do FFT correctly ?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168642#430012</link>
      <author>Steven G. Johnson</author>
      <description>On May 2, 5:26 pm, "Chen Sagiv" &amp;lt;chensagiv...@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; I appreciate your answer, but I am afraid that my question&lt;br&gt;
&amp;gt; is still open. When you run my code, you get an imaginary&lt;br&gt;
&amp;gt; part for the transform that is not supposed to be there !!!&lt;br&gt;
&amp;gt; I still have to check your remark regarding the DFT of a&lt;br&gt;
&amp;gt; square pulse being the ratio of two sines. Still, I do not&lt;br&gt;
&amp;gt; think that one may expect getting complex value for the&lt;br&gt;
&amp;gt; Fourier transform.&lt;br&gt;
&lt;br&gt;
Sorry, I thought I had been clear.  Your square pulse is not centered&lt;br&gt;
at the origin (the origin for the DFT is *not* at the centered at the&lt;br&gt;
middle of the array).  Because your pulse is shifted in the time&lt;br&gt;
domain, it gets multiplied by a complex phase in the frequency domain&lt;br&gt;
by the shift theorem.&lt;br&gt;
&lt;br&gt;
Calling ifftshift on the fft input recenters your data correctly,&lt;br&gt;
giving you a real result for the fft.  However, even with this you&lt;br&gt;
have to be careful, because there is some subtletly in how ifftshift&lt;br&gt;
defines the "center" of the array depending upon whether the length is&lt;br&gt;
even or odd.&lt;br&gt;
&lt;br&gt;
Rest assured that Matlab's results are completely correct and&lt;br&gt;
consistent with the standard definition of the DFT.  Whenever it gives&lt;br&gt;
you something different from what you expect, you should be looking&lt;br&gt;
for a flaw in your understanding.&lt;br&gt;
&lt;br&gt;
Regards,&lt;br&gt;
Steven G. Johnson&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 02 May 2008 21:58:03 -0400</pubDate>
      <title>Re: Does Matlab do FFT correctly ?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168642#430015</link>
      <author>Ken Garrard</author>
      <description>"Chen Sagiv" &amp;lt;chensagivron@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;fvfs3j$bnf$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi all,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; We all know that the DFT of a square pulse is a sinc. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Let's try it:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % First we define the square pulse&lt;br&gt;
&amp;gt; x=-1:0.01:1;&lt;br&gt;
&amp;gt; f=zeros(size(x));&lt;br&gt;
&amp;gt; f(x&amp;gt;-0.5 &amp; x&amp;lt;0.5)=1;&lt;br&gt;
&amp;gt; figure ; plot(x,f); &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Now let's view its Fourier transform:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; figure ; plot(real(fftshift(fft(f))));&lt;br&gt;
&amp;gt; figure ; plot(imag(fftshift(fft(f))));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Hey, the imaginary part is NOT zero&lt;br&gt;
&amp;gt; % a remedy: &lt;br&gt;
&amp;gt; figure ; plot(real(fftshift(fft(ifftshift(f)))));&lt;br&gt;
&amp;gt; figure ; plot(imag(fftshift(fft(ifftshift(f)))));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So, it seems that Matlab does correct FFT to signals that &lt;br&gt;
&amp;gt; are not centered about 0, but we have to ifftshift them &lt;br&gt;
&amp;gt; first. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Is that a known "feature" of Matlab ? I will be glad to &lt;br&gt;
get &lt;br&gt;
&amp;gt; a clarification ! &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Best,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Chen Sagiv&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Chen,&lt;br&gt;
&lt;br&gt;
Matlab does do the FFT and IFFT correctly.  The problem &lt;br&gt;
with your example is that your square pulse is not &lt;br&gt;
perfectly symmetric.  It is symmetric around zero but since &lt;br&gt;
the total number of data points is odd, there has to be one &lt;br&gt;
more point at -1 than +1 or the other way around.&lt;br&gt;
&lt;br&gt;
Try generating data from -1 to (+1-stepsize) as follows,&lt;br&gt;
&lt;br&gt;
---&lt;br&gt;
% First we define the square pulse&lt;br&gt;
x=-1:0.01:(1-0.01);      % NOTE the change.&lt;br&gt;
f=zeros(size(x));&lt;br&gt;
f(x&amp;gt;-0.5 &amp; x&amp;lt;0.5)=1;&lt;br&gt;
figure ; plot(x,f); &lt;br&gt;
&lt;br&gt;
% Now let's view its Fourier transform:&lt;br&gt;
&lt;br&gt;
figure ; plot(real(fftshift(fft(f))));&lt;br&gt;
figure ; plot(imag(fftshift(fft(f))));&lt;br&gt;
---&lt;br&gt;
&lt;br&gt;
Now the imaginary parts are very close to zero.  The same &lt;br&gt;
sort of behaviour happens with a sine wave that starts at &lt;br&gt;
zero and ends at zero; you don't get the expected result &lt;br&gt;
because the signal has a one point "glitch" if you repeat &lt;br&gt;
it to + and - infinity.&lt;br&gt;
&lt;br&gt;
Ken&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 02 May 2008 22:09:03 -0400</pubDate>
      <title>Re: Does Matlab do FFT correctly ?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168642#430017</link>
      <author>Chen Sagiv</author>
      <description>Dear Steven,&lt;br&gt;
&lt;br&gt;
Thanks, I get your point. &lt;br&gt;
&lt;br&gt;
So, basically this is a matter of how to define the origin, &lt;br&gt;
and I agree with you that it seems consistent with DFT &lt;br&gt;
definition. &lt;br&gt;
&lt;br&gt;
Nevertheless, I will have to argue that this point is not &lt;br&gt;
clear at all. Most people refer only to the abs of the FT &lt;br&gt;
anyway, and this problem will simply go un-noticed. Only &lt;br&gt;
when phase is of interest, you really look at this subtle &lt;br&gt;
point. &lt;br&gt;
&lt;br&gt;
Best,&lt;br&gt;
&lt;br&gt;
Chen&lt;br&gt;
&lt;br&gt;
&amp;nbsp;"Steven G. Johnson" &amp;lt;stevenj@alum.mit.edu&amp;gt; wrote in &lt;br&gt;
message &amp;lt;e4eb82ac-4ea7-49b7-8cdd-&lt;br&gt;
aa427d8b064b@i76g2000hsf.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; On May 2, 5:26 pm, "Chen Sagiv" &amp;lt;chensagiv...@gmail.com&amp;gt; &lt;br&gt;
wrote:&lt;br&gt;
&amp;gt; &amp;gt; I appreciate your answer, but I am afraid that my &lt;br&gt;
question&lt;br&gt;
&amp;gt; &amp;gt; is still open. When you run my code, you get an &lt;br&gt;
imaginary&lt;br&gt;
&amp;gt; &amp;gt; part for the transform that is not supposed to be &lt;br&gt;
there !!!&lt;br&gt;
&amp;gt; &amp;gt; I still have to check your remark regarding the DFT of a&lt;br&gt;
&amp;gt; &amp;gt; square pulse being the ratio of two sines. Still, I do &lt;br&gt;
not&lt;br&gt;
&amp;gt; &amp;gt; think that one may expect getting complex value for the&lt;br&gt;
&amp;gt; &amp;gt; Fourier transform.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Sorry, I thought I had been clear.  Your square pulse is &lt;br&gt;
not centered&lt;br&gt;
&amp;gt; at the origin (the origin for the DFT is *not* at the &lt;br&gt;
centered at the&lt;br&gt;
&amp;gt; middle of the array).  Because your pulse is shifted in &lt;br&gt;
the time&lt;br&gt;
&amp;gt; domain, it gets multiplied by a complex phase in the &lt;br&gt;
frequency domain&lt;br&gt;
&amp;gt; by the shift theorem.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Calling ifftshift on the fft input recenters your data &lt;br&gt;
correctly,&lt;br&gt;
&amp;gt; giving you a real result for the fft.  However, even with &lt;br&gt;
this you&lt;br&gt;
&amp;gt; have to be careful, because there is some subtletly in &lt;br&gt;
how ifftshift&lt;br&gt;
&amp;gt; defines the "center" of the array depending upon whether &lt;br&gt;
the length is&lt;br&gt;
&amp;gt; even or odd.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Rest assured that Matlab's results are completely correct &lt;br&gt;
and&lt;br&gt;
&amp;gt; consistent with the standard definition of the DFT.  &lt;br&gt;
Whenever it gives&lt;br&gt;
&amp;gt; you something different from what you expect, you should &lt;br&gt;
be looking&lt;br&gt;
&amp;gt; for a flaw in your understanding.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Regards,&lt;br&gt;
&amp;gt; Steven G. Johnson&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 02 May 2008 22:15:06 -0400</pubDate>
      <title>Re: Does Matlab do FFT correctly ?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168642#430019</link>
      <author>Chen Sagiv</author>
      <description>Dear Ken,&lt;br&gt;
&lt;br&gt;
Thanks for your friendly answer. &lt;br&gt;
I am wondering what are the implications of your input on &lt;br&gt;
real life signals and images. &lt;br&gt;
&lt;br&gt;
I am looking at the Fourier phase of signals and images. &lt;br&gt;
Now, when I calculate the fft of say, an images, will I get &lt;br&gt;
the correct phase, or do I have to do some tricks ? &lt;br&gt;
&lt;br&gt;
I am not sure.&lt;br&gt;
&lt;br&gt;
Best,&lt;br&gt;
&lt;br&gt;
Chen &lt;br&gt;
&lt;br&gt;
"Ken Garrard" &amp;lt;ken_garrardAT@ncsuDOT.edu&amp;gt; wrote in message &lt;br&gt;
&amp;lt;fvg2pb$73p$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; "Chen Sagiv" &amp;lt;chensagivron@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;lt;fvfs3j$bnf$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Hi all,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; We all know that the DFT of a square pulse is a sinc. &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Let's try it:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; % First we define the square pulse&lt;br&gt;
&amp;gt; &amp;gt; x=-1:0.01:1;&lt;br&gt;
&amp;gt; &amp;gt; f=zeros(size(x));&lt;br&gt;
&amp;gt; &amp;gt; f(x&amp;gt;-0.5 &amp; x&amp;lt;0.5)=1;&lt;br&gt;
&amp;gt; &amp;gt; figure ; plot(x,f); &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; % Now let's view its Fourier transform:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; figure ; plot(real(fftshift(fft(f))));&lt;br&gt;
&amp;gt; &amp;gt; figure ; plot(imag(fftshift(fft(f))));&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; % Hey, the imaginary part is NOT zero&lt;br&gt;
&amp;gt; &amp;gt; % a remedy: &lt;br&gt;
&amp;gt; &amp;gt; figure ; plot(real(fftshift(fft(ifftshift(f)))));&lt;br&gt;
&amp;gt; &amp;gt; figure ; plot(imag(fftshift(fft(ifftshift(f)))));&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; So, it seems that Matlab does correct FFT to signals &lt;br&gt;
that &lt;br&gt;
&amp;gt; &amp;gt; are not centered about 0, but we have to ifftshift them &lt;br&gt;
&amp;gt; &amp;gt; first. &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Is that a known "feature" of Matlab ? I will be glad to &lt;br&gt;
&amp;gt; get &lt;br&gt;
&amp;gt; &amp;gt; a clarification ! &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Best,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Chen Sagiv&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Chen,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Matlab does do the FFT and IFFT correctly.  The problem &lt;br&gt;
&amp;gt; with your example is that your square pulse is not &lt;br&gt;
&amp;gt; perfectly symmetric.  It is symmetric around zero but &lt;br&gt;
since &lt;br&gt;
&amp;gt; the total number of data points is odd, there has to be &lt;br&gt;
one &lt;br&gt;
&amp;gt; more point at -1 than +1 or the other way around.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Try generating data from -1 to (+1-stepsize) as follows,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ---&lt;br&gt;
&amp;gt; % First we define the square pulse&lt;br&gt;
&amp;gt; x=-1:0.01:(1-0.01);      % NOTE the change.&lt;br&gt;
&amp;gt; f=zeros(size(x));&lt;br&gt;
&amp;gt; f(x&amp;gt;-0.5 &amp; x&amp;lt;0.5)=1;&lt;br&gt;
&amp;gt; figure ; plot(x,f); &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Now let's view its Fourier transform:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; figure ; plot(real(fftshift(fft(f))));&lt;br&gt;
&amp;gt; figure ; plot(imag(fftshift(fft(f))));&lt;br&gt;
&amp;gt; ---&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Now the imaginary parts are very close to zero.  The same &lt;br&gt;
&amp;gt; sort of behaviour happens with a sine wave that starts at &lt;br&gt;
&amp;gt; zero and ends at zero; you don't get the expected result &lt;br&gt;
&amp;gt; because the signal has a one point "glitch" if you repeat &lt;br&gt;
&amp;gt; it to + and - infinity.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Ken&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 02 May 2008 23:08:01 -0400</pubDate>
      <title>Re: Does Matlab do FFT correctly ?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168642#430025</link>
      <author>Ken Garrard</author>
      <description>Chen,&lt;br&gt;
&lt;br&gt;
There are many little tricks that you can play with real &lt;br&gt;
data that may help with your analysis.  The best one I know &lt;br&gt;
of it to use your intuition about your problem and the &lt;br&gt;
answer that you expect to get. And there are several things &lt;br&gt;
that I would recommend trying.&lt;br&gt;
1) Look at your data carefully before doing the FFT.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;a) Center your data about its mean value&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;b) Level the data to remove any drift&lt;br&gt;
2) Try using windowing functions.&lt;br&gt;
3) Check out www.dspguide.com for lots of practical &lt;br&gt;
information on a wide variety of problems.&lt;br&gt;
4) Sometimes it's helpful to do an autocovariance to see &lt;br&gt;
peaks in terms of lag length (back to time and space) &lt;br&gt;
instead of frequency.&lt;br&gt;
5) Do exactly what you're doing - run simple examples for &lt;br&gt;
which you know what the answer is supposed to be and &lt;br&gt;
resolve any descrepancy.  In this case the input data &lt;br&gt;
wasn't exactly what it was supposed to be.&lt;br&gt;
&lt;br&gt;
Since you asked about phase, one trick that I've found &lt;br&gt;
useful is to apply a simple threshold filter to the result &lt;br&gt;
returned by the FFT before proceeding with a phase angle &lt;br&gt;
calculation or unwrapping process.  This can also clear up &lt;br&gt;
problems when doing an inverse transform and you expect a &lt;br&gt;
real result and get a complex one.&lt;br&gt;
&lt;br&gt;
A post of mine about this from a couple of years ago is &lt;br&gt;
quoted below. &lt;br&gt;
&lt;br&gt;
------------------------&lt;br&gt;
I usually filter the real and imaginary parts of the &lt;br&gt;
complex return vector after an FFT to force values below a &lt;br&gt;
threshold to be exact zeros. Obviously these small values &lt;br&gt;
have little effect on the magnitude of a signal, but they &lt;br&gt;
can really foul up phase calculation. &lt;br&gt;
&lt;br&gt;
For example, generating one period of a sine wave with 100 &lt;br&gt;
samples.  (The last point is not a duplicate of the first) &lt;br&gt;
&lt;br&gt;
&amp;nbsp;t=0:0.01:1-.01; &lt;br&gt;
&amp;nbsp;s=sin(2*pi*t); &lt;br&gt;
&lt;br&gt;
Then calculate &lt;br&gt;
&amp;nbsp;f=fft(s); &lt;br&gt;
&amp;nbsp;m=abs(f); &lt;br&gt;
&amp;nbsp;p=unwrap(angle(f)); &lt;br&gt;
&lt;br&gt;
You would expect the only nonzero values in m to be at &lt;br&gt;
indices 2 and 100 and that they would be 50.0. The latter &lt;br&gt;
is the case; however none of the other values in m are &lt;br&gt;
exactly zero. &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;f(2) is -1.110223024625157e-015-50i &lt;br&gt;
&amp;nbsp;&amp;nbsp;f(end) is -1.110223024625157e-015+50i &lt;br&gt;
&lt;br&gt;
and for example, &lt;br&gt;
&amp;nbsp;&amp;nbsp;f(33) is -1.070083830450843e-015 -6.598918101450678e-016i &lt;br&gt;
&amp;nbsp;&amp;nbsp;m(33) is 1.257193941000705e-015 &lt;br&gt;
&lt;br&gt;
This usually isn't a problem, although I guess it depends &lt;br&gt;
on what you do next with the f or m vectors. &lt;br&gt;
&lt;br&gt;
But the phase angles don't make much sense, wrapped or &lt;br&gt;
unwrapped, &lt;br&gt;
&amp;nbsp;&amp;nbsp;eg, p(33) is 28.82692282385713 &lt;br&gt;
&lt;br&gt;
Again, I would expect a perfect sine wave to only have two &lt;br&gt;
non-zero phase values, p(2) equal to -1.57079632679490 and &lt;br&gt;
p(end) is equal to -p(2). The problem is that the inverse &lt;br&gt;
tangent of a ratio is nonsense if one or both of the values &lt;br&gt;
is "noise" or roundoff from a previous calculation. So we &lt;br&gt;
get (in radians), &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p(2) is 4.71238898038469 &lt;br&gt;
and p(end) is 51.83627878423159 &lt;br&gt;
&lt;br&gt;
If the unwrap is left out you get "noise" between pi and -&lt;br&gt;
pi. But if you apply a threshold filter to the result of &lt;br&gt;
the FFT then the phase angles are as expected. Setting &lt;br&gt;
threshold to an appropriate value say 1e-10, one way to do &lt;br&gt;
this is, &lt;br&gt;
&lt;br&gt;
function ff=cplxf(f,threshold) &lt;br&gt;
&amp;nbsp;fr = real(f); &lt;br&gt;
&amp;nbsp;fi = imag(f); &lt;br&gt;
&amp;nbsp;irz = find(abs(fr)&amp;lt;threshold); &lt;br&gt;
&amp;nbsp;iiz = find(abs(fi)&amp;lt;threshold); &lt;br&gt;
&amp;nbsp;fr(irz) = 0; &lt;br&gt;
&amp;nbsp;fi(iiz) = 0; &lt;br&gt;
&amp;nbsp;ff = fr + i*fi; &lt;br&gt;
&lt;br&gt;
Then, &lt;br&gt;
&amp;nbsp;f=fft(s); &lt;br&gt;
&amp;nbsp;ff=cplxf(f,1e-10); &lt;br&gt;
&amp;nbsp;m=abs(ff); &lt;br&gt;
&amp;nbsp;p=unwrap(angle(ff)); &lt;br&gt;
&lt;br&gt;
Now, &lt;br&gt;
&amp;nbsp;m(2) and m(end) are 50 &lt;br&gt;
&amp;nbsp;all other values in m are zero &lt;br&gt;
&amp;nbsp;p(2) is -1.57079632679490 &lt;br&gt;
&amp;nbsp;p(end) is 1.57079632679490 &lt;br&gt;
&amp;nbsp;and all other values in p are zero&lt;br&gt;
----------------------------------&lt;br&gt;
&lt;br&gt;
I hope this helps and good luck.&lt;br&gt;
Regards,&lt;br&gt;
&lt;br&gt;
Ken Garrard&lt;br&gt;
North Carolina State University&lt;br&gt;
&lt;br&gt;
"Chen Sagiv" &amp;lt;chensagivron@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;fvg3pa$slk$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Dear Ken,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks for your friendly answer. &lt;br&gt;
&amp;gt; I am wondering what are the implications of your input on &lt;br&gt;
&amp;gt; real life signals and images. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I am looking at the Fourier phase of signals and images. &lt;br&gt;
&amp;gt; Now, when I calculate the fft of say, an images, will I &lt;br&gt;
get &lt;br&gt;
&amp;gt; the correct phase, or do I have to do some tricks ? &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I am not sure.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Best,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Chen &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; "Ken Garrard" &amp;lt;ken_garrardAT@ncsuDOT.edu&amp;gt; wrote in &lt;br&gt;
message &lt;br&gt;
&amp;gt; &amp;lt;fvg2pb$73p$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; "Chen Sagiv" &amp;lt;chensagivron@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;gt; &amp;lt;fvfs3j$bnf$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Hi all,&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; We all know that the DFT of a square pulse is a sinc. &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Let's try it:&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; % First we define the square pulse&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; x=-1:0.01:1;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; f=zeros(size(x));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; f(x&amp;gt;-0.5 &amp; x&amp;lt;0.5)=1;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; figure ; plot(x,f); &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; % Now let's view its Fourier transform:&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; figure ; plot(real(fftshift(fft(f))));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; figure ; plot(imag(fftshift(fft(f))));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; % Hey, the imaginary part is NOT zero&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; % a remedy: &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; figure ; plot(real(fftshift(fft(ifftshift(f)))));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; figure ; plot(imag(fftshift(fft(ifftshift(f)))));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; So, it seems that Matlab does correct FFT to signals &lt;br&gt;
&amp;gt; that &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; are not centered about 0, but we have to ifftshift &lt;br&gt;
them &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; first. &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Is that a known "feature" of Matlab ? I will be glad &lt;br&gt;
to &lt;br&gt;
&amp;gt; &amp;gt; get &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; a clarification ! &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Best,&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Chen Sagiv&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Chen,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Matlab does do the FFT and IFFT correctly.  The problem &lt;br&gt;
&amp;gt; &amp;gt; with your example is that your square pulse is not &lt;br&gt;
&amp;gt; &amp;gt; perfectly symmetric.  It is symmetric around zero but &lt;br&gt;
&amp;gt; since &lt;br&gt;
&amp;gt; &amp;gt; the total number of data points is odd, there has to be &lt;br&gt;
&amp;gt; one &lt;br&gt;
&amp;gt; &amp;gt; more point at -1 than +1 or the other way around.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Try generating data from -1 to (+1-stepsize) as follows,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; ---&lt;br&gt;
&amp;gt; &amp;gt; % First we define the square pulse&lt;br&gt;
&amp;gt; &amp;gt; x=-1:0.01:(1-0.01);      % NOTE the change.&lt;br&gt;
&amp;gt; &amp;gt; f=zeros(size(x));&lt;br&gt;
&amp;gt; &amp;gt; f(x&amp;gt;-0.5 &amp; x&amp;lt;0.5)=1;&lt;br&gt;
&amp;gt; &amp;gt; figure ; plot(x,f); &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; % Now let's view its Fourier transform:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; figure ; plot(real(fftshift(fft(f))));&lt;br&gt;
&amp;gt; &amp;gt; figure ; plot(imag(fftshift(fft(f))));&lt;br&gt;
&amp;gt; &amp;gt; ---&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Now the imaginary parts are very close to zero.  The &lt;br&gt;
same &lt;br&gt;
&amp;gt; &amp;gt; sort of behaviour happens with a sine wave that starts &lt;br&gt;
at &lt;br&gt;
&amp;gt; &amp;gt; zero and ends at zero; you don't get the expected &lt;br&gt;
result &lt;br&gt;
&amp;gt; &amp;gt; because the signal has a one point "glitch" if you &lt;br&gt;
repeat &lt;br&gt;
&amp;gt; &amp;gt; it to + and - infinity.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Ken&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 02 May 2008 23:08:02 -0400</pubDate>
      <title>Re: Does Matlab do FFT correctly ?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168642#430026</link>
      <author>Ken Garrard</author>
      <description>Chen,&lt;br&gt;
&lt;br&gt;
There are many little tricks that you can play with real &lt;br&gt;
data that may help with your analysis.  The best one I know &lt;br&gt;
of it to use your intuition about your problem and the &lt;br&gt;
answer that you expect to get. And there are several things &lt;br&gt;
that I would recommend trying.&lt;br&gt;
1) Look at your data carefully before doing the FFT.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;a) Center your data about its mean value&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;b) Level the data to remove any drift&lt;br&gt;
2) Try using windowing functions.&lt;br&gt;
3) Check out www.dspguide.com for lots of practical &lt;br&gt;
information on a wide variety of problems.&lt;br&gt;
4) Sometimes it's helpful to do an autocovariance to see &lt;br&gt;
peaks in terms of lag length (back to time and space) &lt;br&gt;
instead of frequency.&lt;br&gt;
5) Do exactly what you're doing - run simple examples for &lt;br&gt;
which you know what the answer is supposed to be and &lt;br&gt;
resolve any descrepancy.  In this case the input data &lt;br&gt;
wasn't exactly what it was supposed to be.&lt;br&gt;
&lt;br&gt;
Since you asked about phase, one trick that I've found &lt;br&gt;
useful is to apply a simple threshold filter to the result &lt;br&gt;
returned by the FFT before proceeding with a phase angle &lt;br&gt;
calculation or unwrapping process.  This can also clear up &lt;br&gt;
problems when doing an inverse transform and you expect a &lt;br&gt;
real result and get a complex one.&lt;br&gt;
&lt;br&gt;
A post of mine about this from a couple of years ago is &lt;br&gt;
quoted below. &lt;br&gt;
&lt;br&gt;
------------------------&lt;br&gt;
I usually filter the real and imaginary parts of the &lt;br&gt;
complex return vector after an FFT to force values below a &lt;br&gt;
threshold to be exact zeros. Obviously these small values &lt;br&gt;
have little effect on the magnitude of a signal, but they &lt;br&gt;
can really foul up phase calculation. &lt;br&gt;
&lt;br&gt;
For example, generating one period of a sine wave with 100 &lt;br&gt;
samples.  (The last point is not a duplicate of the first) &lt;br&gt;
&lt;br&gt;
&amp;nbsp;t=0:0.01:1-.01; &lt;br&gt;
&amp;nbsp;s=sin(2*pi*t); &lt;br&gt;
&lt;br&gt;
Then calculate &lt;br&gt;
&amp;nbsp;f=fft(s); &lt;br&gt;
&amp;nbsp;m=abs(f); &lt;br&gt;
&amp;nbsp;p=unwrap(angle(f)); &lt;br&gt;
&lt;br&gt;
You would expect the only nonzero values in m to be at &lt;br&gt;
indices 2 and 100 and that they would be 50.0. The latter &lt;br&gt;
is the case; however none of the other values in m are &lt;br&gt;
exactly zero. &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;f(2) is -1.110223024625157e-015-50i &lt;br&gt;
&amp;nbsp;&amp;nbsp;f(end) is -1.110223024625157e-015+50i &lt;br&gt;
&lt;br&gt;
and for example, &lt;br&gt;
&amp;nbsp;&amp;nbsp;f(33) is -1.070083830450843e-015 -6.598918101450678e-016i &lt;br&gt;
&amp;nbsp;&amp;nbsp;m(33) is 1.257193941000705e-015 &lt;br&gt;
&lt;br&gt;
This usually isn't a problem, although I guess it depends &lt;br&gt;
on what you do next with the f or m vectors. &lt;br&gt;
&lt;br&gt;
But the phase angles don't make much sense, wrapped or &lt;br&gt;
unwrapped, &lt;br&gt;
&amp;nbsp;&amp;nbsp;eg, p(33) is 28.82692282385713 &lt;br&gt;
&lt;br&gt;
Again, I would expect a perfect sine wave to only have two &lt;br&gt;
non-zero phase values, p(2) equal to -1.57079632679490 and &lt;br&gt;
p(end) is equal to -p(2). The problem is that the inverse &lt;br&gt;
tangent of a ratio is nonsense if one or both of the values &lt;br&gt;
is "noise" or roundoff from a previous calculation. So we &lt;br&gt;
get (in radians), &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p(2) is 4.71238898038469 &lt;br&gt;
and p(end) is 51.83627878423159 &lt;br&gt;
&lt;br&gt;
If the unwrap is left out you get "noise" between pi and -&lt;br&gt;
pi. But if you apply a threshold filter to the result of &lt;br&gt;
the FFT then the phase angles are as expected. Setting &lt;br&gt;
threshold to an appropriate value say 1e-10, one way to do &lt;br&gt;
this is, &lt;br&gt;
&lt;br&gt;
function ff=cplxf(f,threshold) &lt;br&gt;
&amp;nbsp;fr = real(f); &lt;br&gt;
&amp;nbsp;fi = imag(f); &lt;br&gt;
&amp;nbsp;irz = find(abs(fr)&amp;lt;threshold); &lt;br&gt;
&amp;nbsp;iiz = find(abs(fi)&amp;lt;threshold); &lt;br&gt;
&amp;nbsp;fr(irz) = 0; &lt;br&gt;
&amp;nbsp;fi(iiz) = 0; &lt;br&gt;
&amp;nbsp;ff = fr + i*fi; &lt;br&gt;
&lt;br&gt;
Then, &lt;br&gt;
&amp;nbsp;f=fft(s); &lt;br&gt;
&amp;nbsp;ff=cplxf(f,1e-10); &lt;br&gt;
&amp;nbsp;m=abs(ff); &lt;br&gt;
&amp;nbsp;p=unwrap(angle(ff)); &lt;br&gt;
&lt;br&gt;
Now, &lt;br&gt;
&amp;nbsp;m(2) and m(end) are 50 &lt;br&gt;
&amp;nbsp;all other values in m are zero &lt;br&gt;
&amp;nbsp;p(2) is -1.57079632679490 &lt;br&gt;
&amp;nbsp;p(end) is 1.57079632679490 &lt;br&gt;
&amp;nbsp;and all other values in p are zero&lt;br&gt;
----------------------------------&lt;br&gt;
&lt;br&gt;
I hope this helps and good luck.&lt;br&gt;
Regards,&lt;br&gt;
&lt;br&gt;
Ken Garrard&lt;br&gt;
North Carolina State University&lt;br&gt;
&lt;br&gt;
"Chen Sagiv" &amp;lt;chensagivron@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;fvg3pa$slk$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Dear Ken,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks for your friendly answer. &lt;br&gt;
&amp;gt; I am wondering what are the implications of your input on &lt;br&gt;
&amp;gt; real life signals and images. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I am looking at the Fourier phase of signals and images. &lt;br&gt;
&amp;gt; Now, when I calculate the fft of say, an images, will I &lt;br&gt;
get &lt;br&gt;
&amp;gt; the correct phase, or do I have to do some tricks ? &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I am not sure.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Best,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Chen &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; "Ken Garrard" &amp;lt;ken_garrardAT@ncsuDOT.edu&amp;gt; wrote in &lt;br&gt;
message &lt;br&gt;
&amp;gt; &amp;lt;fvg2pb$73p$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; "Chen Sagiv" &amp;lt;chensagivron@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;gt; &amp;lt;fvfs3j$bnf$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Hi all,&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; We all know that the DFT of a square pulse is a sinc. &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Let's try it:&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; % First we define the square pulse&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; x=-1:0.01:1;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; f=zeros(size(x));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; f(x&amp;gt;-0.5 &amp; x&amp;lt;0.5)=1;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; figure ; plot(x,f); &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; % Now let's view its Fourier transform:&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; figure ; plot(real(fftshift(fft(f))));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; figure ; plot(imag(fftshift(fft(f))));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; % Hey, the imaginary part is NOT zero&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; % a remedy: &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; figure ; plot(real(fftshift(fft(ifftshift(f)))));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; figure ; plot(imag(fftshift(fft(ifftshift(f)))));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; So, it seems that Matlab does correct FFT to signals &lt;br&gt;
&amp;gt; that &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; are not centered about 0, but we have to ifftshift &lt;br&gt;
them &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; first. &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Is that a known "feature" of Matlab ? I will be glad &lt;br&gt;
to &lt;br&gt;
&amp;gt; &amp;gt; get &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; a clarification ! &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Best,&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Chen Sagiv&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Chen,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Matlab does do the FFT and IFFT correctly.  The problem &lt;br&gt;
&amp;gt; &amp;gt; with your example is that your square pulse is not &lt;br&gt;
&amp;gt; &amp;gt; perfectly symmetric.  It is symmetric around zero but &lt;br&gt;
&amp;gt; since &lt;br&gt;
&amp;gt; &amp;gt; the total number of data points is odd, there has to be &lt;br&gt;
&amp;gt; one &lt;br&gt;
&amp;gt; &amp;gt; more point at -1 than +1 or the other way around.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Try generating data from -1 to (+1-stepsize) as follows,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; ---&lt;br&gt;
&amp;gt; &amp;gt; % First we define the square pulse&lt;br&gt;
&amp;gt; &amp;gt; x=-1:0.01:(1-0.01);      % NOTE the change.&lt;br&gt;
&amp;gt; &amp;gt; f=zeros(size(x));&lt;br&gt;
&amp;gt; &amp;gt; f(x&amp;gt;-0.5 &amp; x&amp;lt;0.5)=1;&lt;br&gt;
&amp;gt; &amp;gt; figure ; plot(x,f); &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; % Now let's view its Fourier transform:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; figure ; plot(real(fftshift(fft(f))));&lt;br&gt;
&amp;gt; &amp;gt; figure ; plot(imag(fftshift(fft(f))));&lt;br&gt;
&amp;gt; &amp;gt; ---&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Now the imaginary parts are very close to zero.  The &lt;br&gt;
same &lt;br&gt;
&amp;gt; &amp;gt; sort of behaviour happens with a sine wave that starts &lt;br&gt;
at &lt;br&gt;
&amp;gt; &amp;gt; zero and ends at zero; you don't get the expected &lt;br&gt;
result &lt;br&gt;
&amp;gt; &amp;gt; because the signal has a one point "glitch" if you &lt;br&gt;
repeat &lt;br&gt;
&amp;gt; &amp;gt; it to + and - infinity.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Ken&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Sat, 03 May 2008 04:06:31 -0400</pubDate>
      <title>Re: Does Matlab do FFT correctly ?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168642#430038</link>
      <author>Steven G. Johnson</author>
      <description>On May 2, 6:15 pm, "Chen Sagiv" &amp;lt;chensagiv...@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; Thanks for your friendly answer.&lt;br&gt;
&amp;gt; I am wondering what are the implications of your input on&lt;br&gt;
&amp;gt; real life signals and images.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I am looking at the Fourier phase of signals and images.&lt;br&gt;
&amp;gt; Now, when I calculate the fft of say, an images, will I get&lt;br&gt;
&amp;gt; the correct phase, or do I have to do some tricks ?&lt;br&gt;
&lt;br&gt;
Your question is ill-defined, because it depends on what you mean by&lt;br&gt;
"correct".  From a certain point of view, the "correct" thing is what&lt;br&gt;
Matlab does.&lt;br&gt;
&lt;br&gt;
I think you mean, "will I get the phase corresponding to the origin at&lt;br&gt;
the center of the image" and the answer is no, of course: you will get&lt;br&gt;
the phase corresponding to the origin at the corner, corresponding to&lt;br&gt;
the DFT definition, unless you use ifftshift first.&lt;br&gt;
&lt;br&gt;
In real life, in any real application, it is easy to deal consistently&lt;br&gt;
with the origin corresponding to the DFT definition, and you are never&lt;br&gt;
in practice forced to use ifftshift (unless you want to).  But, as&lt;br&gt;
with all thing in numerical calculation, whenever you use a black box&lt;br&gt;
like fft(...), you don't need to know *how* it computes what it does&lt;br&gt;
but you had better understaht *what* it is computing.  To do any kind&lt;br&gt;
of discrete signal processing with FFTs, you have to understand the&lt;br&gt;
DFT and how it differs from the continuous Fourier transform.&lt;br&gt;
&lt;br&gt;
Regards,&lt;br&gt;
Steven G. Johnson&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Sat, 03 May 2008 04:14:29 -0400</pubDate>
      <title>Re: Does Matlab do FFT correctly ?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168642#430040</link>
      <author>Steven G. Johnson</author>
      <description>On May 2, 6:09 pm, "Chen Sagiv" &amp;lt;chensagiv...@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; Nevertheless, I will have to argue that this point is not&lt;br&gt;
&amp;gt; clear at all. Most people refer only to the abs of the FT&lt;br&gt;
&amp;gt; anyway, and this problem will simply go un-noticed. Only&lt;br&gt;
&amp;gt; when phase is of interest, you really look at this subtle&lt;br&gt;
&amp;gt; point.&lt;br&gt;
&lt;br&gt;
Oh, absolutely.  Fourier transforms are subtle to begin with, and then&lt;br&gt;
when you add discretization etc. on top of that it is a tricky&lt;br&gt;
subject.  Lots of students find it difficult, and digital signal&lt;br&gt;
processing and DFTs are usually a relatively advanced subject in an&lt;br&gt;
university curriculum.  And it's easy to find people online who are&lt;br&gt;
confused about this topic.&lt;br&gt;
&lt;br&gt;
However, it's not Matlab's fault, and I don't think there's much that&lt;br&gt;
they can do to eliminate the subtleties of this subject.&lt;br&gt;
&lt;br&gt;
(If you think they should shift the origin of the FFT to the center of&lt;br&gt;
the array for you, then think again.  First, doing so would make them&lt;br&gt;
incompatible with the universal convention of textbooks, papers, etc.&lt;br&gt;
on this subject.  Second, it's not a trivial matter to "center" the&lt;br&gt;
origin, since the location of the "center" pixel is ambiguous for even&lt;br&gt;
N; this makes the ifftshift function more tricky to use properly than&lt;br&gt;
it may seem.  Third, it makes the DFT formula more complicated-&lt;br&gt;
looking.  Fourth, the location of the origin is really the least of&lt;br&gt;
the subtleties of the DFT.)&lt;br&gt;
&lt;br&gt;
Regards,&lt;br&gt;
Steven G. Johnson&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Sat, 03 May 2008 04:29:02 -0400</pubDate>
      <title>Re: Does Matlab do FFT correctly ?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168642#430041</link>
      <author>Chen Sagiv</author>
      <description>Dear Ken,&lt;br&gt;
&lt;br&gt;
Thanks for your detailed and clear answer. &lt;br&gt;
I will certainly use your input. &lt;br&gt;
&lt;br&gt;
Best,&lt;br&gt;
&lt;br&gt;
Chen Sagiv&lt;br&gt;
&lt;br&gt;
"Ken Garrard" &amp;lt;ken_garrardAT@ncsuDOT.edu&amp;gt; wrote in message &lt;br&gt;
&amp;lt;fvg6sh$6nq$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Chen,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; There are many little tricks that you can play with real &lt;br&gt;
&amp;gt; data that may help with your analysis.  The best one I &lt;br&gt;
know &lt;br&gt;
&amp;gt; of it to use your intuition about your problem and the &lt;br&gt;
&amp;gt; answer that you expect to get. And there are several &lt;br&gt;
things &lt;br&gt;
&amp;gt; that I would recommend trying.&lt;br&gt;
&amp;gt; 1) Look at your data carefully before doing the FFT.&lt;br&gt;
&amp;gt;    a) Center your data about its mean value&lt;br&gt;
&amp;gt;    b) Level the data to remove any drift&lt;br&gt;
&amp;gt; 2) Try using windowing functions.&lt;br&gt;
&amp;gt; 3) Check out www.dspguide.com for lots of practical &lt;br&gt;
&amp;gt; information on a wide variety of problems.&lt;br&gt;
&amp;gt; 4) Sometimes it's helpful to do an autocovariance to see &lt;br&gt;
&amp;gt; peaks in terms of lag length (back to time and space) &lt;br&gt;
&amp;gt; instead of frequency.&lt;br&gt;
&amp;gt; 5) Do exactly what you're doing - run simple examples for &lt;br&gt;
&amp;gt; which you know what the answer is supposed to be and &lt;br&gt;
&amp;gt; resolve any descrepancy.  In this case the input data &lt;br&gt;
&amp;gt; wasn't exactly what it was supposed to be.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Since you asked about phase, one trick that I've found &lt;br&gt;
&amp;gt; useful is to apply a simple threshold filter to the &lt;br&gt;
result &lt;br&gt;
&amp;gt; returned by the FFT before proceeding with a phase angle &lt;br&gt;
&amp;gt; calculation or unwrapping process.  This can also clear &lt;br&gt;
up &lt;br&gt;
&amp;gt; problems when doing an inverse transform and you expect a &lt;br&gt;
&amp;gt; real result and get a complex one.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A post of mine about this from a couple of years ago is &lt;br&gt;
&amp;gt; quoted below. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ------------------------&lt;br&gt;
&amp;gt; I usually filter the real and imaginary parts of the &lt;br&gt;
&amp;gt; complex return vector after an FFT to force values below &lt;br&gt;
a &lt;br&gt;
&amp;gt; threshold to be exact zeros. Obviously these small values &lt;br&gt;
&amp;gt; have little effect on the magnitude of a signal, but they &lt;br&gt;
&amp;gt; can really foul up phase calculation. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; For example, generating one period of a sine wave with &lt;br&gt;
100 &lt;br&gt;
&amp;gt; samples.  (The last point is not a duplicate of the &lt;br&gt;
first) &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  t=0:0.01:1-.01; &lt;br&gt;
&amp;gt;  s=sin(2*pi*t); &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Then calculate &lt;br&gt;
&amp;gt;  f=fft(s); &lt;br&gt;
&amp;gt;  m=abs(f); &lt;br&gt;
&amp;gt;  p=unwrap(angle(f)); &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; You would expect the only nonzero values in m to be at &lt;br&gt;
&amp;gt; indices 2 and 100 and that they would be 50.0. The latter &lt;br&gt;
&amp;gt; is the case; however none of the other values in m are &lt;br&gt;
&amp;gt; exactly zero. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   f(2) is -1.110223024625157e-015-50i &lt;br&gt;
&amp;gt;   f(end) is -1.110223024625157e-015+50i &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; and for example, &lt;br&gt;
&amp;gt;   f(33) is -1.070083830450843e-015 -6.598918101450678e-&lt;br&gt;
016i &lt;br&gt;
&amp;gt;   m(33) is 1.257193941000705e-015 &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This usually isn't a problem, although I guess it depends &lt;br&gt;
&amp;gt; on what you do next with the f or m vectors. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; But the phase angles don't make much sense, wrapped or &lt;br&gt;
&amp;gt; unwrapped, &lt;br&gt;
&amp;gt;   eg, p(33) is 28.82692282385713 &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Again, I would expect a perfect sine wave to only have &lt;br&gt;
two &lt;br&gt;
&amp;gt; non-zero phase values, p(2) equal to -1.57079632679490 &lt;br&gt;
and &lt;br&gt;
&amp;gt; p(end) is equal to -p(2). The problem is that the inverse &lt;br&gt;
&amp;gt; tangent of a ratio is nonsense if one or both of the &lt;br&gt;
values &lt;br&gt;
&amp;gt; is "noise" or roundoff from a previous calculation. So we &lt;br&gt;
&amp;gt; get (in radians), &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     p(2) is 4.71238898038469 &lt;br&gt;
&amp;gt; and p(end) is 51.83627878423159 &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If the unwrap is left out you get "noise" between pi and -&lt;br&gt;
&amp;gt; pi. But if you apply a threshold filter to the result of &lt;br&gt;
&amp;gt; the FFT then the phase angles are as expected. Setting &lt;br&gt;
&amp;gt; threshold to an appropriate value say 1e-10, one way to &lt;br&gt;
do &lt;br&gt;
&amp;gt; this is, &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; function ff=cplxf(f,threshold) &lt;br&gt;
&amp;gt;  fr = real(f); &lt;br&gt;
&amp;gt;  fi = imag(f); &lt;br&gt;
&amp;gt;  irz = find(abs(fr)&amp;lt;threshold); &lt;br&gt;
&amp;gt;  iiz = find(abs(fi)&amp;lt;threshold); &lt;br&gt;
&amp;gt;  fr(irz) = 0; &lt;br&gt;
&amp;gt;  fi(iiz) = 0; &lt;br&gt;
&amp;gt;  ff = fr + i*fi; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Then, &lt;br&gt;
&amp;gt;  f=fft(s); &lt;br&gt;
&amp;gt;  ff=cplxf(f,1e-10); &lt;br&gt;
&amp;gt;  m=abs(ff); &lt;br&gt;
&amp;gt;  p=unwrap(angle(ff)); &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Now, &lt;br&gt;
&amp;gt;  m(2) and m(end) are 50 &lt;br&gt;
&amp;gt;  all other values in m are zero &lt;br&gt;
&amp;gt;  p(2) is -1.57079632679490 &lt;br&gt;
&amp;gt;  p(end) is 1.57079632679490 &lt;br&gt;
&amp;gt;  and all other values in p are zero&lt;br&gt;
&amp;gt; ----------------------------------&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I hope this helps and good luck.&lt;br&gt;
&amp;gt; Regards,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Ken Garrard&lt;br&gt;
&amp;gt; North Carolina State University&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; "Chen Sagiv" &amp;lt;chensagivron@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;lt;fvg3pa$slk$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Dear Ken,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Thanks for your friendly answer. &lt;br&gt;
&amp;gt; &amp;gt; I am wondering what are the implications of your input &lt;br&gt;
on &lt;br&gt;
&amp;gt; &amp;gt; real life signals and images. &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I am looking at the Fourier phase of signals and &lt;br&gt;
images. &lt;br&gt;
&amp;gt; &amp;gt; Now, when I calculate the fft of say, an images, will I &lt;br&gt;
&amp;gt; get &lt;br&gt;
&amp;gt; &amp;gt; the correct phase, or do I have to do some tricks ? &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I am not sure.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Best,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Chen &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; "Ken Garrard" &amp;lt;ken_garrardAT@ncsuDOT.edu&amp;gt; wrote in &lt;br&gt;
&amp;gt; message &lt;br&gt;
&amp;gt; &amp;gt; &amp;lt;fvg2pb$73p$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; "Chen Sagiv" &amp;lt;chensagivron@gmail.com&amp;gt; wrote in &lt;br&gt;
message &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;lt;fvfs3j$bnf$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; Hi all,&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; We all know that the DFT of a square pulse is a &lt;br&gt;
sinc. &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; Let's try it:&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; % First we define the square pulse&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; x=-1:0.01:1;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; f=zeros(size(x));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; f(x&amp;gt;-0.5 &amp; x&amp;lt;0.5)=1;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; figure ; plot(x,f); &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; % Now let's view its Fourier transform:&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; figure ; plot(real(fftshift(fft(f))));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; figure ; plot(imag(fftshift(fft(f))));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; % Hey, the imaginary part is NOT zero&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; % a remedy: &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; figure ; plot(real(fftshift(fft(ifftshift(f)))));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; figure ; plot(imag(fftshift(fft(ifftshift(f)))));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; So, it seems that Matlab does correct FFT to &lt;br&gt;
signals &lt;br&gt;
&amp;gt; &amp;gt; that &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; are not centered about 0, but we have to ifftshift &lt;br&gt;
&amp;gt; them &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; first. &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; Is that a known "feature" of Matlab ? I will be &lt;br&gt;
glad &lt;br&gt;
&amp;gt; to &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; get &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; a clarification ! &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; Best,&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; Chen Sagiv&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Chen,&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Matlab does do the FFT and IFFT correctly.  The &lt;br&gt;
problem &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; with your example is that your square pulse is not &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; perfectly symmetric.  It is symmetric around zero but &lt;br&gt;
&amp;gt; &amp;gt; since &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; the total number of data points is odd, there has to &lt;br&gt;
be &lt;br&gt;
&amp;gt; &amp;gt; one &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; more point at -1 than +1 or the other way around.&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Try generating data from -1 to (+1-stepsize) as &lt;br&gt;
follows,&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; ---&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; % First we define the square pulse&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; x=-1:0.01:(1-0.01);      % NOTE the change.&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; f=zeros(size(x));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; f(x&amp;gt;-0.5 &amp; x&amp;lt;0.5)=1;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; figure ; plot(x,f); &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; % Now let's view its Fourier transform:&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; figure ; plot(real(fftshift(fft(f))));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; figure ; plot(imag(fftshift(fft(f))));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; ---&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Now the imaginary parts are very close to zero.  The &lt;br&gt;
&amp;gt; same &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; sort of behaviour happens with a sine wave that &lt;br&gt;
starts &lt;br&gt;
&amp;gt; at &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; zero and ends at zero; you don't get the expected &lt;br&gt;
&amp;gt; result &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; because the signal has a one point "glitch" if you &lt;br&gt;
&amp;gt; repeat &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; it to + and - infinity.&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Ken&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Sat, 03 May 2008 04:34:03 -0400</pubDate>
      <title>Re: Does Matlab do FFT correctly ?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168642#430043</link>
      <author>Chen Sagiv</author>
      <description>Dear Steven,&lt;br&gt;
&lt;br&gt;
Thanks for your answer.&lt;br&gt;
&lt;br&gt;
Best,&lt;br&gt;
&lt;br&gt;
Chen &lt;br&gt;
&lt;br&gt;
"Steven G. Johnson" &amp;lt;stevenj@alum.mit.edu&amp;gt; wrote in message &lt;br&gt;
&amp;lt;da5827de-b6d8-4f66-9943-&lt;br&gt;
86aa762b2175@8g2000hse.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; On May 2, 6:15 pm, "Chen Sagiv" &amp;lt;chensagiv...@gmail.com&amp;gt; &lt;br&gt;
wrote:&lt;br&gt;
&amp;gt; &amp;gt; Thanks for your friendly answer.&lt;br&gt;
&amp;gt; &amp;gt; I am wondering what are the implications of your input &lt;br&gt;
on&lt;br&gt;
&amp;gt; &amp;gt; real life signals and images.&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; I am looking at the Fourier phase of signals and images.&lt;br&gt;
&amp;gt; &amp;gt; Now, when I calculate the fft of say, an images, will I &lt;br&gt;
get&lt;br&gt;
&amp;gt; &amp;gt; the correct phase, or do I have to do some tricks ?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Your question is ill-defined, because it depends on what &lt;br&gt;
you mean by&lt;br&gt;
&amp;gt; "correct".  From a certain point of view, the "correct" &lt;br&gt;
thing is what&lt;br&gt;
&amp;gt; Matlab does.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I think you mean, "will I get the phase corresponding to &lt;br&gt;
the origin at&lt;br&gt;
&amp;gt; the center of the image" and the answer is no, of course: &lt;br&gt;
you will get&lt;br&gt;
&amp;gt; the phase corresponding to the origin at the corner, &lt;br&gt;
corresponding to&lt;br&gt;
&amp;gt; the DFT definition, unless you use ifftshift first.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; In real life, in any real application, it is easy to deal &lt;br&gt;
consistently&lt;br&gt;
&amp;gt; with the origin corresponding to the DFT definition, and &lt;br&gt;
you are never&lt;br&gt;
&amp;gt; in practice forced to use ifftshift (unless you want &lt;br&gt;
to).  But, as&lt;br&gt;
&amp;gt; with all thing in numerical calculation, whenever you use &lt;br&gt;
a black box&lt;br&gt;
&amp;gt; like fft(...), you don't need to know *how* it computes &lt;br&gt;
what it does&lt;br&gt;
&amp;gt; but you had better understaht *what* it is computing.  To &lt;br&gt;
do any kind&lt;br&gt;
&amp;gt; of discrete signal processing with FFTs, you have to &lt;br&gt;
understand the&lt;br&gt;
&amp;gt; DFT and how it differs from the continuous Fourier &lt;br&gt;
transform.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Regards,&lt;br&gt;
&amp;gt; Steven G. Johnson&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Sat, 03 May 2008 04:39:03 -0400</pubDate>
      <title>Re: Does Matlab do FFT correctly ?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168642#430044</link>
      <author>Chen Sagiv</author>
      <description>Hi again,&lt;br&gt;
&lt;br&gt;
I am not here to blame Matlab for anything, but to &lt;br&gt;
understand better what Matlab is doing. &lt;br&gt;
&lt;br&gt;
I think that a short white paper would do the work. If you &lt;br&gt;
know one, I will be happy to get a pointer. If not, I will &lt;br&gt;
try to find the time to write something up. &lt;br&gt;
&lt;br&gt;
In any case, I am very happy with the fruitful discussion &lt;br&gt;
that took place here, and I certainly learnt quite a lot. &lt;br&gt;
&lt;br&gt;
Best and thanks for your efforts. &lt;br&gt;
&lt;br&gt;
Chen &lt;br&gt;
&lt;br&gt;
"Steven G. Johnson" &amp;lt;stevenj@alum.mit.edu&amp;gt; wrote in message &lt;br&gt;
&amp;lt;3d638b38-6307-4a73-b305-&lt;br&gt;
b1eda3ebbbbd@34g2000hsh.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; On May 2, 6:09 pm, "Chen Sagiv" &amp;lt;chensagiv...@gmail.com&amp;gt; &lt;br&gt;
wrote:&lt;br&gt;
&amp;gt; &amp;gt; Nevertheless, I will have to argue that this point is &lt;br&gt;
not&lt;br&gt;
&amp;gt; &amp;gt; clear at all. Most people refer only to the abs of the &lt;br&gt;
FT&lt;br&gt;
&amp;gt; &amp;gt; anyway, and this problem will simply go un-noticed. Only&lt;br&gt;
&amp;gt; &amp;gt; when phase is of interest, you really look at this &lt;br&gt;
subtle&lt;br&gt;
&amp;gt; &amp;gt; point.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Oh, absolutely.  Fourier transforms are subtle to begin &lt;br&gt;
with, and then&lt;br&gt;
&amp;gt; when you add discretization etc. on top of that it is a &lt;br&gt;
tricky&lt;br&gt;
&amp;gt; subject.  Lots of students find it difficult, and digital &lt;br&gt;
signal&lt;br&gt;
&amp;gt; processing and DFTs are usually a relatively advanced &lt;br&gt;
subject in an&lt;br&gt;
&amp;gt; university curriculum.  And it's easy to find people &lt;br&gt;
online who are&lt;br&gt;
&amp;gt; confused about this topic.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; However, it's not Matlab's fault, and I don't think &lt;br&gt;
there's much that&lt;br&gt;
&amp;gt; they can do to eliminate the subtleties of this subject.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; (If you think they should shift the origin of the FFT to &lt;br&gt;
the center of&lt;br&gt;
&amp;gt; the array for you, then think again.  First, doing so &lt;br&gt;
would make them&lt;br&gt;
&amp;gt; incompatible with the universal convention of textbooks, &lt;br&gt;
papers, etc.&lt;br&gt;
&amp;gt; on this subject.  Second, it's not a trivial matter &lt;br&gt;
to "center" the&lt;br&gt;
&amp;gt; origin, since the location of the "center" pixel is &lt;br&gt;
ambiguous for even&lt;br&gt;
&amp;gt; N; this makes the ifftshift function more tricky to use &lt;br&gt;
properly than&lt;br&gt;
&amp;gt; it may seem.  Third, it makes the DFT formula more &lt;br&gt;
complicated-&lt;br&gt;
&amp;gt; looking.  Fourth, the location of the origin is really &lt;br&gt;
the least of&lt;br&gt;
&amp;gt; the subtleties of the DFT.)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Regards,&lt;br&gt;
&amp;gt; Steven G. Johnson&lt;br&gt;
&lt;br&gt;
</description>
    </item>
  </channel>
</rss>
