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$:
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:
Reconstruct $x_c(t)$ from $x[n]$ with a D/A converter and reconstruction filter;
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:
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):
Fig. 7-1 — The decimator: anti-alias lowpass filter followed by the compressor ↓M(抗混疊低通濾波器 + 壓縮器). The compressor alone is plain downsampling.
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.
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:
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.
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.
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:
Fig. 7-5 — The interpolator: expander ↑L (zero insertion) followed by an anti-imaging lowpass filter with gain $L$ and cutoff $\pi/L$(擴張器 + 去鏡像低通濾波器).
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.
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).
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$:
Sample preservation: $x_i[kL] = x[k]$ — the original samples pass through unchanged.
Exact reconstruction: if $x[n]=x_c(nT)$ came from a properly bandlimited
$x_c(t)$ (i.e. $X_c(j\Omega)=0$ for $|\Omega| \ge \Omega_N$ and $T \le \pi/\Omega_N$), then
$x_i[n] = x_c(nT/L)$ exactly — the interpolator behaves like an ideal C/D converter
running $L$ times faster.
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:
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$.
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
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:
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(內插與抽取濾波器合併為一個低通).
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:
Call
What it does
Aliasing 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 sample
Yes — 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(重新取樣常用函式)
Function
Purpose(用途)
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)
找出最接近輸入數值的分數形式(P/Q)— rational approximation via continued fractions
7.6 Summary
Downsampling $\downarrow M$(降取樣)
Upsampling $\uparrow L$(升取樣)
Sampling rate
reduced by $M$
increased by $L$
Sample period
increases: $T' = MT$
decreases: $T' = T/L$
With its lowpass filter, called
decimation(抽取)
interpolation(內插)
Spectrum
stretched by $M$, amplitude $\times \tfrac1M$, $M$ copies
compressed by $L$, $L-1$ images appear
Hazard
aliasing(混疊)— irreversible
imaging(鏡像)— removable
Filter
before $\downarrow M$: gain 1, cutoff $\pi/M$
after $\uparrow L$: gain $L$, cutoff $\pi/L$
Information
lost 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)$).
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:
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$).
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:
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}$
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$. ✓
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
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.
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?
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
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.