{"id":4,"date":"2010-06-09T08:09:13","date_gmt":"2010-06-09T08:09:13","guid":{"rendered":"http:\/\/www.electronic-products-development.com\/?p=4"},"modified":"2022-02-17T10:13:21","modified_gmt":"2022-02-17T10:13:21","slug":"median-filter","status":"publish","type":"post","link":"https:\/\/ibex.tech\/programming\/filters\/median-filter","title":{"rendered":"Median Filter"},"content":{"rendered":"<p>\nA median filter is a type of FIR filter which typically takes an odd number of inputs (e.g. 5 or more usually more) and sorts them by value. The filtered output is the median value (the middle value). A median filter can be very effective at removing noise spikes.\n<\/p>\n<h4>\nExample Code<br \/>\n<\/h4>\n<p>\n&nbsp;\n<\/p>\n<h5>\nStart New Filter Sequence<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\t#define MEDIAN_FILTER_NO_OF_READINGS\t\t\t512\r\n\tWORD count;\r\n\tstatic WORD filter_current_count;\r\n\tstatic WORD filter_buffer[MEDIAN_FILTER_NO_OF_READINGS];\r\n\r\n\t\/\/START NEW FILTER SEQUENCE\r\n\tfilter_current_count = 0;\r\n\tfor (count = 0; count &lt; MEDIAN_FILTER_NO_OF_READINGS; count++)\r\n\t\tfilter_buffer[count] = 0;\r\n<\/code><\/pre>\n<h5>\nAdd Next Reading<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\t\/\/ADD NEXT READING\r\n\t\/\/w_temp is the new reading value to add\r\n\t\/\/Move through the buffer from the last current entry and add this reading in its correct value ordered position\r\n\tfor (count = filter_current_count; count &lt;= filter_current_count; count--)\r\n\t{\r\n\t\tif (count &gt; 0)\r\n\t\t{\r\n\t\t\tif (filter_buffer[(count - 1)] &lt; w_temp)\r\n\t\t\t{\r\n\t\t\t\tfilter_buffer[count] =  w_temp;\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tfilter_buffer[count] = filter_buffer[(count - 1)];\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tfilter_buffer[count] =  w_temp;\t\t\t\/\/This is a new lowest value to add at the bottom of the buffer\r\n\t\t\tbreak;\r\n\t\t}\r\n\t}\r\n\tfilter_current_count++;\r\n<\/code><\/pre>\n<h5>\nGet the Median Result<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\t\/\/GET MEDIAN RESULT\r\n\tvalue_to_use = filter_buffer[(MEDIAN_FILTER_NO_OF_READINGS \/ 2)]\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A median filter is a type of FIR filter which typically takes an odd number of inputs (e.g. 5 or more usually more) and sorts them by value. The filtered output is the median value (the middle value). A median filter can be very effective at removing noise spikes. Example Code &nbsp; Start New Filter [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-4","post","type-post","status-publish","format-standard","hentry","category-filters"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/programming\/wp-json\/wp\/v2\/posts\/4","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/programming\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/programming\/wp-json\/wp\/v2\/comments?post=4"}],"version-history":[{"count":16,"href":"https:\/\/ibex.tech\/programming\/wp-json\/wp\/v2\/posts\/4\/revisions"}],"predecessor-version":[{"id":812,"href":"https:\/\/ibex.tech\/programming\/wp-json\/wp\/v2\/posts\/4\/revisions\/812"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/programming\/wp-json\/wp\/v2\/media?parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/programming\/wp-json\/wp\/v2\/categories?post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/programming\/wp-json\/wp\/v2\/tags?post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}