%Title : DPSK modulation and Demodulation
clear all
close all
clc
% Transmitter
bitstream = randn(1,10) > 0 % Generating a
bitstream to be transmitted
dk = zeros(1,length(bitstream));
dk(1,1) = not(xor(bitstream(1,1),1)); % Generating DPSK of
the first bit with 1
for i = 2:length(bitstream)
dk(1,i) = not(xor(bitstream(1,i),dk(1,i-1))); % Generating
DPSK of rest of the bitstream
end
% To create the bkline code
repeat = ones(1,100);
linecodebk = bitstream(:) * repeat; %
Replicating each bit 100 times by taking tensor prod (will be
present in a row).
linecodebk = linecodebk.'; % Placing the
replicated bits columnwise.
linecodebk = linecodebk(:)'; % Placing all
the columns one below the other and taking transpose.
linecodebk = (2.* linecodebk) - ones(1,length(linecodebk));
% Converting to a Polar Linecode
%Plotting the bklinecode
t = 0:(100*length(bitstream)-1); %Creating time axis.
subplot(2,2,1)
plot(t,linecodebk)
xlabel('Time(s)')
ylabel('Linecoded bk input')
% To create the dkline code
repeat = ones(1,100);
linecode = dk(:) * repeat;
% Replicating each bit 100 times by taking tensor prod (will be
present in a row).
linecode = linecode.';
% Placing the replicated bits columnwise.
linecode = linecode(:)';
% Placing all the columns one below the other and taking
transpose.
linecode = (2.* linecode) - ones(1,length(linecode));
% Converting to a Polar Linecode
%Plotting the linecode
t = 0:(100*length(bitstream)-1); %Creating time axis.
subplot(2,2,2)
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');
y = (buffer(rece,100)'); %buffer(array,b):Converts array to a
matrix with No.of rows specified by b,places 1st b values in 1st
col.
x = [cos(2*pi*n*(0:99)/100);y((1:9),:)]; % Creating a delayed
version of y.
mult = (x .* y); % Elementwise mul of
y with a delayed version of itself.
decoded = (sum(mult,2) > 0)' % Summing rowwise
and then taking complement.
repeat = ones(1,100);
decodedlinecode = decoded(:) * repeat; %
Replicating each bit 100 times by taking tensor prod (will be
present in a row).
decodedlinecode = decodedlinecode.'; %
Placing the replicated bits columnwise.
decodedlinecode = decodedlinecode(:)'; %
Placing all the columns one below the other and taking
transpose.
decodedlinecode = (2.* decodedlinecode) -
ones(1,length(decodedlinecode)); % Converting to a Polar
Linecode
subplot(2,2,4)
plot(t,decodedlinecode)
xlabel('Time');
ylabel('Decoded input');
%Title : Linecodes.
clear all
close all
clc
%Line codes
% NRZ
N = input('Enter the length of bitstream');
bitstream = rand(1,N) > 0.5
timeperiod = input('Enter the timeperiod (No. of
samples)');%samples
time = 0:(timeperiod*length(bitstream)-1); %Time period
= 100 samples.
repeat = ones(1,timeperiod);
NRZP = bitstream.' * repeat;
NRZP = NRZP.';
NRZP = NRZP(:)';
%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
RZP = NRZP .* intermediate;
subplot(2,2,4)
plot(time,RZP);axis([0,10*timeperiod,-1.5,1.5]);
title('RZ Polar')
xlabel('Time(s)')
ylabel('Voltage(V)')
%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
% ind contain index number and q contain quantized values
l1=length(ind);
l2=length(q);
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--->');
% Demodulation Of PCM signal
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));
evenline = evenstream.' * repeat;
evenline = evenline.';
evenline = evenline(:)';
evenline = (2.* evenline) - ones(1,length(evenline));
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
de_mod_basis1 = modulated_sig .* basis1;
de_mod_basis2 = modulated_sig .* basis2;
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).';
de_mod_basis1 = modulated_buf .* basis1_buf;
de_mod_basis2 = modulated_buf .* basis2_buf;
demodulated_odd = (sum(de_mod_basis1,2) > 0).';
demodulated_odd = demodulated_odd(1:2:end)
demodulated_even = (sum(de_mod_basis2,2) > 0).';
demodulated_even = demodulated_even(1:2:end)
demodulated_op = [demodulated_odd;demodulated_even];
demodulated_op = demodulated_op(:).'
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])
%% Unipolar Raised Cosine
beta = 0.5; % try with different values of
beta from 0 to 1 (roll-off factor)
T = 1;
t = linspace(0,(1+beta)/2/T,50); %linearly spaced vectors
(1x50) vectors
h = 1*(t<((1-beta)/2/T)) + (0.5*(1+cos(pi*T/beta*(t-(1-
beta)/2/T)))) .* (t>=((1-beta)/2/T));
t = [-fliplr(t(2:end)),t]; %Flip matrix in left/right
direction
h = [fliplr(h(2:end)),h];
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]);
%% Polar Raised Cosine
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]);
%% Polar Half Sinusoid
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
N = input('Enter the length of bitstream');
bitstream = rand(1,N) > 0.5
timeperiod = input('Enter the timeperiod (No. of
samples)');%samples
time = 0:(timeperiod*length(bitstream)-1); %Time period
= 100 samples.
repeat = ones(1,timeperiod);
NRZP = bitstream.' * repeat;
NRZP = NRZP.';
NRZP = NRZP(:)';
NRZP = (2.*NRZP) - ones(1,length(NRZP));
figure()
plot(time,NRZP);axis([0,10*timeperiod,-1.5,1.5]);
title('NRZ Polar')
xlabel('Time(s)')
ylabel('Voltage(V)')
eyediagram(NRZP,N);