{"id":555,"date":"2012-05-01T17:06:59","date_gmt":"2012-05-01T21:06:59","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=555"},"modified":"2019-10-31T14:47:07","modified_gmt":"2019-10-31T18:47:07","slug":"the-dft-matrix-and-computation-time","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2012\/05\/01\/the-dft-matrix-and-computation-time\/","title":{"rendered":"The DFT matrix and computation time"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>On my list of potential blog topics today I saw just this cryptic item labeled <tt>dftmtx<\/tt>.  Hmm, the MATLAB <tt>dftmtx<\/tt> function. But have I written about this function before? I better double-check by searching the old blog postings:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2012\/search_dftmtx.png\"> <\/p>\r\n   <p>Oh, I forgot about <a href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/02\/02\/a-pedagogical-tool-for-fourier-transforms\/\">Loren's post!<\/a> She showed how the discrete Fourier transform, which MATLAB users normally compute by calling <tt>fft<\/tt>, can also be computed via a matrix multiply.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x = rand(1000,1);\r\nX = fft(x);\r\n\r\nT = dftmtx(1000);\r\nX2 = T*x;\r\n\r\nmax(abs(X2(:) - X(:)))<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n   3.9790e-13\r\n\r\n<\/pre><p>The difference is just floating-point round-off error.<\/p>\r\n   <p><a href=\"https:\/\/blogs.mathworks.com\/steve\/2008\/09\/11\/independent-computation-in-software-tests\/\">My old post<\/a> talked about the value of having an independent computation method available when you are testing your algorithm.\r\n   <\/p>\r\n   <p>So today let's do something a little different. Let's compare the performance of computing the discrete Fourier transform\r\n      using the DFT matrix versus using the fast Fourier transform.\r\n   <\/p>\r\n   <p>To help with the timing, I'm going to use a function I wrote called <tt>timeit<\/tt> that you can download from the MATLAB Central File Exchange.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">clear\r\nn = 100:50:3000;\r\n<span style=\"color: #0000FF\">for<\/span> k = 1:length(n)\r\n    nk = n(k);\r\n    x = rand(nk,1);\r\n    T = dftmtx(nk);\r\n\r\n    f = @() T*x;\r\n    g = @() fft(x);\r\n\r\n    times_f(k) = timeit(f);\r\n    times_g(k) = timeit(g);\r\n<span style=\"color: #0000FF\">end<\/span>\r\n\r\nplot(n,times_f,n,times_g)\r\nlegend({<span style=\"color: #A020F0\">'Using T*x'<\/span>, <span style=\"color: #A020F0\">'Using fft(x)'<\/span>})<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2012\/timing_dftmtx_01.png\"> <p>That's a pretty dramatic difference. The blue curve, showing the computation time using <tt>T*x<\/tt>, is an n^2 curve. Compared to that, the green curve, showing the computation time using <tt>fft(x)<\/tt>, is so low that you can hardly see it.\r\n   <\/p>\r\n   <p>Let's expand the y axis.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">ylim([0 0.0005])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2012\/timing_dftmtx_02.png\"> <p>The lower green curve is an n*log(n) curve. The dramatic difference between that and the n^2 curve is why everyone got so\r\n      excited when the fast Fourier transform algorithm was invented (or <a href=\"http:\/\/en.wikipedia.org\/wiki\/Fast_Fourier_transform#Cooley.E2.80.93Tukey_algorithm\">re-invented<\/a>) a few decades ago.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_85fcd7bddddb40419a09e40981140863() {\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='85fcd7bddddb40419a09e40981140863 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 85fcd7bddddb40419a09e40981140863';\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 2012 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_85fcd7bddddb40419a09e40981140863()\"><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.14<br><\/p>\r\n<\/div>\r\n<!--\r\n85fcd7bddddb40419a09e40981140863 ##### SOURCE BEGIN #####\r\n%%\r\n% On my list of potential blog topics today I saw just this cryptic item\r\n% labeled |dftmtx|.  Hmm, the MATLAB |dftmtx| function. But have I written\r\n% about this function before? I better double-check by searching the old\r\n% blog postings:\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2012\/search_dftmtx.png>>\r\n%\r\n% Oh, I forgot about\r\n% <https:\/\/blogs.mathworks.com\/loren\/2009\/02\/02\/a-pedagogical-tool-for-fourier-transforms\/\r\n% Loren's post!> She showed how the discrete Fourier transform, which\r\n% MATLAB users normally compute by calling |fft|, can also be computed via\r\n% a matrix multiply.\r\n\r\nx = rand(1000,1);\r\nX = fft(x);\r\n\r\nT = dftmtx(1000);\r\nX2 = T*x;\r\n\r\nmax(abs(X2(:) - X(:)))\r\n\r\n%%\r\n% The difference is just floating-point round-off error.\r\n%\r\n% <https:\/\/blogs.mathworks.com\/steve\/2008\/09\/11\/independent-computation-in-software-tests\/\r\n% My old post> talked about the value of having an independent computation\r\n% method available when you are testing your algorithm.\r\n%\r\n% So today let's do something a little different. Let's compare the\r\n% performance of computing the discrete Fourier transform using the DFT\r\n% matrix versus using the fast Fourier transform.\r\n%\r\n% To help with the timing, I'm going to use a function I wrote called\r\n% |timeit| that you can\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/18798-timeit-benchmarking-function\r\n% download> from the MATLAB Central File Exchange.\r\n\r\n%%\r\nclear\r\nn = 100:50:3000;\r\nfor k = 1:length(n)\r\n    nk = n(k);\r\n    x = rand(nk,1);\r\n    T = dftmtx(nk);\r\n    \r\n    f = @() T*x;\r\n    g = @() fft(x);\r\n    \r\n    times_f(k) = timeit(f);\r\n    times_g(k) = timeit(g);\r\nend\r\n\r\nplot(n,times_f,n,times_g)\r\nlegend({'Using T*x', 'Using fft(x)'})\r\n    \r\n%%\r\n% That's a pretty dramatic difference. The blue curve, showing the\r\n% computation time using |T*x|, is an n^2 curve. Compared to that, the\r\n% green curve, showing the computation time using |fft(x)|, is so low that\r\n% you can hardly see it.\r\n%\r\n% Let's expand the y axis.\r\n\r\nylim([0 0.0005])\r\n\r\n%%\r\n% The lower green curve is an n*log(n) curve. The dramatic difference\r\n% between that and the n^2 curve is why everyone got so excited when the\r\n% fast Fourier transform algorithm was invented (or\r\n% <http:\/\/en.wikipedia.org\/wiki\/Fast_Fourier_transform#Cooley.E2.80.93Tukey_algorithm\r\n% re-invented>) a few decades ago.\r\n\r\n##### SOURCE END ##### 85fcd7bddddb40419a09e40981140863\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   On my list of potential blog topics today I saw just this cryptic item labeled dftmtx.  Hmm, the MATLAB dftmtx function. But have I written about this function before? I better double-check by... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2012\/05\/01\/the-dft-matrix-and-computation-time\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[208,302,897,426,92,705,122,68,460,474,298],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/555"}],"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=555"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/555\/revisions"}],"predecessor-version":[{"id":2638,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/555\/revisions\/2638"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}