Sampling: Upsampling

Solution

Task 1

lab4ali1

Contents

% Clear previously stored variable, close opeed figures and clear console.
clear all;close all;clc

Set Parameters

t_limit=2e-3;           % Time duration: 2 miliseconds
fs = 5e3; T = 1/fs;     % Sampling frequency 5 KHz set
tn = 0:T:t_limit;
f1=1e3;
f2=4e3;
f3=6e3;

Generate and plot the cosine sub-signals

x1=cos(2*pi*f1*tn);
x2=cos(2*pi*f2*tn);
x3=cos(2*pi*f3*tn);
figure;
subplot(4,1,1);
plot(tn,x1);
xlabel('Time');
ylabel('Amplitude');
title('x_1');
subplot(4,1,2);
plot(tn,x2);
xlabel('Time');
ylabel('Amplitude');
title('x_2');
subplot(4,1,3);
plot(tn,x3);
xlabel('Time');
ylabel('Amplitude');
title('x_3');

Generate and plot the combined signal Xn

x=x1+x2+x3;
subplot(4,1,4);
plot(tn,x);
xlabel('Time');
ylabel('Amplitude');
title('x_n');

Generate and plot Xa. Correlate visually with Xn.

xa=cos(2*pi*f1*tn) + 2*cos(2*pi*f1*tn);
figure
stem(tn,x);
hold on
stem(tn,xa);
xlabel('Time_s');
ylabel('x_n,x_a');
legend('x_n','x_a');

Task 2

lab4ali2

Contents

clear all;close all;clc

Set duration and frequency parameters

t_limit=2e-3;
fs = 12e3;      % Sampling Frequency
T = 1/fs;
tn = 0:T:t_limit;
f1=1e3;
f2=4e3;
f3=6e3;

Generate signal

x1=cos(2*pi*f1*tn)+cos(2*pi*f2*tn)+cos(2*pi*f3*tn);
sc=x1;

Add AWGN to simulate ADC noise

x1=awgn(x1,20,'measured');

Plot of original signal without ADC noise

plot(tn,x1);
xlabel('Time'); ylabel('Amplitude'); title('X_n without ADC noise');
% l=4*(length(x1)-1);
% x2=zeros(1,l);

Upsampling via interpolation

usp=4;                                      %Upsampling Factor
zero_p=usp-1; %Zeros between each successive samples of the original signal

Upsample by appending zeros between each successive sample. You can also use the upsample() function, but you'll have to take care of extra zeros appended.

x2=zeros(1, (zero_p+1)*length(x1)-zero_p);
x2(1:(zero_p+1):end)=x1;
figure;
subplot(2,1,1);
stem(tn,x1);
xlabel('Time');ylabel('Amplitude');title('x_n');
subplot(2,1,2);
tn2=0:1/(fs*usp):t_limit;
stem(tn2,x2);
xlabel('Time');ylabel('Amplitude');title('z_n');

Observe spectrum of Xn and its upsampled variant Zn

f_spec=-fs/2:1/t_limit:fs/2;
figure;
subplot(2,1,1);
X1=(1/length(x1)).*abs(fftshift(fft(x1)));
stem(f_spec,X1);
xlabel('Frequency/Hz'); ylabel('Magnitude');title('Spectrum of X_n');
f_spec2=-(fs*usp)/2:1/t_limit:(fs*usp)/2;
subplot(2,1,2);
X2=(1/length(x2)).*abs(fftshift(fft(x2)));
stem(f_spec2,X2);
xlabel('Frequency/Hz'); ylabel('Magnitude');title('Spectrum of Z_n');

Filter Zn and observe spectrum

alpha=1;
h1 = intfilt(usp,2,alpha);       % Filtering performs linear interpolation
y=filter(h1,1,x2);
figure;
Y=(1/length(y)).*abs(fftshift(fft(y)));
stem(f_spec2,Y);
xlabel('Frequency/Hz'); ylabel('Magnitude');title('Spectrum of filtered Z_n');

Time Domain plot of filtered Zn

figure;
stem(tn2,y);
xlabel('Time');ylabel('Amplitude');title('Filtered z_n');

% We can see that the filtered signal matches the original signal, since
% the ADC noise has been reduced i.e. quantization noise has been
% attenuated.

Robotics & Dynamic Systems Lab