{"id":333,"date":"2010-06-25T18:51:14","date_gmt":"2010-06-25T22:51:14","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2010\/06\/25\/plotting-the-dtft-using-the-output-of-fft\/"},"modified":"2019-10-29T13:30:02","modified_gmt":"2019-10-29T17:30:02","slug":"plotting-the-dtft-using-the-output-of-fft","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2010\/06\/25\/plotting-the-dtft-using-the-output-of-fft\/","title":{"rendered":"Plotting the DTFT using the output of fft"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>In my <a href=\"https:\/\/blogs.mathworks.com\/steve\/category\/fourier-transforms\/\">Fourier transform series<\/a> I've been trying to address some of the common points of confusion surrounding this topic.  For today's espisode I want to\r\n      look at how to use the <tt>fft<\/tt> function to produce discrete-time Fourier transform (DTFT) magnitude plots in the form you might see in a textbook. Recall\r\n      that the <tt>fft<\/tt> computes the discrete Fourier transform (DFT). I described the relationship between the DFT and the DTFT in my <a href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/03\/15\/the-dft-and-the-dtft\/\">March 15 post<\/a>.\r\n   <\/p>\r\n   <p>For my example I'll work with a sequence <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_eq17606.png\">  that equals 1 for <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_eq47361.png\">  and equals 0 elsewhere.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/rect[n].png\"> <\/p>\r\n   <p>Here's a plot of the DTFT magnitude of this sequence:<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/F_rect%5Bn%5D%20(1%20period).png\"> <\/p>\r\n   <p>Now let's see what get using <tt>fft<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x = ones(1, 5)<\/pre><pre style=\"font-style:oblique\">\r\nx =\r\n\r\n     1     1     1     1     1\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">X = fft(x);\r\nplot(abs(X))<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_01.png\"> <p>Wow, that's not anywhere close to the DTFT magnitude plot above.  And why does it look like it's only got two points?  Well,\r\n      take a look at the actual values of <tt>X<\/tt>:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">X<\/pre><pre style=\"font-style:oblique\">\r\nX =\r\n\r\n     5     0     0     0     0\r\n\r\n<\/pre><p>We have a 5 and four 0s. What's going on? I explained this back in my <a href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/03\/15\/the-dft-and-the-dtft\/\">March 15 post<\/a> when I discussed the relationship between the DFT and the DTFT.  The outputs of the DFT are samples of the DTFT, and in this\r\n      case the sample locations just happen to align with the locations of four zeros in the DTFT.\r\n   <\/p>\r\n   <p>You can get a finer sampling (and a much nicer-looking DTFT plot) by zero-padding.  Here I'll use the zero-padding syntax\r\n      of <tt>fft<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">N = 256;\r\nX = fft(x, N);\r\nplot(abs(X))<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_02.png\"> <p>That's a smoother-looking curve, but it still looks quite a bit different than the DTFT magnitude plot above.  To explain\r\n      the MATLAB output we're looking at, let me show a DTFT magnitude plot that shows three periods instead of just one.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/F_rect%5Bn%5D%20(3%20periods).png\"> <\/p>\r\n   <p>You can see that the output from MATLAB is one period of the DTFT, but it's not the period normally plotted, which is from\r\n      <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_eq11410.png\">  to <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_eq11731.png\"> . Instead, it's the period from 0 to <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_eq03041.png\"> . To get a plot from <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_eq11410.png\">  to <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_eq11731.png\"> , use the <tt>fftshift<\/tt> function.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">plot(abs(fftshift(X)))<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_03.png\"> <p>That leaves us with the question of labeling the frequency axis. We want a plot in radians from <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_eq11410.png\">  to <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_eq11731.png\"> .\r\n   <\/p>\r\n   <p>The way I always remember the frequency scaling between the DFT and the DTFT is this: the length of the DFT corresponds to\r\n      the frequency <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_eq03041.png\">  in the DTFT.\r\n   <\/p>\r\n   <p>So the frequencies in radians corresponding to the output elements of <tt>fft<\/tt> are:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">w = 2*pi * (0:(N-1)) \/ N;<\/pre><p>But we're calling <tt>fftshift<\/tt> to plot the magnitude of the DTFT, so we have to perform a similar shift on our frequencies:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">w2 = fftshift(w);\r\nplot(w2)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_04.png\"> <p>Now our frequencies start at <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_eq11731.png\">  and have a <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_eq03041.png\">  discontinuity in the middle. Here's one way to fix that up:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">w3 = unwrap(w2 - 2*pi);\r\nplot(w3)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_05.png\"> <p>Now we can redo our magnitude DTFT plot with the x-axis labels.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">plot(w3, abs(fftshift(X)))\r\nxlabel(<span style=\"color: #A020F0\">'radians'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_06.png\"> <p>Often I like to see the multiples of <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_eq11731.png\">  more clearly along the x-axis. One way to accomplish this is to normalize the frequency variable by <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_eq11731.png\"> .\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">plot(w3\/pi, abs(fftshift(X)))\r\nxlabel(<span style=\"color: #A020F0\">'radians \/ \\pi'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dtft_plots_1_07.png\"> <p>Another option is to try <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/64499\">Alan's<\/a> <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27010-pilabels\">pilabels<\/a> contribution on the MATLAB Central File Exchange.\r\n   <\/p>\r\n   <p>For the next time, I'm thinking of tackling the question of why the following output is complex:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">fft([1 2 3 2 1])<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n   9.0000            -2.1180 - 1.5388i   0.1180 + 0.3633i   0.1180 - 0.3633i  -2.1180 + 1.5388i\r\n\r\n<\/pre><p>Many people expect it to be real.<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_45e1a22c361c48a6b6b891d58684eb67() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='45e1a22c361c48a6b6b891d58684eb67 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 45e1a22c361c48a6b6b891d58684eb67';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Steve Eddins';\r\n        copyright = 'Copyright 2010 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_45e1a22c361c48a6b6b891d58684eb67()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.10<br><\/p>\r\n<\/div>\r\n<!--\r\n45e1a22c361c48a6b6b891d58684eb67 ##### SOURCE BEGIN #####\r\n%%\r\n% In my \r\n% <https:\/\/blogs.mathworks.com\/steve\/category\/fourier-transforms\/ \r\n% Fourier transform series> I've been trying to address some of the common\r\n% points of confusion surrounding this topic.  For today's espisode I want to\r\n% look at how to use the |fft| function to produce discrete-time\r\n% Fourier transform (DTFT) magnitude plots in the form you might see in a\r\n% textbook. Recall that the |fft| computes the discrete Fourier transform (DFT).\r\n% I described the relationship between the DFT and the DTFT \r\n% in my <https:\/\/blogs.mathworks.com\/steve\/2010\/03\/15\/the-dft-and-the-dtft\/ March\r\n% 15 post>. \r\n%\r\n% For my example I'll work with a sequence $x[n]$ that equals 1 for $-2 \\leq n\r\n% \\leq 2$ and equals 0 elsewhere.\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2010\/rect[n].png>>\r\n%\r\n% Here's a plot of the DTFT magnitude of this sequence:\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2010\/F_rect%5Bn%5D%20(1%20period).png>>\r\n%\r\n% Now let's see what get using |fft|.\r\n\r\nx = ones(1, 5)\r\n\r\n%%\r\n\r\nX = fft(x);\r\nplot(abs(X))\r\n\r\n%%\r\n% Wow, that's not anywhere close to the DTFT magnitude plot above.  And why does\r\n% it look like it's only got two points?  Well, take a look at the actual values\r\n% of |X|:\r\n\r\nX\r\n\r\n%%\r\n% We have a 5 and four 0s. What's going on? I explained this back in my \r\n% <https:\/\/blogs.mathworks.com\/steve\/2010\/03\/15\/the-dft-and-the-dtft\/ March 15\r\n% post> when I discussed the relationship between the DFT and the DTFT.  The\r\n% outputs of the DFT are samples of the DTFT, and in this case the sample\r\n% locations just happen to align with the locations of four zeros in the DTFT.\r\n%\r\n% You can get a finer sampling (and a much nicer-looking DTFT plot) by\r\n% zero-padding.  Here I'll use the zero-padding syntax of |fft|.\r\n\r\nN = 256;\r\nX = fft(x, N);\r\nplot(abs(X))\r\n\r\n%%\r\n% That's a smoother-looking curve, but it still looks quite a bit different than\r\n% the DTFT magnitude plot above.  To explain the MATLAB output we're looking\r\n% at, let me show a DTFT magnitude plot that shows three periods instead of just\r\n% one.\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2010\/F_rect%5Bn%5D%20(3%20periods).png>>\r\n%\r\n% You can see that the output from MATLAB is one period of the DTFT, but it's\r\n% not the period normally plotted, which is from $-\\pi$ to $\\pi$. Instead, it's\r\n% the period from 0 to $2\\pi$. To get a plot from $-\\pi$ to $\\pi$, use the\r\n% |fftshift| function.\r\n\r\nplot(abs(fftshift(X)))\r\n\r\n%%\r\n% That leaves us with the question of labeling the frequency axis. We want a\r\n% plot in radians from $-\\pi$ to $\\pi$.\r\n%\r\n% The way I always remember the frequency scaling between the DFT and the DTFT\r\n% is this: the length of the DFT corresponds to the frequency $2\\pi$ in the\r\n% DTFT.\r\n%\r\n% So the frequencies in radians corresponding to the output elements of |fft|\r\n% are:\r\n\r\nw = 2*pi * (0:(N-1)) \/ N;\r\n\r\n%%\r\n% But we're calling |fftshift| to plot the magnitude of the DTFT, so we have to\r\n% perform a similar shift on our frequencies:\r\n\r\nw2 = fftshift(w);\r\nplot(w2)\r\n\r\n%%\r\n% Now our frequencies start at $\\pi$ and have a $2\\pi$ discontinuity in the\r\n% middle. Here's one way to fix that up:\r\n\r\nw3 = unwrap(w2 - 2*pi);\r\nplot(w3)\r\n\r\n%%\r\n% Now we can redo our magnitude DTFT plot with the x-axis labels.\r\n\r\nplot(w3, abs(fftshift(X)))\r\nxlabel('radians')\r\n\r\n%%\r\n% Often I like to see the multiples of $\\pi$ more clearly along the x-axis. One\r\n% way to accomplish this is to normalize the frequency variable by $\\pi$.\r\n\r\nplot(w3\/pi, abs(fftshift(X)))\r\nxlabel('radians \/ \\pi')\r\n\r\n%%\r\n% Another option is to try \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/64499 Alan's> \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27010-pilabels \r\n% pilabels> contribution on the MATLAB Central\r\n% File Exchange.\r\n%\r\n% For the next time, I'm thinking of tackling the question of why the following\r\n% output is complex:\r\n\r\nfft([1 2 3 2 1])\r\n\r\n%%\r\n% Many people expect it to be real.\r\n##### SOURCE END ##### 45e1a22c361c48a6b6b891d58684eb67\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   In my Fourier transform series I've been trying to address some of the common points of confusion surrounding this topic.  For today's espisode I want to\r\n      look at how to use the fft... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/06\/25\/plotting-the-dtft-using-the-output-of-fft\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[208,426,400,288,532,68,701,94],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/333"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=333"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/333\/revisions"}],"predecessor-version":[{"id":3685,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/333\/revisions\/3685"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}