{"id":381,"date":"2011-07-22T14:12:21","date_gmt":"2011-07-22T18:12:21","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2011\/07\/22\/filtering-fun\/"},"modified":"2019-10-29T16:49:09","modified_gmt":"2019-10-29T20:49:09","slug":"filtering-fun","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2011\/07\/22\/filtering-fun\/","title":{"rendered":"Filtering fun"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Today I want to take the test pattern I created <a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/07\/19\/jahne-test-pattern-take-3\/\">last time<\/a> and subject it to a variety of frequency-based filters.\r\n   <\/p>\r\n   <p>In this post I'll be using a variety of frequency design and frequency-response visualization tools in MATLAB, Signal Processing\r\n      Toolbox, and Image Processing Toolbox, including <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/signal\/fir1.html\"><tt>fir1<\/tt><\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/images\/ref\/ftrans2.html\"><tt>ftrans2<\/tt><\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/freqspace.html\"><tt>freqspace<\/tt><\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/images\/ref\/fwind1.html\"><tt>fwind1<\/tt><\/a>, and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/images\/ref\/freqz2.html\"><tt>freqz2<\/tt><\/a>. I won't be giving much explanation of these filter design techniques. Perhaps in the future I'll explore filter design here\r\n      in more detail. For now, though, I really just want to have some fun with this test pattern.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[x,y] = meshgrid(-200:200);\r\nr = hypot(x,y);\r\nkm = 0.8*pi;\r\nrm = 200;\r\nw = rm\/10;\r\nterm1 = sin( (km * r.^2) \/ (2 * rm) );\r\nterm2 = 0.5*tanh((rm - r)\/w) + 0.5;\r\ng = (term1 .* term2 + 1) \/ 2;\r\n\r\nimshow(g, <span style=\"color: #A020F0\">'XData'<\/span>, [-.8 .8], <span style=\"color: #A020F0\">'YData'<\/span>, [-.8 .8])\r\naxis <span style=\"color: #A020F0\">on<\/span>\r\nxlabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>), ylabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>)\r\ntitle(<span style=\"color: #A020F0\">'Test pattern'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/filtering_fun_01.jpg\"> <p><b>Example 1:<\/b> Design a one-dimensional lowpass filter and apply it in both directions.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">b = fir1(30,0.3);\r\nh = b' * b;\r\ng1 = imfilter(g, h);\r\n\r\nimshow(g1, <span style=\"color: #A020F0\">'XData'<\/span>, [-.8 .8], <span style=\"color: #A020F0\">'YData'<\/span>, [-.8 .8])\r\naxis <span style=\"color: #A020F0\">on<\/span>\r\nxlabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>), ylabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/filtering_fun_02.jpg\"> <p><b>Example 2:<\/b> Design a one-dimensional highpass filter and apply it in both directions.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">b2 = fir1(30, 0.3, <span style=\"color: #A020F0\">'high'<\/span>);\r\nh2 = b2' * b2;\r\ng2 = imfilter(g, h2);\r\n\r\nimshow(g2, [], <span style=\"color: #A020F0\">'XData'<\/span>, [-.8 .8], <span style=\"color: #A020F0\">'YData'<\/span>, [-.8 .8])\r\naxis <span style=\"color: #A020F0\">on<\/span>\r\nxlabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>), ylabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/filtering_fun_03.jpg\"> <p><b>Example 3:<\/b> Design a one-dimensional bandpass filter and apply it on both directions.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">b3 = fir1(30, [0.3 0.4]);\r\nh3 = b3' * b3;\r\ng3 = imfilter(g, h3);\r\n\r\nimshow(g3, [], <span style=\"color: #A020F0\">'XData'<\/span>, [-.8 .8], <span style=\"color: #A020F0\">'YData'<\/span>, [-.8 .8])\r\naxis <span style=\"color: #A020F0\">on<\/span>\r\nxlabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>), ylabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/filtering_fun_04.jpg\"> <p><b>Example 4:<\/b> Apply the one-dimensional bandpass filter from the previous step in only one direction.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">g4 = imfilter(g, b3);\r\n\r\nimshow(g4, [], <span style=\"color: #A020F0\">'XData'<\/span>, [-.8 .8], <span style=\"color: #A020F0\">'YData'<\/span>, [-.8 .8])\r\naxis <span style=\"color: #A020F0\">on<\/span>\r\nxlabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>), ylabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/filtering_fun_05.jpg\"> <p><b>Example 5:<\/b> Use <tt>ftrans2<\/tt> to turn the one-dimensional bandpass filter from the previous example into a two-dimensional bandpass filter that is approximately\r\n      circularly symmetric.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">h5 = ftrans2(b3);\r\ng5 = imfilter(g, h5);\r\n\r\nimshow(g5, [], <span style=\"color: #A020F0\">'XData'<\/span>, [-.8 .8], <span style=\"color: #A020F0\">'YData'<\/span>, [-.8 .8])\r\naxis <span style=\"color: #A020F0\">on<\/span>\r\nxlabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>), ylabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/filtering_fun_06.jpg\"> <p><b>Example 6:<\/b> Use <tt>fwind1<\/tt> to design a two-dimensional filter that has a wedge-shaped frequency response.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[f1, f2] = freqspace(101, <span style=\"color: #A020F0\">'meshgrid'<\/span>);\r\ntheta = cart2pol(f1, f2);\r\nHd = ((theta &gt;= pi\/6) &amp; (theta &lt; pi\/3)) | <span style=\"color: #0000FF\">...<\/span>\r\n    ((theta &gt;= -5*pi\/6) &amp; (theta &lt; -2*pi\/3));\r\nmesh(f1, f2, double(Hd))\r\ntitle(<span style=\"color: #A020F0\">'Ideal frequency response'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/filtering_fun_07.jpg\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">h6 = fwind1(Hd, hamming(31));\r\nfreqz2(h6)\r\ntitle(<span style=\"color: #A020F0\">'Designed filter frequency response'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/filtering_fun_08.jpg\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">g6 = imfilter(g, h6);\r\n\r\nimshow(g6, [], <span style=\"color: #A020F0\">'XData'<\/span>, [-.8 .8], <span style=\"color: #A020F0\">'YData'<\/span>, [-.8 .8])\r\naxis <span style=\"color: #A020F0\">on<\/span>\r\nxlabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>), ylabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/filtering_fun_09.jpg\"> <p><b>Example 7:<\/b> Compare the use of an averaging filter and a Gaussian filter. This example is inspired by <a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/07\/16\/jahne-test-pattern\/#comment-24327\">Cris Luengo's comment<\/a> from last week.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">g7 = imfilter(g, fspecial(<span style=\"color: #A020F0\">'average'<\/span>, 9));\r\nimshow(g7, [], <span style=\"color: #A020F0\">'XData'<\/span>, [-.8 .8], <span style=\"color: #A020F0\">'YData'<\/span>, [-.8 .8])\r\naxis <span style=\"color: #A020F0\">on<\/span>\r\nxlabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>), ylabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>)\r\ntitle(<span style=\"color: #A020F0\">'13-by-13 averaging filter'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/filtering_fun_10.jpg\"> <p>You can see that some very high frequencies remain in the output. The Gaussian filter performs much better:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">g9 = imfilter(g, fspecial(<span style=\"color: #A020F0\">'gaussian'<\/span>, 9, 2));\r\nimshow(g9, [], <span style=\"color: #A020F0\">'XData'<\/span>, [-.8 .8], <span style=\"color: #A020F0\">'YData'<\/span>, [-.8 .8])\r\naxis <span style=\"color: #A020F0\">on<\/span>\r\nxlabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>), ylabel(<span style=\"color: #A020F0\">'Normalized frequency'<\/span>)\r\ntitle(<span style=\"color: #A020F0\">'13-by-13 Gaussian filter'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/filtering_fun_11.jpg\"> <p>OK, I'm going to give this test pattern a rest for now. Phew!<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_56837dccbfda485894d779f470b165f5() {\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='56837dccbfda485894d779f470b165f5 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 56837dccbfda485894d779f470b165f5';\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 2011 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_56837dccbfda485894d779f470b165f5()\"><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.12<br><\/p>\r\n<\/div>\r\n<!--\r\n56837dccbfda485894d779f470b165f5 ##### SOURCE BEGIN #####\r\n%%\r\n% Today I want to take the test pattern I created\r\n% <https:\/\/blogs.mathworks.com\/steve\/2011\/07\/19\/jahne-test-pattern-take-3\/ \r\n% last time> and subject it to a variety of frequency-based filters.\r\n%\r\n% In this post I'll be using a variety of frequency design and\r\n% frequency-response visualization tools in MATLAB, Signal Processing\r\n% Toolbox, and Image Processing Toolbox, including\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/signal\/fir1.html |fir1|>,\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/images\/ref\/ftrans2.html\r\n% |ftrans2|>, <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/freqspace.html\r\n% |freqspace|>,\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/images\/ref\/fwind1.html |fwind1|>,\r\n% and <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/images\/ref\/freqz2.html\r\n% |freqz2|>. I won't be giving much explanation of these filter design\r\n% techniques. Perhaps in the future I'll explore filter design here in more\r\n% detail. For now, though, I really just want to have some fun with this\r\n% test pattern.\r\n\r\n[x,y] = meshgrid(-200:200);\r\nr = hypot(x,y);\r\nkm = 0.8*pi;\r\nrm = 200;\r\nw = rm\/10;\r\nterm1 = sin( (km * r.^2) \/ (2 * rm) );\r\nterm2 = 0.5*tanh((rm - r)\/w) + 0.5;\r\ng = (term1 .* term2 + 1) \/ 2;\r\n\r\nimshow(g, 'XData', [-.8 .8], 'YData', [-.8 .8])\r\naxis on\r\nxlabel('Normalized frequency'), ylabel('Normalized frequency')\r\ntitle('Test pattern')\r\n\r\n%%\r\n% *Example 1:* Design a one-dimensional lowpass filter and apply it in both\r\n% directions.\r\n% \r\nb = fir1(30,0.3);\r\nh = b' * b;\r\ng1 = imfilter(g, h);\r\n\r\nimshow(g1, 'XData', [-.8 .8], 'YData', [-.8 .8])\r\naxis on\r\nxlabel('Normalized frequency'), ylabel('Normalized frequency')\r\n\r\n%%\r\n% *Example 2:* Design a one-dimensional highpass filter and apply it in both\r\n% directions.\r\n\r\nb2 = fir1(30, 0.3, 'high');\r\nh2 = b2' * b2;\r\ng2 = imfilter(g, h2);\r\n\r\nimshow(g2, [], 'XData', [-.8 .8], 'YData', [-.8 .8])\r\naxis on\r\nxlabel('Normalized frequency'), ylabel('Normalized frequency')\r\n\r\n%%\r\n% *Example 3:* Design a one-dimensional bandpass filter and apply it on both\r\n% directions.\r\n\r\nb3 = fir1(30, [0.3 0.4]);\r\nh3 = b3' * b3;\r\ng3 = imfilter(g, h3);\r\n\r\nimshow(g3, [], 'XData', [-.8 .8], 'YData', [-.8 .8])\r\naxis on\r\nxlabel('Normalized frequency'), ylabel('Normalized frequency')\r\n\r\n%%\r\n% *Example 4:* Apply the one-dimensional bandpass filter from the previous\r\n% step in only one direction.\r\n\r\ng4 = imfilter(g, b3);\r\n\r\nimshow(g4, [], 'XData', [-.8 .8], 'YData', [-.8 .8])\r\naxis on\r\nxlabel('Normalized frequency'), ylabel('Normalized frequency')\r\n\r\n%%\r\n% *Example 5:* Use |ftrans2| to turn the one-dimensional bandpass filter from\r\n% the previous example into a two-dimensional bandpass filter that is\r\n% approximately circularly symmetric.\r\n\r\nh5 = ftrans2(b3);\r\ng5 = imfilter(g, h5);\r\n\r\nimshow(g5, [], 'XData', [-.8 .8], 'YData', [-.8 .8])\r\naxis on\r\nxlabel('Normalized frequency'), ylabel('Normalized frequency')\r\n\r\n%%\r\n% *Example 6:* Use |fwind1| to design a two-dimensional filter that has a\r\n% wedge-shaped frequency response.\r\n\r\n[f1, f2] = freqspace(101, 'meshgrid');\r\ntheta = cart2pol(f1, f2);\r\nHd = ((theta >= pi\/6) & (theta < pi\/3)) | ...\r\n    ((theta >= -5*pi\/6) & (theta < -2*pi\/3));\r\nmesh(f1, f2, double(Hd))\r\ntitle('Ideal frequency response')\r\n\r\n%%\r\nh6 = fwind1(Hd, hamming(31));\r\nfreqz2(h6)\r\ntitle('Designed filter frequency response')\r\n\r\n%%\r\ng6 = imfilter(g, h6);\r\n\r\nimshow(g6, [], 'XData', [-.8 .8], 'YData', [-.8 .8])\r\naxis on\r\nxlabel('Normalized frequency'), ylabel('Normalized frequency')\r\n\r\n%%\r\n% *Example 7:* Compare the use of an averaging filter and a Gaussian filter.\r\n% This example is inspired by\r\n% <https:\/\/blogs.mathworks.com\/steve\/2011\/07\/16\/jahne-test-pattern\/#comment-24327\r\n% Cris Luengo's comment> from last week.\r\n\r\ng7 = imfilter(g, fspecial('average', 9));\r\nimshow(g7, [], 'XData', [-.8 .8], 'YData', [-.8 .8])\r\naxis on\r\nxlabel('Normalized frequency'), ylabel('Normalized frequency')\r\ntitle('13-by-13 averaging filter')\r\n\r\n%%\r\n% You can see that some very high frequencies remain in the output. The\r\n% Gaussian filter performs much better:\r\n\r\ng9 = imfilter(g, fspecial('gaussian', 9, 2));\r\nimshow(g9, [], 'XData', [-.8 .8], 'YData', [-.8 .8])\r\naxis on\r\nxlabel('Normalized frequency'), ylabel('Normalized frequency')\r\ntitle('13-by-13 Gaussian filter')\r\n\r\n%%\r\n% OK, I'm going to give this test pattern a rest for now. Phew!\r\n##### SOURCE END ##### 56837dccbfda485894d779f470b165f5\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Today I want to take the test pattern I created last time and subject it to a variety of frequency-based filters.\r\n   \r\n   In this post I'll be using a variety of frequency design and... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/07\/22\/filtering-fun\/\">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":[50,38,402,811,815,821,813,817,819,334,370,36,356,30,532,34,809,52,94,96],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/381"}],"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=381"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/381\/revisions"}],"predecessor-version":[{"id":3743,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/381\/revisions\/3743"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}