[{"data":1,"prerenderedAt":419},["ShallowReactive",2],{"blog-post-/blog/kalman-filter/":3},{"id":4,"title":5,"body":6,"description":396,"extension":397,"meta":398,"navigation":83,"path":414,"seo":415,"sitemap":416,"stem":417,"__hash__":418},"content/blog/kalman-filter.md","Kalman Filter Explained with MATLAB Code Example",{"type":7,"value":8,"toc":387},"minimark",[9,14,18,23,26,31,42,46,52,56,63,347,351,370,374,383],[10,11,13],"h1",{"id":12},"kalman-filter-explained-with-matlab-code","Kalman Filter Explained with MATLAB Code",[15,16,17],"p",{},"The Kalman Filter is a recursive algorithm for estimating the state of a dynamic system from noisy observations. Developed by Rudolf E. Kálmán in 1960, it's widely used in robotics, navigation, aerospace, finance, and more.",[19,20,22],"h2",{"id":21},"what-is-the-kalman-filter","What is the Kalman Filter?",[15,24,25],{},"The Kalman Filter maintains a probabilistic (Gaussian) estimate of the system state over time, updating recursively with each new measurement using two steps:",[27,28,30],"h3",{"id":29},"prediction-step","Prediction Step",[32,33,38],"pre",{"className":34,"code":36,"language":37},[35],"language-text","x̂_k|k-1 = F_k * x̂_k-1|k-1 + B_k * u_k\nP_k|k-1 = F_k * P_k-1|k-1 * F_k^T + Q_k\n","text",[39,40,36],"code",{"__ignoreMap":41},"",[27,43,45],{"id":44},"update-step","Update Step",[32,47,50],{"className":48,"code":49,"language":37},[35],"K = P_k|k-1 * C^T * (C * P_k|k-1 * C^T + R)^-1\nx̂_k = x̂_k|k-1 + K * (y_k - C * x̂_k|k-1)\nP_k = (I - K * C) * P_k|k-1\n",[39,51,49],{"__ignoreMap":41},[19,53,55],{"id":54},"matlab-code-estimating-position-with-constant-acceleration","MATLAB Code: Estimating Position with Constant Acceleration",[15,57,58,62],{},[59,60,61],"strong",{},"Problem:"," A vehicle has an acceleration of 3 ft/sec², position measured every 0.1 seconds with 5 ft noise, acceleration noise of 0.1 ft/sec². Estimate position over 20 seconds.",[32,64,68],{"className":65,"code":66,"language":67,"meta":41,"style":41},"language-matlab shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","clc; clear;\n\nduration = 20;\ndt = 0.1;\nmeasnoise = 5;       % position measurement noise (feet)\naccelnoise = 0.1;    % acceleration noise (feet/sec^2)\n\nA = [1 dt; 0 1];     % transition matrix\nB = [dt^2/2; dt];    % input matrix\nC = [1 0];           % measurement matrix\nx = [0; 0];          % initial state\nxhat = x;            % initial estimate\n\nSz = measnoise^2;\nSw = accelnoise^2 * [dt^4/4 dt^3/2; dt^3/2 dt^2];\nP = Sw;\n\ntpos = []; poshat = []; posmeasured = [];\n\nfor t = 0:dt:duration\n    u = 3; % constant acceleration\n    ProcessNoise = accelnoise * [(dt^2/2)*randn; dt*randn];\n    x = A*x + B*u + ProcessNoise;\n\n    MeasNoise = measnoise * randn;\n    y = C*x + MeasNoise;\n\n    xhat = A*xhat + B*u;\n    Inn = y - C*xhat;\n    s = C*P*C' + Sz;\n    K = A*P*C' * inv(s);\n    xhat = xhat + K*Inn;\n    P = A*P*A' - A*P*C'*inv(s)*C*P*A' + Sw;\n\n    tpos = [tpos; x(1)];\n    posmeasured = [posmeasured; y];\n    poshat = [poshat; xhat(1)];\nend\n\nt = 0:dt:duration;\nfigure;\nplot(t, tpos, 'g', 'LineWidth', 2); hold on;\nplot(t, posmeasured, 'r.', 'LineWidth', 2);\nplot(t, poshat, 'k', 'LineWidth', 2);\ntitle('Position Estimations');\nxlabel('Time (sec)'); ylabel('Position (feet)');\nlegend('True Position', 'Measured Position', 'Estimated Position');\n","matlab",[39,69,70,78,85,91,97,103,109,114,120,126,132,138,144,149,155,161,167,172,178,183,189,195,201,207,212,218,224,229,235,241,247,253,259,265,270,276,282,288,294,299,305,311,317,323,329,335,341],{"__ignoreMap":41},[71,72,75],"span",{"class":73,"line":74},"line",1,[71,76,77],{},"clc; clear;\n",[71,79,81],{"class":73,"line":80},2,[71,82,84],{"emptyLinePlaceholder":83},true,"\n",[71,86,88],{"class":73,"line":87},3,[71,89,90],{},"duration = 20;\n",[71,92,94],{"class":73,"line":93},4,[71,95,96],{},"dt = 0.1;\n",[71,98,100],{"class":73,"line":99},5,[71,101,102],{},"measnoise = 5;       % position measurement noise (feet)\n",[71,104,106],{"class":73,"line":105},6,[71,107,108],{},"accelnoise = 0.1;    % acceleration noise (feet/sec^2)\n",[71,110,112],{"class":73,"line":111},7,[71,113,84],{"emptyLinePlaceholder":83},[71,115,117],{"class":73,"line":116},8,[71,118,119],{},"A = [1 dt; 0 1];     % transition matrix\n",[71,121,123],{"class":73,"line":122},9,[71,124,125],{},"B = [dt^2/2; dt];    % input matrix\n",[71,127,129],{"class":73,"line":128},10,[71,130,131],{},"C = [1 0];           % measurement matrix\n",[71,133,135],{"class":73,"line":134},11,[71,136,137],{},"x = [0; 0];          % initial state\n",[71,139,141],{"class":73,"line":140},12,[71,142,143],{},"xhat = x;            % initial estimate\n",[71,145,147],{"class":73,"line":146},13,[71,148,84],{"emptyLinePlaceholder":83},[71,150,152],{"class":73,"line":151},14,[71,153,154],{},"Sz = measnoise^2;\n",[71,156,158],{"class":73,"line":157},15,[71,159,160],{},"Sw = accelnoise^2 * [dt^4/4 dt^3/2; dt^3/2 dt^2];\n",[71,162,164],{"class":73,"line":163},16,[71,165,166],{},"P = Sw;\n",[71,168,170],{"class":73,"line":169},17,[71,171,84],{"emptyLinePlaceholder":83},[71,173,175],{"class":73,"line":174},18,[71,176,177],{},"tpos = []; poshat = []; posmeasured = [];\n",[71,179,181],{"class":73,"line":180},19,[71,182,84],{"emptyLinePlaceholder":83},[71,184,186],{"class":73,"line":185},20,[71,187,188],{},"for t = 0:dt:duration\n",[71,190,192],{"class":73,"line":191},21,[71,193,194],{},"    u = 3; % constant acceleration\n",[71,196,198],{"class":73,"line":197},22,[71,199,200],{},"    ProcessNoise = accelnoise * [(dt^2/2)*randn; dt*randn];\n",[71,202,204],{"class":73,"line":203},23,[71,205,206],{},"    x = A*x + B*u + ProcessNoise;\n",[71,208,210],{"class":73,"line":209},24,[71,211,84],{"emptyLinePlaceholder":83},[71,213,215],{"class":73,"line":214},25,[71,216,217],{},"    MeasNoise = measnoise * randn;\n",[71,219,221],{"class":73,"line":220},26,[71,222,223],{},"    y = C*x + MeasNoise;\n",[71,225,227],{"class":73,"line":226},27,[71,228,84],{"emptyLinePlaceholder":83},[71,230,232],{"class":73,"line":231},28,[71,233,234],{},"    xhat = A*xhat + B*u;\n",[71,236,238],{"class":73,"line":237},29,[71,239,240],{},"    Inn = y - C*xhat;\n",[71,242,244],{"class":73,"line":243},30,[71,245,246],{},"    s = C*P*C' + Sz;\n",[71,248,250],{"class":73,"line":249},31,[71,251,252],{},"    K = A*P*C' * inv(s);\n",[71,254,256],{"class":73,"line":255},32,[71,257,258],{},"    xhat = xhat + K*Inn;\n",[71,260,262],{"class":73,"line":261},33,[71,263,264],{},"    P = A*P*A' - A*P*C'*inv(s)*C*P*A' + Sw;\n",[71,266,268],{"class":73,"line":267},34,[71,269,84],{"emptyLinePlaceholder":83},[71,271,273],{"class":73,"line":272},35,[71,274,275],{},"    tpos = [tpos; x(1)];\n",[71,277,279],{"class":73,"line":278},36,[71,280,281],{},"    posmeasured = [posmeasured; y];\n",[71,283,285],{"class":73,"line":284},37,[71,286,287],{},"    poshat = [poshat; xhat(1)];\n",[71,289,291],{"class":73,"line":290},38,[71,292,293],{},"end\n",[71,295,297],{"class":73,"line":296},39,[71,298,84],{"emptyLinePlaceholder":83},[71,300,302],{"class":73,"line":301},40,[71,303,304],{},"t = 0:dt:duration;\n",[71,306,308],{"class":73,"line":307},41,[71,309,310],{},"figure;\n",[71,312,314],{"class":73,"line":313},42,[71,315,316],{},"plot(t, tpos, 'g', 'LineWidth', 2); hold on;\n",[71,318,320],{"class":73,"line":319},43,[71,321,322],{},"plot(t, posmeasured, 'r.', 'LineWidth', 2);\n",[71,324,326],{"class":73,"line":325},44,[71,327,328],{},"plot(t, poshat, 'k', 'LineWidth', 2);\n",[71,330,332],{"class":73,"line":331},45,[71,333,334],{},"title('Position Estimations');\n",[71,336,338],{"class":73,"line":337},46,[71,339,340],{},"xlabel('Time (sec)'); ylabel('Position (feet)');\n",[71,342,344],{"class":73,"line":343},47,[71,345,346],{},"legend('True Position', 'Measured Position', 'Estimated Position');\n",[19,348,350],{"id":349},"applications","Applications",[352,353,354,358,361,364,367],"ul",{},[355,356,357],"li",{},"Navigation & GPS systems",[355,359,360],{},"Robotics and autonomous vehicles",[355,362,363],{},"Aerospace and flight control",[355,365,366],{},"Financial time series estimation",[355,368,369],{},"Signal processing and sensor fusion",[19,371,373],{"id":372},"references","References",[375,376,377,380],"ol",{},[355,378,379],{},"R. E. Kalman, \"A new approach to linear filtering and prediction problems,\" ASME Journal of Basic Engineering, 1960.",[355,381,382],{},"G. Welch & G. Bishop, \"An introduction to the Kalman filter,\" UNC Chapel Hill, 2006.",[384,385,386],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":41,"searchDepth":80,"depth":80,"links":388},[389,393,394,395],{"id":21,"depth":80,"text":22,"children":390},[391,392],{"id":29,"depth":87,"text":30},{"id":44,"depth":87,"text":45},{"id":54,"depth":80,"text":55},{"id":349,"depth":80,"text":350},{"id":372,"depth":80,"text":373},"What is the Kalman Filter? How does it work? Learn the theory behind Kalman filtering and see a complete MATLAB implementation for estimating the position of an accelerating object.","md",{"author":399,"date":400,"image":401,"category":402,"tags":403,"featured":413,"draft":413},"Aysegul Karadan","2023-03-16T10:00:00.000Z","/img/kalman/Kalman.png","Engineering",[67,404,405,406,407,408,409,410,411,412],"engineering","signal-processing","kalman-filter","kalman-filter-matlab","how-kalman-filter-works","state-estimation","kalman-filter-tutorial","kalman-filter-example","control-systems",false,"/blog/kalman-filter",{"title":5,"description":396},{"loc":414},"blog/kalman-filter","m-8lKFzYXuBthE1hgBE7yAUlq8xZIFz8-AVGK2Gprt4",1782986782500]