{"id":317,"date":"2010-03-15T17:59:21","date_gmt":"2010-03-15T17:59:21","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2010\/03\/15\/the-dft-and-the-dtft\/"},"modified":"2010-03-15T17:59:21","modified_gmt":"2010-03-15T17:59:21","slug":"the-dft-and-the-dtft","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2010\/03\/15\/the-dft-and-the-dtft\/","title":{"rendered":"The DFT and the DTFT"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>It's finally time to start looking at the relationship between the discrete Fourier transform (DFT) and the discrete-time\r\n      Fourier transform (DTFT). Let's look at a simple rectangular pulse, <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_eq33869.png\">  for <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_eq75061.png\"> . The DTFT of <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_eq17606.png\">  is:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_eq44745.png\"> <\/p>\r\n   <p>Let's plot <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_eq28803.png\">  for <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_eq66486.png\">  over a couple of periods:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">M = 8;\r\nw = linspace(-2*pi, 2*pi, 800);\r\nX_dtft = (sin(w*M\/2) .\/ sin(w\/2)) .* exp(-1j * w * (M-1) \/ 2);\r\nplot(w, abs(X_dtft))\r\ntitle(<span style=\"color: #A020F0\">'|X(\\omega)|'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_01.png\"> <p>It turns out that, under certain conditions, the DFT is just equally-spaced samples of the DTFT. Suppose <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_eq88255.png\">  is the P-point DFT of <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_eq17606.png\"> . If <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_eq17606.png\">  is nonzero only over the finite domain <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_eq75061.png\"> , then <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_eq88255.png\">  equals <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_eq30711.png\">  at equally spaced intervals of <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_eq84050.png\"> :\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_eq68762.png\"> <\/p>\r\n   <p>The MATLAB function <tt>fft<\/tt> computes the DFT. Here's the 8-point DFT of our 8-point rectangular pulse:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x = ones(1, M);\r\nX = fft(x)<\/pre><pre style=\"font-style:oblique\">\r\nX =\r\n\r\n     8     0     0     0     0     0     0     0\r\n\r\n<\/pre><p>One 8 and a bunch of zeros?? That doesn't seem anything like the DTFT plot above. But when you superimpose the output of <tt>fft<\/tt> in the right places on the DTFT plot, it all becomes clear.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">P = 8;\r\nw_k = (0:P-1) * (2*pi\/P);\r\nX = fft(x);\r\nplot(w, abs(X_dtft))\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nplot(w_k, abs(X), <span style=\"color: #A020F0\">'o'<\/span>)\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_02.png\"> <p>Now you can see that the seven zeros in the output of <tt>fft<\/tt> correspond to the seven places (in each period) where the DTFT equals zero.\r\n   <\/p>\r\n   <p>You can get more samples of the DTFT simply by increasing P. One way to do that is to <b>zero-pad<\/b>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x16 = [x, zeros(1, 8)]<\/pre><pre style=\"font-style:oblique\">\r\nx16 =\r\n\r\n  Columns 1 through 15\r\n\r\n     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0\r\n\r\n  Column 16\r\n\r\n     0\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">P = 16;\r\nX16 = fft(x16);\r\nw_k = (0:P-1) * (2*pi\/P);\r\nX = fft(x);\r\nplot(w, abs(X_dtft))\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nplot(w_k, abs(X16), <span style=\"color: #A020F0\">'o'<\/span>)\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_03.png\"> <p>Another way to increase P is to use the <tt>fft(x,P)<\/tt> syntax of the <tt>fft<\/tt> function.  This syntax computes the P-point DFT of <tt>x<\/tt> by using zero-padding. Let's try a 50-point DFT.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">P = 50;\r\nXp = fft(x, P);\r\nw_k = (0:P-1) * (2*pi\/P);\r\nX = fft(x);\r\nplot(w, abs(X_dtft))\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nplot(w_k, abs(Xp), <span style=\"color: #A020F0\">'o'<\/span>)\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/dft_samples_dtft_04.png\"> <p>If you've ever wondered what that whole zero-padding business was all about with Fourier transforms, now you know.  When you\r\n      tack on a bunch of zeros to a sequence and then compute the DFT, you're just getting more and more samples of the DTFT of\r\n      the original sequence.\r\n   <\/p>\r\n   <p>I think the next logical place to go in our Fourier exploration is to start considering some of the reasons why many people\r\n      find the output of <tt>fft<\/tt> so surprising or puzzling.  Here's a sample:\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Why isn't the zero frequency (or \"DC\" frequency) in the center of the output from <tt>fft<\/tt>?\r\n         <\/li>\r\n         <li>Why isn't the output of <tt>fft<\/tt> real when the input is symmetric?\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Do you have puzzles to add? Let me know by adding your comments.<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_b56b377ac2e84a9599b65dff616b8e3e() {\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='b56b377ac2e84a9599b65dff616b8e3e ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' b56b377ac2e84a9599b65dff616b8e3e';\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 = '';\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_b56b377ac2e84a9599b65dff616b8e3e()\"><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.9<br><\/p>\r\n<\/div>\r\n<!--\r\nb56b377ac2e84a9599b65dff616b8e3e ##### SOURCE BEGIN #####\r\n%%\r\n% It's finally time to start looking at the relationship between the discrete\r\n% Fourier transform (DFT) and the discrete-time Fourier transform (DTFT). Let's\r\n% look at a simple rectangular pulse, $x[n] = 1$ for $0 \\leq n < M$. The DTFT of\r\n% $x[n]$ is:\r\n%\r\n% $$X(\\omega) = \\frac{sin(\\omega M\/2)}{sin(\\omega\/2)} e^{-j \\omega (M-1)\/2}$$\r\n%\r\n% Let's plot $|X(\\omega)|$ for $M=8$ over a couple of periods:\r\n\r\nM = 8;\r\nw = linspace(-2*pi, 2*pi, 800);\r\nX_dtft = (sin(w*M\/2) .\/ sin(w\/2)) .* exp(-1j * w * (M-1) \/ 2);\r\nplot(w, abs(X_dtft))\r\ntitle('|X(\\omega)|')\r\n\r\n%%\r\n% It turns out that, under certain conditions, the DFT is just equally-spaced\r\n% samples of the DTFT. Suppose $X_P[k]$ is the P-point DFT of $x[n]$. If $x[n]$\r\n% is nonzero only over the finite domain $0 \\leq n < M$, then $X_P[k]$ equals\r\n% $X(\\omega)$ at equally spaced intervals of $\\omega$:\r\n%\r\n% $$X_P[k] = X(2\\pi k\/P),\\ k=0, \\ldots, P-1$$\r\n%\r\n% The MATLAB function |fft| computes the DFT. Here's the 8-point DFT of our\r\n% 8-point rectangular pulse:\r\n\r\nx = ones(1, M);\r\nX = fft(x)\r\n\r\n%%\r\n% One 8 and a bunch of zeros?? That doesn't seem anything like the DTFT plot\r\n% above. But when you superimpose the output of |fft| in the right places on the\r\n% DTFT plot, it all becomes clear.\r\n\r\nP = 8;\r\nw_k = (0:P-1) * (2*pi\/P);\r\nX = fft(x);\r\nplot(w, abs(X_dtft))\r\nhold on\r\nplot(w_k, abs(X), 'o')\r\nhold off\r\n\r\n%%\r\n% Now you can see that the seven zeros in the output of |fft| correspond to the\r\n% seven places (in each period) where the DTFT equals zero.\r\n%\r\n% You can get more samples of the DTFT simply by increasing P. One way to do\r\n% that is to *zero-pad*.\r\n\r\nx16 = [x, zeros(1, 8)]\r\n\r\n%%\r\nP = 16;\r\nX16 = fft(x16);\r\nw_k = (0:P-1) * (2*pi\/P);\r\nX = fft(x);\r\nplot(w, abs(X_dtft))\r\nhold on\r\nplot(w_k, abs(X16), 'o')\r\nhold off\r\n\r\n%%\r\n% Another way to increase P is to use the |fft(x,P)| syntax of the |fft|\r\n% function.  This syntax computes the P-point DFT of |x| by using zero-padding.\r\n% Let's try a 50-point DFT.\r\n\r\nP = 50;\r\nXp = fft(x, P);\r\nw_k = (0:P-1) * (2*pi\/P);\r\nX = fft(x);\r\nplot(w, abs(X_dtft))\r\nhold on\r\nplot(w_k, abs(Xp), 'o')\r\nhold off\r\n\r\n%%\r\n% If you've ever wondered what that whole zero-padding business was all about\r\n% with Fourier transforms, now you know.  When you tack on a bunch of zeros to a\r\n% sequence and then compute the DFT, you're just getting more and more samples\r\n% of the DTFT of the original sequence.\r\n%\r\n% I think the next logical place to go in our Fourier exploration is to start\r\n% considering some of the reasons why many people find the output of |fft| so\r\n% surprising or puzzling.  Here's a sample:\r\n%\r\n% * Why isn't the zero frequency (or \"DC\" frequency) in the center of the output\r\n% from |fft|?\r\n% * Why isn't the output of |fft| real when the input is symmetric?\r\n%\r\n% Do you have puzzles to add? Let me know by adding your comments.\r\n##### SOURCE END ##### b56b377ac2e84a9599b65dff616b8e3e\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   It's finally time to start looking at the relationship between the discrete Fourier transform (DFT) and the discrete-time\r\n      Fourier transform (DTFT). Let's look at a simple rectangular... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/03\/15\/the-dft-and-the-dtft\/\">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":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/317"}],"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=317"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/317\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}