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

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);
plot(t,x);

(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);
plot(tt,xx);

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);
plot(tt,xx);

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

subplot(2,1,1);
plot(t,x);
subplot(2,1,2);
plot(tt,xx);

(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);
subplot(2,1,2);
plot(tt,xx);

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);
subplot(1,2,1);
stem(0:99,abs(y(1:100))/1000); % just plotting frequencies up to 100 to make the picture clearer
subplot(1,2,2);
stem(0:49,abs(yy(1:50))/100);

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,
5.2802,
-6.5403,
3.3084,
2.2331,
-3.0000,
4.0872,
-0.2183,
-1.6862,
-0.4261,
2.0000,
-6.8983,
5.9223,
-2.6904,
-0.6151,
5.0000,
-2.4692,
0.8363,
1.0682,
-1.1919];

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

plot(0:0.05:0.95,x);

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

xx = upsample(x,5);
stem(xx);

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
stem(0:19,abs(y));

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);
subplot(1,2,2);
stem(0:99,abs(yy));

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)
plot(0:0.01:0.99,5*real(z),’red’);
hold on;
stem(0:0.05:0.95,x);

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