Block diagrams: delays, adders, gains and direct forms
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
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.
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
اقرأ الدرس خطوة بخطوة
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 samplesDelay 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"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-invariantAdder: 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 foreverFeedback: 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 storen = 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 <- 2n = 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 outputFeedback 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)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 onceDirect 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)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)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01A delay block z⁻¹ has received the inputs 4, 7, 1 (in that order, starting empty). What did it output on those three ticks?
A unit delay hands out what it stored one tick earlier. It starts empty (0), then outputs 4, then 7. The 1 comes out on the next tick.
02For y[n] = 0.5·y[n−1] + 2·x[n] + x[n−1] with both delays starting at 0, what is y[1] when the input is x = 1, 0, 0, ...?
y[0] = 2·1 = 2. Then y[1] = 0.5·2 + 2·0 + 1·1 = 1 + 0 + 1 = 2.
03Which feature of a block diagram makes its impulse response infinitely long (IIR)?
Feedback re-uses past outputs, so a value keeps circulating: here each output is half the one before and never becomes exactly zero. With only input delays and gains (no feedback) the response ends after a few samples: FIR.
04A first-order filter is drawn in direct form I and in direct form II. How many delay blocks does each need?
Direct form I keeps a delay for x[n−1] and one for y[n−1]. Direct form II swaps the two halves so both need the same w[n−1], and one delay serves both: as many delays as the order.
05In SciPy, which call runs y[n] = 0.5·y[n−1] + 2·x[n] + x[n−1]?
lfilter(b, a, x) uses a[0]·y[n] = b[0]·x[n] + b[1]·x[n−1] − a[1]·y[n−1]. To get + 0.5·y[n−1] you need a[1] = −0.5, so a = [1, -0.5] and b = [2, 1].
المسرد
- Block diagram
- A drawing of a system as blocks (delays, gains, adders) joined by arrows that carry samples. It shows exactly what a program or a chip computes for each new sample.
- Unit delay z⁻¹
- A block that outputs the sample it received one tick earlier: in x[n], out x[n−1]. In code it is one stored variable. The name z⁻¹ comes from the z-transform.
- Gain (coefficient)
- A block that multiplies every sample by a fixed number, such as b₀ = 2.
- Adder (summing junction)
- A block, often drawn as a circle with +, whose output is the sum of all the arrows pointing into it.
- Feedback
- A path that brings a past output back into the adder. It is what lets a filter's impulse response go on forever (IIR).
- FIR and IIR
- Finite and infinite impulse response. An FIR filter only combines inputs, so its impulse response ends; an IIR filter has feedback, so it can ring on.
- Direct form I
- The block diagram that draws each term of the difference equation: a delay line for the inputs, one for the outputs, gains and an adder. An order-N filter needs 2N delays.
- Direct form II
- Direct form I with its two halves swapped so they share one delay line: only N delays for order N (canonic). Same output in exact arithmetic.
الملاحظات والمصادر
- The coefficients b₀ = 2, b₁ = 1 and a₁ = 0.5 are chosen example values; the input is the unit impulse. Every token, face value and stem in the figure is computed by stepping the two structures in model.mjs.
- Sign conventions differ: Oppenheim & Schafer write the feedback as + a₁·y[n−1], SciPy as − a[1]·y[n−1]. The diagram is the same; only the number stored for a₁ flips sign.
- One sample (one clock tick) is split into beats so the eye can follow it: the products, the sum, then the delays storing. Real hardware does it all within one sample period.
- A. V. Oppenheim, R. W. Schafer, J. R. Buck, Discrete-Time Signal Processing, 2nd ed., Prentice Hall, 1999, ch. 6 (block diagrams, direct forms I and II); course text of MIT OCW 6.341
- MIT OpenCourseWare 6.341 Discrete-Time Signal Processing (A. V. Oppenheim)
- MIT OpenCourseWare 6.003 Signals and Systems (D. Freeman, Fall 2011)
- J. O. Smith III, Introduction to Digital Filters, CCRMA, Stanford University: Direct-Form I
- J. O. Smith III, Introduction to Digital Filters, CCRMA, Stanford University: Direct-Form II (series LTI sections commute; shared delays; canonical with respect to delay)
- SciPy documentation: scipy.signal.lfilter (difference equation and its sign convention; implemented as a direct II transposed structure)
- Digital filter (Wikipedia): direct form I needs 2N delays, direct form II needs N
اختبار يتاح بعد الخطوة الأخيرة.