MP2: Sampling and Interpolation

In this lab, you'll create sinusoids at a variety of sampling rates, both above and below Nyquist, and compare the resulting stem plots. Then you'll downsample a sinusoid, and interpolate it back to the original sampling rate, using a variety of different interpolation kernels.

In order to make sure everything works, you might want to go to the command line, and run

pip install -r requirements.txt

This will install the modules that are used on the autograder, including numpy, h5py, and the gradescope utilities.


Part 1: Comparing sinusoids at different frequencies

In this first part of the MP, you will create a sinusoid, and sample it at different sampling rates. The four parameters will be

The returned signal should be $$x[n] = \Re\{ z e^{j2\pi nf/F_s}\}$$

First, let's generate a sinusoid with plenty of samples, so we make sure it plots correctly. We'll use stem to plot it, so we can see each sample.

Now let's see what happens when we gradually lower the sampling rate.

Notice that, as long as you have at least two samples per period ($F_s\ge 2f$), you can reconstruct the original cosine (e.g., there are four sign-reversals in one second, so you can see that it's a 4Hz sinusoid).

When $F_s < 2f$, you can no longer see the real underlying cosine. It has been "aliased" to a new "alias frequency," $f_a=F_s-f$. That's because, for any integer $n$,

$$\cos\left(\frac{2\pi f n}{F_s}\right) = \cos\left(\frac{2\pi (F_s-f) n}{F_s}\right)$$

The highest frequency at which the cosine can be is called the "Nyquist rate", and it is $$F_N = \frac{1}{2}F_s$$

Let's adjust the sampling frequency so that 4Hz is just barely above or just barely below Nyquist.

Notice that, when $f > F_s/2$, it looks as though the sinusoid has a frequency of $F_s-f$. This is called its "aliased frequency." When the sampling rate gets even lower, aliasing can be even worse. The aliased frequency in general is

$$f_a = \min\left(f \mbox{mod} F_s, (F_s-f)\mbox{mod} F_s\right)$$

where $\mbox{mod}$ means modulo (written in python as f % Fs). Let's see some examples.

Exactly the same thing happens to sine waves as to cosines, except that:

  1. When $f=F_s/2$ exactly, the sine wave disappears.
  2. When $F_s/2 < f < F_s$, the sign of the sine reverses. That's because $$\sin\left(\frac{2\pi f n}{F_s}\right) = -\sin\left(\frac{2\pi (F_s-f) n}{F_s}\right)$$

In order to plot sine waves, we'll use a phasor of $-j=e^{-j\pi/2}$, to give it a phase of $-\pi/2$.

Part 2: Computing the alias frequency and aliased phasor

These two equations: $$\cos\left(\frac{2\pi f n}{F_s}\right) = \cos\left(\frac{2\pi (F_s-f) n}{F_s}\right)$$ $$\sin\left(\frac{2\pi f n}{F_s}\right) = -\sin\left(\frac{2\pi (F_s-f) n}{F_s}\right)$$

...can be combined to give this equation: $$\Re\left\{z \exp\left(j\frac{2\pi f n}{F_s}\right)\right\}=\Re\left\{z^* \exp\left(j\frac{2\pi (F_s-f) n}{F_s}\right)\right\}$$

In other words,

For example, let's look at the signal $$x(t) = \cos\left(2\pi ft-\frac{\pi}{4}\right)$$

This is a cosine delayed by $\pi/4$ radians. It's positive for about $3/8$ of a period, then negative for $1/2$ period, and so on.

Sometimes it's useful to explicitly calculate the aliased frequency and aliased phasor (HINT: you probably want to use the np.mod method):

If we plot a sinusoid at the aliased frequency and aliased phasor, it should look exactly the same as the original sinusoid.


Part 3: Fourier analysis

Remember that, if you have a signal that's periodic with a period $T_0$, then you can find its Fourier series coefficients using the Fourier analysis formula: $$X_k = \frac{1}{T_0}\int_{0}^{T_0} x(t) e^{-j\frac{2\pi kt}{T_0}}dt$$

We can get a pretty good measurement of $X_k$ on a computer by setting the sampling rate high enough, replacing the integral by a sum, and multiplying the integrand by $F_s$ (to represent $dt=1/F_s$). After simplifying a little, we get: $$X_k = \frac{1}{N_0} \sum_{n=0}^{N_0-1} x[n]e^{-j\frac{2\pi kn}{N_0}}$$ where $$N_0 = T_0F_s$$ During the rest of this MP, it will be useful to have a good Fourier analysis function, so let's write it.

Remember that, if a signal is periodic with period $N_0$, then it's also periodic with any period that's a multiple of $N_0$. We can use that fact to make the plot easier to read, e.g., by using four periods of a cosine as if they were just one period; that way the coefficient $X_1$ will be plotted as if it were $X_4$.

Let's choose a cosine with a really long period, say, 50 samples, and then compute the Fourier series coefficients over a space of 200 samples.


Part 4: Triangular kernel

Let's define the triangle function as follows: $$h[n]=\begin{cases}1-\frac{|t|}{T}&-T+1\le n\le T-1\\0&\mbox{otherwise}\end{cases}$$

This function will be useful when we try to interpolate signals, in part 5. Go ahead and write a function to compute it. Notice that you only need to include the nonzero samples, not the zero-valued samples.


Part 5: Interpolation

We are now going to simulate a discrete-to-continuous time converter by downsampling our 50-sample cosine by a factor of 25, and then upsampling it by the same factor of 25. The upsampling operation will use the same four types of interpolation that are used in typical D/C converters: rectangle, triangle, spline, and sinc.

First, let's downsample:

Now let's pretend we're interpolating it into continuous time. The general formula for interpolation is

$$x(t) = \sum_{n=-\infty}^\infty x[n] h\left(t-nT\right)$$

where $T$ is the sampling period.

In order to simulate D-to-C conversion, we will use $t$ to represent time in the highrate signal (e.g., $0\le t<200$) instead of time in the continuous-time signal. We'll use $n$ to represent time in the lowrate signal (e.g., in the example, $0\le n< 8$), while $T$ is the ratio between the two sampling rates (e.g., $T=25$).

Notice those last few lines from the docstring. The triangle function has a length of $2T-1$. The lowrate signal has a length of $N$. If you put in a triangle function in place of every lowrate sample, you'd get a total length of $NT+(T-1)$, but that's not what we want -- what we want is a signal of length $NT$. In order to make that happen, we'll use modulo indexing, i.e., the interpolation formula should be implemented as

for n in range(N):

$$x\left((t+nT) \mbox{mod} NT\right) += h(t) x[n]$$

You might find the function np.mod to be useful.

Well, it doesn't look very much like a cosine! In fact, it's just a linear interpolation between the lowrate samples.

In order to better understand what's going on, let's look at its spectrum. In the following plot you should see that most of the spectral energy is at $X_1$, but there is significant nonzero energy also at $X_3$, $X_5$, and so on:


Part 6: Rectangle interpolation

Rectangle interpolation is much, much worse than triangle interpolation, but it's much easier to implement in hardware, so it's actually much more common in the real world. Let's try it, and see how it turns out:


Part 7: Spline interpolation

One way of thinking about the superiority of triangle interpolation over rectangle interpolation is to say that the triangle-interpolated $x(t)$ is continuous, while the rectangle-interpolated $x(t)$ is not continuous.

We can make an even better interpolation if we try to make one that is continuous, and that has continuous first derivatives.

Remember that $$x(t) = \sum x[n] h(t-nT)$$

All of those constraints can be satisfied by using a cubic spline interpolation kernel. For example, the following cubic spline function satisfies these properties:

$$h(t) = \begin{cases} 1-\frac{3}{2}\left(\frac{|t|}{T}\right)^2+\frac{1}{2}\left(\frac{|t|}{T}\right)^3 & |t|\le T\\ -\frac{3}{2}\left(\frac{|t|-2T}{T}\right)^2\left(\frac{|t|-T}{T}\right)&T\le |t|\le 2T\\ 0 &\mbox{otherwise}\end{cases}$$

Part 8: Sinc interpolation

The spline-interpolated sinusoid looks a whole lot better than the triangle-interpolated sinusoid, doesn't it? The Fourier series coefficients $X_3$ and $X_5$ are almost zero, but not quite -- it's hard to see in the plot above, but they do include some energy. Such energy may not be visible in an image, but human ears can sometimes hear it.

One might ask, is there any interpolation kernel for which all the derivatives are continuous? In other words, not just $h'(t)=\frac{dh}{dt}$, but also $h''(t)=\frac{d^2h}{dt^2}$ is continuous, and so is $h'''(t)=\frac{d^3h}{dt^3}$, and so on? The answer is yes. The kernel with all derivatives continuous is called the sinc function, defined as:

$$h(t)=\begin{cases} 1 & t=0\\ \frac{\sin(\pi t/T)}{(\pi t/T)} & \mbox{otherwise} \end{cases}$$

This is actually an infinite-length interpolation kernel, which is kind of inconvenient! In order to actually use the sinc interpolator in practice, we need to truncate it after some fixed duration, as described in the docstring:


Part 8: Debugging using solutions.hdf5

If you reached this point in the notebook, then probably your code is working well, but before you run the autograder on the server, you should first run it on your own machine.

You can do that by going to a terminal, and running the following command line:

python grade.py

In order to help you debug, you have the file solutions.hdf5. You can use it to debug like this:

As you can see, this file contains a lot of objects, created during a sample run of the solution code with random parameters. Let's plot the song that it was expecting:

If the lower plot has any nonzero values, those nonzero values are places where your solution differs from the reference solution. If they differ by more than 0.01, then your solution will probably be marked incorrect, so you should figure out why those things have happened.


Extra Credit

You can earn up to 10% extra credit on this MP by finishing the file called extra.py, and submitting it to the autograder.

This file has just one function, called extra.rate_conversion, with the following signature:

Let's rewrite that docstring in terms of equations, rather than words.

The goal is to convert a signal from a high sampling rate to a low sampling rate, by doing Fourier analysis at the high rate, and then performing Fourier synthesis at the low rate. At the low rate, we'll keep only the terms that are below Nyquist at the low rate.

Fourier analysis at the high rate is done using:

$$X_k = \frac{1}{N_0T} \sum_{n=0}^{N_0T-1} x_h[n] e^{-j2\pi kn/N_0T}$$

where $x_h[n]$ is the high-rate signal, $N_0$ is the length of one period at the low rate (equal to the length of lowrate_signal), and $N_0T$ is the length of one period at the high rate (equal to the length of highrate_signal).

Fourier synthesis at the low rate is then done using: $$x_l[n] = \sum_{k=-\mbox{int}((N_0-1)/2)}^{\mbox{int}((N_0-1)/2)} X_ke^{j2\pi kn/N_0}$$

Notice that, if $N_0$ is odd, this summation includes $N_0$ harmonics, all of which have frequencies below $F_s/2$. If $N_0$ is even, this summation includes only $N_0-1$ harmonics; the harmonic right at $kF_0=F_s/2$ is omitted.

Here's what happens if we load one period of the violin from MP1, and apply the extra credit assignment to it.

You can compare your solutions to the reference solutions by opening extra_solutions.hdf5, as shown here: