{"id":26,"date":"2011-08-15T18:37:52","date_gmt":"2011-08-15T23:37:52","guid":{"rendered":"http:\/\/www.graygoo.net\/blog\/?p=26"},"modified":"2011-08-19T14:57:10","modified_gmt":"2011-08-19T19:57:10","slug":"fun-with-filters","status":"publish","type":"post","link":"http:\/\/www.graygoo.net\/blog\/2011\/08\/fun-with-filters\/","title":{"rendered":"Fun with filters"},"content":{"rendered":"<p>In this post, I am going to review a number of Matlab functions useful for discrete-time signal processing.<\/p>\n<h2>The transfer function<\/h2>\n<p style=\"text-align: justify;\">Suppose we have a discrete-time signal x[n] sampled with frequency Fs, and wish to pass it through a filter with output y[n]. The filter has impulse response h[n]. The output of the system y[n] is the <a href=\"http:\/\/jhu.edu\/~signals\/discreteconv2\/index.html\">convolution<\/a> of the input and the filter, x[n]*h[n]. Taking the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Z-transform\">z-transform<\/a> of the system takes convolution to multiplication, resulting in the equation<\/p>\n<p><a href=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/gif.latex_.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27 aligncenter\" title=\"z1\" src=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/gif.latex_.gif\" alt=\"\" width=\"138\" height=\"19\" \/><\/a>H(z) is known as the transfer function of the filter, and is represented in the general case as a fraction B\/A of two polynomials in z.<\/p>\n<p><a href=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/img21.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-32 aligncenter\" title=\"img2\" src=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/img21-300x42.png\" alt=\"\" width=\"300\" height=\"42\" srcset=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/img21-300x42.png 300w, http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/img21.png 316w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>In practice, it is easier to work with powers of 1\/z than z, so we factor out an appropriate power of z to put H in terms of 1\/z.<\/p>\n<p><a href=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/img3.png\"><br \/>\n<\/a>This gives two vectors, B and A, of polynomial coefficients in ascending powers of 1\/z. These vectors characterize the filter in Matlab.<\/p>\n<h2>A moving average filter<\/h2>\n<p style=\"text-align: justify;\">The basic Matlab filter command is <code>filter.<\/code><code>filter<\/code> takes a filter [B,A] and an input vector x[n] and returns its output y[n]. Let&#8217;s create a 63-point moving average filter, with sampling frequency 1kHz. This filter &#8220;smooths&#8221; its output by averaging the last N=63 samples. This is a finite impulse response filter, so A is equal to 1. B is the average of the last 63 samples; i.e. a length 63 vector in 1\/z, divided by 63.<\/p>\n<pre>a=1; b=ones(1,63)\/63;<\/pre>\n<p>Now let&#8217;s look at its effect on a couple of sine waves. The output is an attenuated version of the input.<\/p>\n<pre>\r\nFs=1000; t=linspace(0,2*pi,Fs); x1=sin(t);x2=sin(10*t);\r\ny1=filter(b,a,x1);y2=filter(b,a,x2);\r\nsubplot(2,1,1); plot(t,y1); subplot(2,1,2); plot(t,y2);<\/pre>\n<p><a href=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/filt1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-36\" title=\"filt1\" src=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/filt1-300x225.png\" alt=\"\" width=\"300\" height=\"225\" srcset=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/filt1-300x225.png 300w, http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/filt1.png 560w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h2>Matlab z-plane functions<\/h2>\n<p style=\"text-align: justify;\">We can look at the entire frequency response with the <code>freqz<\/code> function. One invocation of <code>freqz<\/code> takes our filter [B,A], along with the number of (uniformly spaced) frequencies and the sampling rate Fs, and returns a plot of the frequency response from 0 to Fs. Our discrete time moving average filter is a low pass filter, with some interesting oscillating behavior compared to an analog filter (to be explored later). Note that the response is symmetric with respect to Fs\/2, which is the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Nyquist%E2%80%93Shannon_sampling_theorem\">Nyquist frequency<\/a> of the input signal.<\/p>\n<p><code>freqz(b,a,512,'whole',Fs)<\/code><\/p>\n<p><a href=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/filt2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-37\" title=\"filt2\" src=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/filt2-300x224.png\" alt=\"\" width=\"300\" height=\"224\" srcset=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/filt2-300x224.png 300w, http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/filt2.png 562w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">The corresponding impulse response (i.e. in the time domain) is computed by the <code>impz<\/code> command. The impulse response for this uniformly weighted filter is pretty boring.<\/p>\n<pre>impz(b,a)<\/pre>\n<p><a href=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/impulse.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-39\" title=\"impulse\" src=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/impulse-300x225.png\" alt=\"\" width=\"300\" height=\"225\" srcset=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/impulse-300x225.png 300w, http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/impulse.png 560w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><code>zplane<\/code> produces a pole-zero plot (where the poles and zeros are the roots of the polynomials B and A, obtained from the <code>roots<\/code> command) on the complex plane.<\/p>\n<pre>\r\nz=roots(b);\r\nzplane(b,a);\r\n<\/pre>\n<p><a href=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/zplane.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-38\" title=\"zplane\" src=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/zplane-300x225.png\" alt=\"\" width=\"300\" height=\"225\" srcset=\"http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/zplane-300x225.png 300w, http:\/\/www.graygoo.net\/blog\/wp-content\/uploads\/2011\/08\/zplane.png 560w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, I am going to review a number of Matlab functions useful for discrete-time signal processing. The transfer function Suppose we have a discrete-time signal x[n] sampled with frequency Fs, and wish to pass it through a filter &hellip; <a href=\"http:\/\/www.graygoo.net\/blog\/2011\/08\/fun-with-filters\/\">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":[11,13],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-ugoo","tag-dsp","tag-matlab"],"_links":{"self":[{"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/posts\/26","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=26"}],"version-history":[{"count":0,"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.graygoo.net\/blog\/wp-json\/wp\/v2\/tags?post=26"}],"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. -->