You are on page 1of 6

Kulliyyah of Engineering, IIUM

Department of Electrical and Electronics Engineering

Digital Signal Processing Lab


Semester II, 2016/2017, Due Date: March 21, 2017

Lab 2. Continuous Time to Discrete Time Conversion (Sampling and Aliasing)

Introduction:

In order to perform digital processing of analog signals, you need to do analog to discrete
conversion. The first step in A/D conversion is CT to DT conversion, which is the objective of
this lab.
1: Nyquist-Shannon sampling theorem: States that perfect reconstruction of a signal is
possible when the sampling frequency is greater than the twice the maximum frequency of the
signal being sampled. If lower sampling rates are used, the original signals information may not
be completely recovered from the sampled signal.
For example if a signal has an upper band limit of 100 hz, a sampling frequency greater than 200
hz will avoid aliasing and allow theoretically perfect reconstruction.
Fs >= 2 Fmax
2: Aliasing Effect: Aliasing is the phenomenon that results in loss of information when a
CT signal is reconstructed from its sampled values. In principle the original analog signal can be
reconstructed back from the samples and aliasing can be avoided, provided that Nyquist
sampling theorem was not violated while sampling i.e. the sampling rate was at least twice the
frequency of the highest frequency components present in the signal.
Sampling of Sinusoidal signal:
%original analog signal
t=0:0.0005:1;
F=13;
xa=cos(2*pi*F*t);
subplot(2,1,1)
plot(t,xa);
grid on;
xlabel('Time, sec')
ylabel('Amplitude')
title('continuous time original signal')
axis([0 1 -1.2 1.2])
% sampled signal
T=0.1;
n=0:T:1;
xs=cos(2*pi*F*n);
k=0:length(n)-1;
subplot(2,1,2)
stem(k,xs);
grid on;
xlabel('Time index n')
ylabel('Amplitude')
title('Sampled signal')

axis([0 length(n)-1 -1.2 1.2]);

After execution the following subplot appears:

Verification of Nyquist Theorem by taking user input:


%original analog signal
f=input('Give original signal frequency in hertz, f=');
t=0:0.005:1;
xa=cos(2*pi*f*t);
subplot(2,1,1)
plot(t,xa);
grid on;
xlabel('Time, sec')
ylabel('Amplitude')
title('continuous time original signal')
axis([0 1 -1.2 1.2])
% sampled signal when sampling rate is less than Nyquist criteria
fs1=input('Enter sampling frequency, less than twice the maximum signal frequency in hertz,
fs1=')
T1=1/fs1;
n=0:T1:1;
w=2*pi*f;
xs1=cos(w*n);
k=0:length(n)-1;
subplot(2,1,2)
stem(k,xs1);
grid on;
xlabel('Time index n')
ylabel('Amplitude')
title('Sampled signal with Fs<=2Fmax')
axis([0 length(n)-1 -1.2 1.2]);

After execution enter following frequencies:


Give original signal frequency in hertz, f=15
Enter sampling frequency, less than twice the maximum signal frequency in hertz, fs1=25

Aliasing Effect:
%original analog signal
t=0:0.0005:1;
F=13;
xa=cos(2*pi*F*t);
subplot(2,1,1)
plot(t,xa);
grid on;
xlabel('Time, sec')
ylabel('Amplitude')
title('continuous time original signal')
axis([0 1 -1.2 1.2])
% Reconstructed signal with aliasing effect
T=1/(13*1.5);
n=(0:T:1)';
xs=cos(2*pi*F*n);
t=(-0.5:0.004:1.5)';
%Reconstruction low pass filter, implemented in time domain
Ya=sinc((1/T)*t(:,ones(size(n)))-(1/T)*n(:,ones(size(t)))')*xs;
subplot(2,1,2)
plot(n,xs,'o',t,Ya);
grid on;
xlabel('Time seconds')
ylabel('Amplitude')
title('Reconstructed signal')
axis([0 1 -1.2 1.2])

After simulation, following plot will show up.

Question 1:
In the above sampling topic plot the sampled signal for the remaining two cases.
1: Take sampling frequency exact 2 times the original frequency.
2: Take sampling frequency 2.5 times the maximum frequency.

Question 2:
Reconstruct the original signal without aliasing. Take sampling frequency of your own choice.

Question 3:

I. Oversampling.

a. Using MATLAB, create a sinusoidal signal with the frequency 1000 Hz.
b. Sample it at 8000 Hz and collect 512 (28) samples.
c. Using a subplot with 2 rows and 2 columns, plot the waveform in the subplot on
the first row and first column.
d. Set the axis so that you get three waves.
e. Convert from the time domain to frequency domain by taking the Fourier
Transform.
f. Plot the FFT vs. frequency.
f=1000; %frequency of the sine wave
fs=8000; %sampling frequency
Ts=1/fs; %sampling time
Ns=512; %512 points
t= [0:Ts:Ts*(Ns-1)]; %50/8000~6.25ms
x=sin(2*pi*f*t); %sine function for 50 samples
subplot(2,2,1),plot(t,x)
grid on; %plot of the sine wave
axis([0,.003,-1.2,1.2]) %set the x,y axis
X=(abs(fft(x,Ns))); %take the FFT
y=X(1:length(X)/2);
f=(1:length(y));
subplot(2,1,2),plot(f*fs/Ns,y);
grid on;

II. Undersampling.

1. Repeat part(I) with the frequency 7000hz.


2. Are the graphs the same?
3. Can you explain the reason?
4. Which plot shows the effect of aliasing?
5. How can you tell?

Question 4:

1. Store in the vector x the samples of the chirp signal on the interval 0
t
1, and let T
is the same value as above.
2. Use sound(x,fs) to play the chirp signal contained in x. Can you explain what you
just heard?
3. Can you predict the time at which the played signal has zero (or very low)
frequency? Use a longer x sequence to verify your prediction.

f=3000; %frequency of the sine wave


fs=8192; %sampling frequency
Ts=1/fs; %sampling time
Ns=512; %512 points
t= [0:Ts:Ts*20*(fs-1)]; %50/8000~6.25ms
x=sin(2*pi*f*t+0.5*2000*t.^2);%sine function for 50 samples
sound(x,fs) %play the wave
Question 5:
A sinusoid type signal can be generated as follows:
% Generates a 40Hz cosine wave
%
N = 64; % 64 data points
fs = 1000; % 1kHz sampling frequency
T = 1/fs; % sample period
f = 40; % signal frequency
n = 0:N-1; % discrete time vector
x = cos(2*pi*f*n*T); %signal definition
%plot the signal against time
stem(n*T,x);
title('40Hz cosine')
xlabel('Time')
ylabel('x(nT)')

5.1 Enter the code in a script file named sineGen.m and verify that what you get on the plot
really is a sampled 40Hz cosine wave. You should be able to identify the outline of the
sinusoid.
5.2 Now plot the signal using the plot function. This connects the points with a straight line and
can be considered to be a form of signal reconstruction. It is not optimal but it is sufficient
here.
5.3 Modify the code to generate sampled cosines at signal frequencies of 200Hz, 400Hz, 600Hz
and 800Hz, with durations of 1 second but still a sampling frequency, fs, of 1kHz. In each
case, generate plots of the first few oscillations of the signals using stem and plot on the same
figure.
5.4 Add a plot of the signal sampled at 10000Hz (this is highly over sampled and can be viewed
as a good approximation to the analogue signal).

You can also listen to the signal using the command:

soundsc(interp(x,8),8*fs);

This plays the signal x assuming a sampling frequency of fs (the code actually interpolates the
signal and uses a sampling frequency of 8*fs). Can you explain what you hear?

You might also like