AMAT 415 — Matlab activity 5: Downsampling, upsampling and interpolation

(As usual, please report errors or typos by email.)

Let’s plot the cosine wave of frequency 60 Hz, sampled 1000 times per second for 1 second:

t = 0:0.001:1-0.001;
x = cos(2*pi*60*t);

(Maximize your figure window if the graph is to squooshed.) Now let’s plot the same wave, sampled at 200 times per second for one second. There are two ways to do this. One way is to proceed as above:

tt = 0:0.005:1-0.005;
xx = cos(2*pi*60*tt);

Alternatively, MATLAB builds in functionality to downsample by a factor of n, i.e., to keep 1 out of every n samples
starting with the first one. We want 200=1000/n, so n=5.

tt = downsample(t,5);
xx = downsample(x,5);

To compare the graphs, it helps to plot them one below the other. To do this, type:


(Look up subplot in the online help for an explanation of the syntax.)
The graphs look pretty similar, at least in the sense that both graphs seem to be displaying waves of the same frequency. So it seems that we didn’t lose that much information by downsampling by a factor of 5.

Now let’s see what happens when we downsample by a factor of 10:

xx = downsample(x,10);
tt = downsample(t,10);

Now the graphs look significantly different. If you count the peaks (local maxima) on the downsampled graph — both the sharp ones and the squared off ones — you should count 40 of them. This doesn’t sound right for a cosine wave that’s supposed to have frequency 60 Hz. The problem here is aliasing. Since 60 (frequency of wave) > 100/2 (half the sampling rate), our wave is aliased with one of frequency 100 – 60 = 40 Hz. The aliasing can also be seen in the frequency domain.

y = fft(x);
yy = fft(xx);
stem(0:99,abs(y(1:100))/1000); % just plotting frequencies up to 100 to make the picture clearer

Note the locations of the spikes. In summary: For applications like data compression, it is often desirable to downsample a signal. When doing this, you need to be aware of the aliasing phenomena that can creep in.

The process opposite to downsampling is upsampling — increasing the number of samples/second of a prerecorded signal.
Upsampling is closely related to (and sometimes used synonymously with) interpolation.

Close the figure window to start fresh.

Suppose we sample a signal 20 times per second and obtain the following:

x = [-4.0000,

Make x a row vector:

x = x’;

How might we fit a curve to this data set? How can we “connect the dots” in the stem plot in a reasonable way? The output of


isn’t very satisfying — too jagged. Upsampling gives a way. Try:

xx = upsample(x,5);

What we’ve just done is just upsampled x by a factor of 5, i.e., stuck 4 zeros between each component of x. The plot doesn’t look any better than the last one, though. But now let’s pass to the frequency domain. First, we examine the spectrum of our original signal x:

y = fft(x);
subplot(1,2,1); % we’ll add another plot below

The plot suggests that our signal is a superposition of sinusoidal components of frequencies 2, 7, and 9 Hz. (Recall that we can only resolve frequencies less than half the sampling rate, in this case 10=20/2.)

Now let’s examine the spectrum of the upsampled signal xx:

yy = fft(xx);

What do you observe? (Maximize the figure window if the plot is to squooshed.)

The spectrum is a bunch of (5, actually) copies of the spectrum of the original signal lined up. If we kill the extra copies, we’re left with the spectrum of our original signal, but at a higher resolution.

w =1;

The inverse Fourier transform of w.*yy interpolates x nicely:

z = ifft(w.*yy); % this is the same as the convolution of xx with ifft(w)
hold on;

So we have found a “nice” smooth curve interpolating the data set x using Fourier analysis.

This is an illustration of a standard method for recovering a continuous signal from a sampled version. This method is the content of the famous Shannon Sampling Theorem. Time permitting, we’ll talk a bit about that towards the end of the course.

FOR 2 BONUS POINTS ON HOMEWORK 3: In the last snippit of code, I’m plotting 5*real(z) as opposed to just z. Can you explain the presence of this correction factor conceptually? (“Because it makes the picture look right” is not sufficient.)

  1. 1:100) < 21);
    stem(0:99,w.*abs(yy []
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to AMAT 415 — Matlab activity 5: Downsampling, upsampling and interpolation

  1. Janeen says:

    You can earn some additional $$$ from your blog, i see several opportunities here.
    You should search in google for:
    Yoogurn’s money making

Leave a Reply

Your email address will not be published. Required fields are marked *