# Trying to understand downsampling and then upsampling

I am trying to understand how this works, specifically, what the DTFT of each step looks like in each step of the chain (for understanding). I am not looking for an answer like because the input signal has a frequency of $\pi/3$, it will be reconstructed correctly. I plotted the DTFT of each step in MATLAB and simply don't understand the result. (DTFT of $x_d[n]$ and $x_r[n]$)

I would also like to know what the transfer functions of the downsampler and upsampler are.

The following pictures shows the DTFT of $x[n]$, $x_d[n]$ and $x_e[n]$ respectively I don't know where the frequency components in the upsampled signal ($x_e[n]$) are coming from. I have noticed though, that it is what you would get if you convolved the original signal ($x[n]$) with an impulse train with period $2\pi/3$.

PS: Why is there no DTFT tag on this site?

Downsampling can be defined as follows:

$s_{out}[n]= s_{in}[n\cdot M]$ (taking every M-th signal sample)

After applying Z-transform we obtain:

$\bar{s}_{out}[z]=\dfrac{1}{M}\sum_{m=0}^{M-1}\bar{s}_{in}\left[ z^{1/M} \cdot w_{M}^{m} \right]$, where $w_{M}=e^{-2\pi i /M}$

By substituting $z=e^{2\pi i f_n}$ ($f_n$ is the normalised frequency: $f_n = \dfrac{f}{f_s} \in [0, 0.5]$

This yields following relationship between input and output spectrum:

$\hat{s}_{out}[f_n]= \dfrac{1}{M}\sum_{m=0}^{M-1}\hat{s}_{in}\left[ (f_n-m)/M \right]$

You can understand that as a fact that your spectrum is being copied M times (even outside of your original sampling frequency) and stretched by factor of M.

Upsampling on the other hand is governed by following equations:

$s_{out}[n]=\begin{cases} s_{in}(n/M) & \text{n} \; mod \;\text{M} =0 \\ 0 & \text{otherwise}\end{cases}$

After applying Z-transform:

$\sum_{n} s_{out}[n]\cdot z^{-n} = \sum_{n} s_{in}[n]\cdot z^{-nM}$, which gives: $\bar{s}_{out}[z]=\bar{s}_{in}[z^M]$

By doing same substitution of a unit circle instead of z variable:

$\hat{s}_{out}[f_n] = \hat{s}_{in}[M\cdot f_n]$

You can imagine that this equation is squeezing your spectrum by factor of M, bringing at the same time whatever is outside of your sampling frequency range.

Plots you've obtained are great example how it works!

P.S. There is a dft tag.

if you start with the original spectrum, then you have spectra at pi/4 and -pi/4. you will also have spectra either side of 2pi, i.e at 7pi/4 and 9pi/4. when you down sample by 3, these spectra (just consider the positive frequencies for now) will move from pi/4 to 3pi/4, the one at 7pi/4 will move down to 5pi/4. when you upsample by 3 the spectra at 3pi/4 will go to pi/4 again, and the one at 5pi/4 will move to 5pi/(4*3) = 5pi/12. this is why you see those 2 spectra close together. down sample spreads the spectra out, up sample squashes them back inwards. if you filter the signal at -pi/3 to pi/3 you will get rid of these spectra at 5pi/12. to recover the signal will need to have gain of 3.

Look at your filter. Ask yourself if the input falls within the passband of the filter when converted to the frequency spectrum. A $cos(n\pi/4)$ will have an impulse at π/4 and -π/4 with a height of 1/2. Since this is within the passband of the filter, the output will be the same as the input if the filter has a gain of 3.