Experiment 1
Basics of Digital Signal and operations, Sampling Theorem and its effect:
MATLAB
Aim: To generate the basic discrete time signal and understand the sampling theorem
1.1. Generation of Continuous time signal and Discrete time signal
%%%%% Generation of analog sinusoidal signals %%%%%%%%%%%
clear all
clc
freq1 = 100; % frequency 1
freq2 = 600; % frequency 2
mag = 2; % magnitude
%%%%% Time length with increment of 0.0001 %%%%
t=0:0.0001:0.02;
%%%%% x and y are continuous time (CT-analog) signals
x = mag*cos(2*pi*freq1*t); %%%% Magnitude*cos(2*pi*f*t)
y = mag*cos(2*pi*freq2*t);
figure(1)
plot(t,x,'r'); hold on; plot(t,y, 'b');
xlabel('time(sec)'); ylabel('Amplitude');
title('CT Signal x = blue, y = red');
%%%%%% Discrete time (DT) signal generation %%%%%%
fs = 600; % user defined sampling frequency
Ts = 1/fs; % sampling period = 0.002 sec (2ms)
num_samples = 11; % Number of samples = 1 + (Total Time / sampling period )
n = 0:num_samples-1; % sample index
x2(n+1) = mag*cos(2*pi*freq1*n*Ts ); %%%%% DT signal x
y2(n+1) = mag*cos(2*pi*freq2*n*Ts ); %%%%% DT signal y
%%%% Plot the CT and DT signals of x and y
figure(2)
subplot(2,1,1);
plot(t, x,'r.-');
title('CT signal x');
xlabel('time(sec)');ylabel('Amplitude');
subplot(2,1,2);
stem(n,x2,'rx'); hold on %%%% Command to plot the Discrete time signal
title('DT signal x');
xlabel('samples (n)'); ylabel('Amplitude');
figure(3)
subplot(2,1,1);
plot(t, y,'r.-');
title('CT signal y');
xlabel('time(sec)');ylabel('Amplitude');
subplot(2,1,2);
stem(n,y2,'rx');
Output Plots:
2
(Need to write 1 or 2 sentence about the observation made out of the plot for
each simulation plots)
1.2 Elementary Discrete time signal generation
%%%%% Generation of elementary DT signals %%%%%%%%%%%
close all
clear all
clc
%%%%% Code to generate the Unit step sequence %%%%%%
N=input('Enter the length of unit step sequence(N)= ');
n=[Link]N-1;
y=ones(1,N);
subplot(3,2,1);
stem(n,y,'k'); xlabel('Sample '); ylabel('Amplitude')
title('Unit step sequence');
% Code for unit ramp sequence
N1=input('Enter the length of unit ramp sequence(N1)= ');
n1=[Link]N1-1;
y1=n1;
subplot(3,2,2);
stem(n1,y1,'k');
xlabel('Sample'); ylabel('Amplitude');
title('Unit ramp sequence');
% Code for sinusoidal sequence
N2=input('Enter the length of sinusoidal sequence(N2)= ');
n2=[Link]N2-1;
y2=sin(0.5*pi*n2); subplot(3,2,3);
stem(n2,y2,'k');
xlabel('Sample'); ylabel('Amplitude'); title('Sinusoidal sequence');
% Code for cosine sequence
N3=input('Enter the length of the cosine sequence(N3)=');
n3=[Link]N3-1;
y3=cos(0.5*pi*n3); subplot(3,2,4);
stem(n3,y3,'k');
xlabel('Sample'); ylabel('Amplitude'); title('Cosine sequence');
%Program for exponential sequence
N4=input('Enter the length of the exponential sequence(N4)= ');
n4=[Link]N4-1;
a=input('Enter the value of the exponential sequence(a)= ');
y4=exp(a*n4);
subplot(3,2,5);
stem(n4,y4,'k');
xlabel('Time'); ylabel('Amplitude');
title('Exponential sequence');
%Program for unit impulse
n=-[Link];
y=[zeros(1,3),ones(1,1),zeros(1,3)];
subplot(3,2,6);
stem(n,y,'k');
xlabel('Time'); ylabel('Amplitude'); title('Unit impulse');
3
Output Plots:
1.3 Linear Convolution
(A) MATLAB Code to compute the linear convolution of two
signals
close all
clear all
clc
x=input('Enter x: ')
h=input('Enter h: ')
m=length(x);
n=length(h);
X=[x,zeros(1,n)];
H=[h,zeros(1,m)];
for i=1:n+m-1
Y(i)=0;
for j=1:i
Y(i)=Y(i)+X(j)*H(i-j+1);
end
end
Y
stem(Y); ylabel('Y[n]'); xlabel(' Samples (n)');
title('Convolution of Two Signals without using Inbuilt conv function');
Output Plot:
(B) Verify user answer with the MATLAB Inbuilt function: conv
4
1.4 Shannon- Nyquist Sampling Theorem
clear all;close all;clc;
% signal parameters
T=0.001; %%%%% time step
t=0:10^(-5):T; %Time increment makes smooth signal ( You can verify 10^-3 or 10^-2
to see the non-smooth effect)
f1=1000; %Signal 1 frequency=1kHz
f2=1900; %Signal 2 frequency=1.9kHz
%Generate the original Continuous time signal ;
x=cos(2*pi*t*f1)+cos(2*pi*t*f2); % CT signal
subplot(2,2,1);
plot(t,x);grid on;
title('Continuous time signal');
xlabel('t');
ylabel('x(t)');
% Maximum frequency component of the signal
fmax=max(f1,f2);
%% sampling condition fs>2fmax
fs1=5*fmax;
n1=0:1/fs1:T; %Time scale
x1=cos(2*pi*f1*n1)+cos(2*pi*f2*n1); %Generated sampled signal
subplot(2,2,2);
stem(n1,x1);
hold on;
plot(n1,x1,'r');grid on;
hold off;
title('Over sampling condition:Fs=5*Fmax');
xlabel('n');
ylabel('x[n]');
% Exact sampling condition = Nyquist rate;
fs2=2*fmax;%Exact sampling(fs=2fmax)
n2=0:1/fs2:T;%Time scale
x2=cos(2*pi*f1*n2)+cos(2*pi*f2*n2); %Generated sampled signal
subplot(2,2,3);
stem(n2,x2);
hold on;
plot(n2,x2,'r');grid on;
hold off;
title('Exact sampling condition:Fs=2Fmax');
xlabel('n');
ylabel('x[n]');
%Under sampling condition;
fs3=1.2*fmax;%Under sampling(fs=2fmax)
n3=0:1/fs3:T;%Time scale
x3=cos(2*pi*f1*n3)+cos(2*pi*f2*n3); %Generated sampled signal
subplot(2,2,4);
stem(n3,x3);
hold on;
plot(n3,x3,'r');grid on;
hold off;
title('Under sampling condition:Fs=1.2Fmax');
xlabel('n');
ylabel('x[n]');
Output Plots:
5
1.5 Aliasing Effect
%%%%% Generation of analog sinusoidal signals %%%%%%%%%%%
clear all
clc
freq1 = 100; % frequency 1
freq2 = 600; % frequency 2
mag = 2; % magnitude
%%%%% Time length with increment of 0.0001 %%%%
t=0:0.0001:0.02;
%%%%% x and y are continuous time (CT-analog) signals
x = mag*cos(2*pi*freq1*t); %%%% Magnitude*cos(2*pi*f*t)
y = mag*cos(2*pi*freq2*t);
figure(1)
plot(t,x,'r'); hold on; plot(t,y, 'b');
xlabel('time(sec)'); ylabel('Amplitude');
title('CT Signal x = blue, y = red');
%%%%%% Discrete time (DT) signal generation %%%%%%
fs = 500; % sampling frequency Ts = 1/fs; % sampling period = 0.002 sec (2ms)
num_samples = 1+ (0.02/Ts); % Number of samples = 1 + (Total Time / sampling
period )
n = 0:num_samples-1; % sample index
x2(n+1) = mag*cos(2*pi*freq1*n*Ts ); %%%%% DT signal x
y2(n+1) = mag*cos(2*pi*freq2*n*Ts ); %%%%% DT signal y
%keyboard
% Plot the "analog" signals
subplot(2,1,1);
plot(t, x,'r.-', t, y,'b-');
my_title = sprintf('Simulated analog signals x= dots, y=solid');
title(my_title);
xlabel('time');ylabel('Amplitude');
% Plot the "sampled" signals
subplot(2,1,2);
%plot(n,x2,'rx', n,y2,'bo')
stem(n,x2,'rx'); hold on
stem(n,y2,'bo')
my_title = sprintf('Simulated digital signals x=x, y=o');
title(my_title);
xlabel('samples');
ylabel('Amplitude');
Output Plots:
Observe the DT signal for different sampling frequencies: 100, 500,1000, 1200,
1800
Conclusion: