{"id":2494,"date":"2009-11-13T13:46:00","date_gmt":"2009-11-13T13:46:00","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2009\/11\/13\/this-peaks-my-interest\/"},"modified":"2009-11-13T13:46:00","modified_gmt":"2009-11-13T13:46:00","slug":"this-peaks-my-interest","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2009\/11\/13\/this-peaks-my-interest\/","title":{"rendered":"This &#8220;Peaks&#8221; My Interest"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/25500-peakfinder-x0--sel--thresh--extrema--includeendpoints--interpolate--peakfinder\"><tt>PeakFinder<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/39794\">Nate Yoder<\/a>.\r\n   <\/p>\r\n   <p><i>\"What? Another peak finder?\"<\/i> you might say. Some of you may classify this as one of those utilities that has been created by many people over the years,\r\n      like <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?term=tag:&#34;sudoku&#34;\">sudoku<\/a> and <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?term=tag:&#34;waitbar&#34;\">waitbar<\/a>. Well, peak finding happens to be something dear to my heart.\r\n   <\/p>\r\n   <p>I have been using MATLAB for almost 10 years since my first year of graduate school. I initially learned by trying to decipher\r\n      my advisor's code. One day, I was struggling to write some code for finding peaks in my data.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Sample data<\/span>\r\nt = 0:0.01:10;\r\nx = sin(2*t) - 3*cos(3.8*t);<\/pre><p>That's when my advisor showed me his code:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dx = diff(x);          <span style=\"color: #228B22\">% get differences between consecutive points<\/span>\r\npkIDX = (dx(1:end-1) &gt;= 0) &amp; (dx(2:end) &lt; 0); <span style=\"color: #228B22\">% look for slope changes<\/span>\r\npkIDX = [dx(1)&lt;0, pkIDX, dx(end)&gt;=0];         <span style=\"color: #228B22\">% deal with edges<\/span>\r\nplot(t, x, t(pkIDX), x(pkIDX), <span style=\"color: #A020F0\">'ro'<\/span>);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/potw_peakfinder_01.png\"> <p>This was an eye-opener and was the moment I experienced the power of vector operation for the first time. The way I code in\r\n      MATLAB had changed from that point on. ... So when I see \"peak finding\", it brings back memories.\r\n   <\/p>\r\n   <p>There are quite a few File Exchange entries for finding peaks (and valleys), including two previous POTW selections: <a href=\"https:\/\/blogs.mathworks.com\/pick\/2004\/03\/17\/find-spikes-in-data\/\"><tt>FPEAK<\/tt><\/a> and <a href=\"https:\/\/blogs.mathworks.com\/pick\/2008\/05\/09\/finding-local-extrema\/\"><tt>EXTREMA<\/tt><\/a>. But I really like <tt>peakfinder<\/tt> by Nate. Not only does his code deal with noisy data (my algorithm above will be useless if the signal is noisy), but also\r\n      his coding practice is quite solid. He has a great help section, robust error-checking of input arguments, and variable input\r\n      and output arguments for ease of use.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">xNoise = x + 0.3*sin(40*t);   <span style=\"color: #228B22\">% add a few more bumps<\/span>\r\npeakfinder(xNoise);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/potw_peakfinder_02.png\"> <p>I looked through a few peak finding entries, but I'm sure I may have missed some. Feel free to let me know of others you really\r\n      like <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2494#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_8053d02cf91d4435b157ae0a3f8e14ed() {\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='8053d02cf91d4435b157ae0a3f8e14ed ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 8053d02cf91d4435b157ae0a3f8e14ed';\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 = 'Jiro Doke';\r\n        copyright = 'Copyright 2009 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_8053d02cf91d4435b157ae0a3f8e14ed()\"><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\n8053d02cf91d4435b157ae0a3f8e14ed ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/25500-peakfinder-x0--sel--thresh--extrema--includeendpoints--interpolate--peakfinder\r\n% |PeakFinder|> by \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/39794\r\n% Nate Yoder>.\r\n%\r\n% _\"What? Another peak finder?\"_ you might say. Some of you may classify\r\n% this as one of those utilities that has been created by many people over\r\n% the years, like\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?term=tag:\"sudoku\"\r\n% sudoku> and\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?term=tag:\"waitbar\"\r\n% waitbar>. Well, peak finding happens to be something dear to my heart.\r\n% \r\n% I have been using MATLAB for almost 10 years since my first year of\r\n% graduate school. I initially learned by trying to decipher my advisor's\r\n% code. One day, I was struggling to write some code for finding peaks in\r\n% my data.\r\n\r\n% Sample data\r\nt = 0:0.01:10;\r\nx = sin(2*t) - 3*cos(3.8*t);\r\n\r\n%%\r\n% That's when my advisor showed me his code:\r\n\r\ndx = diff(x);          % get differences between consecutive points\r\npkIDX = (dx(1:end-1) >= 0) & (dx(2:end) < 0); % look for slope changes\r\npkIDX = [dx(1)<0, pkIDX, dx(end)>=0];         % deal with edges\r\nplot(t, x, t(pkIDX), x(pkIDX), 'ro');\r\n\r\n%%\r\n% This was an eye-opener and was the moment I experienced the power of\r\n% vector operation for the first time. The way I code in MATLAB had changed\r\n% from that point on. ... So when I see \"peak finding\", it brings back\r\n% memories.\r\n%\r\n% There are quite a few File Exchange entries for finding peaks (and\r\n% valleys), including two previous POTW selections:\r\n% <https:\/\/blogs.mathworks.com\/pick\/2004\/03\/17\/find-spikes-in-data\/ |FPEAK|>\r\n% and <https:\/\/blogs.mathworks.com\/pick\/2008\/05\/09\/finding-local-extrema\/\r\n% |EXTREMA|>. But I really like |peakfinder| by Nate. Not only does his\r\n% code deal with noisy data (my algorithm above will be useless if the\r\n% signal is noisy), but also his coding practice is quite solid. He has a\r\n% great help section, robust error-checking of input arguments, and\r\n% variable input and output arguments for ease of use.\r\n\r\nxNoise = x + 0.3*sin(40*t);   % add a few more bumps\r\npeakfinder(xNoise);\r\n\r\n%%\r\n% I looked through a few peak finding entries, but I'm sure I may have\r\n% missed some. Feel free to let me know of others you really like \r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2494#respond here>.\r\n##### SOURCE END ##### 8053d02cf91d4435b157ae0a3f8e14ed\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Jiro's pick this week is PeakFinder by Nate Yoder.\r\n   \r\n   \"What? Another peak finder?\" you might say. Some of you may classify this as one of those utilities that has been created by many... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2009\/11\/13\/this-peaks-my-interest\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2494"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2494"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2494\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}