You are on page 1of 12

Experiment 1

Aim: To study Sampling and Reconstruction

Code:

clc; title('Sample wave 1');


clear all; xlabel('Time');
close all; ylabel('Amplitude');
a=input('Enter the Amplitude:'); subplot(3,2,4);
fm=input('Enter the frequency'); plot(t,sam2);
t=0:1/(1000*fm):5/fm; grid on;
s=a*sin(2*pi*fm*t); title('Sample wave 2');
fs=5*fm; xlabel('Time');
p=(1+square(2*pi*fs*t,50))/2; ylabel('Amplitude');
%square wave with 50% duty cycle
% reconstruction
p1=(1+square(2*pi*fs*t,0.1))/2; [n,d]=butter(10,1/50);
%nearly equal to an impulse train y=filter(n,d,sam1); %Low Pass Filtering
y1=filter(n,d,sam2);
sam1=s.*p; % Natural Sampling subplot(3,2,5);
sam2=s.*p1; % Near Ideal Sampling plot(t,y);
subplot(3,1,1); title('Reconstructed Sample wave 1');
plot(t,s); xlabel('Time');
grid on; ylabel('Amplitude');
title(' sinusoidal signal'); grid on;
xlabel('Time'); subplot(3,2,6);
ylabel('Amplitude'); plot(t,y1);
subplot(3,2,3); title('Reconstructed Sample wave 2');
plot(t,sam1); xlabel('Time');
grid on; ylabel('Amplitude');
grid on;
Output:

Enter the Amplitude:5


Enter the frequency100

1
Experiment 2

Aim: To study the different Line codes

Code:
clc; axis([1 10 -1.5 1.5]);
close all; subplot(4,2,5);
clear all; plot(t,ami_nrz);
x=round(rand(1,10)) ylabel('AMI NRZ');
nx=length(x); hold on;
sign=1; grid on;
for i=1:nx axis([1 10 -1.5 1.5]);
t = i:0.001:i+1-0.001; subplot(4,2,2);
if x(i)==1 plot(t,unipolar_rz);
unipolar_nrz=square(t*2*pi,100); ylabel('unipolar RZ');
bipolar_nrz=square(t*2*pi,100); hold on;
ami_nrz=sign*square(t*2*pi,100); grid on;
unipolar_rz=(1+square(t*2*pi,50))/2; axis([1 10 -1.5 1.5]);
polar_rz=(1+square(t*2*pi,50))/2; subplot(4,2,4);
ami_rz=sign*(1+square(t*2*pi,50))/2; plot(t,polar_rz);
nrz_m=sign*square(2*pi*t,100); ylabel('polar RZ');
sign=sign*-1; hold on;
manchester_code=square(t*2*pi,50); grid on;
else axis([1 10 -1.5 1.5]);
unipolar_nrz=0; subplot(4,2,6);
bipolar_nrz=-square(t*2*pi,100); plot(t,ami_rz);
ami_nrz=0; ylabel('AMI RZ');
unipolar_rz=0; hold on;
polar_rz=-(1+square(t*2*pi,50))/2; grid on;
ami_rz=0; axis([1 10 -1.5 1.5]);
manchester_code=-square(t*2*pi,50); subplot(4,2,8);
end plot(t,manchester_code);
subplot(4,2,1); ylabel('manchester code');
plot(t,unipolar_nrz); hold on;
ylabel('unipolar NRZ'); grid on;
hold on; axis([1 10 -1.5 1.5]);
grid on; subplot(4,2,7);
axis([1 10 -1.5 1.5]); plot(t,nrz_m);
subplot(4,2,3); ylabel('NRZ-M code');
plot(t,bipolar_nrz); hold on;
ylabel('bipolar NRZ'); grid on;
hold on; axis([1 10 -1.5 1.5]);
grid on; end
Output:
x= 1 1 0 1 1 0 0 1 1 1

2
Experiment 3

Aim: To study the Pulse Code Modulation and Demodulation


Code:
clc;
close all; % Encoding Process
clear all; figure
n=input(‘PCM system bits required :’ ); enc=de2bi(ind,'left-msb');
fs=input('Sampling rate:');
L=2^n; % Convert the decimal to binary
t=0:1/fs:2; k=1;
for i=1:l_1
s=8*sin(2*pi*t); for j=1:n
subplot(3,1,1); coded(k)=enc(i,j);
plot(t,s); % convert code matrix to a coded row vector
title('Analog Signal'); k=k+1;
ylabel('Amplitude--->'); end
xlabel('Time--->'); end
subplot(3,1,2);
stem(t,s); subplot(3,1,1); grid on;
grid on; stairs(0:(length(t)*n)-1,coded);
title('Sampled Signal'); % Display the encoded signal
ylabel('Amplitude--->'); axis([0 (length(t)*n)-1 -0.5 1.5]);
xlabel('Time--->'); title('Encoded Signal');
ylabel('Amplitude--->');
% Quantization Process xlabel('Time--->');
vmax = max(s);
vmin = min(s); % Demodulation Of PCM signal
del = (vmax-vmin)/L; k=1;
for i=1:l_1
part=vmin:del:vmax; for j=1:n
% level are between vmin and vmax with quant(i,j)=coded(k);
difference of del % convert coded row vector to code matrix
k=k+1;
code=vmin-(del/2):del:vmax+(del/2); end
% Contaion Quantized valuses end

[ind,q]=quantiz(s,part,code); index=bi2de(quant','left-msb');
% Quantization process
% ind contain index number and q contain % Getback the index in decimal form
quantized values q=del*index+vmin+(del/2);

l_1=length(ind); % getback Quantized values


[n d]=butter(5,0.5);
for i=1:l_1 de=filter(n,d,q);
if(ind(i)~=0)
% To make index as binary decimal so subplot(3,1,2); grid on;
started from 0 to N stairs(t,q);
ind(i)=ind(i)-1; % Plot Demodulated signal
end title('Demodulated Signal');
end ylabel('Amplitude--->');
xlabel('Time--->');
subplot(3,1,3);
stairs(t,q); subplot(3,1,3);
grid on; plot(t,de);
% Display the Quantize values title('Reconstructed Analog Signal');
title('Quantized Signal'); ylabel('Amplitude--->');
ylabel('Amplitude--->'); xlabel('Time--->');
xlabel('Time--->');

3
Output:

PCM system bits required : 4


Sampling Rate: 20

Figure 1

Figure 2

4
Experiment 5

Aim: To study Delta Modulation and Demodulation

Code:
clc; subplot(2,1,2);
clear all; stairs(t,d);
close all; ylim([-0.2 1.2]);
a=2; title(‘Encoded Bit stream’);
t=0:1/100:1; r=0;
x=a*sin(2*pi*t); for i=1:length(d)
l=length(x); if d(i)==0
delta=input('Required Step Size: '); r(i+1)=r(i)-delta;
%variation of this step size results else
%in the problems of delta modulation r(i+1)=r(i)+delta;
%like granular noise and slope end
%overload noise leading to improper end
%reconstruction [p, q] = butter(2,1/20);
xn=0; rec = filter(p,q,r);
for i=1:l figure;
if x(i)>=xn(i) subplot(2,1,1);
d(i)=1; stairs(t,r(1:l));
xn(i+1)=xn(i)+delta; hold on
else plot(t,x,'--');
d(i)=0; legend('Recovered
xn(i+1)=xn(i)-delta; approximation','Original Signal');
end title('DELTA MODULATION /
end DEMODULATION ')
subplot(2,1,1); subplot(2,1,2);
stairs(t,xn(1:l)); plot(t,rec(1:l));
title('Staircase approximation') title('Recovered Analog waveform');

Output:

Required Step Size: pi/2

5
Required Step Size: pi/25

Required Step Size: pi/50

6
Experiment 6

Aim: To plot and analyse the PDFs of Gaussian, Uniform and Binomial Random Variables
Code:
clc; else
clear all; U(i) = 0;
close all; end
% Gaussian end
display('Gaussian Random Variable'); figure(2);
u=input('Mean : '); plot(x,U);
v=input('variance : '); axis([a-5 b+5 -0.5 1])
x = -10: 0.001: 10; title(‘Uniform PDF’);
N = (1/(sqrt(2*pi*v)))*exp(-(x-u).^2/(2*v)); % Binomial
figure(1); display('Binomial Distribution');
plot(x,N); p = input('Probability of event success : ');
title(‘Gaussian PDF’); n = input('Number of test cases : ');
% Uniform for k=0:1:n
display('Uniform Random Variable') nCr =
a = input('minimum value of RV : '); factorial(n)/(factorial(k)*factorial(n-k));
b = input('maximum value of RV : '); B(k+1) = nCr*(p^k)*((1-p)^(n-k));
x = a-5:0.001:b+5; end
for i=1:length(x) figure(3);
if x(i)>= a && x(i)<=b stem(0:n,B);
U(i) = 1/(b-a); title(‘Binomial PDF’);

Output:

Gaussian Random Variable


Mean : 0
variance : 1
Unifomr Random Variable
minimum value of RV : -2
maximum value of RV : 2
Binomial Distribution
Probability of event success : 0.25
Number of test cases : 10

7
Experiment 7

Aim: To simulate the response of a Matched filter using MATLAB

Code:
% A template is given
temp = randn(100,1);

% Create a matched filter based on the template


b = flipud(temp(:));

% For testing the matched filter, create a random signal which


% contains a match for the template at some time index
x = [randn(200,1); temp(:); randn(300,1)];
n = 1:length(x);

% Process the signal with the matched filter


y = filter(b,1,x);

% Set a detection threshold (exmaple used is 90% of template)


thresh = 0.9;

% Compute normalizing factor


u = temp.'*temp;

% Find matches
matches = n(y>thresh*u);

% Plot the results


plot(n,y,'b', n(matches), y(matches), 'ro');
title('Response of a Matched Filter')
% Print the results to the console
display(matches);

Output:

matches = 300

8
Experiment 8

Aim: To study Binary ASK (On Off Keying) modulation

Code:
clc
clear all
close all
x=round(rand(1,10));
t=0:0.001:0.999;
s=sin(2*pi*t);
ask=[];
for i=1:10
if x(i)==1
ask=[ask s];
else
ask=[ask zeros(size(s))];
end
end
subplot(211)
stairs(0:10,[x x(10)]);
axis([0,10,-0.2,1.2]);
subplot(212)
plot(0:0.001:9.999,ask);

Output

x= 1 0 1 1 0 0 1 1 1 1

9
Experiment 9

Aim: To study Binary Frequency Shift Keying

Code:
clc
clear all
close all
x=round(rand(1,10));
t=0:0.001:0.999;
s0=sin(2*pi*2*t);
s1=sin(2*pi*5*t)
fsk=[];
for i=1:10
if x(i)==1
fsk=[fsk s1];
else
fsk=[fsk s0];
end
end
subplot(211)
stairs(0:10,[x x(10)]);
axis([0,10,-0.2,1.2]);
subplot(212)
plot(0:0.001:9.999,fsk);

Output:

x= 0 0 1 0 1 1 1 1 0 0

10
Experiment 10

Aim: To study Binary Phase Shift Keying

Code:
clc
clear all
close all
x=round(rand(1,10))
t=0:0.001:0.999;
s=sin(2*pi*t);
psk=[];
for i=1:10
if x(i)==1
psk=[psk s];
else
psk=[psk -s];
end
end
subplot(211)
stairs(0:10,[x x(10)]);
axis([0,10,-0.2,1.2]);
subplot(212)
plot(0:0.001:9.999,psk);

Output:

x= 1 1 0 0 1 0 0 0 0 1

11
Experiment 12

Aim: To study Quaternary Phase Shift Keying

Code:
clear all
close all
clc

b=round(rand(1,12));
t=0:1/60:2-1/60;
s00=cos(2*pi*t+3*pi/4);
s01=cos(2*pi*t+5*pi/4);
s10=cos(2*pi*t+pi/4);
s11=cos(2*pi*t+7*pi/4);
qpsk_1=[];
for i=1:2:12
if [b(i) b(i+1)] == [0 0]
qpsk_1 = [qpsk_1 s00];
elseif [b(i) b(i+1)] == [0 1]
qpsk_1 = [qpsk_1 s01];
elseif [b(i) b(i+1)] == [1 0]
qpsk_1 = [qpsk_1 s10];
elseif [b(i) b(i+1)] == [1 1]
qpsk_1 = [qpsk_1 s11];
end
end
n=length(b);
subplot(2,1,1);
stairs(0:12,[b b(12)]);
subplot(2,1,2);
tb=0:1/60:12-1/60;
plot(tb, qpsk_1);

Output:

b = 1 0 1 0 0 1 1 0 1 1 0 0

12

You might also like