A digital filter(數位濾波器)is an LTI system designed to pass certain frequency components of a signal and attenuate(衰減)others. Because an LTI system simply multiplies each frequency component by its frequency response, the whole story of filtering is contained in
Frequencies where $|H(e^{j\omega})| \approx 1$ form the passband(通帶); frequencies where $|H(e^{j\omega})| \approx 0$ form the stopband(阻帶). This chapter introduces the simplest members of the two great families of realizable filters:
| FIR (Finite Impulse Response, 有限脈衝響應) | IIR (Infinite Impulse Response, 無限脈衝響應) | |
|---|---|---|
| Difference equation | $y[n]=\sum_{k=0}^{M} b_k\,x[n-k]$ (no feedback) | $y[n]=\sum_{k=0}^{M} b_k x[n-k]-\sum_{k=1}^{N} a_k y[n-k]$ (feedback) |
| Impulse response | finite length | infinite length |
| Poles | only at $z=0$ — always stable | anywhere; stable iff poles inside unit circle |
| Exact linear phase | possible (Section 11.6) | not possible (only approximately) |
| Order needed for sharp cutoff | high | low |
Filter gains span many orders of magnitude, so magnitude responses are usually plotted on a logarithmic decibel scale:
| $|H|$ | $|H|^2$ (power) | gain in dB | read as |
|---|---|---|---|
| $1$ | $1$ | $0$ dB | unity gain |
| $1/\sqrt{2}\approx 0.707$ | $1/2$ | $-3$ dB | half-power point(半功率點) |
| $1/2$ | $1/4$ | $-6$ dB | halving the amplitude |
| $0.1$ | $0.01$ | $-20$ dB | one decade down |
| $0.01$ | $10^{-4}$ | $-40$ dB | two decades down |
The simplest lowpass filter(低通濾波器)averages the current and previous samples:
Its frequency response factors into a real amplitude and a linear phase:
Generalizing to an average of $M$ samples gives the moving-average filter(移動平均濾波器), the workhorse of noise smoothing:
The magnitude is a periodic-sinc (Dirichlet) shape with nulls at $\omega = 2\pi k/M$, $k=1,\dots,M-1$: any sinusoid completing a whole number of cycles inside the averaging window averages to zero. Increasing $M$ narrows the passband and increases the smoothing, at the cost of a longer delay $(M-1)/2$ samples.
b = ones(1,M)/M; y = filter(b,1,x); implements the causal moving average. Use freqz(b,1) to inspect its magnitude/phase. It is a fine smoother but a poor lowpass filter: its first sidelobe is only about 13 dB down, so stopband rejection is weak.
Flipping the sign of the second tap turns the averager into a differencer — the simplest highpass filter(高通濾波器):
Now the zero sits at $z=+1$ ($\omega=0$): DC is annihilated, while the fastest alternation $(-1)^n$ passes with gain 1. Its 3-dB cutoff is again $\omega_c = \pi/2$. In fact $h_1[n]=(-1)^n h_0[n]$ — replacing $z$ by $-z$ shifts a frequency response by $\pi$, converting lowpass to highpass(把 $z\to-z$,頻率響應平移 π,低通變高通).
Adding one feedback coefficient buys far more selectivity per multiply. All four classical responses — LP, HP, BP, BS — have standard first/second-order IIR prototypes, parameterized so that the gain is exactly 1 at the center of the passband and exactly 0 at the center of the stopband.
The zero at $z=-1$ forces $H_{LP}(e^{j\pi})=0$; the normalizing constant forces $H_{LP}(e^{j0})=1$. The pole at $z=\alpha$ (real, $0<\alpha<1$ in practice) boosts low frequencies — the closer $\alpha$ is to 1, the narrower the passband. The 3-dB cutoff is controlled directly by $\alpha$:
Same pole, but the zero moves to $z=+1$: now DC is blocked and $\omega=\pi$ has unit gain. The same $\alpha \leftrightarrow \omega_c$ relation holds, so one design formula serves both filters.
A bandpass filter(帶通濾波器)needs zeros at both $\omega=0$ and $\omega=\pi$ and a resonant pole pair near the center frequency $\omega_0$:
The two parameters decouple beautifully: $\beta$ sets where the peak is ($|H_{BP}(e^{j\omega_0})|=1$ exactly), while $\alpha$ sets how narrow it is. Stability requires $|\alpha|<1$ and $|\beta|<1$.
Now the zeros sit on the unit circle at $e^{\pm j\omega_0}$ with $\omega_0=\cos^{-1}\beta$, so the gain is exactly zero at the notch frequency(陷波頻率)— the classic tool for removing power-line hum(去除電源雜訊). The same $\alpha$ formula gives the 3-dB notch width, and with matching parameters $H_{BP}(z)+H_{BS}(z)=1$: another doubly complementary pair.
A single first-order section rolls off slowly. The cheapest way to sharpen it is to cascade(串接)identical copies: overall $H(z)=G_1(z)G_2(z)\cdots G_K(z)$, magnitudes multiply, dB curves add.
Cascading $K$ copies of the two-point average gives $H(z)=\bigl[\tfrac12(1+z^{-1})\bigr]^K$ with magnitude $\cos^K(\omega/2)$. The stopband improves dramatically — at $\omega=3\pi/4$ one section gives −8.3 dB but four sections give −33 dB — yet there is a price: the passband shrinks too.
An ideal filter(理想濾波器)has unit gain in the passband, zero gain in the stopband, and an instantaneous transition between them — a "brick wall"(磚牆式)response. The four classical types, on $0\le\omega\le\pi$ (responses are even in $\omega$ and $2\pi$-periodic):
Take the inverse DTFT of the ideal lowpass response $H_{LP}(e^{j\omega})=1$ for $|\omega|\le\omega_c$, $0$ otherwise:
Since brick walls are impossible, a practical lowpass spec relaxes the ideal in three ways: allow a small passband ripple(通帶漣波)$\delta_p$, allow a small nonzero stopband level(阻帶漣波)$\delta_s$, and insert a transition band(過渡帶)between the passband edge $\omega_p$ and stopband edge $\omega_s$ where the response is unconstrained:
Specifications are usually quoted in dB:
For example $\delta_s=0.01$ means $A_s=40$ dB; $\delta_p=0.05$ means $R_p\approx0.45$ dB. (An alternative convention quotes the total passband swing $20\log_{10}\frac{1+\delta_p}{1-\delta_p}$ — read the problem statement carefully.) A complete lowpass specification is then the quadruple $(\omega_p,\ \omega_s,\ \delta_p,\ \delta_s)$, or equivalently $(\omega_p,\ \omega_s,\ R_p,\ A_s)$. The filter design problem of the coming chapters is: find the lowest-order filter whose response stays inside the template.
Magnitude is only half the story. If the phase response $\angle H(e^{j\omega})$ is not linear in $\omega$, different frequency components experience different delays — the group delay(群延遲)$\tau(\omega)=-\,d\angle H/d\omega$ varies — and the waveform is smeared even when all magnitudes are untouched. This phase distortion(相位失真)is fatal in applications that care about waveform shape (ECG, seismic data, images, data pulses).
Every frequency is delayed by the same $D$ samples: the output is a filtered, shifted, but unsmeared copy of the input.
For offline data there is an elegant trick. Filter the signal, time-reverse the result, filter again, and reverse once more. Each pass contributes $H(e^{j\omega})$ once with a conjugation, so the overall response is
— real and nonnegative: exactly zero phase, with the magnitude squared (doubling attenuation in dB). This is MATLAB's filtfilt. The catch: the procedure is noncausal, so it only works on recorded (finite, stored) signals, not in real time(僅適用於離線資料,無法即時處理).
A causal length-$N$ FIR filter $h[n]$, $n=0,\dots,N-1$, has (generalized) linear phase if and only if its impulse response is symmetric or antisymmetric about its midpoint:
with $A(\omega)$ real (the amplitude response) and constant group delay $\tau=(N-1)/2$ samples. Two symmetry choices times two length parities give exactly four types:
| Type | Symmetry | Length $N$ | Group delay $\frac{N-1}{2}$ | Forced zeros | Can realize |
|---|---|---|---|---|---|
| 1 | symmetric $h[n]=h[N{-}1{-}n]$ | odd | integer | none | LP, HP, BP, BS — anything |
| 2 | symmetric | even | half-integer | $z=-1$ ($\omega=\pi$) | LP, BP (not HP, not BS) |
| 3 | antisymmetric $h[n]=-h[N{-}1{-}n]$ | odd | integer | $z=+1$ and $z=-1$ | BP, differentiator, Hilbert |
| 4 | antisymmetric | even | half-integer | $z=+1$ ($\omega=0$) | HP, BP, differentiator, Hilbert |
The structural zeros in the table follow directly from the symmetry. Evaluate $H(z)=\sum h[n]z^{-n}$ at $z=\pm1$:
Lowpass $\tfrac12(1+z^{-1})$: $|H|=\cos\frac\omega2$, zero at $\pi$. Highpass $\tfrac12(1-z^{-1})$: $|H|=\sin\frac\omega2$, zero at DC. Both cut off at $\pi/2$; $M$-point moving average generalizes the LP with nulls at $2\pi k/M$.
First-order LP/HP with pole $\alpha$: $\alpha=\frac{1-\sin\omega_c}{\cos\omega_c}$ sets the 3-dB cutoff. Second-order BP/BS: $\beta=\cos\omega_0$ sets the center/notch, $\cos B_w=\frac{2\alpha}{1+\alpha^2}$ sets the width. Matching pairs satisfy $H_1+H_2=1$ and $|H_1|^2+|H_2|^2=1$.
Gain $=20\log_{10}|H|$ dB; $-3$ dB $=$ half power $=1/\sqrt2$ amplitude. Cascading: magnitudes multiply, dB add — stopband deepens but the passband narrows.
Brick-wall LP/HP/BP/BS. Ideal LP has $h[n]=\frac{\sin\omega_c n}{\pi n}$: noncausal, infinite, not absolutely summable ⇒ unrealizable; real filters only approximate it.
Spec $=(\omega_p,\omega_s,\delta_p,\delta_s)$: passband within $1\pm\delta_p$, stopband below $\delta_s$, free transition band. In dB: $R_p=-20\log_{10}(1-\delta_p)$, $A_s=-20\log_{10}\delta_s$.
$h[n]=\pm h[N{-}1{-}n]$ ⇒ constant group delay $\frac{N-1}{2}$. Type 1 (sym, odd): anything. Type 2 (sym, even): no HP/BS. Type 3 (antisym, odd): zeros at $0$ and $\pi$. Type 4 (antisym, even): no LP/BS. Offline zero phase: filtfilt ⇒ $|H|^2$.
Problem: Consider the filters $H_0(z)=\tfrac12(1+z^{-1})$ and $H_1(z)=\tfrac12(1-z^{-1})$. (a) Derive the magnitude and phase responses of both filters. (b) Find the 3-dB cutoff frequency of each. (c) Show that the pair is power-complementary. (d) What is the gain of $H_0$ in dB at $\omega = 3\pi/4$?
(a) Factor out the half-sample delay:
$$H_0(e^{j\omega})=\tfrac12\bigl(1+e^{-j\omega}\bigr)=e^{-j\omega/2}\,\frac{e^{j\omega/2}+e^{-j\omega/2}}{2}=e^{-j\omega/2}\cos\tfrac\omega2$$ $$H_1(e^{j\omega})=\tfrac12\bigl(1-e^{-j\omega}\bigr)=e^{-j\omega/2}\,\frac{e^{j\omega/2}-e^{-j\omega/2}}{2}=j\,e^{-j\omega/2}\sin\tfrac\omega2$$So $|H_0|=\cos\frac\omega2$, $\angle H_0=-\frac\omega2$; and $|H_1|=\sin\frac\omega2$ (for $0\le\omega\le\pi$), $\angle H_1=\frac\pi2-\frac\omega2$. Both have linear phase (Types 2 and 4 respectively).
(b) $\cos\frac{\omega_c}2=\frac1{\sqrt2}\Rightarrow\omega_c=\frac\pi2$; likewise $\sin\frac{\omega_c}2=\frac1{\sqrt2}\Rightarrow\omega_c=\frac\pi2$. Same cutoff — they split the band evenly.
(c) $|H_0|^2+|H_1|^2=\cos^2\frac\omega2+\sin^2\frac\omega2=1$ for all $\omega$. $\blacksquare$
(d) $|H_0(e^{j3\pi/4})|=\cos\frac{3\pi}8=0.3827$, so the gain is $20\log_{10}0.3827=-8.34$ dB.
Problem: Design the first-order IIR lowpass $H_{LP}(z)=\frac{1-\alpha}{2}\cdot\frac{1+z^{-1}}{1-\alpha z^{-1}}$ with a 3-dB cutoff at $\omega_c=\pi/3$. (a) Find $\alpha$ and write out $H_{LP}(z)$ numerically and the difference equation. (b) Verify the gains at $\omega=0$, $\omega=\omega_c$, and $\omega=\pi$. (c) Write the corresponding highpass $H_{HP}(z)$ with the same cutoff and state the two complementarity relations linking the pair.
(a) With $\sin\frac\pi3=0.8660$, $\cos\frac\pi3=0.5$:
$$\alpha=\frac{1-\sin\omega_c}{\cos\omega_c}=\frac{1-0.8660}{0.5}=0.2679$$ $$H_{LP}(z)=0.3660\,\frac{1+z^{-1}}{1-0.2679\,z^{-1}} \qquad y[n]=0.2679\,y[n-1]+0.3660\,x[n]+0.3660\,x[n-1]$$(b) At $\omega=0$: $H=0.3660\cdot\frac{2}{1-0.2679}=\frac{0.7321}{0.7321}=1$ (0 dB). At $\omega=\pi$: numerator $1+e^{-j\pi}=0$, so $H=0$ ($-\infty$ dB). At $\omega_c=\pi/3$: by construction of the $\alpha$ formula, $|H|=1/\sqrt2$ ($-3$ dB); direct substitution $z=e^{j\pi/3}$ confirms $|H|=0.7071$.
(c) $H_{HP}(z)=\dfrac{1+\alpha}{2}\cdot\dfrac{1-z^{-1}}{1-\alpha z^{-1}}=0.6340\,\dfrac{1-z^{-1}}{1-0.2679\,z^{-1}}$. The pair is delay-complementary, $H_{LP}(z)+H_{HP}(z)=1$, and power-complementary, $|H_{LP}(e^{j\omega})|^2+|H_{HP}(e^{j\omega})|^2=1$ — so the highpass output can be computed as $x[n]-y_{LP}[n]$.
Problem: An ideal lowpass filter has $H(e^{j\omega})=1$ for $|\omega|\le0.3\pi$ and $0$ for $0.3\pi<|\omega|\le\pi$. (a) Derive its impulse response $h[n]$ and evaluate $h[0]$, $h[1]$, $h[5]$. (b) Give three independent reasons why this filter cannot be implemented. (c) If we tolerate a delay, can a sufficiently long causal FIR filter implement it exactly? Explain.
(a) Inverse DTFT over the passband only:
$$h[n]=\frac1{2\pi}\int_{-0.3\pi}^{0.3\pi}e^{j\omega n}\,d\omega =\frac{e^{j0.3\pi n}-e^{-j0.3\pi n}}{2\pi jn} =\frac{\sin(0.3\pi n)}{\pi n}$$Values: $h[0]=\dfrac{\omega_c}{\pi}=0.3$; $h[1]=\dfrac{\sin 0.3\pi}{\pi}=\dfrac{0.8090}{3.1416}=0.2575$; $h[5]=\dfrac{\sin 1.5\pi}{5\pi}=\dfrac{-1}{15.708}=-0.0637$.
(b) (1) Noncausal: $h[-1]=h[1]\ne0$ — it needs future inputs. (2) Infinite duration: $h[n]\ne0$ for arbitrarily large $|n|$, so no finite implementation exists. (3) Unstable in the BIBO sense: $|h[n]|\sim\frac1{\pi|n|}$ and the harmonic series diverges, so $\sum_n|h[n]|=\infty$.
(c) No. Delaying by $D$ and truncating to $0\le n\le N-1$ gives a causal FIR approximation $h[n-D]w[n]$, but truncation in time means convolving the brick wall with a sinc-like kernel in frequency: ripples (Gibbs phenomenon, ≈9% overshoot for rectangular truncation) and a nonzero transition band appear for every finite $N$. Exact realization would require all infinitely many taps. This is precisely why practical specs use the tolerance scheme of Section 11.5.3.
Problem: A lowpass filter must satisfy the tolerance scheme with $\omega_p=0.35\pi$, $\omega_s=0.5\pi$, $\delta_p=0.05$, $\delta_s=0.01$. (a) Compute the peak passband ripple $R_p$ and the minimum stopband attenuation $A_s$ in dB. (b) A different spec sheet demands $A_s = 60$ dB and $R_p = 0.2$ dB. Convert these back to $\delta_s$ and $\delta_p$. (c) The transition bandwidth is $\Delta\omega=\omega_s-\omega_p$. Qualitatively, what happens to the required filter order if $\Delta\omega$ is halved with tolerances unchanged?
(a)
$$R_p=-20\log_{10}(1-\delta_p)=-20\log_{10}0.95=0.446\ \text{dB} \qquad A_s=-20\log_{10}\delta_s=-20\log_{10}0.01=40\ \text{dB}$$(b) Invert the formulas:
$$\delta_s=10^{-A_s/20}=10^{-3}=0.001 \qquad \delta_p=1-10^{-R_p/20}=1-10^{-0.01}=1-0.97724=0.0228$$(c) The order grows roughly in inverse proportion to the transition width (for FIR designs, $N\propto A_s/\Delta\omega$ by standard estimates such as the Kaiser formula). Halving $\Delta\omega$ approximately doubles the required order — sharpness is the most expensive item in a filter spec.
Problem: A slow sinusoid $s[n]=2\sin(2\pi\cdot0.02\,n)$, $n=0,\dots,199$, is corrupted by white Gaussian noise of standard deviation $0.8$. In MATLAB, (a) filter the noisy signal with an $M=11$ point moving average and plot the input and output; (b) plot the filter's magnitude response and explain why $M=11$ works well here; (c) state the delay introduced and how to compensate it.
(a) MATLAB code:
N = 200; n = (0:N-1).';
s = 2*sin(2*pi*0.02*n); % clean signal (f0 = 0.02 cycles/sample)
x = s + 0.8*randn(N,1); % noisy observation
M = 11;
b = ones(1,M)/M; % moving-average coefficients
y = filter(b, 1, x); % causal filtering, delay (M-1)/2 = 5
y_aligned = [y((M+1)/2:end); zeros((M-1)/2,1)]; % advance by 5 samples
subplot(2,1,1); plot(n, x, 'Color', [.5 .5 .5]); hold on;
plot(n, s, 'b', 'LineWidth', 1.5); legend('noisy x[n]','clean s[n]');
subplot(2,1,2); plot(n, y_aligned, 'r', 'LineWidth', 1.5); hold on;
plot(n, s, 'b--'); legend('filtered (aligned)','clean s[n]');
figure; % (b) magnitude response
[H, w] = freqz(b, 1, 1024);
plot(w/pi, abs(H)); grid on;
xlabel('\omega/\pi'); ylabel('|H(e^{j\omega})|');
Regenerated result (numpy/matplotlib, seed 11):
(b) The signal lives at $\omega_0=2\pi\cdot0.02=0.04\pi$, deep inside the main lobe of the $M=11$ response (first null at $2\pi/11\approx0.18\pi$): its gain is $\bigl|\frac{\sin(11\omega_0/2)}{11\sin(\omega_0/2)}\bigr|\approx0.978$, i.e. a loss of only about 0.2 dB. The noise, by contrast, is spread uniformly over all $\omega$, and averaging $11$ independent samples cuts the noise power by a factor of $11$ ($\approx10.4$ dB). Hence: signal kept, noise reduced.
(c) The filter is a Type-1 linear-phase FIR with group delay $(M-1)/2=5$ samples at every frequency, so the output is simply shifted by 5. Compensate by advancing the output 5 samples (as in the code), by plotting against $n-5$, or — offline — by using filtfilt, which yields zero phase and squares the magnitude response.
Problem: A causal FIR filter has impulse response $h[n]=\{1,\,-2,\,0,\,2,\,-1\}$ for $n=0,\dots,4$. (a) Determine whether it has linear phase, and if so, which type and what group delay. (b) Compute $H(e^{j0})$ and $H(e^{j\pi})$ and confirm they agree with the structural zeros of that type. (c) Could this filter be used as a lowpass filter? A highpass? What applications suit it?
(a) Check $h[n]$ against $h[4-n]$: $h[0]=1$ vs $h[4]=-1$; $h[1]=-2$ vs $h[3]=2$; $h[2]=0$. So $h[n]=-h[4-n]$: antisymmetric with odd length $N=5$ — a Type 3 linear-phase filter (note the center sample $h[2]=0$, as Type 3 requires). Group delay $\tau=(N-1)/2=2$ samples, plus the constant $90^\circ$ phase factor $j$ of antisymmetric filters (generalized linear phase).
(b) $H(e^{j0})=\sum_n h[n]=1-2+0+2-1=0$. $H(e^{j\pi})=\sum_n(-1)^n h[n]=h[0]-h[1]+h[2]-h[3]+h[4]=1+2+0-2-1=0$. Both are zero — exactly the forced zeros at $z=\pm1$ that define Type 3.
(c) With zero gain at both $\omega=0$ and $\omega=\pi$ it can be neither a lowpass nor a highpass (nor a bandstop) — passbands at the band edges are impossible. It is shaped for bandpass-like behavior; Type 3 structures are the natural home of differentiators and Hilbert transformers(微分器與希爾伯特轉換器), both of which require $A(0)=0$.