{"id":46,"date":"2005-09-12T09:42:29","date_gmt":"2005-09-12T14:42:29","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/arduinowiring\/46"},"modified":"2014-09-09T08:28:36","modified_gmt":"2014-09-09T13:28:36","slug":"peak-finding","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/46\/","title":{"rendered":"Peak Finding"},"content":{"rendered":"<p><em>updated 9 Sept 2014<\/em><\/p>\n<p>This example finds the peak value of an analog sensor over time. It assumes the sensor is moving in a simple curve.<\/p>\n<p>The peak value is initially set at zero. The program checks to see that the current value is greater than the current peak value. If so, then it saves the current value as a peak. \u00a0If the current value is below a given threshold and the current peak value is above it, then the current peak is taken as the final peak. This example also includes a noise value, that allows you to adjust for sensor fluctuation.<\/p>\n<p>The graph below gives you a visual sense of how the program operates. At readings 1, 2, and 3, the latest reading becomes the new peak. At reading 4, the peak remains unchanged because reading 4 is less than the last recorded peak. But the peak isn&#8217;t reported until reading 5, when the current reading goes below the threshold. This is the one disadvantage to this peak finding method: you don&#8217;t know you&#8217;ve had a peak until after it&#8217;s over.<\/p>\n<p><a href=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2005\/09\/peak_explained.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1238 size-full\" src=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2005\/09\/peak_explained.png\" alt=\"peak_explained\" width=\"406\" height=\"248\" srcset=\"https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2005\/09\/peak_explained.png 406w, https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2005\/09\/peak_explained-300x183.png 300w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/a><\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<h2>Beware local peaks<\/h2>\n<p>This example works well for simple situations, where a sensor will generate a reasonably simple, sharp peak like this one below:<\/p>\n<p><a href=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2005\/09\/peak_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1239 size-full\" src=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2005\/09\/peak_1.png\" alt=\"peak_1\" width=\"405\" height=\"245\" srcset=\"https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2005\/09\/peak_1.png 405w, https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2005\/09\/peak_1-300x181.png 300w\" sizes=\"auto, (max-width: 405px) 100vw, 405px\" \/><\/a><\/p>\n<p>It will also work in situations where your sensor generates some local peaks\u00a0on the way to a highest peak, like this one:<\/p>\n<p><a href=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2005\/09\/peak_2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1240 size-full\" src=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2005\/09\/peak_2.png\" alt=\"peak_2\" width=\"405\" height=\"245\" srcset=\"https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2005\/09\/peak_2.png 405w, https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2005\/09\/peak_2-300x181.png 300w\" sizes=\"auto, (max-width: 405px) 100vw, 405px\" \/><\/a><\/p>\n<p>It will fail, however, if your sensor goes below the threshold again on the way to a highest peak, like this:<\/p>\n<p><a href=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2005\/09\/peak_3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1241 size-full\" src=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2005\/09\/peak_3.png\" alt=\"peak_3\" width=\"405\" height=\"245\" srcset=\"https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2005\/09\/peak_3.png 405w, https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2005\/09\/peak_3-300x181.png 300w\" sizes=\"auto, (max-width: 405px) 100vw, 405px\" \/><\/a><\/p>\n<pre class=\"brush: cpp; light: true; title: ; notranslate\" title=\"\">\r\n\r\n&#x5B;github_cv url='https:\/\/github.com\/tigoe\/SensorExamples\/blob\/master\/PeakDetection\/PeakDetection.ino']\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>It assumes the sensor is moving in a simple curve.The program checks to see that the current value is above a given threshold, then checks to see if the value is greater than the previous value&#8230;.  When the current value goes below the threshold again, it outputs the last peak value recorded.Wiring\/Arduino:\/*Peak Finder This example finds the peak value of an analog sensor over time.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,10],"tags":[],"class_list":["post-46","post","type-post","status-publish","format-standard","hentry","category-arduinowiring","category-picbasic-pro"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/46","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=46"}],"version-history":[{"count":4,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"predecessor-version":[{"id":1244,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/46\/revisions\/1244"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}