1 00:00:00,012 --> 00:00:04,088 In this video we're going to implement a digital filter. 2 00:00:04,088 --> 00:00:10,285 Now, I'm assuming that we have a signal. That we have run through an A2D converter, 3 00:00:10,285 --> 00:00:15,802 it's been sampled appropriately. And now we're have it inside the computer 4 00:00:15,802 --> 00:00:20,091 and now we want to do some signal processing on that signal. 5 00:00:20,091 --> 00:00:25,950 The most common thing to do of course is to filter it and we have to figure out how 6 00:00:25,950 --> 00:00:31,933 to actually implement additional filter. Implementation here, of course, means 7 00:00:31,933 --> 00:00:35,882 software and we have many things we can do in software. 8 00:00:35,882 --> 00:00:41,918 You can do esentially anything you want, but we need something that will wind up, 9 00:00:41,918 --> 00:00:48,218 giving us a linear shifting during filter, and the technique I'm going to talk about 10 00:00:48,218 --> 00:00:54,951 today is the difference equation approach. This is a quite general approach, and will 11 00:00:54,951 --> 00:01:00,711 allow us to have any kind of filter we want low pass, high pass, band pass. 12 00:01:00,712 --> 00:01:05,816 We're going to talk though about some filter categories that come up in the 13 00:01:05,816 --> 00:01:11,064 digital world that this kind of categorization doesn't come up in analog 14 00:01:11,064 --> 00:01:14,859 filters at all. So it turns out to be very important for 15 00:01:14,859 --> 00:01:20,599 the digital filters to understand this category, and we're going to develop new 16 00:01:20,599 --> 00:01:25,869 input, output relationships both in the time domain and in the frequency. 17 00:01:25,869 --> 00:01:32,441 And we'll see how that works. Well first of all we have to define what a 18 00:01:32,441 --> 00:01:38,656 difference equation is. So, we have for example a, what we want to 19 00:01:38,656 --> 00:01:43,613 do is to implement a linear shift invariant system. 20 00:01:43,613 --> 00:01:48,069 And as always we are going to let x be our input. 21 00:01:48,069 --> 00:01:52,642 And y would be our output. And the big question is going to be how do 22 00:01:52,642 --> 00:01:57,235 you actually build filters? How do you implement them for digital 23 00:01:57,235 --> 00:02:00,925 signals? Don't have circuits anymore you're inside 24 00:02:00,925 --> 00:02:03,836 the computer. It's all done in software. 25 00:02:03,836 --> 00:02:09,272 So the technique we're going to use is one called the difference equation. 26 00:02:09,272 --> 00:02:16,622 And a difference equation is quite simple. You have here the output and you can see 27 00:02:16,622 --> 00:02:23,256 that the output depends on previous outputs, the one just before it and the 28 00:02:23,256 --> 00:02:30,483 one that was [unknown] samples away. You multiply them by the constants and you 29 00:02:30,483 --> 00:02:35,381 add them all up. Then you take the current input, the 30 00:02:35,381 --> 00:02:42,677 previous input when q back in time, multiply them by their own constants, add 31 00:02:42,677 --> 00:02:48,856 them up, take the sum of all that, and that is your current output. 32 00:02:48,856 --> 00:02:54,957 So the output now. Equals the previous output times A1, plus 33 00:02:54,957 --> 00:03:01,946 the 1P times ago times AP, plus B0 times the current input, et cetera. 34 00:03:01,946 --> 00:03:09,638 So, you can see, it's a pretty simple thing, and it turns out we will only need 35 00:03:09,638 --> 00:03:17,301 to know these What are known as filter coefficients. 36 00:03:17,301 --> 00:03:24,621 The a's and the b's. And how many of them there are. 37 00:03:24,621 --> 00:03:30,538 That's the p, and q. Once we have those and on the filter 38 00:03:30,538 --> 00:03:36,763 coefficients, we can do anything we want. Literally, you can do some very fancy 39 00:03:36,763 --> 00:03:41,947 filters, even ones that go beyond bandpass, low-pass, high-pass, that kind 40 00:03:41,947 --> 00:03:45,138 of thing. Now I want to point out, that this is an 41 00:03:45,138 --> 00:03:48,823 explicit input-output formula. What I mean by that. 42 00:03:48,824 --> 00:03:54,214 Not only does this equation, the difference equation, specify the kind of 43 00:03:54,214 --> 00:04:00,181 filter we want through its coefficients. But this is a way you could implement it. 44 00:04:00,181 --> 00:04:05,153 You can actually program this up. You set up a table consisting of the 45 00:04:05,153 --> 00:04:09,426 previous ys that you need. The previous xes that you have. 46 00:04:09,426 --> 00:04:13,056 Input comes in. You stick it into this formula. 47 00:04:13,056 --> 00:04:17,386 Multiply those things you stored in tables by constants. 48 00:04:17,386 --> 00:04:22,531 You've got the previous outputs. Multiply them by their constants. 49 00:04:22,531 --> 00:04:26,596 Add em all up, then now you've got the current output. 50 00:04:26,596 --> 00:04:32,544 For the next in, output value to compute, you Put this over that place in the table. 51 00:04:32,544 --> 00:04:36,667 Put this one over here, etcetera. Do the same thing to the xes. 52 00:04:36,667 --> 00:04:39,739 And you have now implemented a digital filter. 53 00:04:39,739 --> 00:04:43,664 You can actually, it's a very simple programming exercise. 54 00:04:43,664 --> 00:04:49,275 Just consistent multiplies and additions. And turns out it's a very, very easy way 55 00:04:49,275 --> 00:04:52,978 to implement a wide variety of folders. Alright. 56 00:04:52,978 --> 00:04:58,749 So let's see what the output is for a very simple filter here. 57 00:04:58,749 --> 00:05:04,317 For a very simple input. So I only have one previous output there 58 00:05:04,317 --> 00:05:10,091 that enters into the difference equation, just the previous one. 59 00:05:10,091 --> 00:05:13,808 And I'm just going to take the current input. 60 00:05:13,809 --> 00:05:17,776 Input. So, the current input gets multiplied by 61 00:05:17,776 --> 00:05:21,420 b, the previous output gets multiplied by a. 62 00:05:21,420 --> 00:05:26,813 Add those two things up, and you get the output of the, the filter. 63 00:05:26,813 --> 00:05:32,039 I do emphasize, this is linear and is shifting variant, system. 64 00:05:32,039 --> 00:05:36,542 The simple input I'm going to use is just a, a unit sample. 65 00:05:36,543 --> 00:05:43,698 So, it's a, a very, very simple input, I mean one value is not here. 66 00:05:43,698 --> 00:05:52,476 We'll learn in this video that this is a very important input to know the output 67 00:05:52,476 --> 00:05:54,993 for. So, what do we do? 68 00:05:54,994 --> 00:06:00,498 Well, what I like to do to point out how the difference equation works, is to 69 00:06:00,498 --> 00:06:04,654 construct a table. So, what I'm going to do is have the in, 70 00:06:04,654 --> 00:06:07,944 the time index. The input value at that time. 71 00:06:07,944 --> 00:06:12,969 And the output value of that time. And we want to fill in the right hand 72 00:06:12,969 --> 00:06:18,231 column of the table. Now let's start at n equals minus 1. 73 00:06:18,231 --> 00:06:26,362 So, we know that at this time the input is zero, because that's that value there. 74 00:06:26,362 --> 00:06:34,089 The question is now, what's y at minus 1? What the difference equation says. 75 00:06:35,136 --> 00:06:42,994 Is that, that is equal to a times y of n minus 2 plus b times x at minus 1. 76 00:06:42,994 --> 00:06:48,143 And we know that's zero. But what's y of minus 2? 77 00:06:48,143 --> 00:06:56,689 Well, that y of minus 2 from the difference equation of course depends on y 78 00:06:56,689 --> 00:07:00,693 of minus 3. Y minus 3 depends on Y minus 4. 79 00:07:00,693 --> 00:07:06,706 Going on, and on, and on back in time. Well, how do we get around this? 80 00:07:06,706 --> 00:07:13,032 Have these values been specified? And there is an assumption that we make 81 00:07:13,032 --> 00:07:18,186 and [inaudible] is that all sigmas if minus infinity are 0. 82 00:07:18,186 --> 00:07:26,885 So, with that and the fact that the input is 0 for all these values back in negative 83 00:07:26,885 --> 00:07:31,505 time, we know that Y of minus 2 is equal to 0. 84 00:07:31,505 --> 00:07:39,665 Well, that, of course, gives us that the output at time N equals minus 1 is 0. 85 00:07:39,665 --> 00:07:46,107 And now lets let that input come in, and now we're going to talk about what happens 86 00:07:46,107 --> 00:07:50,596 at n equals zero. We know that the input is a 1 there. 87 00:07:50,596 --> 00:07:53,808 And what we're going to do is to find y of n. 88 00:07:53,808 --> 00:07:58,890 We're going to multiply that by b. We're going to multiply that by a. 89 00:07:58,890 --> 00:08:06,063 Add them up and of course you get just b. And that also applies to the next value. 90 00:08:06,063 --> 00:08:13,061 We now, again, all we do is multiply that value by B and that value by A. 91 00:08:13,061 --> 00:08:17,877 Add them up. That's exactly what the difference 92 00:08:17,877 --> 00:08:23,647 equation says to do. And, of course, we just get A times B. 93 00:08:23,648 --> 00:08:29,702 And to go into the next moment in time and all succeeding ones, I think it's pretty 94 00:08:29,702 --> 00:08:35,552 clear that what happens, for example at time 2, if we multiply that by B and that 95 00:08:35,552 --> 00:08:40,952 times A we're just going to get a B squared and it's, kind of think, pretty 96 00:08:40,952 --> 00:08:47,778 obvious that at, at any time N. As long as it's greater than or equal to 97 00:08:47,778 --> 00:08:55,517 0, the output is b times a to the n. So, when I put in a unit sample, my output 98 00:08:55,517 --> 00:09:03,437 is this signal for n positive and I can write that very succinctly Using the unit 99 00:09:03,437 --> 00:09:10,282 step[INAUDIBLE] in my notation here. So, unit step, of course, is equal to zero 100 00:09:10,282 --> 00:09:14,376 for all previous values. And then negative values. 101 00:09:14,376 --> 00:09:19,077 And then it's equal to 1. Full values [inaudible] and that are zero 102 00:09:19,077 --> 00:09:22,735 are positive. And that's a very concise way of using 103 00:09:22,735 --> 00:09:27,899 this to write down this. This output we're going to find this 104 00:09:27,899 --> 00:09:31,725 happens all the time and is very, very useful. 105 00:09:31,725 --> 00:09:38,815 Okay, so what does this output look like? So there it is our difference equation our 106 00:09:38,815 --> 00:09:42,697 input and we have computed that for an output. 107 00:09:42,697 --> 00:09:46,744 Let's plot it. For various values of a and d. 108 00:09:46,744 --> 00:09:53,268 Now, I think you can see that the coefficient b here amounts to a gain. 109 00:09:53,268 --> 00:09:59,685 If I change b all it's going to do, is make the signal bigger or smaller. 110 00:09:59,685 --> 00:10:04,007 So in all of these plots I've said b equal to 1. 111 00:10:04,008 --> 00:10:13,091 To simplify things and here's my favorite value for filtered coefficient one half 112 00:10:13,091 --> 00:10:19,222 and you can see. The output filter here for this unit 113 00:10:19,222 --> 00:10:25,283 sample input is a one half to the end and it just decays. 114 00:10:25,284 --> 00:10:30,611 And in fact, it decays exponentially. If you have n equals minus a half, it's 115 00:10:30,611 --> 00:10:35,270 also decaying exponentially. But now, every other, the odd value 116 00:10:35,270 --> 00:10:39,256 samples alternate in sine. They're negative, the pos-. 117 00:10:39,256 --> 00:10:44,696 The ones for the even ones are positive. And now, he has something that goes up 118 00:10:44,696 --> 00:10:48,842 and. Up and down, and has a very different 119 00:10:48,842 --> 00:10:55,581 character than when a is positive. Another interesting value here to play 120 00:10:55,581 --> 00:11:01,670 with is when a is greater than 1. And when you do that, does, the output 121 00:11:01,670 --> 00:11:06,497 here is going to be 1 point 1 to the nth. And it's going to grow. 122 00:11:06,497 --> 00:11:09,917 And this turns out to be, not be terribly useful. 123 00:11:09,917 --> 00:11:13,440 If you think about it for a second. This part here. 124 00:11:13,440 --> 00:11:18,711 All these previous outputs, when you're out in here, are going to keep getting 125 00:11:18,711 --> 00:11:23,691 bigger and bigger and bigger. And swamp out whatever values you had for 126 00:11:23,691 --> 00:11:27,120 the input. And basically, the filter ignores the 127 00:11:27,120 --> 00:11:30,488 input. Good, the n gets big enough. 128 00:11:30,488 --> 00:11:38,294 So what we have is that we had better have the absolute value of a to be less than 1 129 00:11:38,294 --> 00:11:42,955 or we don't have a filter that's interesting. 130 00:11:42,955 --> 00:11:47,449 It will [unknown] of a outside that range to. 131 00:11:47,450 --> 00:11:54,957 And the sample response will just take off, and the filter has a mind of its own, 132 00:11:54,957 --> 00:11:56,633 as it were. Okay. 133 00:11:56,633 --> 00:12:03,372 So, lets think about what this filter is. We know how to compute it. 134 00:12:03,372 --> 00:12:07,665 We can plot it, but what kind of filter is it? 135 00:12:07,666 --> 00:12:12,935 And that of course means that we have to hop into the frequency domain. 136 00:12:12,935 --> 00:12:18,596 Well, to determine what the filter is, we're going to do exactly the same thing 137 00:12:18,596 --> 00:12:23,670 we did with analog circuits. I'm going to assume the input is a complex 138 00:12:23,670 --> 00:12:28,673 exponential, at some frequency f, having a complex amplitude of x. 139 00:12:28,673 --> 00:12:34,070 And, we're going to assume that, the output has the same form, complex 140 00:12:34,070 --> 00:12:40,566 exponential in, complex exponential out. And we're going to see if this assumption 141 00:12:40,566 --> 00:12:47,098 is correct by seeing if we can determine an output amplitude that will satisfy the 142 00:12:47,098 --> 00:12:52,792 difference eq [inaudible]. So I'm just going to stick in this these 143 00:12:52,792 --> 00:12:58,766 assumptions for x, y and x. And what you get is something that looks 144 00:12:58,766 --> 00:13:05,316 like this, and notice this intermediate term in here, y of n minus 1. 145 00:13:05,316 --> 00:13:09,959 When you stick that in for the complex exponential. 146 00:13:09,959 --> 00:13:15,552 A factor of e to the minus j two pi f comes out and we're left with our friend 147 00:13:15,552 --> 00:13:20,657 the complex exponential. And that's what I used when I wrote this 148 00:13:20,657 --> 00:13:25,522 equation. But I think you can see right away that 149 00:13:25,522 --> 00:13:34,681 what happens is the complex exponentials cancel And sure enough, if I bring the AY 150 00:13:34,681 --> 00:13:43,453 term to the other side, factor out the Y and divide by 1 minus A, I have a solution 151 00:13:43,453 --> 00:13:49,572 for capital Y that works. As long as the ratio of Y and X is equal 152 00:13:49,572 --> 00:13:53,881 to this. I stick in a complex exponential I get out 153 00:13:53,881 --> 00:13:59,061 a complex exponential. Well, we call this ratio a transfer 154 00:13:59,061 --> 00:14:03,283 function. So we can, have used this technique to 155 00:14:03,283 --> 00:14:07,424 find that the transfer function has this form. 156 00:14:07,424 --> 00:14:13,326 Well as we've seen already before, the b here just amounts to the gain or a simple 157 00:14:13,326 --> 00:14:16,827 filter. So, whatever number you put there just 158 00:14:16,827 --> 00:14:22,935 supplies a gain for the transfer function. For the filtering those affected by a, 159 00:14:22,935 --> 00:14:26,620 what kind of filter it is, how good a filter it is. 160 00:14:26,621 --> 00:14:35,070 Is all determined by A because that's where the terms involving frequency are. 161 00:14:35,070 --> 00:14:42,996 Alright so, what we've seen is that the output is equal to the transfer function 162 00:14:42,996 --> 00:14:51,276 times the complex amplitude of the input Times the complex exponential, output's 163 00:14:51,276 --> 00:14:59,874 exponential N, complex exponential N. Well, what does this, transfer function 164 00:14:59,874 --> 00:15:05,440 look like? So, here are some plots and, for various 165 00:15:05,440 --> 00:15:10,101 values of A. And again, just like I did before, I set B 166 00:15:10,101 --> 00:15:16,975 equal to 1 because that's just the game, we don't need to worry about it too much. 167 00:15:16,975 --> 00:15:21,204 Okay. So, for my favorite filter value here, an 168 00:15:21,204 --> 00:15:27,131 A of a half, you can see that it starts out and kind of gets small. 169 00:15:27,131 --> 00:15:31,344 So, I guess you'd call that a low pass folder. 170 00:15:31,344 --> 00:15:37,235 It's not a very good one. And what are these values here at the low 171 00:15:37,235 --> 00:15:43,349 and high frequencies here? At F equals zero, you're putting in F 172 00:15:43,349 --> 00:15:48,290 equals 0 into this formula, you get 1 over 1 minus. 173 00:15:48,290 --> 00:15:53,899 Hey. Stick in f equals one half, well, e to the 174 00:15:53,899 --> 00:16:02,839 minus j two pi f times a half, well that's b to the minus j pi, and if you know 175 00:16:02,839 --> 00:16:12,375 that's minus one, And so what we get for the value at frequency half, is 1 over 1 176 00:16:12,375 --> 00:16:17,424 plus a. So this value is something like 2 thirds, 177 00:16:17,424 --> 00:16:23,634 and this value up here is 2. So it doesn't have much difference in the 178 00:16:23,634 --> 00:16:31,393 game at high and low frequencies. However, if you stick in a Bigger value. 179 00:16:31,394 --> 00:16:37,160 A like .9. Remember, we have to keep the values of a 180 00:16:37,160 --> 00:16:44,404 in magnitude between 0 and 1. So, A is a pretty big value for A. 181 00:16:44,404 --> 00:16:49,157 And you can see, we get a much nicer filter. 182 00:16:49,158 --> 00:16:55,808 And it is low pass. So what we have found out here is that for 183 00:16:55,808 --> 00:17:04,894 a positive, that implies low pass open/g. So if you now think about the, negative 184 00:17:04,894 --> 00:17:09,662 values of a You get a high pass [inaudible]. 185 00:17:09,662 --> 00:17:16,927 And, if I use a value of A that's minus point 9 then you get something that's 186 00:17:16,927 --> 00:17:22,704 symmetric and gives us a very nice high pass[INAUDIBLE]. 187 00:17:22,704 --> 00:17:28,230 So, a greater than 0 but less than 1. I get a low pass filter. 188 00:17:28,230 --> 00:17:34,153 If I get, have an a less than zero and greater than minus 1, I get a high-pass 189 00:17:34,153 --> 00:17:35,400 filter. Okay. 190 00:17:35,400 --> 00:17:40,971 So, what happens in general? How do we find the transfer function for a 191 00:17:40,971 --> 00:17:46,711 difference equation in general? And you do exactly what we've just gone 192 00:17:46,711 --> 00:17:50,530 through. Set x equal to a complex exponential. 193 00:17:50,531 --> 00:17:56,557 And assume that the output is the same. And I think it's pretty easy to see. 194 00:17:56,557 --> 00:18:01,693 It's all going to work. And what you get for a transfer function. 195 00:18:01,694 --> 00:18:07,935 Always looks like the following. You have a numerator term, which consists 196 00:18:07,935 --> 00:18:11,948 of the input terms from the difference equation. 197 00:18:11,948 --> 00:18:18,262 You are going to have a denominator that consists of all the terms that involved 198 00:18:18,262 --> 00:18:23,537 the previous outputs. The coefficients of the filter are staring 199 00:18:23,537 --> 00:18:27,171 you in the face from the difference equation. 200 00:18:27,171 --> 00:18:32,441 Very, very easy to write this down. That's not very difficult at all. 201 00:18:32,441 --> 00:18:37,631 I do note that these minus signs come about because of the way you have to solve 202 00:18:37,631 --> 00:18:40,887 things. Remember we have to bring these terms to 203 00:18:40,887 --> 00:18:44,391 the other side when we found the transfer function. 204 00:18:44,391 --> 00:18:49,796 So the Ds all come in with plus signs. The As enter downstairs with minus signs. 205 00:18:49,796 --> 00:18:55,676 And this we can use to do what's called filter design. 206 00:18:55,676 --> 00:19:04,262 So, what you want is a way of choosing these, filter coefficients a and b in such 207 00:19:04,262 --> 00:19:09,218 a way that you get the kind of filter you want. 208 00:19:09,219 --> 00:19:12,250 Where there are software packages that do this. 209 00:19:12,251 --> 00:19:17,111 It's not nearly as hard as it might be in the analogue world if you're stuck with 210 00:19:17,111 --> 00:19:19,717 circuits. You can use software packages. 211 00:19:19,717 --> 00:19:24,655 And they will you tell them what kind of filter you, you tell the program what kind 212 00:19:24,655 --> 00:19:27,970 of filter you want. And they'll pass let's say cut off 213 00:19:27,970 --> 00:19:32,879 frequency of .2. And you specify how good a filter it is 214 00:19:32,880 --> 00:19:40,816 how sharply it goes to 0 from 2 and it will pop out filter coefficients that you 215 00:19:40,816 --> 00:19:43,811 need. It's not very hard at all. 216 00:19:43,811 --> 00:19:50,924 Well, let me point out something that is different than the analog world. 217 00:19:50,924 --> 00:19:54,568 And this is a special filter, special kind of filter. 218 00:19:54,568 --> 00:20:00,163 And what makes it special is that there's no term here that talks about the previous 219 00:20:00,163 --> 00:20:02,135 outputs. That's right. 220 00:20:02,135 --> 00:20:06,901 So, this filter, the difference equation only depends on the input. 221 00:20:06,902 --> 00:20:14,040 And we want to figure out how this works. Well, I'm going to do the same thing I did 222 00:20:14,040 --> 00:20:20,637 before and assume I have a unit sample input and I'm going to build my table. 223 00:20:20,637 --> 00:20:26,350 Well it turns out to be a whole lot easier than it was before. 224 00:20:26,350 --> 00:20:36,067 Since this output here only depends on the previews, inputs, there's no concern about 225 00:20:36,067 --> 00:20:42,314 what y minus 2 is. So we were [unknown] n minus 1 what this 226 00:20:42,314 --> 00:20:49,485 filter does, is it takes together We all the in the input values here. 227 00:20:49,485 --> 00:20:53,576 Adds them up. Divide by the number of terms. 228 00:20:53,576 --> 00:20:58,116 And puts out the answer. Well of course that's 0. 229 00:20:58,116 --> 00:21:03,361 Well if we now encompass that unit sample. We look here. 230 00:21:03,362 --> 00:21:08,901 We go back, q returns, add them all up divide by q. 231 00:21:08,901 --> 00:21:13,784 Well it's pretty clear that you get 1 over q. 232 00:21:13,784 --> 00:21:22,902 And if you go to time 2, we're now right here, so what the filter does is it takes 233 00:21:22,902 --> 00:21:27,964 that guy you. N K and this previous adds them up divide 234 00:21:27,964 --> 00:21:34,269 by q well you're only getting this innate sample in here everything else is 0 so 235 00:21:34,269 --> 00:21:38,957 we[UNKNOWN] for q. And we can keep going with this I think 236 00:21:38,957 --> 00:21:42,692 it's pretty clear we still get answer 1 over q. 237 00:21:42,692 --> 00:21:51,194 Until we get to the time Q minus 1. And that turns out to be right here. 238 00:21:51,194 --> 00:22:02,592 So, you look at this last term, respond to this, x of q minus 1 minus q plus 1, and 239 00:22:02,592 --> 00:22:08,392 that's x of 0. So this is going to be the last time in 240 00:22:08,392 --> 00:22:14,120 which our filter grabs the input at timing equals zero. 241 00:22:14,120 --> 00:22:22,114 And of course, we get 1 over q for that. Now, beyond that point, the filter is 242 00:22:22,114 --> 00:22:28,707 sitting out there. And it grabs only these inputs, adds those 243 00:22:28,707 --> 00:22:31,822 up. They're all 0 so you get 0. 244 00:22:31,822 --> 00:22:38,053 So, for all subsequent times the output is just, just 0. 245 00:22:38,053 --> 00:22:44,855 So, for our special filter, which has this difference equation. 246 00:22:44,855 --> 00:22:51,056 We put in the unit sample. The output basically looks like a pulse. 247 00:22:51,056 --> 00:22:54,351 And that's what it is. It's a pulse. 248 00:22:54,351 --> 00:23:02,127 So here I plotted for specific value q and pretty easy to implement and see what's 249 00:23:02,127 --> 00:23:06,460 going on. Let me point out something about this 250 00:23:06,460 --> 00:23:11,615 filter that makes it special. What is this filter doing? 251 00:23:11,615 --> 00:23:18,991 It's taking q values of the input, adding them up, and dividing by q, the number of 252 00:23:18,991 --> 00:23:22,076 terms. What would you call that? 253 00:23:22,077 --> 00:23:29,489 In, standard, non-techno plots . You take Q values together, add them up, 254 00:23:29,489 --> 00:23:35,160 divide by the number of terms, and that's called averaging. 255 00:23:35,160 --> 00:23:42,690 So, this filter is special in the sense that it averages it's input over Q values 256 00:23:42,690 --> 00:23:45,313 and that's. What the output is. 257 00:23:45,313 --> 00:23:51,148 Sometimes this is called running average because you keep doing this every N and 258 00:23:51,148 --> 00:23:56,890 whichever N you pick, you're averaging this, then you're averaging that, then 259 00:23:56,890 --> 00:24:01,993 you're averaging that, etc. And what kind of filter is the averager? 260 00:24:01,993 --> 00:24:07,543 Remember we want to implement our And software our filter with a difference 261 00:24:07,543 --> 00:24:13,118 equation, but what kind of filter is it? Well, what I'm going to do is find that 262 00:24:13,118 --> 00:24:17,323 transfer function. And remember, the general form is that 263 00:24:17,323 --> 00:24:22,741 when you only have, the inputs, what you do is your going to get a transfer 264 00:24:22,741 --> 00:24:26,161 function that just has that in the numerator. 265 00:24:26,161 --> 00:24:36,692 So, for the transfer function here is going to be, so we're going to get a 1 266 00:24:36,692 --> 00:24:47,132 over q, times 1 plus e to the minus j 2 pie f plus E to the minus j, 2 pie f times 267 00:24:47,132 --> 00:24:56,000 q minus 1, and all the [inaudible] coefficients are 1 over q. 268 00:24:56,000 --> 00:25:04,380 Well, we've seen this sum before. And that should remind you of the digital 269 00:25:04,380 --> 00:25:09,702 sin, and there it is. So there's our digital sink. 270 00:25:09,702 --> 00:25:18,276 There's over q, and we get this linear phase term that we know comes from adding 271 00:25:18,276 --> 00:25:24,262 up all of these terms. Well, what does that look like? 272 00:25:24,262 --> 00:25:30,311 And what we see is that our averager. Is a low pass filter. 273 00:25:30,311 --> 00:25:39,181 This is a very important concept to have. Averager is equivalent to low pass filter. 274 00:25:39,181 --> 00:25:43,851 The cutoff frequency if you will is at 1 over q. 275 00:25:43,851 --> 00:25:50,392 This is a plot for q as follows. And so if I come up with a filter with a 276 00:25:50,392 --> 00:25:57,542 bigger value of q, I average more terms the gain at the origin is still 1, but the 277 00:25:57,542 --> 00:26:03,969 cutoff frequency is down here and the ripples get smaller actually. 278 00:26:03,969 --> 00:26:09,927 So you can add a better low pass filter By averaging more terms we'll also, the 279 00:26:09,927 --> 00:26:13,643 cutoff frequency moves to lower frequencies. 280 00:26:13,643 --> 00:26:18,049 And I think intuitively this is exactly what averaging does. 281 00:26:18,049 --> 00:26:23,290 It removes high frequencies, and leaves only low frequency variations. 282 00:26:23,290 --> 00:26:27,145 And I think it makes intuitive sense that averaging. 283 00:26:27,145 --> 00:26:32,547 Could be just a low pass filter. And I think it's very neat that, in, 284 00:26:32,547 --> 00:26:37,767 electrical engineering. We can actually implement averaging. 285 00:26:37,767 --> 00:26:42,228 And think about it as a filter. Especially on a computer. 286 00:26:42,228 --> 00:26:49,230 Well, and now I want to do something special and point out why the unit sample 287 00:26:49,230 --> 00:26:55,385 response is so important. So, here's our linear shipped invariance 288 00:26:55,385 --> 00:27:02,729 system and it turns out I'm not going to worry about whether it's implemented with 289 00:27:02,729 --> 00:27:09,576 a difference equation or not. Is just linear in shift and variant. 290 00:27:09,576 --> 00:27:15,864 What I want to do is for an input I want to use a unit sample. 291 00:27:15,864 --> 00:27:24,335 And I'm going to refer to the output for that special input, as little h of n. 292 00:27:24,336 --> 00:27:34,983 So in notation, a little concise notation. If the input signal is a unit sample, 293 00:27:34,983 --> 00:27:41,969 we're going to call that special output little h of n. 294 00:27:41,969 --> 00:27:46,382 Okay? Now, because this filter is shift 295 00:27:46,382 --> 00:27:54,757 invariant, if I delay that unit sample, by n at the input, the output is just going 296 00:27:54,757 --> 00:28:03,132 to be a delayed version of the output, little h, okay, a very important fact to 297 00:28:03,132 --> 00:28:06,667 realize. For what's coming up. 298 00:28:06,667 --> 00:28:15,383 Well as we've seen, you can think about every signal in discrete time as a super 299 00:28:15,383 --> 00:28:22,578 position of unit samples. So, the unit sample at the origin, and 300 00:28:22,578 --> 00:28:29,198 it's aplitude is x0. Sample it time one, it's amplitude is x 301 00:28:29,198 --> 00:28:33,894 sub 1. Here's x of minus 2, and that's just a 302 00:28:33,894 --> 00:28:41,886 unit-sample of time minus at time. What am I saying, minus here should be 303 00:28:41,886 --> 00:28:46,389 plus. So unit-sample This is a unit sample whose 304 00:28:46,389 --> 00:28:52,608 amplitude is exa plus 2, exa plus 1. And I can write that concisely, that 305 00:28:52,608 --> 00:28:58,339 expresses the super position. Well, if I stick a super position and 306 00:28:58,339 --> 00:29:04,034 signals into a linear shift and variance system, what do I get out? 307 00:29:04,035 --> 00:29:08,640 I get a super position of the applets to each. 308 00:29:08,640 --> 00:29:16,686 And therefore we can immediately write that the output for our input x expressed 309 00:29:16,686 --> 00:29:24,606 as a super position of unit samples is given by, the, a super position of delayed 310 00:29:24,606 --> 00:29:30,478 unit sample responses. That's what little h is it's called unit 311 00:29:30,478 --> 00:29:35,307 sample response. Well, in fact we now have a very general 312 00:29:35,307 --> 00:29:41,317 way of expressing the input output relationship for a linear system. 313 00:29:41,317 --> 00:29:44,076 You give me. The input signal. 314 00:29:44,076 --> 00:29:50,823 You give me the unit sample response. I can use this sum to compute, what the 315 00:29:50,823 --> 00:29:55,487 output is. You don't need the difference equation, 316 00:29:55,487 --> 00:30:00,505 this is an alternative. It's usually not a efficient way of 317 00:30:00,505 --> 00:30:04,809 computing the output. The difference equation is much, much, 318 00:30:04,809 --> 00:30:10,035 much better but for theoretical reasons as you' re about to see this is important 319 00:30:10,035 --> 00:30:14,673 input output relationship. Well I want to find the transfer function 320 00:30:14,673 --> 00:30:19,521 cause it should be as we've seen for a linear shifting variant system. 321 00:30:19,521 --> 00:30:24,342 The, we should be able to find the, the 4A transformer. 322 00:30:24,342 --> 00:30:28,874 The output is related to the 4A transformer, the input. 323 00:30:28,874 --> 00:30:35,216 And for that N, I'm going to compute the DTFT of the output, which means I take the 324 00:30:35,216 --> 00:30:40,742 output signal, multiply it by a complex exponential and add them up. 325 00:30:40,743 --> 00:30:47,344 I do the same thing to the other side. And what I've done is, since I'm summing 326 00:30:47,344 --> 00:30:51,978 over n here. I've moved that sum on in-, inside, and do 327 00:30:51,978 --> 00:30:56,025 it first. 'Because there's only 1 term that depends 328 00:30:56,025 --> 00:30:58,148 on n. Well, what's this? 329 00:30:58,148 --> 00:31:01,535 What is that? That, to me, looks like. 330 00:31:01,536 --> 00:31:06,491 The DTFT of a delayed signal. And so I know what that is. 331 00:31:06,491 --> 00:31:11,741 That's the same as the DTFT of little h times a linear phase. 332 00:31:11,741 --> 00:31:15,541 Well, what do you call the DTFT of little h? 333 00:31:15,541 --> 00:31:20,441 You call it big H. So I think you see where this is going. 334 00:31:20,441 --> 00:31:24,823 Now, we're going to get to the transfer. Function. 335 00:31:24,823 --> 00:31:30,542 There's what we're going to call the transfer function in just a second. 336 00:31:30,542 --> 00:31:35,635 And so there's the linear phase that has to do with that delay. 337 00:31:35,635 --> 00:31:40,642 If I pull this term out of the sum on m. It doesn't depend on m. 338 00:31:40,642 --> 00:31:45,029 I now see that I've got the dtft. Of the input. 339 00:31:45,029 --> 00:31:53,852 And I arrive at a very nice result. That the DTFT of the output equals the 340 00:31:53,852 --> 00:31:59,693 DTFT of the input times the transfer function. 341 00:31:59,693 --> 00:32:09,134 But what the new thing is here. Is that this, is the forier transform, of 342 00:32:09,134 --> 00:32:16,489 the unit sample response. So transfer function here. 343 00:32:16,489 --> 00:32:23,618 Unit sample response here. And they are Fourier, they constitute a 344 00:32:23,618 --> 00:32:29,189 Fourier Transform pair. So, this is a way of finding the transfer 345 00:32:29,189 --> 00:32:33,836 function. That's why finding the end sample response 346 00:32:33,836 --> 00:32:40,775 in all those examples, what you did for difference equations, was so important. 347 00:32:40,775 --> 00:32:47,102 It's because I know that if I can computer the DTFT of that unit sample response, 348 00:32:47,102 --> 00:32:51,900 I've got the transfer function right away. Very, very nice. 349 00:32:51,900 --> 00:32:56,714 Well, let's talk about those filter categories I mentioned. 350 00:32:56,714 --> 00:33:02,591 What we like to think about digital filters as falling into one of two 351 00:33:02,591 --> 00:33:05,335 Classes. The FIR and IIR. 352 00:33:05,335 --> 00:33:13,716 And what does these acronyms mean? Well, it turns out, the FIR case. 353 00:33:13,716 --> 00:33:21,159 It turns out it is finite. There's a word missing in the acronym, 354 00:33:21,159 --> 00:33:27,043 duration. Impulse, response. 355 00:33:28,364 --> 00:33:40,210 That's what FIR stands for. And the impulse is a, is another name for 356 00:33:40,210 --> 00:33:43,318 unit-sample. Example. 357 00:33:43,318 --> 00:33:49,462 So to keep things short, this has been adopted over, since the beginning of 358 00:33:49,462 --> 00:33:55,277 digital-signal processing. And so what that means, as we've seen, is 359 00:33:55,277 --> 00:34:01,595 that the difference equation has no terms that depend on previous outputs. 360 00:34:01,595 --> 00:34:07,646 Only consists of terms that add together, previous out, input values. 361 00:34:07,646 --> 00:34:14,652 When you have a difference equation that's a little bit more general, that includes 362 00:34:14,652 --> 00:34:21,752 those output values, in addition to the input values, we know that the unit sample 363 00:34:21,752 --> 00:34:27,017 response as we've seen and our example is infinitely long. 364 00:34:27,017 --> 00:34:33,557 Remember, our, when we had one term our unit sample response, what looked like A 365 00:34:33,557 --> 00:34:39,383 to the N, was lasted forever. Even though the unit sample, which is only 366 00:34:39,383 --> 00:34:43,571 one sample long, yet the output lasts forever. 367 00:34:43,571 --> 00:34:52,749 So, that's what IIR stands for, it stands for infinite duration impulse response. 368 00:34:52,749 --> 00:34:59,272 So, these categories depend entirely on the behavior of h. 369 00:34:59,272 --> 00:35:07,234 Well, so one little thing I want to do is compute the duration of the output for a 370 00:35:07,234 --> 00:35:14,906 finite duration input in the FIR case. And this turns out to be really important. 371 00:35:14,906 --> 00:35:20,003 So, lets assume that my input is something very simple. 372 00:35:20,003 --> 00:35:25,935 Is three samples long. Not very interesting, but will serve the 373 00:35:25,935 --> 00:35:32,264 purpose for this example. And my little h is something that's going 374 00:35:32,264 --> 00:35:36,255 down, looks like that, and then it's 0. Okay? 375 00:35:36,255 --> 00:35:43,445 Well, what's the output going to be? We know by the superposition principle 376 00:35:43,445 --> 00:35:50,577 that all we do is found the output for each of these inputs and then add them up. 377 00:35:50,577 --> 00:35:56,439 So the first one gives that for an output and goes through zero. 378 00:35:56,439 --> 00:36:04,982 The next one gives that for an output. [unknown] And this one gives that output, 379 00:36:04,982 --> 00:36:13,623 [unknown] and then we add them up. Well how long is this imput, I mean sorry 380 00:36:13,623 --> 00:36:19,231 is this output? The imput went to time N minus 1. 381 00:36:19,231 --> 00:36:27,146 This end, goes to output. Q minus 1, it's duration is q, they're q 382 00:36:27,146 --> 00:36:33,137 non 0 values. And this time right here, is n plus q 383 00:36:33,137 --> 00:36:38,657 minus 2. So that means it has duration n plus q 384 00:36:38,657 --> 00:36:45,840 minus 1. Duration of the output equals the duration 385 00:36:45,840 --> 00:36:56,339 of the input ti-, plus the duration of the unit sample response, q, minus 1. 386 00:36:56,339 --> 00:37:01,396 So this little minus 1 here. Sort of a bookkeeping kind of thing but 387 00:37:01,396 --> 00:37:07,359 that's important to get this down as you're going to see we're going to be talk 388 00:37:07,359 --> 00:37:10,752 about this later. So, back to the IIR case. 389 00:37:10,752 --> 00:37:16,370 If you have a finite duration input the output has an infinite duration in 390 00:37:16,370 --> 00:37:20,042 general. So, there's no real calculations to do 391 00:37:20,042 --> 00:37:25,064 here, but this is very important as we'll see in, in another video. 392 00:37:25,064 --> 00:37:31,117 Well another property of FIR filters is very important, has to do with the phase. 393 00:37:31,117 --> 00:37:36,303 You can have a linear phase and we seen that already for our averager. 394 00:37:36,303 --> 00:37:41,191 But in the IIR case, it always has a non linear phase. 395 00:37:41,191 --> 00:37:48,336 And, let me bring up the phase plots for our filters we've been talking about. 396 00:37:48,336 --> 00:37:53,701 So, here's our linear phase coming out of an FIR averager. 397 00:37:53,701 --> 00:37:58,694 And what does linear phase mean? [inaudible]. 398 00:37:58,694 --> 00:38:06,288 Linear phase means delay. So what this means when you have a 399 00:38:06,288 --> 00:38:15,204 transfer function at, with a linear phase. Is that every frequency. 400 00:38:15,205 --> 00:38:21,784 Maybe filtered, this amplitude is changed but every frequency, no matter what it is, 401 00:38:21,784 --> 00:38:27,190 experiences this same delay. When you have a non-linear phase you're 402 00:38:27,190 --> 00:38:32,618 talking about a frequency here and a frequency here, well for this 1, it 403 00:38:32,618 --> 00:38:36,368 experiences that, but since it's non-linear. 404 00:38:36,368 --> 00:38:41,001 These two frequencies experience different delays. 405 00:38:41,001 --> 00:38:47,734 Well, why is this important? This has to do with the subtleties in ways 406 00:38:47,734 --> 00:38:51,705 signals look. So it turns out your ear is very 407 00:38:51,705 --> 00:38:57,734 insensitive to fa-, to fe- Phase. If you had the same filter implemented 408 00:38:57,734 --> 00:39:03,374 with a FIR filter or done with an IIR filter and listened to the result you 409 00:39:03,374 --> 00:39:08,713 couldn't tell them apart. You can phase shift the input, the speech 410 00:39:08,713 --> 00:39:13,830 input, music input it doesn't really matter as much as you want. 411 00:39:13,831 --> 00:39:20,438 With non-linear phase, linear phase you cannot with your ears tell the original 412 00:39:20,438 --> 00:39:26,427 from the phase shifted input. We just can't hear, your ear doesn't care. 413 00:39:26,427 --> 00:39:31,377 However, if you reply a non-linear phase shift to an image. 414 00:39:31,378 --> 00:39:34,801 And filter that, it will really mess it up. 415 00:39:34,801 --> 00:39:38,282 It will be entirely gibberish in most cases. 416 00:39:38,282 --> 00:39:44,705 So for image processing they like to stick with FIR filters, because you can design 417 00:39:44,705 --> 00:39:50,183 them to have linear phase in which everything just gets shifted over. 418 00:39:50,183 --> 00:39:53,890 But [unknown] phase for an image is very, very. 419 00:39:53,890 --> 00:39:58,131 Very bad. And you'll see that as you take other in, 420 00:39:58,131 --> 00:40:03,585 or advanced digital signal processing. Okay, so, digital filters. 421 00:40:03,585 --> 00:40:09,233 Very interesting and very important classes are described by difference 422 00:40:09,233 --> 00:40:13,317 equations. And now only is this the way of describing 423 00:40:13,317 --> 00:40:19,645 the filter, this is a way of implementing. Now it turns out that the difference 424 00:40:19,645 --> 00:40:25,905 equation is not always the most efficient. And we're going to talk about this in 425 00:40:25,905 --> 00:40:29,815 another video. It's not the best way implementing a 426 00:40:29,815 --> 00:40:34,190 digital filter. What I mean by that is, it is not as fast. 427 00:40:34,190 --> 00:40:39,567 Clearly you don't want to wait for your. Your results to come in, you want your 428 00:40:39,567 --> 00:40:43,711 filter to run very quickly. Well it turns out in terms of me getting 429 00:40:43,711 --> 00:40:48,225 to the details of counting the multiplications and additions it takes to 430 00:40:48,225 --> 00:40:53,117 find the output for a given input. It turns out the difference equation works 431 00:40:53,117 --> 00:40:56,993 fine, but it isn't the best. And we're going to talk about that in 432 00:40:56,993 --> 00:41:02,908 another video. So again the input output relationship for 433 00:41:02,908 --> 00:41:11,476 general of linear filter can be expressed at either the time domain or in the 434 00:41:11,476 --> 00:41:16,543 frequency domain. Time domain is this. 435 00:41:16,544 --> 00:41:21,848 Frequency domain is given by our old friend the transfer function kind of 436 00:41:21,848 --> 00:41:26,723 relationship and we can do either specify how what the filter does. 437 00:41:26,723 --> 00:41:32,993 Either way I find using the transfer function is more useful when you're trying 438 00:41:32,993 --> 00:41:36,726 to figure out what kind of filter it is for example. 439 00:41:36,727 --> 00:41:44,756 What the new thing is that the unit-sample response and that transfer function are 440 00:41:44,756 --> 00:41:51,288 Fourier transform pairs. So, if you can find this, you can find the 441 00:41:51,288 --> 00:41:58,068 transfer function very easily. It's not hard at all, you just find. 442 00:41:58,069 --> 00:42:02,773 Fine with the DTFT. And in succeeding videos we're going to 443 00:42:02,773 --> 00:42:06,621 talk about efficient ways of implementing filters. 444 00:42:06,621 --> 00:42:12,657 And it turns out many of those cases wind up being implementing filters in the 445 00:42:12,657 --> 00:42:17,003 frequency domain. Kind of a not obvious thing, and we'll 446 00:42:17,003 --> 00:42:19,050 talk about that coming up.