Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 38 ยท Discrete Signals

Difference Equations and Impulse Response

Write LTI systems as difference equations, run them sample by sample, compare with scipy.signal.lfilter, and see how feedback creates infinite impulse responses.

Convolution is a neat formula, but it is not how most systems are written down or programmed. The usual description is a difference equation: a rule that computes each output sample from recent inputs and recent outputs. It is the discrete counterpart of a differential equation and the direct recipe for running a filter in real time.

The general form

y[n] = b0*x[n] + b1*x[n-1] + ... + bM*x[n-M] - a1*y[n-1] - a2*y[n-2] - ... - aN*y[n-N]

  • The b coefficients (the feedforward part) weight the current and past inputs.
  • The a coefficients (the feedback part) weight past outputs.
  • N is the order of the feedback; max(M, N) is the filter order.

If all a are zero the output depends on inputs only: an FIR filter. If any a is nonzero the system has feedback and generally an infinite impulse response (IIR).

flowchart LR
    X["x[n]"] --> FF["feedforward<br/>b0 x[n] + b1 x[n-1] + ..."]
    FF --> SUM["+"]
    FB["feedback<br/>-a1 y[n-1] - ..."] --> SUM
    SUM --> Y["y[n]"]
    Y --> D["delays"]
    D --> FB

Feedback sends delayed outputs back into the sum.

Running a difference equation by hand

Take y[n] = 0.5*y[n-1] + x[n] with y[-1] = 0. Start with an impulse input. Then y[0] = 1, y[1] = 0.5, y[2] = 0.25, and so on: the impulse response is h[n] = 0.5^n for n >= 0, which never reaches exactly zero. That is what infinite impulse response means, and it comes from a two-term rule.

Python A first-order recursive filter, sample by sample

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

scipy.signal.lfilter(b, a, x) is the standard routine: pass the numerator coefficients b, the denominator coefficients a (with a[0] = 1 meaning y[n] itself) and the input. Beware of the sign convention: in a, the feedback terms appear with the opposite sign from the equation, because a describes y[n] + a1*y[n-1] + ... = ....

Step response and the effect of feedback

Applying a step input shows how a system settles. For y[n] = a*y[n-1] + x[n]:

  • |a| < 1: the output approaches the steady value 1/(1-a).
  • a = 1: the output is a running sum (an accumulator) that grows forever.
  • a > 1: the output blows up.
  • a < 0: the output oscillates around its final value.
Python Step response for several feedback values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The same system as an impulse response

Every difference equation has an impulse response. Feed an impulse through lfilter and you get h[n] (as above). In the other direction, if h is finite, the coefficients b = h and a = [1] realize it. Convolution with h and the difference equation give identical outputs.

Python Recursion equals convolution with h

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two agree to numerical precision, yet feedback needs two multiplications per sample while the truncated impulse response needs hundreds. That efficiency is the reason IIR filters exist.

Useful examples

Difference equation What it does
y[n] = x[n] - x[n-1] First difference (high-pass, a derivative)
y[n] = (x[n] + x[n-1] + x[n-2]) / 3 3-point moving average
y[n] = y[n-1] + x[n] Accumulator (integrator)
y[n] = x[n] + 0.6*x[n-100] Echo after 100 samples
y[n] = x[n] + 0.6*y[n-100] Repeating echoes (comb filter)
y[n] = 0.1*x[n] + 0.9*y[n-1] Exponential smoothing
Python Echo versus repeating echoes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Initial conditions and causality

Recursion needs starting values for y[-1], y[-2], .... The default is zero ("system at rest"), and then the system is causal and LTI. Nonzero starting values add a decaying transient that does not depend on the input. lfilter accepts an initial state through its zi argument, which you will use when filtering signals in blocks.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In y[n] = 0.5 y[n-1] + x[n], what is the impulse response for n >= 0?

    Start y[0] = 1, then each step multiplies by 0.5, giving 1, 0.5, 0.25, ...
  2. 2. A filter whose difference equation contains past OUTPUT terms is generally:

    Feedback lets one input sample influence the output forever, giving an infinite impulse response.
  3. 3. For y[n] = a y[n-1] + x[n] with a step input and |a| < 1, the output tends to:

    At steady state y = a y + 1, so y = 1/(1-a).
  4. 4. In scipy.signal.lfilter(b, a, x), what does b hold?

    b is the numerator: it weights the current and past inputs; a is the denominator for the feedback.

Summary

  • A difference equation computes each output from past inputs (b coefficients) and past outputs (a coefficients).
  • No feedback gives an FIR filter; feedback gives an IIR filter.
  • The impulse response of y[n] = a y[n-1] + x[n] is a^n; it decays if |a| < 1.
  • scipy.signal.lfilter(b, a, x) runs any such filter; a[0] is 1 and feedback signs are reversed in a.
  • Recursion reaches long impulse responses with very few multiplications.

Next: The z-Transform Basics