Return to Video

5.2 - Filtering by example

  • 0:01 - 0:03
    Welcome to Module 5.2.
  • 0:03 - 0:07
    In the previous module, we introduced the concept of filtering and that of convolution.
  • 0:07 - 0:12
    In this module, we will see some filtering in action by setting up a mock problem
  • 0:12 - 0:14
    where we try to remove some noise from the signal.
  • 0:15 - 0:17
    The two characters that we will introduce in this chapter
  • 0:17 - 0:20
    are two friends that will remain with us for a very long time:
  • 0:20 - 0:23
    the Moving Average and the Leaky Integrator.
  • 0:24 - 0:28
    Hi, and welcome to Module 5.2 of Digital Signal Processing,
  • 0:28 - 0:33
    in which we will talk about filters, by showing some filtering examples.
  • 0:33 - 0:37
    In particular, we will introduce two characters
  • 0:37 - 0:39
    that will remain with us until the end of the class,
  • 0:39 - 0:42
    the Moving Average filter and the Leaky Integrator.
  • 0:43 - 0:44
    So, in the previous module,
  • 0:44 - 0:48
    we talked a lot about the properties of Linear Time Invariant filters,
  • 0:48 - 0:53
    but we haven't said much about the applications that can be addressed by using filters.
  • 0:53 - 0:55
    So here, let me give you an example.
  • 0:55 - 0:59
    Suppose you're taking measurements of a physical quantity
  • 0:59 - 1:02
    that you know to be smooth and slowly varying:
  • 1:02 - 1:07
    for instance, the tide level at the sea or the blood pressure of a patient.
  • 1:07 - 1:09
    Because of the measurement process,
  • 1:09 - 1:12
    your values, the values that you actually measure,
  • 1:12 - 1:15
    are corrupted by some form of additive noise.
  • 1:15 - 1:17
    Which at this point, we could just say
  • 1:17 - 1:21
    it's some extraneous quantity that perturbs the measured value.
  • 1:21 - 1:24
    So, what you get in the end, instead of the smooth curve of the first panel,
  • 1:24 - 1:28
    is a curve where you see that there's really fast wiggles
  • 1:28 - 1:31
    that you interpret as being a noise corruption.
  • 1:31 - 1:36
    And the question is, can we process this data as a discrete time signal
  • 1:36 - 1:39
    so that most of the noise is removed and we get back a measurement
  • 1:39 - 1:43
    that is in line with our expectations of smoothness?
  • 1:43 - 1:47
    The first idea is to replace each sample of the input sequence
  • 1:47 - 1:49
    with a local average.
  • 1:49 - 1:55
    Averages are usually good when you want to eliminate random variations
  • 1:55 - 1:56
    that you don't know much about.
  • 1:57 - 2:00
    So, for instance, assume a situation where we process the signal
  • 2:00 - 2:04
    and the process (? processed?) signal is at each sample,
  • 2:04 - 2:09
    the simple two-point average of the current sample and the past sample.
  • 2:09 - 2:13
    Well, it could very well be that the two-point average
  • 2:13 - 2:16
    is not enough to remove most of the noise.
  • 2:16 - 2:19
    And so, in general, we could take longer averages
  • 2:19 - 2:26
    by considering the output samples to be the average of M points from the input.
  • 2:26 - 2:29
    If we try to do that experimentally, we can see immediately
  • 2:29 - 2:33
    that the strategy works for increasing values of M.
  • 2:33 - 2:36
    So, if we take just a two-point average,
  • 2:36 - 2:40
    we see that we don't really get a lot of leverage(?).
  • 2:40 - 2:45
    You can see in blue the original signal, and in red the filtered output.
  • 2:45 - 2:48
    And with a two-point average, we still have a lot of wiggles.
  • 2:48 - 2:53
    But, as we increase the number of points that we use to compute the average,
  • 2:53 - 2:57
    we can see that we start to remove a little bit more of the noise.
  • 2:57 - 3:03
    And by a 12 point average, we already can see that the strategy actually pays off:
  • 3:03 - 3:06
    we have already smoothed out the signal quite significantly,
  • 3:06 - 3:13
    and if we're willing to increase the average up to, say, 100-point,
  • 3:13 - 3:17
    then we can see that the signal we get at the output is actually very smooth.
  • 3:17 - 3:20
    You will also see that, with respect to the original signal,
  • 3:20 - 3:24
    the output has been delayed by a certain amount.
  • 3:24 - 3:29
    This is the price we pay because we are actually using up 100 samples
  • 3:29 - 3:31
    to compute the output value.
  • 3:31 - 3:36
    So, in a sense, we're accumulating a certain delay with respect to the input
  • 3:36 - 3:38
    because we're averaging from the past.
  • 3:38 - 3:40
    We will see this in more detail later on.
  • 3:40 - 3:43
    What we have done is really a filtering operation.
  • 3:43 - 3:49
    And if we want to compute the impulse response, we need to do nothing more
  • 3:49 - 3:53
    than just put the δ function inside our averaging machine,
  • 3:53 - 3:58
    and it's very easy to see that if we put the δ function in here,
  • 3:58 - 4:02
    -- this is, remember, is the averaging relation that we saw before, --
  • 4:02 - 4:10
    we have that the impulse response is equal to 1/M for n that goes from 0 to M -1, and 0 otherwise.
  • 4:10 - 4:13
    It's easy to convince yourself that in this summation,
  • 4:13 - 4:19
    there's only one non-zero term, which is the one where n is equal to k.
  • 4:19 - 4:23
    Since the index goes from 0 to M-1,
  • 4:23 - 4:25
    if your value of n is outside of this range,
  • 4:25 - 4:29
    none of these terms will be nonzero and you will get 0.
  • 4:29 - 4:32
    If we plot the impulse response, it looks like this.
  • 4:32 - 4:40
    So, you will have a string of samples that are equal to 1/M from 0 to M-1.
  • 4:40 - 4:44
    So, what we have learned experimentally is that the smoothing effect of the filter
  • 4:44 - 4:48
    is proportional to the length of the impulse response.
  • 4:48 - 4:51
    As a consequence of that, a number of operations that we have to perform
  • 4:51 - 4:55
    to compute each output sample, and also the storage
  • 4:55 - 4:59
    -- because we have to keep in the memory of our processing unit
  • 4:59 - 5:02
    the past values in order to be able to compute the average --
  • 5:02 - 5:07
    well, both of these quantities are proportional to the length of the filter.
  • 5:07 - 5:13
    So, as the filter becomes longer and as its smoothing effect increases,
  • 5:13 - 5:18
    the price we pay in terms of computational resources and memory are higher.
  • 5:18 - 5:21
    So, we might not like that, and we might want to see
  • 5:21 - 5:24
    whether there's a smarter way to achieve the same effect
  • 5:24 - 5:29
    without using up so much in terms of computing power.
  • 5:29 - 5:31
    Let's look at the equation for the Moving Average.
  • 5:31 - 5:39
    It's just 1/M times the sum of input samples from n to n-M + 1.
  • 5:39 - 5:42
    Now, we can split this into two parts.
  • 5:42 - 5:43
    We have the first term,
  • 5:43 - 5:46
    which is 1/M times the current input
  • 5:46 - 5:55
    + 1/M times the sum of past input samples from n-1 to n-M + 1.
  • 5:55 - 5:59
    And this is the sum of M-1 terms.
  • 5:59 - 6:05

    So, the second term here looks suspiciously close to the Moving Average,
  • 6:05 - 6:08
    computed this time over M-1 points.
  • 6:08 - 6:12
    So, 1 point less than before and delayed by 1.
  • 6:13 - 6:15
    So, it's close, but it is not quite the same thing,
  • 6:15 - 6:19
    so let's look at ways to formalize this relationship.
  • 6:19 - 6:24
    To do so, we first write out the standard equation for the Moving Average filter,
  • 6:24 - 6:25
    which we have seen before.
  • 6:25 - 6:26
    So, nothing new here.
  • 6:26 - 6:29
    Now, we try and compute the delayed output.
  • 6:29 - 6:43
    So, y of M of n-1 is the sum, it's 1/M times the sum from k that goes from 0 to M-1 of x[(n-1) - k].
  • 6:43 - 6:47
    We can rearrange the index of the signal like so,
  • 6:47 - 6:51
    and now this + 1 that affects only this emission (?) index
  • 6:51 - 6:54
    can be propagated to the summation range.
  • 6:54 - 6:58
    And we have that, if we delay the Moving Average by 1,
  • 6:58 - 7:02
    we basically just affect the range of the summation
  • 7:02 - 7:06
    that now goes from 1 to M, instead of from 0 to M-1.
  • 7:06 - 7:12
    Okay. Now, let's look at the formula for the Moving Average over M-1 point,
  • 7:12 - 7:15
    and this is just the Moving Average.
  • 7:15 - 7:23
    So, 1 over M-1 times the sum from k that goes to zero to M-2 of n[x - k].
  • 7:23 - 7:27
    And now we do the same, we try to delay this by one.
  • 7:27 - 7:33
    And again, this delay will propagate to the summation range.
  • 7:33 - 7:38
    And we will have the sum that goes now from k=1 to M-1.
  • 7:38 - 7:43
    So, formula for the Moving Average over M points.
  • 7:43 - 7:51
    And here, formula for the Moving Average over n-1 points, delayed by 1.
  • 7:51 - 7:58
    Alright, but now, if we take the sum of the input from zero to M-1,
  • 7:58 - 8:06
    we can split this as current sample + n-2 samples in the past.
  • 8:06 - 8:13
    and so, this part here corresponds to this and this part here corresponds to this, right?
  • 8:13 - 8:20
    So, we can swap terms and we obtain the relationship that we were looking for
  • 8:20 - 8:24
    between the Moving Average over M points,
  • 8:24 - 8:29
    and the Moving Average over M-1 points delayed by 1.
  • 8:29 - 8:32
    So, we have rearranged the terms
  • 8:32 - 8:38
    and we have that the Moving Average over M points in n is equal to
  • 8:38 - 8:48
    M-1 over n times the Moving Average over M-1 points delayed by 1, plus 1/M times x[n].
  • 8:48 - 8:51
    And so, if we want to write this a little bit better,
  • 8:51 - 8:55
    we define a parameter λ as M-1 over M,
  • 8:55 - 8:59
    and we have this final (?) relationship here.
  • 9:00 - 9:02
    Now, there's nothing new, what we saw so far.
  • 9:02 - 9:05
    We just rearranged terms in finite sums.
  • 9:05 - 9:09
    But, here comes the trick that defines the Leaky Integrator filter.
  • 9:09 - 9:15
    When M is very large, we assume that the average over M-1 or over M point (?)
  • 9:15 - 9:17
    is going to be pretty much the same:
  • 9:17 - 9:22
    not much is going to change if we add a single data point to a very long average.
  • 9:22 - 9:27
    And also, when M is very large, λ, which was defined as M-1 over M,
  • 9:27 - 9:29
    is going to be very close to 1.
  • 9:29 - 9:35
    So, we can rewrite the previous equation as current estimate for the Moving Average
  • 9:35 - 9:39
    is equal to λ times the previous estimate for a Moving Average
  • 9:39 - 9:43
    plus 1-λ times the current input.
  • 9:43 - 9:46
    And this defines a filter that we can use to process our signal.
  • 9:46 - 9:50
    Now, the filter is recursive because we're using output values,
  • 9:50 - 9:56
    previous output values, to compute the current value of the output.
  • 9:56 - 10:00
    So, there's a feedback path that goes from the output, back into the filter.
  • 10:01 - 10:04
    Before we even try to analyze this, let's see if it works.
  • 10:04 - 10:07
    So, we take our smooth signal corrupted by noise,
  • 10:07 - 10:11
    and we filter it with a Leaky Integrator for varying values of λ.
  • 10:11 - 10:16
    So, if we start with λ rather small, we don't really see much happening.
  • 10:16 - 10:19
    But, as we increase the value of λ towards 1,
  • 10:19 - 10:22
    which is really the operating assumption for the duration of the filter anyway,
  • 10:22 - 10:26
    we see that we start to smooth the signal as we expected.
  • 10:26 - 10:31
    And when λ is very close to 1, the smoothing power is comparable to that
  • 10:31 - 10:35
    of a Moving Average filter with a very high number of taps.
  • 10:35 - 10:40
    But now, the difference is that each output value only requires three operations
  • 10:40 - 10:45
    because remember, the value is simply one multiplication,
  • 10:45 - 10:51
    and then there's going to be another multiplication and one sum.
  • 10:51 - 10:54
    And this is independent of the value of λ.
  • 10:54 - 10:57
    So: high smoothing power at a fixed price.
  • 10:57 - 11:00
    Of course, the natural question when we have a filter
  • 11:00 - 11:02
    is what the impulse response is going to be
  • 11:02 - 11:05
    and to compute the impulse response of the Leaky Integrator,
  • 11:05 - 11:09
    all we need to do is to plug the δ function into the recursive equations,
  • 11:09 - 11:11
    and see what happens.
  • 11:11 - 11:18
    So, when n is less than 0, since the δ function is 0 from minus infinity to 0,
  • 11:18 - 11:22
    nothing "non-zero" has ever happened inside this equation.
  • 11:22 - 11:28
    And so, it's very safe to assume that the output will be zero for all values of n less than 0.
  • 11:28 - 11:32
    Things start to happen when n reaches zero.
  • 11:32 - 11:36
    At which point, the δ function kicks and assumes the value of one.
  • 11:36 - 11:40
    So, if we compute the value of the output for n equal to 0,
  • 11:40 - 11:43
    we have λ times the previous value of the output,
  • 11:43 - 11:46
    -- which we know to be 0, so this is 0 --
  • 11:46 - 11:51
    plus 1-λ times the value of δ and 0 (?δn0), which is 1.
  • 11:51 - 11:53
    And so, the output would be 1-λ.
  • 11:53 - 11:59
    At the next step, y[1] is equal to λ times the previous value of the output,
  • 11:59 - 12:03
    which now is not non-zero, it's 1-λ as we computed before,
  • 12:03 - 12:08
    plus 1-λ times δ 1, which we know to be 0,
  • 12:08 - 12:09
    and so, this is cancelled.
  • 12:09 - 12:12
    And the final output is λ times 1-λ.
  • 12:12 - 12:16
    At the next step, once again, the recursive equation kicks in,
  • 12:16 - 12:19
    and we have λ times the previous value,
  • 12:19 - 12:25
    which we know to be λ times 1- λ + 1-λ δ of 2,
  • 12:25 - 12:28
    but again, δ from now on is going to be zero,
  • 12:28 - 12:30
    so this term, we don't need to have to look at.
  • 12:30 - 12:33
    And so, here we have λ squared times 1-λ.
  • 12:34 - 12:36
    Next step is going to be the same.
  • 12:36 - 12:39
    And you have pretty much understood what's going on here.
  • 12:39 - 12:45
    If we plot the impulse response, we have an exponentially decaying function
  • 12:45 - 12:51
    where the exponential basis is λ, and the whole curve is weighted by 1-λ
  • 12:51 - 12:58
    and it's, of course multiplied by uni step (?) because everything is 0 for n less than 0.
  • 12:58 - 13:01
    You might wonder why we call this filter a Leaky Integrator.
  • 13:01 - 13:06
    The reason is, because if you consider, for instance, this very simple sum:
  • 13:06 - 13:10
    it's the sum of all input samples from minus infinity to current time.
  • 13:10 - 13:13
    This is really the discrete time approximation to an integral,
  • 13:13 - 13:17
    because you're really accumulating all past values up to now.
  • 13:17 - 13:21
    And we can rewrite this summation here as such.
  • 13:21 - 13:26
    So, at each step, we get what we have accumulated up to the previous step,
  • 13:26 - 13:28
    and we sum the current input.
  • 13:28 - 13:32
    So you see, it's very close to the equation for the Leaky Integrator.
  • 13:32 - 13:35
    Except that in the Leaky Integrator, what we do is the following.
  • 13:35 - 13:41
    We scale the previous accumulated value, by λ.
  • 13:41 - 13:44
    Which means, we -- remember λ is close to one,
  • 13:44 - 13:48
    so that means we keep pretty much all of what we have accumulated before,
  • 13:48 - 13:50
    but we "forget" a little bit.
  • 13:50 - 13:52
    We leak a part of it.
  • 13:52 - 13:54
    If λ is equal to 0.9,
  • 13:54 - 13:57
    then it means that 10% of what we have accumulated is lost.
  • 13:57 - 14:03
    And we replace what we've lost by a fraction of the input,
  • 14:03 - 14:05
    and this fraction is 1-λ.
  • 14:05 - 14:07
    So, what we've lost from the accumulator,
  • 14:07 - 14:12
    we'll replace with an equal percentage of the input.
  • 14:12 - 14:14
    The idea is that by picking λ less than one,
  • 14:14 - 14:17
    we are forgetting, we are leaking the past,
  • 14:17 - 14:19
    and the accumulation will never blow up.
Title:
5.2 - Filtering by example
Video Language:
English

English subtitles

Incomplete

Revisions