{"id":341,"date":"2010-09-22T17:30:14","date_gmt":"2010-09-22T21:30:14","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2010\/09\/22\/fourier-transforms-vertical-lines-and-horizontal-lines\/"},"modified":"2019-10-29T13:34:57","modified_gmt":"2019-10-29T17:34:57","slug":"fourier-transforms-vertical-lines-and-horizontal-lines","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2010\/09\/22\/fourier-transforms-vertical-lines-and-horizontal-lines\/","title":{"rendered":"Fourier transforms, vertical lines, and horizontal lines"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>A reader asked in a blog comment recently why a vertical line (or edge) shows up in the Fourier transform of an image as a\r\n      horizontal line. I thought I would try to explain this using the simplest example I could think of.\r\n   <\/p>\r\n   <p>I'll start with an image that is a constant black except for a single vertical line through the middle of it.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x = zeros(200, 200);\r\nx(:, 100) = 1;\r\nimshow(x)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/fft_vertical_horizontal_01.png\"> <p>Next I'll compute and display the log magnitude of the 2-D Fourier transform.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">X = fft2(x);\r\nimshow(fftshift(log(abs(X) + 1)), [])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/fft_vertical_horizontal_02.png\"> <p>There's the question in black and white, so to speak: Why is there a horizontal line in the 2-D Fourier transform?<\/p>\r\n   <p>Although I'm going to avoid equations and other complicated mumbo-jumbo in my answer, I do have to explain a couple of things\r\n      about 1-D and 2-D Fourier transforms.\r\n   <\/p>\r\n   <p>First, the magnitude of the 1-D Fourier transform an \"impulse sequence\" is a constant. An \"impulse sequence\" is a sequence\r\n      that is nonzero only at one place, like this one:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x1 = [0 0 1 0 0]<\/pre><pre style=\"font-style:oblique\">\r\nx1 =\r\n\r\n     0     0     1     0     0\r\n\r\n<\/pre><p>The magnitude of the 1-D Fourier transform of <tt>x<\/tt> is constant:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">abs(fft(x1))<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n    1.0000    1.0000    1.0000    1.0000    1.0000\r\n\r\n<\/pre><p>Second, the magnitude of the 1-D Fourier transform of a constant sequence is an impulse. That is, the Fourier transform is\r\n      nonzero only at one place.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x2 = [1 1 1 1 1]<\/pre><pre style=\"font-style:oblique\">\r\nx2 =\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)\">abs(fft(x2))<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     5     0     0     0     0\r\n\r\n<\/pre><p>Finally, computing the 2-D Fourier transform is mathematically equivalent to computing the 1-D Fourier transform of all the\r\n      rows and then computing the 1-D Fourier transforms of all the columns of the result. The order (row-first or column-first)\r\n      doesn't actually matter.\r\n   <\/p>\r\n   <p>Now let's go back to <tt>x<\/tt>, our 200-by-200 matrix with a vertical line down the middle of it.  Each row of <tt>x<\/tt> is an impulse sequence:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">plot(x(50,:))<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/fft_vertical_horizontal_03.png\"> <p>So if we compute the Fourier transform of all the rows, they'll all be constant (in magnitude):<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">X_rows = fft(x, [], 2);\r\nplot(abs(X_rows(50, :)))\r\nylim([0 2])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/fft_vertical_horizontal_04.png\"> <p>The next step in computing the 2-D Fourier transform is to compute the 1-D Fourier transforms of the columns of <tt>X_rows<\/tt>. But those columns are constant. Here's the 100th column of <tt>X_rows<\/tt>:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">plot(abs(X_rows(:, 100)))\r\nylim([0 2])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/fft_vertical_horizontal_05.png\"> <p>As I said above, the Fourier transform of a constant sequence is an impulse. If you stack up all the resulting columns containing\r\n      impulse sequences, the result looks like a horizontal line.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">X = fft(X_rows, [], 1);\r\nimshow(fftshift(log(abs(X) + 1)), [])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/fft_vertical_horizontal_06.png\"> <p>That's a step-by-step computational explanation. Let me leave you with more of a conceptual (that is, hand-wavy) explanation.\r\n      Let's look at the input image and the 2-D Fourier transform side by side.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">subplot(1,2,1)\r\nimshow(x)\r\ntitle(<span style=\"color: #A020F0\">'x'<\/span>)\r\nsubplot(1,2,2)\r\nimshow(fftshift(log(abs(X) + 1)), [])\r\ntitle(<span style=\"color: #A020F0\">'Log magnitude of fft2(x)'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2010\/fft_vertical_horizontal_07.png\"> <p>You can think in terms of horizontal and vertical cross-sections. Each horizontal cross-section of <tt>x<\/tt> is an impulse sequence. The Fourier transform of an impulse sequence is constant, so horizontal cross-sections of the Fourier\r\n      transform are constant.\r\n   <\/p>\r\n   <p>Similarly, each vertical cross-section of <tt>x<\/tt> is a constant sequence. The Fourier transform of a constant sequence is an impulse sequence, so the vertical cross-sections\r\n      of the Fourier transform are impulses.  The impulses all line up each other, resulting in the appearance of a horizontal line.\r\n   <\/p>\r\n   <p>Do these explanations work for you?  Do you know of another way to think about it that makes a better explanation?  Post your\r\n      thoughts as comments below.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_496f10acbfbf45b6b508b688b5735537() {\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='496f10acbfbf45b6b508b688b5735537 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 496f10acbfbf45b6b508b688b5735537';\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_496f10acbfbf45b6b508b688b5735537()\"><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.11<br><\/p>\r\n<\/div>\r\n<!--\r\n496f10acbfbf45b6b508b688b5735537 ##### SOURCE BEGIN #####\r\n%%\r\n% A reader asked in a blog comment recently why a vertical line (or edge)\r\n% shows up in the Fourier transform of an image as a horizontal line. I\r\n% thought I would try to explain this using the simplest example I could\r\n% think of.\r\n%\r\n% I'll start with an image that is a constant black except for a\r\n% single vertical line through the middle of it.\r\n\r\nx = zeros(200, 200);\r\nx(:, 100) = 1;\r\nimshow(x)\r\n\r\n%%\r\n% Next I'll compute and display the log magnitude of the 2-D Fourier\r\n% transform. \r\n\r\nX = fft2(x);\r\nimshow(fftshift(log(abs(X) + 1)), [])\r\n\r\n%%\r\n% There's the question in black and white, so to speak: Why is there a\r\n% horizontal line in the 2-D Fourier transform?\r\n%\r\n% Although I'm going to avoid equations and other complicated mumbo-jumbo\r\n% in my answer, I do have to explain a couple of things about 1-D and 2-D\r\n% Fourier transforms. \r\n% \r\n% First, the magnitude of the 1-D Fourier transform an \"impulse sequence\"\r\n% is a constant. An \"impulse sequence\" is a sequence that is nonzero only at\r\n% one place, like this one:\r\n\r\nx1 = [0 0 1 0 0]\r\n\r\n%%\r\n% The magnitude of the 1-D Fourier transform of |x| is constant:\r\n\r\nabs(fft(x1))\r\n\r\n%%\r\n% Second, the magnitude of the 1-D Fourier transform of a constant sequence\r\n% is an impulse. That is, the Fourier transform is nonzero only at one\r\n% place.\r\n\r\nx2 = [1 1 1 1 1]\r\n\r\n%%\r\nabs(fft(x2))\r\n\r\n%%\r\n% Finally, computing the 2-D Fourier transform is mathematically equivalent\r\n% to computing the 1-D Fourier transform of all the rows and then computing\r\n% the 1-D Fourier transforms of all the columns of the result. The order\r\n% (row-first or column-first) doesn't actually matter. \r\n%\r\n% Now let's go back to |x|, our 200-by-200 matrix with a vertical line down\r\n% the middle of it.  Each row of |x| is an impulse sequence:\r\n\r\nplot(x(50,:))\r\n\r\n%%\r\n% So if we compute the Fourier transform of all the rows, they'll all be\r\n% constant (in magnitude):\r\n\r\nX_rows = fft(x, [], 2);\r\nplot(abs(X_rows(50, :)))\r\nylim([0 2])\r\n\r\n%%\r\n% The next step in computing the 2-D Fourier transform is to compute the\r\n% 1-D Fourier transforms of the columns of |X_rows|. But those columns are\r\n% constant. Here's the 100th column of |X_rows|:\r\n\r\nplot(abs(X_rows(:, 100)))\r\nylim([0 2])\r\n\r\n%%\r\n% As I said above, the Fourier transform of a constant sequence is an\r\n% impulse. If you stack up all the resulting columns containing impulse\r\n% sequences, the result looks like a horizontal line.\r\n\r\nX = fft(X_rows, [], 1);\r\nimshow(fftshift(log(abs(X) + 1)), [])\r\n\r\n%%\r\n% That's a step-by-step computational explanation. Let me leave you with\r\n% more of a conceptual (that is, hand-wavy) explanation. Let's look at the\r\n% input image and the 2-D Fourier transform side by side.\r\n\r\nsubplot(1,2,1)\r\nimshow(x)\r\ntitle('x')\r\nsubplot(1,2,2)\r\nimshow(fftshift(log(abs(X) + 1)), [])\r\ntitle('Log magnitude of fft2(x)')\r\n\r\n%%\r\n% You can think in terms of horizontal and vertical cross-sections.\r\n% Each horizontal cross-section of |x| is an impulse sequence. The Fourier\r\n% transform of an impulse sequence is constant, so horizontal\r\n% cross-sections of the Fourier transform are constant.\r\n%\r\n% Similarly, each vertical cross-section of |x| is a constant sequence. The\r\n% Fourier transform of a constant sequence is an impulse sequence, so the\r\n% vertical cross-sections of the Fourier transform are impulses.  The\r\n% impulses all line up each other, resulting in the appearance of a\r\n% horizontal line.\r\n%\r\n% Do these explanations work for you?  Do you know of another way to think\r\n% about it that makes a better explanation?  Post your thoughts as comments\r\n% below.\r\n##### SOURCE END ##### 496f10acbfbf45b6b508b688b5735537\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   A reader asked in a blog comment recently why a vertical line (or edge) shows up in the Fourier transform of an image as a\r\n      horizontal line. I thought I would try to explain this using the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/09\/22\/fourier-transforms-vertical-lines-and-horizontal-lines\/\">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,392,400,36,224,68,72,52,298,130],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/341"}],"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=341"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/341\/revisions"}],"predecessor-version":[{"id":3695,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/341\/revisions\/3695"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}