{"id":2574,"date":"2010-07-02T20:47:17","date_gmt":"2010-07-02T20:47:17","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2010\/07\/02\/learning-the-linear-kalman-filter\/"},"modified":"2010-07-06T14:00:00","modified_gmt":"2010-07-06T14:00:00","slug":"learning-the-linear-kalman-filter","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2010\/07\/02\/learning-the-linear-kalman-filter\/","title":{"rendered":"Learning the (Linear) Kalman Filter"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction><\/introduction>\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/53-shift-learning-the-kalman-filter\">\"Learning the Kalman Filter\"<\/a>, by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/7965\">Michael Kleder<\/a>.\r\n   <\/p>\r\n   <p>If one were to sort the entries of the File Exchange by the number of downloads in the past 30 days, an interesting trend\r\n      would become apparent: a handful of files have been downloaded far more times than the vast majority of the rest of the files\r\n      on the Exchange. Of those, one file in particular has been dowloaded 43% more times than the next most popular file.\r\n   <\/p>\r\n   <p>The popularity of Michael's \"Learning the Kalman Filter\" mini tutorial (as of this writing, it has been downloaded 2803 times\r\n      in the past 30 days--even though the file has been up for 6 years!), along with the great feedback it has garnered (73 comments\r\n      and 67 ratings, averaging 4.5 out of 5 stars), initially caught my eye.\r\n   <\/p>\r\n   <p>Michael wrote in the preamble to his tutorial:<\/p>\r\n   <p>%%%%%%%%%%%%%%%%%%%<\/p>\r\n   <p>Many people have heard of Kalman filtering, but regard the topic as mysterious. While it's true that deriving the Kalman filter\r\n      and proving mathematically that it is \"optimal\" under a variety of circumstances can be rather intense, applying the filter\r\n      to a basic linear system is actually very easy. This Matlab file is intended to demonstrate that.\r\n   <\/p>\r\n   <p>%%%%%%%%%%%%%%%%%%%<\/p>\r\n   <p>In his in-file example, Michael then steps through a Kalman filter example in which a voltmeter is used to measure the output\r\n      of a 12-volt automobile battery. The model simulates both randomness in the output of the battery, and error in the voltmeter\r\n      readings. Then, even without defining an initial state for the true battery voltage, Michael demonstrates that with only 5\r\n      lines of code, the Kalman filter can be implemented to predict the true output based on (not-necessarily-accurate) uniformly\r\n      spaced, measurements:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/..\/images\/pick\/automotivebatteryoutput.png\"> <\/p>\r\n   <p>This is a simple but powerful example that shows the utility and potential of Kalman filters. It's sure to help those who\r\n      are trepid about delving into the world of Kalman filtering.\r\n   <\/p>\r\n   <p>A couple notes on Michael's implementation... First, note that the <tt>return<\/tt> statement at the end of his function is extraneous. But perhaps this code was culled from a larger file in which that command\r\n      served some purpose? Also, as one commenter noted, and as the MLINT Code Analyzer points out, inv(A)*B is better written as\r\n      A\\B. There's a similar suggested substitution for A*Inv(B), both of which can be rendered faster and more accurate using the\r\n      <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/symbolic\/arithmeticoperations.html#bqtg55c\">backslash<\/a> (matrix left division) operator.\r\n   <\/p>\r\n   <p><a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2574#respond\"> Comments?<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_9f96982629a84993be07e91568054671() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='9f96982629a84993be07e91568054671 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 9f96982629a84993be07e91568054671';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Brett Shoelson';\r\n        copyright = 'Copyright 2010 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_9f96982629a84993be07e91568054671()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.11<br><\/p>\r\n<\/div>\r\n<!--\r\n9f96982629a84993be07e91568054671 ##### SOURCE BEGIN #####\r\n%% Learning the (Linear) Kalman Filter\r\n%% \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s Pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/53-shift-learning-the-kalman-filter \"Learning the Kalman Filter\">, by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/7965 Michael Kleder>.\r\n\r\n%%\r\n% If one were to sort the entries of the File Exchange by the number of\r\n% downloads in the past 30 days, an interesting trend would become\r\n% apparent: a handful of files have been downloaded far more times than the\r\n% vast majority of the rest of the files on the Exchange. Of those, one\r\n% file in particular has been dowloaded 43% more times than the next most\r\n% popular file. \r\n\r\n%%\r\n% The popularity of Michael's \"Learning the Kalman Filter\" mini tutorial\r\n% (as of this writing, it has been downloaded 2803 times in the past 30\r\n% daysREPLACE_WITH_DASH_DASHeven though the file has been up for 6 years!), along with the great feedback it has garnered (73 comments and 67\r\n% ratings, averaging 4.5 out of 5 stars), initially caught my eye.\r\n\r\n%% \r\n% Michael wrote in the preamble to his tutorial:\r\n%%\r\n% %%%%%%%%%%%%%%%%%%%\r\n%%\r\n% Many people have heard of Kalman filtering, but regard the topic\r\n% as mysterious. While it's true that deriving the Kalman filter and\r\n% proving mathematically that it is \"optimal\" under a variety of\r\n% circumstances can be rather intense, applying the filter to a basic\r\n% linear system is actually very easy. This Matlab file is intended to\r\n% demonstrate that.\r\n%%\r\n% %%%%%%%%%%%%%%%%%%%\r\n%%\r\n% In his in-file example, Michael then steps through a Kalman filter\r\n% example in which a voltmeter is used to measure the output of a 12-volt\r\n% automobile battery. The model simulates both randomness in the output of\r\n% the battery, and error in the voltmeter readings. Then, even\r\n% without defining an initial state for the true battery voltage, Michael\r\n% demonstrates that with only 5 lines of code, the Kalman filter can be\r\n% implemented to predict the true output based on (not-necessarily-accurate) uniformly\r\n% spaced, measurements:\r\n\r\n%%\r\n% \r\n% <<AutomotiveBatteryOutput.png>>\r\n% \r\n%%\r\n% This is a simple but powerful example that shows the utility and\r\n% potential of Kalman filters. It's sure to help those who are\r\n% trepid about delving into the world of Kalman filtering.\r\n\r\n%%\r\n% A couple notes on Michael's implementation...\r\n% First, note that the |return| statement at the end of his function is\r\n% extraneous. But perhaps this code was culled from a larger file in which that\r\n% command served some purpose? Also, as one commenter noted, and as the\r\n% MLINT Code Analyzer points out, inv(A)*B is better written as A\\B.\r\n% There's a similar suggested substitution for A*Inv(B), both of which can\r\n% be rendered faster and more accurate using the <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/symbolic\/arithmeticoperations.html#bqtg55c backslash> (matrix left division) operator.\r\n%%\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2574#respond  Comments?>.\r\n\r\n##### SOURCE END ##### 9f96982629a84993be07e91568054671\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n   Brett's Pick this week is \"Learning the Kalman Filter\", by Michael Kleder.\r\n   \r\n   If one were to sort the entries of the File Exchange by the number of downloads in the past 30 days, an... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2010\/07\/02\/learning-the-linear-kalman-filter\/\">read more >><\/a><\/p>","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2574"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2574"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2574\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}