Outline
7.1 Rate Conversion in Discrete Time
7.2 Downsampling by M
7.3 Upsampling by L
7.4 Rational Rate Change L/M
7.5 MATLAB Practice
7.6 Summary
Exercises
DSP Study Guide · Chapter 7

Changing the Sampling Rate

Downsampling, upsampling, decimation, interpolation, and rational-factor resampling — done entirely with discrete-time processing

7.1 Rate Conversion by Discrete-Time Processing

Suppose a continuous-time signal $x_c(t)$ was sampled with period $T$ to give $x[n] = x_c(nT)$, but we now need the same underlying signal sampled at a different period $T_d$:

Goal of sampling-rate conversion(取樣率轉換) $$x[n] = x_c(nT) \;\longrightarrow\; x_d[n] = x_c(nT_d)$$

This is needed constantly in practice: audio moves between 48 kHz (studio) and 44.1 kHz (CD), biomedical recorders sample at 2000 Hz but analysis may only need 250 Hz, and a modem may need fractional rate offsets corrected. The question of this chapter: can we obtain $x_d[n]$ from $x[n]$ using only discrete-time operations?

7.1.1 The analog detour vs the DSP path

One conceptual approach is to go back to the analog world:

  1. Reconstruct $x_c(t)$ from $x[n]$ with a D/A converter and reconstruction filter;
  2. Resample $x_c(t)$ with an A/D converter at the new period $T_d$.
Why the analog detour is undesirable
The reconstruction filter, D/A and A/D converters are all non-ideal: the analog lowpass has finite roll-off and passband ripple, and each conversion adds noise and distortion. Every trip through the analog domain degrades the signal. 經過 D/A 與 A/D 的類比路徑會引入非理想濾波、雜訊與失真,因此實務上盡量避免。
The discrete-time alternative
All rate changes can instead be built from two purely discrete-time primitives — the compressor(壓縮器 / 降取樣) $\downarrow M$ and the expander(擴張器 / 升取樣) $\uparrow L$ — combined with digital lowpass filters. The whole system then behaves like an ideal LTI resampler with no analog imperfection. This chapter develops both primitives, their spectra, and the rational-rate combination $L/M$.

7.2 Downsampling: Rate Reduction by an Integer Factor M

7.2.1 The compressor in the time domain

The sampling-rate compressor(取樣率壓縮器), written $\downarrow M$, keeps every $M$-th sample and discards the rest:

Compressor / downsampling(降取樣) $$x_d[n] = x[nM] = x_c(nMT), \qquad M \in \mathbb{Z}^{+}$$

The output is exactly what we would have obtained by sampling $x_c(t)$ directly with the longer period $T' = MT$ — the sampling rate drops by $M$, the sample period grows by $M$. A decimator adds a protective lowpass filter in front of the compressor (next subsection):

x[n] period T anti-alias lowpass gain 1, cutoff π/M x̃[n] ↓M xd[n] = x̃[nM] period T′ = MT
Fig. 7-1 — The decimator: anti-alias lowpass filter followed by the compressor ↓M(抗混疊低通濾波器 + 壓縮器). The compressor alone is plain downsampling.
2026-06-12T22:24:30.636461 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0 5 10 15 20 25 30 n −1 0 1 x[n] x[n] (red samples kept by the compressor, M = 3) 0 2 4 6 8 10 n −1 0 1 x_d[n] x_d[n] = x[nM] = x[3n] (new period T' = 3T)
Fig. 7-2 — Time-domain view of downsampling with $M=3$: the compressor keeps the red samples $x[0], x[3], x[6], \dots$ and re-indexes them as $x_d[n]$(保留每第 3 個樣本並重新編號).

7.2.2 Spectrum of the downsampled signal

To see what downsampling does in frequency, write $x_d[n]$ as a two-step operation: (1) multiply $x[n]$ by the periodic sampling train $c_M[n] = \frac{1}{M}\sum_{i=0}^{M-1} e^{j2\pi i n/M}$, which equals 1 when $n$ is a multiple of $M$ and 0 otherwise; (2) discard the zeros. Step (1) gives $\tilde{x}[n] = x[n]\,c_M[n]$, whose DTFT is an average of $M$ shifted copies of $X(e^{j\omega})$; step (2) rescales the frequency axis by $M$. The result:

DTFT after downsampling(降取樣後的頻譜) $$X_d(e^{j\omega}) \;=\; \frac{1}{M}\sum_{i=0}^{M-1} X\!\left(e^{j(\omega - 2\pi i)/M}\right)$$

Equivalently, in terms of the original continuous-time spectrum $X_c(j\Omega)$, the downsampled sequence looks exactly like sampling with period $MT$:

In terms of $X_c$ — sampling with period $T_d = MT$ $$X_d(e^{j\omega}) \;=\; \frac{1}{MT}\sum_{r=-\infty}^{\infty} X_c\!\left(j\Big(\frac{\omega}{MT} - \frac{2\pi r}{MT}\Big)\right)$$
Read the formula
$X_d(e^{j\omega})$ is built from $M$ copies of $X(e^{j\omega})$, each frequency-scaled (stretched) by $M$(頻率軸被拉寬 $M$ 倍), shifted by integer multiples of $2\pi$, and scaled in amplitude by $1/M$. A band edge that sat at $\omega_N$ before downsampling moves out to $M\omega_N$ after.
直觀解釋(點擊展開)
丟掉樣本之後,剩下的樣本「間隔變大」,等效取樣率變低。在正規化頻率 $\omega = \Omega T$ 中, 同一個實體頻率 $\Omega$ 乘上更大的 $T' = MT$,所以頻譜在 $\omega$ 軸上被「拉寬」$M$ 倍。 原本擠在 $|\omega| \le \omega_N$ 的頻譜,現在佔到 $|\omega| \le M\omega_N$。 只要拉寬後不超過 $\pi$(即 $\omega_N \le \pi/M$),各週期複本不重疊,沒有混疊; 一旦超過 $\pi$,相鄰複本互相重疊相加,混疊就發生了,而且疊上去之後再也分不開。 振幅多了 $1/M$ 是因為平均了 $M$ 個複本(能量守恆的自然結果)。
2026-06-12T22:24:30.866104 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0.00 0.25 0.50 0.75 1.00 amplitude X(e^jw), wN = pi/3 0.0 0.1 0.2 0.3 0.4 amplitude X_d(e^jw), M = 3: copies just touch at w = pi (no aliasing, amplitude 1/3) −2 −1 0 1 2 w / pi 0.0 0.1 0.2 0.3 0.4 amplitude X_d(e^jw), M = 4: copies overlap -> aliasing (solid = sum)
Fig. 7-3 — Spectra in downsampling. Top: original $X(e^{j\omega})$ with band edge $\omega_N=\pi/3$. Middle: $M=3$ stretches the band edge exactly to $\pi$ — copies just touch, no aliasing(複本恰好相接,無混疊). Bottom: $M=4$ stretches past $\pi$; the dashed copies overlap and the solid sum is aliased(複本重疊 → 混疊).

7.2.3 Aliasing and the anti-alias prefilter — the decimator

From Fig. 7-3, downsampling is alias-free only when the original spectrum leaves room to be stretched:

No-aliasing condition for ↓M(避免混疊條件) $$X(e^{j\omega}) = 0 \quad \text{for } \tfrac{\pi}{M} \le |\omega| \le \pi \qquad\Longleftrightarrow\qquad \omega_N \le \frac{\pi}{M}$$

If $x[n]$ is not bandlimited to $\pi/M$, we must lowpass-filter first. The cascade of an ideal lowpass filter (gain 1, cutoff $\pi/M$) and the compressor $\downarrow M$ is called a decimator(十中抽一器/降頻器), and the whole operation decimation(抽取). The prefilter deliberately throws away the band $|\omega| > \pi/M$ so that the surviving band comes through clean.

直觀解釋:為什麼寧可先濾掉高頻?(點擊展開)
不先濾波:高頻成分會「反摺」到低頻,跟原本的低頻成分混在一起,整個低頻帶都被汙染, 而且事後無法分離。先濾波:高頻成分被乾淨地移除,低頻帶保持正確。 兩種做法都會失去高頻資訊(這是降取樣必然的代價),但前者連低頻都毀了,後者保住低頻。 所以 decimation = 「犧牲頻寬、保全留下來的頻帶」。
Aliasing is irreversible(混疊不可逆)
Once spectral copies overlap and add, no later processing can separate them — the folded high-frequency energy is permanently mixed into the low band. 重新取樣前務必確認高頻成分: 高頻會反摺(fold)到低頻與其他低頻成分混合。This is exactly the difference between MATLAB's downsample (no protection) and decimate (lowpass first) demonstrated below.
2026-06-12T22:24:31.087038 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0 25 50 75 100 125 150 175 200 0.0 0.5 1.0 |X| |X(f)| of x[n], fs = 1000 Hz (peaks at 20 Hz and 100 Hz) 0 10 20 30 40 50 60 0.0 0.5 1.0 |X| downsample(x, 8): fs' = 125 Hz -- 100 Hz aliases to 25 Hz 0 10 20 30 40 50 60 frequency (Hz) 0.0 0.5 1.0 |X| decimate(x, 8): lowpass prefilter removes 100 Hz before the compressor
Fig. 7-4 — Real demonstration: $x[n]=\sin(2\pi 20t)+\sin(2\pi 100t)$ sampled at 1000 Hz, rate reduced by $M=8$ to 125 Hz (Nyquist 62.5 Hz). Plain downsample folds the 100 Hz tone to a false 25 Hz tone; decimate removes it with the prefilter first(downsample 讓 100 Hz 假冒成 25 Hz;decimate 先低通把它濾掉).

7.3 Upsampling: Rate Increase by an Integer Factor L

7.3.1 The expander

To raise the sampling rate we want $x_i[n] = x_c(nT_i)$ with $T_i = T/L$ — i.e. we must create $L-1$ new samples between every pair of existing ones. The first stage is the sampling-rate expander(取樣率擴張器) $\uparrow L$, which inserts $L-1$ zeros between samples:

Expander / zero insertion(補零擴張) $$x_e[n] = \begin{cases} x[n/L], & n = 0, \pm L, \pm 2L, \dots \\ 0, & \text{otherwise} \end{cases} \;=\; \sum_{k=-\infty}^{\infty} x[k]\,\delta[n - kL]$$
x[n] period T ↑L xe[n] period T/L anti-imaging lowpass gain L, cutoff π/L xi[n] = xc(nT/L) period T/L
Fig. 7-5 — The interpolator: expander ↑L (zero insertion) followed by an anti-imaging lowpass filter with gain $L$ and cutoff $\pi/L$(擴張器 + 去鏡像低通濾波器).
2026-06-12T22:24:31.300709 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0 1 2 3 4 5 6 −0.5 0.0 0.5 1.0 x[n] x[n] (period T) 0.0 2.5 5.0 7.5 10.0 12.5 15.0 17.5 −0.5 0.0 0.5 1.0 x_e[n] x_e[n]: L-1 = 2 zeros inserted between samples (period T/3) 0.0 2.5 5.0 7.5 10.0 12.5 15.0 17.5 n 0 1 x_i[n] x_i[n]: after ideal lowpass (gain L, cutoff pi/L) -- sinc interpolation
Fig. 7-6 — Time domain of interpolation with $L=3$: original $x[n]$ (top); $x_e[n]$ after inserting two zeros between each sample (middle); $x_i[n]$ after the ideal lowpass — the zeros are "filled in" by sinc interpolation and the original samples are preserved at $n=kL$(補零 → 低通濾波把零填成內插值,原樣本不變).

7.3.2 Spectrum: compression and images

The DTFT of the zero-stuffed sequence follows directly from the definition:

DTFT after expansion(擴張後的頻譜) $$X_e(e^{j\omega}) \;=\; \sum_{k=-\infty}^{\infty} x[k]\,e^{-j\omega L k} \;=\; X\!\left(e^{j\omega L}\right)$$
Read the formula
The spectrum is frequency-compressed by $L$(頻譜被壓縮 $L$ 倍): the baseband copy now occupies $|\omega| \le \omega_N/L$, but the $2\pi$-periodicity of the original spectrum drags in $L-1$ extra copies — images(鏡像) — centered at $\omega = 2\pi i/L,\ i = 1,\dots,L-1$. No information is lost (zero insertion is invertible); the images are simply unwanted and must be filtered away.
2026-06-12T22:24:31.618690 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0.00 0.25 0.50 0.75 1.00 1.25 amplitude X(e^jw), wN = 0.7 pi 0.00 0.25 0.50 0.75 1.00 1.25 amplitude X_e(e^jw) = X(e^jwL), L = 2: compressed + image at w = pi (dashed: H_i, cutoff pi/2) −2 −1 0 1 2 w / pi 0.0 0.5 1.0 1.5 2.0 2.5 amplitude X_i(e^jw): image removed, gain L = 2 restores amplitude
Fig. 7-7 — Spectra in upsampling with $L=2$: original (top); after zero insertion the spectrum is compressed and an image appears at $\omega=\pi$ (middle, dashed = ideal anti-imaging filter, cutoff $\pi/2$); after filtering with gain $L=2$ the interpolated spectrum has the correct amplitude (bottom).
直觀解釋:imaging 跟 aliasing 哪裡不同?為什麼增益要 L?(點擊展開)
Aliasing(混疊)是複本「疊在一起、加起來」,資訊被破壞,不可逆。 Imaging(鏡像)是複本「彼此分開地」出現在新的頻帶裡,資訊沒被破壞, 只要低通濾波就能乾淨移除,完全可逆。 增益要 $L$ 的原因:補零後每 $L$ 個樣本只有 1 個非零,序列的「平均能量」掉了 $L$ 倍; 等效地說,內插輸出對應 $1/T_i = L/T$ 的取樣率,頻譜高度應為 $\frac{1}{T_i}X_c = \frac{L}{T}X_c$, 所以濾波器要把高度乘回 $L$ 才能讓 $x_i[n] = x_c(nT/L)$ 的振幅正確。

7.3.3 Ideal bandlimited interpolation

The ideal anti-imaging filter has gain $L$ and cutoff $\pi/L$:

Ideal interpolation filter(理想內插濾波器) $$H_i(e^{j\omega}) = \begin{cases} L, & |\omega| < \pi/L \\ 0, & \pi/L \le |\omega| \le \pi \end{cases} \qquad\Longleftrightarrow\qquad h_i[n] = \frac{\sin(\pi n / L)}{\pi n / L}$$

Convolving $x_e[n]$ with $h_i[n]$ gives the discrete-time version of Shannon sinc interpolation (the formula on slide 17):

Sinc interpolation of the zero-stuffed sequence $$x_i[n] \;=\; \sum_{k=-\infty}^{\infty} x[k]\;\frac{\sin\!\big(\pi(n-kL)/L\big)}{\pi(n-kL)/L}$$

Two properties follow immediately, because $h_i[0]=1$ and $h_i[kL]=0$ for $k \ne 0$:

7.3.4 Practical interpolators: zero-order hold and linear

The ideal sinc filter is infinitely long and non-causal, so practical systems use short approximations. The two classics:

Zero-order hold(零階保持)and linear interpolator(線性內插) $$h_{\mathrm{zoh}}[n] = \begin{cases} 1, & 0 \le n \le L-1 \\ 0, & \text{else} \end{cases} \qquad h_{\mathrm{lin}}[n] = \begin{cases} 1 - |n|/L, & |n| \le L \\ 0, & \text{else} \end{cases}$$

The ZOH simply repeats each sample $L$ times (a staircase); the linear interpolator draws straight lines between samples (note $h_{\mathrm{lin}} = \tfrac{1}{L}\, h_{\mathrm{zoh}} * h_{\mathrm{zoh}}$, a triangle). Their frequency responses,

Frequency responses of practical interpolators $$\big|H_{\mathrm{zoh}}(e^{j\omega})\big| = \left|\frac{\sin(\omega L/2)}{\sin(\omega/2)}\right|, \qquad H_{\mathrm{lin}}(e^{j\omega}) = \frac{1}{L}\left[\frac{\sin(\omega L/2)}{\sin(\omega/2)}\right]^{2}$$

only approximate the ideal brick wall: the passband droops slightly and the images around $2\pi i/L$ are attenuated but not eliminated (the linear interpolator at least places spectral zeros exactly at the image centers $\omega = 2\pi i /L$). They work well when the signal is heavily oversampled, i.e. its energy is concentrated at $|\omega| \ll \pi$.

2026-06-12T22:24:31.903379 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0 5 10 15 20 25 30 35 n (high-rate index) −1.0 −0.5 0.0 0.5 1.0 Interpolating by L = 5: ZOH vs linear vs ideal bandlimited ideal (sinc) zero-order hold linear original samples 0.0 0.2 0.4 0.6 0.8 1.0 w / pi 0 1 2 3 4 5 |H(e^jw)| Frequency responses, L = 5: imperfect image suppression above pi/L ideal LPF (gain L, cutoff pi/L) linear h_lin[n] ZOH h_zoh[n]
Fig. 7-8 — Practical interpolators for $L=5$. Top: ZOH staircase vs linear vs ideal sinc interpolation through the same samples. Bottom: their magnitude responses — both leak energy above $\pi/5$ where the ideal filter would remove all images(ZOH/線性內插只能部分抑制鏡像).

7.4 General Rate Change by a Rational Factor L/M

7.4.1 Cascade and combined filter

A noninteger rate change is achieved by interpolating by $L$ first, then decimating by $M$, giving a new sampling period

Rational rate change(有理數倍率轉換) $$T' = \frac{M}{L}\,T \qquad\text{i.e.}\qquad f_s' = \frac{L}{M}\, f_s$$

In the cascade, the interpolation filter (gain $L$, cutoff $\pi/L$) and the decimation filter (gain 1, cutoff $\pi/M$) operate back-to-back at the same (high) rate, so they collapse into one lowpass filter taking the tighter of the two cutoffs:

Combined filter(合併濾波器) $$H(e^{j\omega}) = \begin{cases} L, & |\omega| < \min\!\left(\dfrac{\pi}{L}, \dfrac{\pi}{M}\right) \\ 0, & \text{otherwise} \end{cases}$$
(a) interpolator followed by decimator x[n] ↑L interp. lowpass gain L, π/L decim. lowpass gain 1, π/M ↓M y[n] (b) the two filters merge into one lowpass at the high rate x[n] T ↑L T/L combined lowpass gain L, cutoff min(π/L, π/M) T/L ↓M y[n] TM/L
Fig. 7-9 — Changing the sampling rate by the noninteger factor $L/M$: (a) interpolator followed by decimator; (b) simplified system with the interpolation and decimation filters combined into one(內插與抽取濾波器合併為一個低通).
直觀解釋:為什麼一定要「先升後降」?(點擊展開)
$\uparrow L$ 完全不丟資訊,$\downarrow M$ 一定丟資訊(頻寬被砍到 $\pi/M$)。 若先 $\downarrow M$ 再 $\uparrow L$,會先把頻寬砍到 $\pi/M$,砍掉的東西再也回不來; 先 $\uparrow L$ 再 $\downarrow M$ 則只在「最後不得不」的時候才砍,保留的頻寬是 $\min(\pi/L, \pi/M)$ 換算回原始頻率軸後可能完整保留整個訊號 (例如 $L/M = 3/2$:先升 3 倍後,$\pi/2$ 的削減在原訊號座標是 $3\pi/2 > \pi$,毫無損失)。 另外 $L$ 與 $M$ 應互質(取 $\gcd$ 約分),否則白做工。
Polyphase implementation(多相實現)
Filtering at the high rate $LT$ seems wasteful: most input samples of the filter are the inserted zeros, and most output samples are immediately discarded by $\downarrow M$. A polyphase implementation reorganizes the FIR filter into $L$ short subfilters so that only the samples actually kept are ever computed. MATLAB's resample uses exactly this ("using a polyphase implementation", per its help text) — that is why it is fast.

7.4.2 Worked example: 3/2 rate change

Example 7.1 — From 2000 Hz to 3000 Hz ($L=3$, $M=2$)
Step 1 — factor the ratio. $f_s'/f_s = 3000/2000 = 3/2$, so $L=3$, $M=2$ (already coprime).
Step 2 — expand. $\uparrow 3$ raises the rate to 6000 Hz; the spectrum compresses by 3 and two images appear.
Step 3 — one lowpass. Combined filter: gain $3$, cutoff $\min(\pi/3, \pi/2) = \pi/3$. It removes the images (interpolation role); since $\pi/3 \le \pi/2$, it simultaneously guarantees the later $\downarrow 2$ cannot alias (decimation role).
Step 4 — compress. $\downarrow 2$ brings 6000 Hz down to 3000 Hz. Output: $y[n] = x_c(n \cdot \tfrac{2}{3}T)$, assuming $x_c$ was sampled above its Nyquist rate originally.
MATLAB: y = resample(x, 3, 2); — and remember to rebuild the time axis: ty = (0:length(y)-1)/3000;

7.5 MATLAB Practice: decimate, downsample, resample, rat

7.5.1 decimate vs downsample(有無抗混疊濾波的差別)

The lecture's running example: a signal sampled at 1000 Hz must be reduced to $1000/8 = 125$ Hz. Two one-line options exist, and they are not equivalent:

CallWhat it doesAliasing protection
down_signal = downsample(signal, 8);keeps every 8th sample (pure compressor $\downarrow 8$)None — high frequencies fold into the low band
deci_signal = decimate(signal, 8);lowpass-filters (8th-order Chebyshev Type I, cutoff $0.8\,(f_s/2)/8$, applied forward-backward), then keeps every 8th sampleYes — band above the new Nyquist is removed first
Slide takeaway(test_aliasing.m)
重新取樣要注意高頻成分的 aliasing!高頻成分會反摺到低頻、與其他低頻成分混合。 decimate 會在降低取樣頻率前先做低通濾波;downsample 不會。 If the signal has any energy above the new Nyquist frequency, use decimate (or resample), never bare downsample.

7.5.2 resample and rat(任意倍率重新取樣)

Y = resample(X, P, Q) resamples X at $P/Q$ times the original rate (polyphase implementation; length of Y is $\lceil P/Q \cdot \mathrm{length}(X)\rceil$). It applies an anti-aliasing lowpass FIR filter during the conversion and compensates the filter delay. [N, D] = rat(X, tol) returns integers with $|N/D - X| \le tol$ (continued-fraction truncation; default tolerance $10^{-6}\,\lVert X\rVert_1$) — the standard way to get $P$ and $Q$ from a ratio of sampling rates:

org_SR = 2000;                       % original rate (Hz)
new_SR3 = 300;                       % target rate (Hz)
[p, q] = rat(new_SR3/org_SR);        % p = 3, q = 20
new_signal3 = resample(org_signal, p, q);
new_taxis3 = (1:length(new_signal3))'/new_SR3;   % REBUILD the time axis!
Rebuild the time axis(別忘記重新取樣後要重新定義時間軸!!)
After resampling, the old time vector no longer matches the data length or spacing. Always recompute it from the new sampling rate, e.g. t_new = (1:length(y))'/new_SR; — forgetting this stretches or squeezes the waveform on every later plot and analysis.
Resampling function cheat-sheet(重新取樣常用函式)
FunctionPurpose(用途)
interp(x, L)內插訊號以提高取樣頻率 — interpolate, rate × L (anti-imaging filter built in)
decimate(x, M)低通濾波過後降低取樣頻率 — lowpass then rate ÷ M
downsample(x, M) / upsample(x, L)bare compressor / bare zero-insertion expander (no filters)
resample(x, P, Q)執行 P 倍的 interp 提高取樣頻率,再執行 Q 倍的 decimate 降低取樣頻率 — rational P/Q change, polyphase
rat(x, tol)找出最接近輸入數值的分數形式(P/Q)— rational approximation via continued fractions

7.6 Summary

Downsampling $\downarrow M$(降取樣)Upsampling $\uparrow L$(升取樣)
Sampling ratereduced by $M$increased by $L$
Sample periodincreases: $T' = MT$decreases: $T' = T/L$
With its lowpass filter, calleddecimation(抽取)interpolation(內插)
Spectrumstretched by $M$, amplitude $\times \tfrac1M$, $M$ copiescompressed by $L$, $L-1$ images appear
Hazardaliasing(混疊)— irreversibleimaging(鏡像)— removable
Filterbefore $\downarrow M$: gain 1, cutoff $\pi/M$after $\uparrow L$: gain $L$, cutoff $\pi/L$
Informationlost above $\pi/M$fully preserved
Downsampling spectrum
$X_d(e^{j\omega}) = \dfrac{1}{M}\displaystyle\sum_{i=0}^{M-1} X\big(e^{j(\omega-2\pi i)/M}\big)$ — stretch by $M$, average $M$ copies. Safe iff $\omega_N \le \pi/M$.
Upsampling spectrum
$X_e(e^{j\omega}) = X(e^{j\omega L})$ — compress by $L$; remove images with lowpass of gain $L$, cutoff $\pi/L$; then $x_i[n]=x_c(nT/L)$ exactly.
Rational rate change
$\uparrow L \to$ lowpass (gain $L$, cutoff $\min(\pi/L,\pi/M)$) $\to\ \downarrow M$ gives $T' = MT/L$. Interpolate first so information is only lost when unavoidable.
Practical interpolators
ZOH repeats samples; linear connects them ($h_{\mathrm{lin}} = $ triangle, $H_{\mathrm{lin}} = \frac{1}{L}\big[\frac{\sin(\omega L/2)}{\sin(\omega/2)}\big]^2$). Both leave residual images — fine only when oversampled.
MATLAB
decimate = filter + $\downarrow M$; downsample = bare $\downarrow M$ (danger); interp = $\uparrow L$ + filter; resample(x,P,Q) = polyphase $P/Q$; get $P,Q$ with rat.
Golden rules
(1) Filter before you compress, after you expand. (2) Aliasing is forever; images wash off. (3) Rebuild the time axis after every rate change.

Exercises

All five problems of ICE503 DSP Homework #7, restated with full worked solutions.

Exercise 1 — Multirate system analysis (C/D → ↑L → H → ↓M → D/C)

Problem: Consider the system below. Assume $R_c(j\Omega)$ is bandlimited, $R_c(j\Omega) = 0$ for $|\Omega| \ge 2\pi(1000)$, with the triangular shape of Fig. 2 of the homework (peak $R_c(0)$ at $\Omega = 0$, decreasing linearly to zero at $|\Omega| = 2\pi(1000)$).

rc(t) C/D T₁ r[n] ↑L x[n] H(e^jω) LPF, cutoff ω₀ v[n] ↓M y[n] D/C T₃ sc(t)
The Exercise 1 system: C/D at period $T_1$, expander ↑L, ideal lowpass $H(e^{j\omega})$ (gain 1, cutoff $\omega_0$), compressor ↓M, D/C at period $T_3$.

(a) For $L = 3$, $T_1 = \tfrac{1}{2000}$ s, and $H(e^{j\omega}) = 1$ for $|\omega|\le\omega_0$, $0$ otherwise — sketch $R(e^{j\omega})$ and $X(e^{j\omega})$.
(b) For $M = 2$, choose nonzero $\omega_0$ and $T_2$ such that $y[n] = \alpha\, r_c(nT_2)$ for some nonzero constant $\alpha$.
(c) Using the $\omega_0$ from (b), determine $T_3$ such that $s_c(t) = \beta\, r_c(t)$ for some nonzero $\beta$.
(d) Now let $M = 3$ and $T_3 = \tfrac{1}{2000}$. If the output must be $s_c(t) = r_c\!\big(t - \tfrac{1}{1000}\big)$, is it possible to obtain $s[n] \,(= y[n])$ from $r[n]$? If so, what should $H(e^{j\omega})$ be?

Click to reveal solution

(a) Sketch $R(e^{j\omega})$ and $X(e^{j\omega})$

Sampling at $f_1 = 1/T_1 = 2000$ Hz a signal whose highest frequency is 1000 Hz puts the band edge exactly at $\Omega_N T_1 = 2\pi(1000)/2000 = \pi$ — critical sampling, no aliasing, the spectrum fills the whole $[-\pi,\pi]$ interval:

$$R(e^{j\omega}) = \frac{1}{T_1}\sum_{k} R_c\!\left(j\Big(\frac{\omega - 2\pi k}{T_1}\Big)\right) \;\Rightarrow\; \text{triangles of peak } \frac{R_c(0)}{T_1} = 2000\,R_c(0) \text{ centered at } \omega = 2\pi k,$$

each triangle reaching zero at odd multiples of $\pi$. The expander output is $X(e^{j\omega}) = R(e^{j3\omega})$: the same shape compressed by $L=3$, now periodic with period $2\pi/3$ — a baseband triangle on $|\omega| \le \pi/3$ plus images centered at $\pm 2\pi/3$ (and $2\pi$-translates). Both are sketched below (taking $R_c(0)=1$).

2026-06-12T22:24:32.255906 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0 500 1000 1500 2000 amplitude R(e^jw) = (1/T1) Rc(jw/T1): peak 2000, fills |w| <= pi 0 500 1000 1500 2000 amplitude X(e^jw) = R(e^j3w): period 2pi/3; dashed: H with w0 = pi/3 −2 −1 0 1 2 w / pi 0 250 500 750 1000 amplitude Y(e^jw) after compression by M = 2: baseband to 2pi/3 (y[n] = a rc(nT2))
Fig. 7-10 — Exercise 1(a)/(b): $R(e^{j\omega})$ (top); $X(e^{j\omega}) = R(e^{j3\omega})$ with the part (b) filter $\omega_0 = \pi/3$ dashed (middle); spectrum of $y[n]$ after $\downarrow 2$ (bottom) — a clean stretched triangle, i.e. samples of $r_c$ at rate 3000 Hz.

(b) Choose $\omega_0$ and $T_2$ for $y[n] = \alpha\, r_c(nT_2)$ — with $M=2$

Step 1 — kill the images. After $\uparrow 3$ the desired baseband occupies $|\omega| \le \pi/3$ and the images begin right at $\pi/3$. The only cutoff that keeps all of the baseband and none of the images is $$\boxed{\omega_0 = \frac{\pi}{3}}$$
Step 2 — check the compressor. $v[n]$ is bandlimited to $\pi/3$; downsampling by $M=2$ stretches this to $2\pi/3 < \pi$ — no aliasing. ✓
Step 3 — effective sampling period. The chain C/D($T_1$) → $\uparrow 3$ → ideal LPF → $\downarrow 2$ is equivalent to sampling $r_c(t)$ with period $$T_2 = \frac{M}{L}T_1 = \frac{2}{3}\cdot\frac{1}{2000} = \boxed{\frac{1}{3000}\ \text{s}}\qquad (f = 3000\ \text{Hz}).$$ So $y[n] = \alpha\, r_c(n/3000)$. (For the record, since $H$ has gain 1 instead of $L=3$, and $\downarrow 2$ contributes $\tfrac12$, one finds $\alpha = 1/3$ — not required.)

(c) Choose $T_3$ for $s_c(t) = \beta\, r_c(t)$

$y[n]$ holds samples of $r_c$ taken every $T_2 = 1/3000$ s, and $y[n]$ is alias-free. The ideal D/C converter reconstructs the original continuous-time signal precisely when its period matches the actual sample spacing:

$$\boxed{T_3 = T_2 = \frac{1}{3000}\ \text{s}} \;\Rightarrow\; s_c(t) = \beta\, r_c(t).$$

(Any other $T_3$ would produce a time-scaled $r_c(t\,T_2/T_3)$, not $\beta r_c(t)$.)

(d) $M = 3$, $T_3 = \tfrac{1}{2000}$, target $s_c(t) = r_c\big(t - \tfrac{1}{1000}\big)$

Step 1 — what must $y[n]$ be? With $T_3 = 1/2000$ and D/C ideal, $$y[n] = s_c(nT_3) = r_c\!\Big(nT_3 - \tfrac{1}{1000}\Big) = r_c\big((n-2)T_3\big) = r[n-2],$$ because $\tfrac{1}{1000} = 2T_3$ and $r[n] = r_c(nT_1)$ with $T_1 = T_3$. So we need a pure 2-sample delay of $r[n]$.
Step 2 — can ↑3 → H → ↓3 produce a delay of 2? Yes. With $L = M = 3$ the rate is unchanged. A delay of 2 at the output rate equals a delay of $2L = 6$ at the high (expanded) rate, and 6 is an integer — so it is realizable by an LTI filter at the high rate.
Step 3 — specify $H$. Take the ideal interpolation filter (gain $L = 3$, cutoff $\pi/3$, which both removes the expander images and prevents aliasing in $\downarrow 3$) and add the 6-sample delay: $$\boxed{H(e^{j\omega}) = \begin{cases} 3\,e^{-j6\omega}, & |\omega| < \pi/3 \\ 0, & \pi/3 \le |\omega| \le \pi \end{cases}}$$ Then $\uparrow 3 \to H \to \downarrow 3$ gives exactly $y[n] = r[n-2]$, hence $s_c(t) = r_c(t - \tfrac{1}{1000})$. Yes, it is possible.

Note the delay had to be an integer multiple of $M = 3$ at the high rate (here $6 = 2\cdot 3$); a half-sample-type shift such as $r_c(t - \tfrac{1}{4000})$ would still be realizable with a fractional-delay allpass term $e^{-j\omega d}$, $d$ non-integer — the multirate chain is more flexible than a plain delay line.

Exercise 2 — DFTs of three finite-length sequences

Problem: Compute the $N$-point DFT ($N$ even) of:

(a) $x[n] = \begin{cases}1, & n \text{ even},\ 0 \le n \le N-1\\ 0, & n \text{ odd},\ 0 \le n \le N-1\end{cases}$   (b) $x[n] = \begin{cases}1, & 0 \le n \le N/2 - 1\\ 0, & N/2 \le n \le N-1\end{cases}$   (c) $x[n] = \begin{cases}\alpha^{n}, & 0 \le n \le N-1\\ 0, & \text{otherwise}\end{cases}$

Click to reveal solution

Throughout, $W_N = e^{-j2\pi/N}$ and $X[k] = \sum_{n=0}^{N-1} x[n] W_N^{kn}$, $k = 0,\dots,N-1$.

(a) Ones on the even indices

Substitute $n = 2r$, $r = 0,\dots,N/2-1$: $$X[k] = \sum_{r=0}^{N/2-1} e^{-j\frac{2\pi}{N}k(2r)} = \sum_{r=0}^{N/2-1} \Big(e^{-j\frac{2\pi k}{N/2}}\Big)^{r}.$$
This is a geometric sum of an $(N/2)$-th root of unity: it equals $N/2$ when $e^{-j2\pi k/(N/2)} = 1$, i.e. when $k$ is a multiple of $N/2$, and 0 otherwise. Within $0 \le k \le N-1$: $$\boxed{X[k] = \frac{N}{2}\,\big(\delta[k] + \delta[k - N/2]\big)}$$
Check: $x[n] = \tfrac12\big(1 + (-1)^n\big) = \tfrac12 + \tfrac12 e^{j\pi n}$ — a DC term (bin $k=0$) plus the frequency $\pi$ (bin $k = N/2$), each contributing $N/2$. ✓

(b) Ones on the first half

Geometric sum with $M = N/2$ terms: $$X[k] = \sum_{n=0}^{N/2-1} e^{-j\frac{2\pi k}{N}n} = \frac{1 - e^{-j\pi k}}{1 - e^{-j2\pi k/N}} = \frac{1 - (-1)^k}{1 - e^{-j2\pi k/N}} \quad (k \ne 0), \qquad X[0] = \frac{N}{2}.$$
So $X[k] = 0$ for even $k \ne 0$, and $X[k] = \dfrac{2}{1 - e^{-j2\pi k/N}}$ for odd $k$. Factoring out the half-angle phase gives the closed form $$\boxed{X[k] = e^{-j\frac{\pi k}{N}\left(\frac{N}{2}-1\right)}\;\frac{\sin(\pi k/2)}{\sin(\pi k/N)}, \qquad X[0] = \frac{N}{2}}$$ (a periodic-sinc / Dirichlet kernel: magnitude $1/\sin(\pi k/N)$ on odd bins, zero on nonzero even bins).

(c) Finite geometric (exponential) sequence

Direct geometric sum with ratio $\alpha e^{-j2\pi k/N}$: $$X[k] = \sum_{n=0}^{N-1}\big(\alpha\, e^{-j2\pi k/N}\big)^{n} = \frac{1 - \alpha^{N} e^{-j2\pi k}}{1 - \alpha\, e^{-j2\pi k/N}} \;\Rightarrow\; \boxed{X[k] = \frac{1 - \alpha^{N}}{1 - \alpha\, e^{-j2\pi k/N}}}$$ using $e^{-j2\pi k} = 1$. Valid for $\alpha \ne e^{j2\pi k/N}$; if $\alpha = 1$ the sum degenerates to $X[k] = N\delta[k]$, which is also the limit of the boxed expression. Note the numerator $1-\alpha^N$ is the same for every bin — only the denominator (the distance from $\alpha$ to the $k$-th root of unity) shapes the spectrum.
Exercise 3 — MATLAB: decimate vs downsample on a two-tone signal

Problem: Generate $x(t) = \sin(2\pi\,20t) + \sin(2\pi\,100t)$ and sample it at 1000 Hz to obtain $x[n]$. (a) Reduce the rate by 5 using both decimate and downsample; plot each result against $x[n]$. (b) Repeat with factor 8.

Click to reveal solution
fs = 1000;  t = 0:1/fs:2-1/fs;
x  = sin(2*pi*20*t) + sin(2*pi*100*t);

for M = [5 8]                          % (a) M = 5, (b) M = 8
    x_down = downsample(x, M);         % bare compressor, no filter
    x_deci = decimate(x, M);           % Chebyshev LPF, then compressor
    t_M    = downsample(t, M);

    figure;
    subplot(2,1,1);
    plot(t, x, 'Color', [.75 .75 .75]); hold on;
    stem(t_M, x_down, '.'); xlim([0 0.25]);
    title(sprintf('downsample, M = %d  (fs'' = %g Hz)', M, fs/M));
    subplot(2,1,2);
    plot(t, x, 'Color', [.75 .75 .75]); hold on;
    stem(t_M, x_deci, '.'); xlim([0 0.25]);
    title(sprintf('decimate, M = %d', M));
    xlabel('time (s)');
end
2026-06-12T22:24:32.674065 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0.00 0.05 0.10 0.15 0.20 0.25 −2 −1 0 1 2 downsample by 5 (fs' = 200 Hz) x[n] (1000 Hz) downsample, M=5 0.00 0.05 0.10 0.15 0.20 0.25 −2 −1 0 1 2 downsample by 8 (fs' = 125 Hz) x[n] (1000 Hz) downsample, M=8 0.00 0.05 0.10 0.15 0.20 0.25 time (s) −2 −1 0 1 2 decimate by 5 (prefilter + compress) x[n] (1000 Hz) decimate, M=5 0.00 0.05 0.10 0.15 0.20 0.25 time (s) −2 −1 0 1 2 decimate by 8 (prefilter + compress) x[n] (1000 Hz) decimate, M=8
Fig. 7-11 — Exercise 3 results (regenerated numerically; decimate reproduced as 8th-order Chebyshev-I zero-phase filtering + compression). Left column $M=5$, right column $M=8$; top row downsample, bottom row decimate. Gray: original 1000 Hz signal.

What you should observe(觀察與解釋)

(a) $M = 5$ → $f_s' = 200$ Hz, Nyquist 100 Hz. The 100 Hz tone sits exactly at the new Nyquist frequency. Its retained samples are $\sin(2\pi 100 \cdot 5n/1000) = \sin(\pi n) = 0$ — downsample happens to hit every zero crossing, so the 100 Hz tone silently vanishes and only the 20 Hz tone remains. decimate gives nearly the same 20 Hz result because its lowpass cutoff ($0.8 \times 100 = 80$ Hz) also removes the 100 Hz tone. The two outputs look similar here, but for downsample it is pure luck of sampling phase — a cosine at 100 Hz would have aliased to a frozen $\pm 1$ pattern instead.
(b) $M = 8$ → $f_s' = 125$ Hz, Nyquist 62.5 Hz. Now 100 Hz is above the new Nyquist: with downsample it folds to $125 - 100 = 25$ Hz, and the output is $\sin(2\pi 20t) + $ a false 25 Hz tone — visible as a slow 5 Hz beating envelope against the true 20 Hz component (and clearly a different waveform from the gray original). With decimate the prefilter removes the 100 Hz tone before compression, leaving a clean 20 Hz sinusoid. This is precisely the spectral picture of Fig. 7-4.
Conclusion: 兩者的差別 = 有無抗混疊低通濾波。當訊號在新 Nyquist 頻率以上還有能量時,必須用 decimate(或 resample)。
Exercise 4 — MATLAB: resample + rat on an EMG signal

Problem: Load EMG_ICA.mat (from demodata_L4.zip, course site) and take channel 1: org_signal = fdata(:,1);, already sampled at 2000 Hz. (a) Use rat and resample to upsample to 5000 Hz, then downsample back to 2000 Hz; plot against the original. What happened? (b) Downsample to 300 Hz, then upsample back to 2000 Hz; plot against the original. What happened?

Click to reveal solution
load('EMG_ICA.mat');
org_signal = fdata(:,1);               % channel 1, fs = 2000 Hz
org_SR = 2000;
t_org  = (1:length(org_signal))'/org_SR;

% ---------- (a) 2000 -> 5000 -> 2000 ----------
[p1, q1] = rat(5000/org_SR);           % p1 = 5,  q1 = 2
up_sig   = resample(org_signal, p1, q1);     % now 5000 Hz
[p2, q2] = rat(org_SR/5000);           % p2 = 2,  q2 = 5
back_a   = resample(up_sig, p2, q2);         % back to 2000 Hz
t_a = (1:length(back_a))'/org_SR;

figure; plot(t_org, org_signal); hold on; plot(t_a, back_a);
legend('original 2000 Hz', 'up 5000 \rightarrow down 2000');

% ---------- (b) 2000 -> 300 -> 2000 ----------
[p3, q3] = rat(300/org_SR);            % p3 = 3,  q3 = 20
low_sig  = resample(org_signal, p3, q3);     % now 300 Hz
[p4, q4] = rat(org_SR/300);            % p4 = 20, q4 = 3
back_b   = resample(low_sig, p4, q4);        % back to 2000 Hz
t_b = (1:length(back_b))'/org_SR;

figure; plot(t_org, org_signal); hold on; plot(t_b, back_b);
legend('original 2000 Hz', 'down 300 \rightarrow up 2000');
2026-06-12T22:24:33.029422 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0.250 0.275 0.300 0.325 0.350 0.375 0.400 0.425 0.450 −1 0 1 2 (a) upsample then downsample: waveform essentially recovered original (2000 Hz) up 5000 -> down 2000 0.250 0.275 0.300 0.325 0.350 0.375 0.400 0.425 0.450 time (s) −1 0 1 2 (b) downsample then upsample: content above 150 Hz is gone -- smoothed original (2000 Hz) down 300 -> up 2000
Fig. 7-12 — Exercise 4 behavior reproduced on a simulated surface-EMG-like burst signal (bandpass 20–450 Hz noise with burst envelopes, $f_s = 2000$ Hz), since the course .mat file is not redistributable. The phenomena are identical to what you will see with EMG_ICA.mat.

(a) Up to 5000 Hz, back down to 2000 Hz — what happened?

Essentially nothing: the curves overlay almost perfectly. Upsampling $2000 \to 5000$ ($P/Q = 5/2$) adds no information and loses none; coming back $5000 \to 2000$ ($P/Q = 2/5$) needs an anti-alias cutoff at 1000 Hz — but the signal never had content above its original Nyquist of 1000 Hz anyway. The only (tiny) differences are the transition-band edge effects of the two FIR anti-aliasing filters and small end transients. 升頻不丟資訊,降回原頻率時截止頻率剛好是原本的 Nyquist,因此訊號幾乎完整復原。

(b) Down to 300 Hz, back up to 2000 Hz — what happened?

The reconstruction is a smoothed, low-frequency caricature of the original. Going $2000 \to 300$ Hz forces an anti-aliasing lowpass at the new Nyquist, 150 Hz; the EMG's substantial energy from 150–450 Hz is permanently deleted. Upsampling back to 2000 Hz interpolates beautifully between the surviving samples but cannot re-create the destroyed band — the bursts lose their fast spikes and look rounded/blurred, with reduced amplitude. 降到 300 Hz 時 150 Hz 以上的成分被抗混疊濾波器刪除,再升回 2000 Hz 也救不回來:升取樣只能內插,不能無中生有。
Moral: down-then-up is lossy whenever the intermediate Nyquist is below the signal bandwidth; up-then-down (with the same start/end rates) is essentially lossless. Order of operations matters — this is the practical face of Section 7.4.
Exercise 5 — MATLAB: DFT/IDFT with the DFT matrix vs fft/ifft

Problem: Generate one second of $x(t) = \cos(2\pi 5 t)$ and sample at 100 Hz to get $x[n]$ ($N = 100$). (a) Compute the DFT $X[k]$ with an explicit DFT matrix. (b) Compute the IDFT of $X[k]$ with the DFT matrix to recover $x[n]$. (c) Compute $X[k]$ with fft. (d) Recover $x[n]$ with ifft. (e) stem-plot $|X[k]|$ and $x[n]$ for (a)–(d).

Click to reveal solution

The DFT matrix is $\mathbf{W}$ with entries $W_{kn} = e^{-j2\pi kn/N}$; then $\mathbf{X} = \mathbf{W}\mathbf{x}$ and the inverse is $\mathbf{x} = \tfrac{1}{N}\mathbf{W}^{H}\mathbf{X}$ (because $\mathbf{W}^{H}\mathbf{W} = N\mathbf{I}$ — $\mathbf{W}/\sqrt{N}$ is unitary).

fs = 100;  N = 100;
n  = 0:N-1;
x  = cos(2*pi*5*(n/fs));               % 1 s of a 5 Hz cosine, N = 100

k  = 0:N-1;
W  = exp(-1j*2*pi/N) .^ (k'*n);        % N-by-N DFT matrix, W(k+1,n+1) = e^{-j2pi kn/N}

X_mat  = W * x.';                      % (a) DFT via matrix
x_mat  = (W' * X_mat) / N;             % (b) IDFT via matrix  (W' = conjugate transpose)
X_fft  = fft(x).';                     % (c) DFT via fft
x_ifft = ifft(X_fft);                  % (d) IDFT via ifft

% (e) stem plots
figure;
subplot(2,2,1); stem(k, abs(X_mat));   title('(a) |X[k]|, DFT matrix');
subplot(2,2,2); stem(n, real(x_mat));  title('(b) x[n], IDFT matrix');
subplot(2,2,3); stem(k, abs(X_fft));   title('(c) |X[k]|, fft');
subplot(2,2,4); stem(n, real(x_ifft)); title('(d) x[n], ifft');

max(abs(X_mat - X_fft))    % ~1e-12  : matrix DFT == fft
max(abs(x_mat.' - x))      % ~1e-14  : perfect reconstruction
2026-06-12T22:24:33.306862 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0 20 40 60 80 100 k 0 10 20 30 40 50 |X[k]| |X[k]| via DFT matrix: peaks of height N/2 = 50 at k = 5 and k = 95 0 20 40 60 80 100 n −1.0 −0.5 0.0 0.5 1.0 x[n] IDFT (1/N) W^H X recovers x[n] exactly (dots: original cos(pi n/10))
Fig. 7-13 — Exercise 5 (regenerated numerically): $|X[k]|$ from the DFT matrix shows two spikes of height $N/2 = 50$ at $k = 5$ and $k = N-5 = 95$; the IDFT $\tfrac{1}{N}\mathbf{W}^H\mathbf{X}$ returns $x[n]$ exactly (max error $\approx 5\times10^{-14}$). The fft/ifft plots are visually identical.
Why two spikes of height 50? One second at 100 Hz gives exactly 5 full cycles, so the 5 Hz cosine sits exactly on bin $k=5$ (bin spacing $f_s/N = 1$ Hz): $$x[n] = \cos\!\Big(\frac{2\pi 5 n}{100}\Big) = \tfrac12 e^{j2\pi 5n/100} + \tfrac12 e^{-j2\pi 5n/100} \;\Rightarrow\; X[k] = \tfrac{N}{2}\big(\delta[k-5] + \delta[k-95]\big),$$ the $k = 95$ spike being the negative-frequency partner ($-5$ Hz mod $N$). No leakage occurs because the frequency is an exact bin — and (a)=(c), (b)=(d) to machine precision, confirming that fft is just a fast algorithm for the same DFT matrix product.