{"id":46,"date":"2011-10-23T23:00:03","date_gmt":"2011-10-24T04:00:03","guid":{"rendered":"http:\/\/www.graygoo.net\/blog\/?p=46"},"modified":"2011-10-24T13:55:36","modified_gmt":"2011-10-24T18:55:36","slug":"more-matlab-filtering","status":"publish","type":"post","link":"http:\/\/www.graygoo.net\/blog\/2011\/10\/more-matlab-filtering\/","title":{"rendered":"More Matlab filtering"},"content":{"rendered":"<p>Previously, I covered discrete-time filters. Matlab has functions for continuous-time filters as well. The transform of interest is the Laplace transform. As before, we obtain a transfer function, this time in the s-domain. We have the numerator B(s) and denominator A(s) in <em>descending\u00a0powers<\/em>\u00a0of s. It is common, but not necessary, to normalize the leading term of A to 1. Suppose<\/p>\n<p><a href=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/10\/CodeCogsEqn1.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-48\" title=\"CodeCogsEqn\" src=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/10\/CodeCogsEqn1.gif\" alt=\"\" width=\"152\" height=\"44\" srcset=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/10\/CodeCogsEqn1.gif 152w, http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/10\/CodeCogsEqn1-150x44.gif 150w\" sizes=\"auto, (max-width: 152px) 100vw, 152px\" \/><\/a>In Matlab,<\/p>\n<pre>B=[5];A=[1,2,3];<\/pre>\n<p>Through basic algebra, we have the partial fraction expansion<\/p>\n<p><a href=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/10\/CodeCogsEqn-1.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-49\" title=\"CodeCogsEqn (1)\" src=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/10\/CodeCogsEqn-1.gif\" alt=\"\" width=\"233\" height=\"41\" \/><\/a><\/p>\n<p>Matlab can calculate the partial fraction expansion of this function with the residue command. The output vectors r,p,k contain the <em>residues <\/em>(r terms above), poles (p terms), and the direct term k, which is not present in this function.<\/p>\n<pre>[r,p,k]=residue(B,A);\r\n\r\nr =\r\n        0 - 1.7678i\r\n        0 + 1.7678i\r\np =\r\n  -1.0000 + 1.4142i\r\n  -1.0000 - 1.4142i\r\nk =\r\n     []<\/pre>\n<p>Often we&#8217;ll want to graph the impulse or step response of a filter. To have Matlab do it, we need to box it up into a transfer function object using the tf command on B and A, and then use the intuitively-named impulse or step commands. In passing, it can be noted that since the Laplace transform of the unit step is just 1\/s vs. 1 for the impulse, that the step response is just the impulse response with A multiplied by s (i.e. with a trailing zero).<\/p>\n<pre>figure; step(tf(B,A))\r\nfigure; impulse(tf(B,A))<\/pre>\n<p>The resulting plots are shown below.<\/p>\n<p><a href=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/10\/step.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-50 alignnone\" title=\"step\" src=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/10\/step-300x225.png\" alt=\"\" width=\"300\" height=\"225\" srcset=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/10\/step-300x225.png 300w, http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/10\/step.png 560w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-51 alignnone\" style=\"border-style: initial; border-color: initial;\" title=\"impulse\" src=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/10\/impulse-300x225.png\" alt=\"\" width=\"300\" height=\"225\" srcset=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/10\/impulse-300x225.png 300w, http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/10\/impulse.png 560w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>More generally, we can graph the response of an arbitrary linear function u(t) on the time interval [0,T] with the lsim command. Define vector u on a time interval t (in the <em>time<\/em>\u00a0domain), and the lsim command graphs the response. Note the similarity between the lsim response and the step response above.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-53 alignnone\" style=\"border-style: initial; border-color: initial;\" title=\"linear\" src=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/10\/linear1-300x225.png\" alt=\"\" width=\"300\" height=\"225\" srcset=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/10\/linear1-300x225.png 300w, http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/10\/linear1.png 560w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<pre>t=linspace(0,10,10000); u=ones(1,length(t));\r\nfigure; lsim(tf(B,A),u,t)<\/pre>\n<p>We can get the poles and zeros from the transfer function object with the pole() and zero() command. If we are performing multiple operations with the object, it&#8217;s worthwhile to create another variable for it.<\/p>\n<pre>\r\nH=tf(B,A);\r\npole(H)\r\n\r\nans =\r\n\r\n  -1.0000 + 1.4142i\r\n  -1.0000 - 1.4142i\r\n\r\nzero(H)\r\n\r\nans =\r\n\r\n   Empty matrix: 0-by-1\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Previously, I covered discrete-time filters. Matlab has functions for continuous-time filters as well. The transform of interest is the Laplace transform. As before, we obtain a transfer function, this time in the s-domain. We have the numerator B(s) and denominator &hellip; <a href=\"http:\/\/www.graygoo.net\/blog\/2011\/10\/more-matlab-filtering\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/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],"tags":[13],"class_list":["post-46","post","type-post","status-publish","format-standard","hentry","category-ugoo","tag-matlab"],"_links":{"self":[{"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/posts\/46","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/comments?post=46"}],"version-history":[{"count":0,"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<!-- WP Super Cache is installed but broken. The constant WPCACHEHOME must be set in the file wp-config.php and point at the WP Super Cache plugin directory. -->