{"id":734,"date":"2022-01-27T14:58:53","date_gmt":"2022-01-27T14:58:53","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=734"},"modified":"2022-06-16T10:22:35","modified_gmt":"2022-06-16T10:22:35","slug":"stm32-and-digital-filters-infinite-impulse-response-filter-iir-filter","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=734","title":{"rendered":"STM32 and digital filters: Infinite Impulse Response Filter IIR Filter"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/d2mvzyuse3lwjc.cloudfront.net\/doc\/en\/UserGuide\/images\/Algorithm_(IIR_Filters)\/IIR_Filter_1.png?v=30535\" alt=\"\"\/><\/figure><\/div>\n\n\n\n<p>In this guide, we shall see how to implement the digital filters, specifically IIR filter.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is IIR filter<\/li><li>Software implementation <\/li><li>Deploying the algorithm on STM32<\/li><li>Code<\/li><li>Demo<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.1 What is Impulse Response Filter:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Infinite impulse response<\/strong>&nbsp;(<strong>IIR<\/strong>) is a property applying to many&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Linear_time-invariant_system\">linear time-invariant systems<\/a>&nbsp;that are distinguished by having an&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Impulse_response\">impulse response<\/a>&nbsp;<img decoding=\"async\" src=\"https:\/\/wikimedia.org\/api\/rest_v1\/media\/math\/render\/svg\/66abbb8ae1d9f30bb529739b109e1e5bbe83c626\" alt=\"h(t)\">&nbsp;which does not become exactly zero past a certain point, but continues indefinitely. This is in contrast to a&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Finite_impulse_response\">finite impulse response<\/a>&nbsp;(FIR) system in which the impulse response&nbsp;&nbsp;<em>does<\/em>&nbsp;become exactly zero at times&nbsp;<img decoding=\"async\" src=\"https:\/\/wikimedia.org\/api\/rest_v1\/media\/math\/render\/svg\/15e924e5552cecd10feee46d05efe1f91bfb6f86\">T&#8221;&gt;&nbsp;for some finite&nbsp;<img decoding=\"async\" src=\"https:\/\/wikimedia.org\/api\/rest_v1\/media\/math\/render\/svg\/ec7200acd984a1d3a3d7dc455e262fbe54f7f6e0\" alt=\"T\">, thus being of finite duration. Common examples of linear time-invariant systems are most&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Electronic_filter\">electronic<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Digital_filter\">digital filters<\/a>. Systems with this property are known as&nbsp;<em>IIR systems<\/em>&nbsp;or&nbsp;<em>IIR filters<\/em>.<\/p>\n\n\n\n<p>In practice, the impulse response, even of IIR systems, usually approaches zero and can be neglected past a certain point. However the physical systems which give rise to IIR or FIR responses are dissimilar, and therein lies the importance of the distinction. For instance, analog electronic filters composed of resistors, capacitors, and\/or inductors (and perhaps linear amplifiers) are generally IIR filters. On the other hand,&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Discrete-time_filter\">discrete-time filters<\/a>&nbsp;(usually digital filters) based on a tapped delay line&nbsp;<em>employing no feedback<\/em>&nbsp;are necessarily FIR filters. The capacitors (or inductors) in the analog filter have a &#8220;memory&#8221; and their internal state never completely relaxes following an impulse (assuming the classical model of capacitors and inductors where quantum effects are ignored). But in the latter case, after an impulse has reached the end of the tapped delay line, the system has no further memory of that impulse and has returned to its initial state; its impulse response beyond that point is exactly zero. (from wikipedia)<\/p>\n\n\n\n<p>A first way to express this filter with an equation is the following:<br>yn&nbsp;=(1\u2212a)yn\u22121&nbsp;+axn&nbsp;(1)<\/p>\n\n\n\n<p>where&nbsp;xn&nbsp;is the input signal,&nbsp;yn&nbsp;the output signal, and&nbsp;\u03b3&nbsp;the (fixed) coefficient of the filter. This coefficient&nbsp;\u03b3&nbsp;is also sometimes known as the&nbsp;\u201dforget factor\u201d: the closer&nbsp;\u03b3&nbsp;is to 1, the faster the filter \u201dforgets\u201d the old inputs.<\/p>\n\n\n\n<p>In this interpretation, we consider the filter as a smoother: each output sample is a&nbsp;weighted mean&nbsp;betwen the last input sample and the previously computed value.<\/p>\n\n\n\n<p>The relation with an electrical RC filter appears clearly if we apply this filter to an inversed step1&nbsp;(xn\u22640&nbsp;= 1, xn&gt;0&nbsp;= 0) :<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-27-at-5.42.14-PM.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"910\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-27-at-5.42.14-PM-1024x910.png\" alt=\"\" class=\"wp-image-735\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-27-at-5.42.14-PM-1024x910.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-27-at-5.42.14-PM-300x267.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-27-at-5.42.14-PM-768x682.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-27-at-5.42.14-PM-1536x1365.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-27-at-5.42.14-PM-1150x1022.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-27-at-5.42.14-PM-750x666.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-27-at-5.42.14-PM-400x355.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-27-at-5.42.14-PM-250x222.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-27-at-5.42.14-PM.png 1940w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>yn&nbsp;=(1\u2212\u03b3)yn\u22121&nbsp;=(1\u2212\u03b3)ny0&nbsp;=(1\u2212\u03b3)n&nbsp;supposing that y0&nbsp;=1<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.Software Implementation:<\/h2>\n\n\n\n<p>If one uses a processor with floating point computing available, then the imple- mentation is trivial and correspond directly to the equation (1) :<\/p>\n\n\n\n<p>y = (1 &#8211; alpha) * y + alpha * x;<br>where&nbsp;y&nbsp;is used both as an accumulator and as an output variable.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Deploying the algorithm on STM32: <\/h2>\n\n\n\n<p>We start of by creating a header file and name it First_Order_IIR_Filter.h<\/p>\n\n\n\n<p>Inside this header file, we shall use a structure that holds the  alpha and output values as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">typedef struct\n\t{\n\tfloat alpha;\n\tfloat out;\n\n\t}FirstOrderIIR;<\/pre><\/div>\n\n\n\n<p>and two functions that shall initialize the filter and calculate the output as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void FirstOrderIIRFilter_Init(FirstOrderIIR *filter, float alpha);\nfloat FirstOrderIRR_Calculate(FirstOrderIIR *filter, float in);\n<\/pre><\/div>\n\n\n\n<p>Hence, the header file is as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#ifndef FIRST_ORDER_IIR_FILTER_H_\n#define FIRST_ORDER_IIR_FILTER_H_\n\n\ntypedef struct\n\t{\n\tfloat alpha;\n\tfloat out;\n\n\t}FirstOrderIIR;\n\nvoid FirstOrderIIRFilter_Init(FirstOrderIIR *filter, float alpha);\nfloat FirstOrderIRR_Calculate(FirstOrderIIR *filter, float in);\n\n#endif \/* FIRST_ORDER_IIR_FILTER_H_ *\/\n<\/pre><\/div>\n\n\n\n<p>Next, we create source file with same name of the header file.<\/p>\n\n\n\n<p>For the initializing function, we shall first check if the alpha value is between 0 and 1 and set the output to zero<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void FirstOrderIIRFilter_Init(FirstOrderIIR *filter, float alpha)\n\t{\n\tif(alpha&lt;0.0f){alpha=0.0f;}\n\telse if (alpha&gt;1.0f){alpha=1.0f;}\n\telse {filter-&gt;alpha=alpha;}\n\tfilter-&gt;out=0.0f;\n\t}<\/pre><\/div>\n\n\n\n<p>For the calculation, we can use the equation from the software implementation as following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">float FirstOrderIRR_Calculate(FirstOrderIIR *filter, float in)\n\t{\n\n\tfilter-&gt;out=(1.0f-filter-&gt;alpha)*in+(filter-&gt;alpha)*filter-&gt;out;\n\n\n\treturn filter-&gt;out;\n\t}\n<\/pre><\/div>\n\n\n\n<p>For the output, HMC5883L Magnetic Field Sensor is used and you can get the code from <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=724\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=724\" target=\"_blank\">here<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Code:<\/h2>\n\n\n\n<p>You can download the code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/IIR_Filter_STM32.zip\">IIR_Filter_STM32<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/IIR_Filter_STM32.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall see how to implement the digital filters, specifically IIR filter. In this guide, we shall cover the following: What is IIR filter Software implementation Deploying the algorithm on STM32 Code Demo 1.1 What is Impulse Response Filter: Infinite impulse response&nbsp;(IIR) is a property applying to many&nbsp;linear time-invariant systems&nbsp;that are distinguished [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2,11,12],"tags":[],"class_list":["post-734","post","type-post","status-publish","format-standard","hentry","category-data-structures","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/734"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=734"}],"version-history":[{"count":10,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/734\/revisions"}],"predecessor-version":[{"id":1032,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/734\/revisions\/1032"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}