ENع
←Difference equations
Lesson 18 · Discrete-time signals and systems

Difference equations

14 min10 steps5 questionsIntermediate
About this lesson

A difference equation is a rule a digital system runs once per sample: it makes each new output from the new input and a few earlier outputs. Follow one, y[n] = 0.5·y[n−1] + 0.5·x[n], as an input step of height 8 goes in: a delay box remembers the last output, feedback brings it back, and the output climbs 4, 6, 7, 7.5, 7.75 toward 8. You will see why feedback gives an infinite impulse response, check that convolution gives the same answer, and run the rule in Python. It builds on the lessons "The impulse response of a digital system" and "Discrete convolution, step by step".

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 18 · Interactive
01 / 10

A rule for every new sample

A difference equation is a rule a system follows once per sample. Ours: y[n] = 0.5·y[n−1] + 0.5·x[n]. The new output is half the new input plus half the last output.

y[n] = 0.5·y[n−1] + 0.5·x[n]

x[n]    the input, sample number n
y[n]    the output, sample number n
y[n−1]  the output one sample earlier

four parts do the work:
  ×0.5   multiply the new input
  +      the adder
  delay  remembers the last output
  ×0.5   multiply that old output
Loading figure…
or ← →
Read the lesson step by step
  1. A rule for every new sample

    A difference equation is a rule a system follows once per sample. Ours: y[n] = 0.5·y[n−1] + 0.5·x[n]. The new output is half the new input plus half the last output.

    y[n] = 0.5·y[n−1] + 0.5·x[n]
    
    x[n]    the input, sample number n
    y[n]    the output, sample number n
    y[n−1]  the output one sample earlier
    
    four parts do the work:
      ×0.5   multiply the new input
      +      the adder
      delay  remembers the last output
      ×0.5   multiply that old output
  2. Start at rest: y[−1] = 0

    The delay box (written z⁻¹ in books) holds one number for one tick: the last output. Before the input starts, nothing has happened yet, so it holds y[−1] = 0. This start is called initial rest.

    the rule needs y[n−1]
    for n = 0 that is y[−1]: before the start
    
    initial rest:
      input 0 before n = 0  ->  output 0 too
      so the delay box starts at y[−1] = 0
    
    a different start value gives
    different outputs from the same rule
  3. n = 0: the first sample

    The input jumps to 8. x[0] = 8 is multiplied by 0.5: 4. The delay box gives 0, and 0.5 × 0 = 0. The adder sums them: y[0] = 4 + 0 = 4.

    n = 0
      0.5·x[0]  = 0.5 × 8 = 4
      0.5·y[−1] = 0.5 × 0 = 0
      y[0] = 4 + 0 = 4
    
    input  x[n] = 8 for n ≥ 0
           (a step of height 8)
  4. Remember the output

    The new output is copied into the delay box. One tick later it comes back as y[n−1]. This loop from the output back into the adder is called feedback.

    after each sample:
      delay box <- y[n]
    
    next tick it is read as y[n−1]
    
    output -> delay -> ×0.5 -> adder
    is a loop: feedback
    
    the delay box is the system's memory:
    one number
  5. n = 1: feedback at work

    x[1] = 8 gives 4 again. The delay box returns y[0] = 4, and half of it is 2. So y[1] = 4 + 2 = 6. The output now depends on the past, not only on the input.

    n = 1
      0.5·x[1] = 0.5 × 8 = 4
      0.5·y[0] = 0.5 × 4 = 2
      y[1] = 4 + 2 = 6
    
    same input as before (8),
    different output (6, not 4):
    the system has memory
  6. Run it: 7, 7.5, 7.75

    The same rule, again and again: y[2] = 4 + 3 = 7, y[3] = 4 + 3.5 = 7.5, y[4] = 4 + 3.75 = 7.75. Each tick closes half of the gap to 8.

    n   0.5·x[n]  0.5·y[n−1]  y[n]
    0   4         0           4
    1   4         2           6
    2   4         3           7
    3   4         3.5         7.5
    4   4         3.75        7.75
    5   4         3.875       7.875
    
    gap to 8:  4, 2, 1, 0.5, 0.25 ...
    halved every tick
  7. Where it settles

    If the output stops changing, y = 0.5·y + 0.5·8, so y = 8. The output creeps up to the input instead of jumping. That makes this rule a smoother, known as exponential smoothing.

    steady state: y[n] = y[n−1] = y
      y = 0.5·y + 0.5·8
      0.5·y = 4
      y = 8
    
    closed form (check n = 2):
      y[n] = 8·(1 − 0.5ⁿ⁺¹)
      y[2] = 8 × (1 − 0.125) = 7
    
    exponential smoothing:
      s[n] = α·x[n] + (1 − α)·s[n−1], α = 0.5
  8. Impulse response: halves forever

    Now feed a single 8, then zeros: 8·δ[n]. Out come 4, 2, 1, 0.5 ... Each output is half the last and never reaches exactly 0. Feedback gives an infinite impulse response (IIR): h[n] = 0.5ⁿ⁺¹.

    input 8·δ[n] = 8, 0, 0, 0 ...
    output        = 4, 2, 1, 0.5, 0.25 ...
    
    so the impulse response is
      h[n] = 0.5·0.5ⁿ = 0.5ⁿ⁺¹  for n ≥ 0
    
    never exactly 0: IIR
    (infinite impulse response)
    without feedback (no y[n−1] term)
    it would be FIR: finite
  9. Same answer as convolution

    Convolving the step with h[n] gives the same 4, 6, 7, 7.5 ... But h[n] never ends, so that sum grows with n. The rule needs only 2 multiplies, 1 add and one stored number per sample.

    y[n] = Σ h[k]·x[n−k],  k = 0 .. n
    
    y[2] = h[0]·8 + h[1]·8 + h[2]·8
         = 4 + 2 + 1 = 7   (same as the rule)
    
    convolution at sample n: n + 1 products
    the rule at any n:       2 products, 1 add
    
    two views of one system:
    impulse response, or difference equation
  10. In Python: a loop with one memory

    In Python 3, the rule is a loop. prev is the delay box: it starts at 0 and is updated once per sample. scipy.signal.lfilter([0.5], [1, -0.5], x) runs the same rule and gives the same numbers.

    prev = 0.0
    for xn in [8, 8, 8, 8, 8]:
        y = 0.5*prev + 0.5*xn
        prev = y
        print(y)
    
    prints: 4.0  6.0  7.0  7.5  7.75
    
    with scipy (and numpy):
    >>> from scipy.signal import lfilter
    >>> lfilter([0.5], [1, -0.5], [8]*5)
    array([4.  , 6.  , 7.  , 7.5 , 7.75])
    
    lfilter's a = [1, −0.5] means
      y[n] − 0.5·y[n−1] = 0.5·x[n]

Quiz Unlocks after the last step.