{"id":124,"date":"2007-10-30T13:55:58","date_gmt":"2007-10-30T18:55:58","guid":{"rendered":"http:\/\/www.tigoe.net\/pcomp\/code\/category\/code\/processing\/124"},"modified":"2021-01-12T10:55:48","modified_gmt":"2021-01-12T15:55:48","slug":"peak-finding-in-processing","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/Processing\/124\/","title":{"rendered":"Peak finding in Processing"},"content":{"rendered":"<p>In a <a href=\"https:\/\/tigoe.com\/pcomp\/code\/category\/code\/arduinowiring\/46\">previous example<\/a>, I showed how to detect a peak in a changing analog value on a microcontroller.  This example shows how to do it in Processing, assuming the microcontroller is just sending you binary values from 0 to 255.  It graphs the incoming bytes, and when a peak is detected, it draws an ellipse and prints a message in the message pane.<\/p>\n<p>Thanks to Matt Young for helping me debug this.<\/p>\n<p><!-- technorati tags start --><\/p>\n<p style=\"text-align:right;font-size:10px;\">Technorati Tags: <a href=\"http:\/\/www.technorati.com\/tag\/pcomp\" rel=\"tag\">pcomp<\/a>, <a href=\"http:\/\/www.technorati.com\/tag\/physical computing\" rel=\"tag\">physical computing<\/a>, <a href=\"http:\/\/www.technorati.com\/tag\/sensors\" rel=\"tag\">sensors<\/a><\/p>\n<p><!-- technorati tags end --><br \/>\n<!--more--><\/p>\n<pre>\/*\n  Peak Finding graphing sketch\n by Tom Igoe with help from Matt Young\n \n This program takes raw bytes from the serial port at 9600 baud and\n graphs them. It also detects peaks in the changing values as they come in,\n and displays an ellipse when a peak is detected.\n \n Created 20 April 2005\n Updated 23 Oct 2007\n *\/\n\nimport processing.serial.*;\n\nSerial myPort;  \/\/ The serial port\n\n\/\/ initial variables:\nint hPosition = 1;     \/\/ the horizontal position on the graph\nint threshold = 10;    \/\/ minimum threshold for peak finding\nint peakValue = 0;     \/\/ the current peak value\n\n\nvoid setup () {\n  size(400, 300);        \/\/ window size\n\n  \/\/ List all the available serial ports\n  println(Serial.list());\n\n  \/\/ Open whatever port is the one you're using.\n  myPort = new Serial(this, Serial.list()[0], 9600);\n\n  \/\/ set inital background:\n  background(0);\n}\n\nvoid draw () {\n  \/\/ all the action is in the serialEvent()\n}\n\nvoid serialEvent (Serial myPort) {\n  \/\/ read the byte:\n  int inByte = myPort.read();\n\n  \/\/ draw the line:\n  stroke(0,255,0);\n  line(hPosition, height, hPosition, height - inByte);\n\n  \/\/ if the current value &gt; threshold, we can look for a peak:\n  if (inByte &gt;= threshold) {\n    \/\/ if current value &gt; last peak value, we're going up:\n    if (inByte &gt;= peakValue) {\n      \/\/ save the current value as the highest\n      peakValue = inByte;\n    }\n    \/\/ if we're &gt; threshold, but &lt; lastValue, then\n    \/\/ the last peak value we got is indeed a peak:\n    else {\n      \/\/ this is when we have a peak\n      println(\" Peak = \" + peakValue);\n      fill(peakValue);\n      ellipse(10, 10, 10, 10);\n    }\n    \/\/ if we're below the threshold, reset the peaK:\n  } \n  else {\n    peakValue = 0;\n    fill(peakValue);\n    stroke(0);\n    ellipse(10, 10, 10, 10);\n  }\n\n  \/\/ at the edge of the screen, go back to the beginning:\n  if (hPosition &gt;= width) {\n    hPosition = 0;\n    background(0);\n  }\n  else {\n    hPosition++;\n  }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This example shows how to do it in PRocessing, assuming the microcontroller is ju\/* Peak Finding graphing sketch by Tom Igoe with help from Matt Young This program takes raw bytes from the serial port at 9600 baud and graphs them&#8230;.  Created 20 April 2005 Updated 23 Oct 2007 *\/import processing.serial.*;Serial myPort; \/\/ The serial port\/\/ initial variables:int hPosition = 1; \/\/ the horizontal position on the graphint threshold = 10; \/\/ minimum threshold for peak findingint peakValue = 0; \/\/ the current peak valuevoid setup () { size(400, 300); \/\/ window size \/\/ List all the available serial ports println(Serial.list()); \/\/ Open whatever port is the one you&#8217;re using.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-124","post","type-post","status-publish","format-standard","hentry","category-Processing"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/124","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=124"}],"version-history":[{"count":1,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/124\/revisions"}],"predecessor-version":[{"id":1430,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/124\/revisions\/1430"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}