PRACTICAL MATLAB® FOR ENGINEERS PRACTICAL MATLAB

(sharon) #1

Time Domain Representation of Continuous and Discrete Signals 83


Example 1.17

Create the script fi le up_down_samples that returns the approximation plots of the fol-
lowing time discrete function:

f(n) = cos(2 0.05n) + 2 sin(20.03n)

for a length of N = 100 samples, for the following cases:


  1. Down-sample or decimate the sequence f(n) with the integer factors of M = 2 , 4 ,
    and 10.

  2. Up-sample or interpolate the sequence f(n) with the integer factors of L = 2 , 4 ,
    and 10.

  3. Resample f(n) by the ratio of the two integers given by L/M, for the following cases:
    L = 3 , M = 2 , and L = 2 , M = 3.


MATLAB Solution
% Script file: up _ down _ samples
n = 0:1:99;
f = cos(2*pi*.05*n)+2*sin(2*pi*.03*n);
yzero = zeros(1,500);

figure(1); % down-sample with M=2, 4, 10
subplot(2,2,1)
stem(n,f);hold on; plot(n,f,n,yzero(1:100))
title(‘f(n) vs n’); ylabel(‘Amplitude’);
xlabel(‘time index n’);
subplot(2,2,2)
gm2 = decimate(f,2,’fir’);
nm2 = 0:100/2-1;
stem(nm2,gm2(1:50)); hold on; plot(nm2,yzero(1:50));
title(‘f(n) is decimated with M=2’); ylabel(‘Amplitude’);
xlabel(‘time index n’);
subplot(2,2,3)
gm4 = decimate(f,4,’fir’);
nm4 = 0:100/4-1;
stem(nm4,gm4(1:25));hold on;
plot(nm4,gm4(1:25),nm4,yzero(1:25));
ylabel(‘Amplitude’); xlabel(‘time index n’);
title(‘f(n) is decimated with M=4’)
subplot(2,2,4)
gm10 = decimate(f,10,‘fir’);
nm10=0:100/10-1;
stem(nm10,gm10(1:10));hold on;
plot(nm10,gm10(1:10),nm10,yzero(1:10));
Free download pdf