Matlab Codes
Matlab Codes
clear all
close all
clc
% Transmitter
plot(t,linecode)
xlabel('Time');
ylabel('Linecoded input');
n = 5;
% No. of cycles in one time period (100 samples = 1 time
Period).
cosine = cos(2*pi*n*t/100);
% subplot(3,1,2)
% plot(t,cosine)
trans = (linecode.*cosine);
% Multiplying Line code with cosine wave.
subplot(2,2,3)
plot(t,trans)
xlabel('time');
ylabel('Transmitted signal');
%---------------------------------------------------------------
---------
% Receiver
rece = trans;
% Received signal = Transmitted signal.
% subplot(3,1,3)
% xlabel('Time');
% ylabel('Received signal');
clear all
close all
clc
%Line codes
% NRZ
%Unipolar
NRZU = NRZP;
figure(1)
subplot(2,2,1)
plot(time,NRZU);axis([0,10*timeperiod,-1.5,1.5]);
title('NRZ Unipolar')
xlabel('Time(s)')
ylabel('Voltage(V)')
%Polar
NRZP = (2.*NRZP) - ones(1,length(NRZP));
subplot(2,2,2)
plot(time,NRZP);axis([0,10*timeperiod,-1.5,1.5]);
title('NRZ Polar')
xlabel('Time(s)')
ylabel('Voltage(V)')
%RZ
intermediate = ones(1,length(bitstream)*2);
intermediate(2:2:length(intermediate)) = 0;
intermediate = intermediate.' * ones(1,(timeperiod/2));
intermediate = intermediate.';
intermediate = intermediate(:)';
%Unipolar
RZU = NRZU .* intermediate;
subplot(2,2,3)
plot(time,RZU);axis([0,10*timeperiod,-1.5,1.5]);
title('RZ Unipolar')
xlabel('Time(s)')
ylabel('Voltage(V)')
%Polar
%Power Spectrum
freq = linspace(-timeperiod/2,timeperiod/2,length(NRZP));
NRZUK = ((abs(fftshift(fft(NRZU)))));
NRZPK = ((abs(fftshift(fft(NRZP)))));
RZUK = ((abs(fftshift(fft(RZU)))));
RZPK = ((abs(fftshift(fft(RZP)))));
NRZUK = NRZUK/max(NRZUK);
NRZPK = NRZPK/max(NRZPK);
RZUK = RZUK/max(RZUK);
RZPK = RZPK/max(RZPK);
figure(2)
subplot(221)
plot(freq,NRZUK); axis([-5,5,0,1.2]);
xlabel('frequency(f)')
ylabel('S(X(f))')
title('NRZ Unipolar')
% figure(3)
subplot(222)
plot(freq,NRZPK); axis([-5,5,0,1.2]);
xlabel('frequency(f)')
ylabel('S(X(f))')
title('NRZ Polar')
% figure(4)
subplot(223)
plot(freq,RZUK); axis([-5,5,0,1.2]);
xlabel('frequency(f)')
ylabel('S(X(f))')
title('RZ Unipolar')
% figure(5)
subplot(224)
plot(freq,RZPK); axis([-5,5,0,1.2]);
xlabel('frequency(f)')
ylabel('S(X(f))')
title('RZ Polar')
% Code for Pulse Code Modulation
clc;
close all;
clear all;
n=input('Enter n value for n-bit PCM system : ');
n1=input('Enter number of samples in a period : ');
L=2^n;
% % Signal Generation
% x=0:1/100:4*pi;
% y=8*sin(x); % Amplitude Of signal
is 8v
% subplot(2,2,1);
% plot(x,y);grid on;
% Sampling Operation
x=0:2*pi/n1:4*pi; % n1 nuber of samples have tobe
selected
s=8*sin(x);
subplot(3,1,1);
plot(s);
title('Analog Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
subplot(3,1,2);
stem(s);grid on; title('Sampled Sinal'); ylabel('Amplitude---
>'); xlabel('Time--->');
% Quantization Process
vmax=8;
vmin=-vmax;
del=(vmax-vmin)/L;
part=vmin:del:vmax; % level
are between vmin and vmax with difference of del
code=vmin-(del/2):del:vmax+(del/2);
% Contaion Quantized valuses
[ind,q]=quantiz(s,part,code); %
Quantization process
for i=1:l1
if(ind(i)~=0) %
To make index as binary decimal so started from 0 to N
ind(i)=ind(i)-1;
end
i=i+1;
end
for i=1:l2
if(q(i)==vmin-(del/2)) % To make
quantize value inbetween the levels
q(i)=vmin+(del/2);
end
end
subplot(3,1,3);
stem(q);grid on; %
Display the Quantize values
title('Quantized Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
% Encoding Process
figure
code=de2bi(ind,'left-msb');
% Cnvert the decimal to binary
k=1;
for i=1:l1
for j=1:n
coded(k)=code(i,j);
% convert code matrix to a coded row vector
j=j+1;
k=k+1;
end
i=i+1;
end
subplot(2,1,1); grid on;
stairs(coded); % Display the
encoded signal
axis([0 100 -2 3]); title('Encoded Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
qunt=reshape(coded,n,length(coded)/n);
index=bi2de(qunt','left-msb'); % Getback the
index in decimal form
q=del*index+vmin+(del/2); % getback
Quantized values
subplot(2,1,2); grid on;
plot(q);
% Plot Demodulated signal
title('Demodulated Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
%Title : QPSK modulation and Demodulation
%Modulation
bitstream = randi([0,1],1,10)
% bitstream = [1 0 0 0 0 1 1 1]
oddstream = bitstream(1:2:end)
evenstream = bitstream(2:2:end)
repeat = ones(1,100);
linecode = bitstream.' * repeat;
linecode = linecode.';
linecode = linecode(:)';
linecode = (2.* linecode) - ones(1,length(linecode));
repeat = ones(1,2*100);
oddline = oddstream.' * repeat;
oddline = oddline.';
oddline = oddline(:)';
oddline = (2.* oddline) - ones(1,length(oddline));
t = 0:(100*length(bitstream)-1);
n = 4;
basis1 = cos(2*pi*n*t/100);
basis2 = sin(2*pi*n*t/100);
bpsk1 = (oddline.*basis1);
bpsk2 = (evenline.*basis2);
modulated_sig = bpsk1 + bpsk2;
figure(1)
subplot(311)
plot(t,linecode)
xlabel('time (s)')
ylabel('Voltage (V)')
title('Linecode')
% subplot(512)
% plot(t,oddline)
% subplot(513)
% plot(t,evenline)
subplot(312)
plot(t,basis1)
xlabel('time (s)')
ylabel('Voltage (V)')
title('Basis1(Cosine)')
subplot(313)
plot(t,basis2)
xlabel('time (s)')
ylabel('Voltage (V)')
title('Basis2(Sine)')
figure(2)
subplot(311)
plot(t,bpsk1)
xlabel('time (s)')
ylabel('Voltage (V)')
title('Bpsk1')
subplot(312)
plot(t,bpsk2)
xlabel('time (s)')
ylabel('Voltage (V)')
title('Bpsk2')
subplot(313)
plot(t,modulated_sig)
xlabel('time (s)')
ylabel('Voltage (V)')
title('Modulated Signal')
%De-Modulation
figure(3)
subplot(311)
plot(t,de_mod_basis1)
subplot(312)
plot(t,de_mod_basis2)
modulated_buf = buffer(modulated_sig,100).';
basis1_buf = buffer(basis1,100).';
basis2_buf = buffer(basis2,100).';
repeat = ones(1,100);
demodulated_oplinecode = demodulated_op.' * repeat;
demodulated_oplinecode = demodulated_oplinecode.';
demodulated_oplinecode = demodulated_oplinecode(:)';
demodulated_oplinecode = (2.* demodulated_oplinecode)
- ones(1,length(demodulated_oplinecode));
subplot(313)
plot(t,demodulated_oplinecode)
%To plot the scatterplot.
scatterplot([-0.707,-0.707;-0.707,0.707;0.707,-
0.707;0.707,0.707])
bk = rand(1,100)>0.5;
x = bk'*h;
x = x';
x = x(:)';
t = 0:1/100:98+99/100;
subplot(211);plot(t,x); axis([0,10,-1,1]);
X = abs(fftshift(fft(x)));
X = X/max(X);
f = linspace(-50,50,length(X));
subplot(212);plot(f,X);axis([-5,5,0,1]);
bk = 2*bk-1;
x = bk'*h;
x = x';
x = x(:)';
t = 0:1/100:98+99/100;
figure();
subplot(211);plot(t,x); axis([0,10,-1,1]);
X = abs(fftshift(fft(x)));
X = X/max(X);
f = linspace(-50,50,length(X));
subplot(212);plot(f,X);axis([-5,5,0,1]);
close all;
%% Unipolar Half-sinusoid
bk = rand(1,100)>0.5;
x = bk'*sin(2*pi/200*[0:99]);
x = x';
x = x(:)';
t = 0:1/100:100-1/100;
subplot(211);plot(t,x);axis([0,10,-1,1]);
X = abs(fftshift(fft(x)));
X = X/max(X);
f = linspace(-50,50,length(X));
subplot(212);plot(f,X);axis([-5,5,0,1]);
bk = 2*bk-1;
x = bk'*sin(2*pi/200*[0:99]);
x = x';
x = x(:)';
t = 0:1/100:100-1/100;
figure();
subplot(211);plot(t,x);axis([0,10,-1,1]);
X = abs(fftshift(fft(x)));
X = X/max(X);
f = linspace(-50,50,length(X));
subplot(212);plot(f,X);axis([-5,5,0,1]);
%Title : Linecodes.
%_______________________________________________________________
___________
clear all
close all
clc
%Line codes
% NRZ