ENع
←Block diagrams
Lesson 19 · Discrete-time signals and systems

Block diagrams: delays, adders, gains and direct forms

18 min11 steps5 questionsIntermediate
About this lesson

A difference equation only ever does three things: delay a sample, multiply it by a number and add. A block diagram draws each of those as a block, and arrows carry the samples between them. Follow one filter, y[n] = 0.5·y[n−1] + 2·x[n] + x[n−1], as a unit impulse runs through it one tick at a time: first drawn term by term (direct form I, two delays), then with its two halves swapped so they share a single delay (direct form II). Both give the same output, 2, 2, 1, 0.5, 0.25, 0.125 ..., and you finish by running it with scipy.signal.lfilter. It builds on the lesson on difference equations and on the impulse response.

You'll learn to

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

Fig 19 · Interactive
01 / 11

A difference equation is a recipe

A difference equation is a recipe: y[n] = 0.5·y[n−1] + 2·x[n] + x[n−1]. It only does three things: remember an old sample, multiply by a number, and add. A block diagram draws each one as a block.

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

x[n]    the input list   (n = 0, 1, 2 ...)
y[n]    the output list
x[n−1]  the input one sample ago
y[n−1]  the output one sample ago

only three operations appear:
  delay     remember one old sample
  gain      multiply by a fixed number
  adder     add

each one gets a block; arrows carry samples
Loading figure…
or ← →
Read the lesson step by step
  1. A difference equation is a recipe

    A difference equation is a recipe: y[n] = 0.5·y[n−1] + 2·x[n] + x[n−1]. It only does three things: remember an old sample, multiply by a number, and add. A block diagram draws each one as a block.

    y[n] = 0.5·y[n−1] + 2·x[n] + x[n−1]
    
    x[n]    the input list   (n = 0, 1, 2 ...)
    y[n]    the output list
    x[n−1]  the input one sample ago
    y[n−1]  the output one sample ago
    
    only three operations appear:
      delay     remember one old sample
      gain      multiply by a fixed number
      adder     add
    
    each one gets a block; arrows carry samples
  2. Delay z⁻¹: remember one sample

    The delay box, written z⁻¹, remembers one sample. On every clock tick it takes in the current value and hands out the one it stored a tick earlier. In: x[n]. Out: x[n−1]. In code, it is one variable.

    delay (unit delay), drawn as a box marked z⁻¹
    
      in:   x[n]
      out:  x[n−1]   (stored one tick ago)
    
    tick 0: stores x[0], outputs 0 (starts empty)
    tick 1: stores x[1], outputs x[0]
    tick 2: stores x[2], outputs x[1]
    
    in code: one variable
      out = xd      # read the old sample
      xd  = xn      # store the new one
    
    z⁻¹ comes from the z-transform (a later
    module); here it just means "one sample late"
  3. Gain: multiply by a number

    A gain multiplies every sample that passes through it by a fixed number. This one is b₀ = 2: x[n] goes in, 2·x[n] comes out. In code, it is one multiplication.

    gain (multiplier, coefficient)
    
      in:   x[n]
      out:  b0·x[n],   b0 = 2
    
    x = 1    ->  2
    x = 0.5  ->  1
    x = −3   ->  −6
    
    in code: one multiplication
      p0 = 2 * xn
    
    multiplying makes it linear; a number
    that never changes while the filter runs
    makes it time-invariant
  4. Adder: sum the arrows

    An adder outputs the sum of every arrow that points into it. Here it adds 2·x[n] and x[n−1]. Using only present and past inputs like this gives an FIR filter (finite impulse response).

    adder (summing junction): a disc marked +
    
      ins:  2·x[n]  and  1·x[n−1]
      out:  2·x[n] + x[n−1]
    
    the x[n−1] path: delay, then gain b1 = 1
    (a gain of 1 is often left out of a drawing)
    
    only inputs feed the adder so far:
      FIR = finite impulse response
      an impulse in -> 2, 1, then zeros forever
  5. Feedback: direct form I

    Now the y side. The output goes into a second delay, comes back one tick later as y[n−1], is scaled by a₁ = 0.5 and added in. Drawing each term of the equation like this is called direct form I.

    direct form I: one block per term
    
      2·x[n]      gain b0
      x[n−1]      delay (x side), gain b1
      0.5·y[n−1]  delay (y side), gain a1
      +           one adder for all three
    
    the y[n−1] path is FEEDBACK:
    the output comes back into the adder
    
    delays: 2 (one per old sample it remembers)
    one tick = 3 beats in the figure:
      products -> adder -> output -> delays store
  6. n = 0: the impulse goes in

    Feed in the unit impulse: x[0] = 1, then zeros. Both delays start at 0. So y[0] = 2·1 + 0 + 0.5·0 = 2. On the tick, the delays store x[0] = 1 and y[0] = 2.

    input: x[n] = δ[n] = 1, 0, 0, 0 ...
    delays start empty: x[−1] = 0, y[−1] = 0
    
    n = 0
      2·x[0]     = 2·1   = 2
      1·x[−1]    = 1·0   = 0
      0.5·y[−1]  = 0.5·0 = 0
      y[0]       = 2
    
    tick: x-side delay <- 1, y-side delay <- 2
  7. n = 1: the delays speak

    n = 1: the input is 0 now, but the delays still hold the past. y[1] = 2·0 + 1 + 0.5·2 = 2. Then the delays store 0 and 2.

    n = 1
      2·x[1]     = 2·0   = 0
      1·x[0]     = 1·1   = 1
      0.5·y[0]   = 0.5·2 = 1
      y[1]       = 2
    
    tick: x-side delay <- 0, y-side delay <- 2
    
    the input is already gone,
    but the memory still shapes the output
  8. Feedback keeps it ringing

    From n = 2 the x side holds only zeros, but the loop keeps going: each output is half the one before. 1, 0.5, 0.25, 0.125 ... It never quite reaches zero: an IIR filter (infinite impulse response).

    n ≥ 2: x[n] = 0 and x[n−1] = 0
      y[n] = 0.5·y[n−1]
    
      y[2] = 0.5·2    = 1
      y[3] = 0.5·1    = 0.5
      y[4] = 0.5·0.5  = 0.25
      y[5] = 0.5·0.25 = 0.125
    
    h[n] = 2, 2, 1, 0.5, 0.25, 0.125 ...
         = 4·0.5ⁿ for n ≥ 1 (h[0] = 2)
    IIR = infinite impulse response
    |a1| < 1, so it dies away (stable)
  9. Swap the two halves

    Both halves are linear and time-invariant, so their order can be swapped. Feedback first: w[n] = x[n] + 0.5·w[n−1]. Then the input part: y[n] = 2·w[n] + w[n−1]. Both halves now need the same w[n−1].

    form I = (input half) then (feedback half)
    swap them (LTI blocks in series commute):
    
      feedback half:  w[n] = x[n] + 0.5·w[n−1]
      input half:     y[n] = 2·w[n] + 1·w[n−1]
    
    w[n] is a helper signal in the middle
    
    the feedback half needs w[n−1]
    the input half needs   w[n−1]
    -> the same number, so keep it once
  10. Direct form II: one delay

    So one delay serves both: direct form II. Run the same impulse through it: y = 2, 2, 1, 0.5, 0.25, 0.125. The same output as before, with one delay instead of two. Fewer delays means less memory.

    direct form II, n = 0 .. 5
    
     n   w[n−1]  w[n]    y[n] = 2·w + w[n−1]
     0   0       1       2
     1   1       0.5     2
     2   0.5     0.25    1
     3   0.25    0.125   0.5
     4   0.125   0.0625  0.25
     5   0.0625  0.0313  0.125
    
    same y[n] as direct form I
    delays: 1 instead of 2
    (canonic: as few delays as the order, here 1)
  11. In Python: scipy.signal.lfilter

    In Python, scipy.signal.lfilter([2, 1], [1, -0.5], x) runs this filter. SciPy writes the feedback with the opposite sign, so a₁ = 0.5 becomes −0.5. Inside, it uses a close cousin: the transposed direct form II.

    import numpy as np
    from scipy.signal import lfilter
    x = np.zeros(6); x[0] = 1
    y = lfilter([2, 1], [1, -0.5], x)
    
    >>> y
    array([2.   , 2.   , 1.   , 0.5  , 0.25 , 0.125])
    
    SciPy: y[n] = b0·x[n] + b1·x[n−1] − a1·y[n−1]
      so [1, -0.5] means + 0.5·y[n−1]
    
    by hand, direct form I in plain Python:
    xd = yd = 0.0
    for xn in x:
        yn = 2*xn + 1*xd + 0.5*yd
        xd, yd = xn, yn
    
    (Python 3, numpy, scipy)

Quiz Unlocks after the last step.