{"id":278,"date":"2016-08-05T23:18:29","date_gmt":"2016-08-05T23:18:29","guid":{"rendered":"http:\/\/martinvb.com\/wp\/?p=278"},"modified":"2016-08-06T20:49:15","modified_gmt":"2016-08-06T20:49:15","slug":"minimalist-low-pass-filter-library","status":"publish","type":"post","link":"https:\/\/martinvb.com\/wp\/minimalist-low-pass-filter-library\/","title":{"rendered":"Minimalist low-pass filter library"},"content":{"rendered":"<p>So, the other day I needed to compute some low-pass filters on the fly on an Arduino Mega. By &#8220;on the fly&#8221; I mean that the filters&#8217; parameters would eventually be recomputed mid-operation, so setting some equation with static\u00a0<strong>const<\/strong><em>\u00a0<\/em>parameters would not cut it. Using a static filter, is, however, the most common application scenario. If that&#8217;s your\u00a0case &#8211; and I insist &#8211; tweak the sh*t out of your filter with a <a href=\"http:\/\/www.micromodeler.com\/dsp\/\" target=\"_blank\">decent tool<\/a>\u00a0then just add it to your code. If, on the other hand, you need to update your filter dynamically (or if you&#8217;re just plain lazy to compute poles and zeros), then this is for you.<\/p>\n<p>I ended up putting together a minimalist library for that,\u00a0<em>libFilter<\/em>\u00a0(<a href=\"https:\/\/github.com\/MartinBloedorn\/libFilter\" target=\"_blank\">github!<\/a>). Works with the Arduino IDE and as a regular C++ library (just uncommenting one <strong>#define<\/strong> does the trick).<\/p>\n<figure id=\"attachment_280\" aria-describedby=\"caption-attachment-280\" style=\"width: 519px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/martinvb.com\/wp\/wp-content\/uploads\/2016\/08\/g_comp_b2b3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-280 \" src=\"http:\/\/martinvb.com\/wp\/wp-content\/uploads\/2016\/08\/g_comp_b2b3.png\" alt=\"Using two filter instances on a signal from a load cell getting punched. \" width=\"529\" height=\"472\" srcset=\"https:\/\/martinvb.com\/wp\/wp-content\/uploads\/2016\/08\/g_comp_b2b3.png 829w, https:\/\/martinvb.com\/wp\/wp-content\/uploads\/2016\/08\/g_comp_b2b3-300x267.png 300w\" sizes=\"(max-width: 529px) 100vw, 529px\" \/><\/a><figcaption id=\"caption-attachment-280\" class=\"wp-caption-text\">Using two filter instances on a signal from a load cell getting punched.<\/figcaption><\/figure>\n<p>For now it only implements low-pass filters based on \u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Butterworth_filter\" target=\"_blank\">normalized butterworth polynomials<\/a>, but who knows what necessity might add to it next. Let&#8217;s take a look at some ADC filtering:<\/p>\n<pre class=\"theme:monokai lang:c++ decode:true \" title=\"libFilter usage example.\">#include &lt;filters.h&gt;\r\n\r\nconst float cutoff_freq   = 20.0;  \/\/Cutoff frequency in Hz\r\nconst float sampling_time = 0.005; \/\/Sampling time in seconds.\r\nIIR::ORDER  order  = IIR::ORDER::OD3; \/\/ Order (OD1 to OD4)\r\n\r\n\/\/ Low-pass filter\r\nFilter f(cutoff_freq, sampling_time, order);\r\n\r\nvoid setup() {\r\n  Serial.begin(115200);\r\n  pinMode(A0, INPUT);\r\n  \/\/ Enable pull-ups if necessary\r\n  digitalWrite(A0, HIGH);\r\n}\r\n\r\nvoid loop() {\r\n  int value = analogRead(0);\r\n  float filteredval = f.filterIn(value);\r\n  \/\/View with Serial Plotter\r\n  Serial.print(value, DEC);\r\n  Serial.print(\",\");\r\n  Serial.println(filteredval, 4);\r\n  delay(5); \/\/ Loop time will approx. match the sampling time.\r\n}<\/pre>\n<p>&#8217;til next time!<\/p>\n<p><strong>P.S.:\u00a0<\/strong>I just can&#8217;t avoid letting this page eventually fall into temporary ostracism. Geez.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, the other day I needed to compute some low-pass filters on the fly on an Arduino Mega. By &#8220;on the fly&#8221; I mean that the filters&#8217; parameters would eventually be recomputed mid-operation, so setting some equation with static\u00a0const\u00a0parameters would not cut it. Using a static filter, is, however, the most common application scenario. If &#8230; <a title=\"Minimalist low-pass filter library\" class=\"read-more\" href=\"https:\/\/martinvb.com\/wp\/minimalist-low-pass-filter-library\/\" aria-label=\"More on Minimalist low-pass filter library\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,14,13],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/martinvb.com\/wp\/wp-json\/wp\/v2\/posts\/278"}],"collection":[{"href":"https:\/\/martinvb.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/martinvb.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/martinvb.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/martinvb.com\/wp\/wp-json\/wp\/v2\/comments?post=278"}],"version-history":[{"count":8,"href":"https:\/\/martinvb.com\/wp\/wp-json\/wp\/v2\/posts\/278\/revisions"}],"predecessor-version":[{"id":287,"href":"https:\/\/martinvb.com\/wp\/wp-json\/wp\/v2\/posts\/278\/revisions\/287"}],"wp:attachment":[{"href":"https:\/\/martinvb.com\/wp\/wp-json\/wp\/v2\/media?parent=278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/martinvb.com\/wp\/wp-json\/wp\/v2\/categories?post=278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/martinvb.com\/wp\/wp-json\/wp\/v2\/tags?post=278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}