Thursday 10 November 2022

Verilog Code for Universal Shift Register


 Universal Shift Register

module universalshift (clr,clk,sel,in,out);

input clr,clk; input [1:0]sel; input [3:0]parin; output reg[3:0]out; always @(posedge clk) begin if(clr) out=4'b0000; else begin case(sel) 2'b00: out=out; 2'b01: out={in[0],in[3:1]}; 2'b10: out={in[2:0],in[3]}; 2'b11: out=in; endcase end end endmodule

Wednesday 9 November 2022

Communication Systems - Convolutional Code

 clear ;

close ;
clc ;
g1 = input ('Enter the input Top Adder Sequence:= ' )
g2 = input ('Enter the input Bottom Adder Sequence:= ')
m = input ('Enter the message sequence:= ')
x1 = round ( convol (g1 ,m));
x2 = round ( convol (g2 ,m));
x1 = modulo (x1 ,2);
x2 = modulo (x2 ,2);
N = length (x1);
for i =1: length (x1)
x(i ,:) =[x1(N-i +1) ,x2(N-i +1)];
end
x = string (x)
disp (x, ' x')

Communication Systems - Hamming Code - Syndrome

 clc;

D=poly(0,'D');
g=1+D+0+D^3;// g e n e r a t o r polynomial
C1=0+D+D^2+D^3+0+0+D^6;// e r r o r f r e ecodeword
C2=0+D+D^2+0+0+0+D^6;//middl e b i t i s e r r o r
[r1,q1]=pdiv(C1,g);
S1=coeff(r1);
S1=modulo(S1,2);
disp(r1,' remainder in polynomial form ')
disp(S1,' Syndrome bits for error free codeword')
[r2,q2]=pdiv(C2,g);
S2=coeff(r2);
S2=modulo(S2,2);
disp(r2,' remainder in polynomial form for errored codeword ')
disp(S2,' Syndrome bits for errored codeword  ')

Communication Systems - Hamming Code - Encoder

 clc;

close;
clear;
D = poly (0, 'D' );
g = 1+D+0+D ^3; // generator polynomial
m = (D^3) *(1+0+0+ D^3); //message sequence
[r,q] = pdiv (m,g);
p = coeff (r);
disp (r, ' remainder in polynomial form ' )
disp (p, ' Parity bits are : ' )
disp ( ' Table 8.3 Contents of the Shift Register in the Encoder of fig 8.7 for Message Sequence (1 0 0 1 ) ')
disp ( '--------------------------------------------------------------------')
disp ( ' Shift Input Register Contents ' )
disp ( '-----------------------------------------------' )
disp ( ' 1 1 1 1 0 ' )
disp ( ' 2 0 0 1 1 ' )
disp ( ' 3 0 1 1 1 ' )
disp ( ' 4 1 0 1 1 ' )
disp ( '--------------------------' )

Communication Systems - QPSK - Signal Constellations

 clear

clc;
close;
M =4;
i = 1:M;
y =cos((2*i -1) *%pi/4)-sin((2*i -1)*%pi/4)*%i;
annot = dec2bin([0:M-1],log2(M));
disp (y, 'coordinates of message points')
disp (annot,'dibits value')
figure;
a = gca();
a.data_bounds = [ -1 , -1;1 ,1];
a.x_location = "origin";
a.y_location = "origin";
plot2d (real (y (1) ),imag (y (1) ) ,-2)
plot2d (real (y (2) ),imag (y (2) ) ,-4)
plot2d (real (y (3) ),imag (y (3) ) ,-5)
plot2d (real (y (4) ),imag (y (4) ) ,-9)
xlabel ('In-Phase');
ylabel ('Quadrature');
title ('Constellation for QPSK')
legend (['message point 1 ( dibit 1 0 ) ' ; ' message point 2 ( dibit 0 0 ) ' ; ' message point 3 (dibit 0 1 ) ' ;
'message point 4 (dibit 1 1 ) '],5)

Communication Systems - BPSK - Signal Constellations

clear all;
clc ;
close ;
M =2;
i = 1:M;
y = cos (2* %pi +(i -1)* %pi);
annot = dec2bin ([length(y)-1: -1:0] ,log2(M));
disp (y, ' coordinates of message points ' )
disp (annot , 'Message points ' )
figure ;
a = gca ();
a.data_bounds = [ -2 , -2;2 ,2];
a.x_location = "origin";
a.y_location = "origin";
plot2d (real (y (1) ),imag (y (1) ) ,-9)
plot2d (real (y (2) ),imag (y (2) ) ,-5)
xlabel ('In-Phase');
ylabel ('Quadrature');
title (' Constellation for BPSK') 

legend ([ ' message point 1 ( binary 1) ' ; ' message point 2 ( binary 0) '] ,5) 

Communication System - QAM

 clear;

clc;
close;
T=3;//One Symbol period
t=0:0.01:T;// Sampling Matrix for one symbol period
f=1/T;// Carrier frequency (cycles per bit period)
I=[000001010011100101110111];//data stream giving tribits equivalent to 0,1,2,3,4,5,6,7
//Polar NRZ Converter
I_PNRZ=[]//empty matrix for Polar NRZ data
for n=1:length(I)
if I(n)==0 then
I_PNRZ=[I_PNRZ,-1]
else
I_PNRZ=[I_PNRZ,1]
end
end
I_Carrier=sqrt(2/T)*cos(2*3.14*f*t);// In phase carrier
Q_Carrier=sqrt(2/T)*sin(2*3.14*f*t);// Quadrature phase carrier
//Generation of 8-QAM Waveform
z=0;//Starting point of plot on x-axis
for n=1:3:length(I_PNRZ)
Q_Bit=I_PNRZ(n) //Set Q Bit Value
I_Bit=I_PNRZ(n)+1 //Set I Bit Value
C_Bit=I_PNRZ(n)+2//Set C Bit Value
if C_Bit==-1 then//Set PAM, Product of C with I or Q
QC=0.5*Q_Bit//Set half amplitude
IC=0.5*I_Bit//Set half amplitude
else
QC=Q_Bit//Set full amplitude
IC=I_Bit//Set full amplitude
end
subplot(3,1,1)//QC Plot
a=gca();
a.data_bounds=[0,-1.5;length(I_PNRZ),1.5];
a.x_location="origin";
a.grid=[1,1];
title('Q-PAM')
plot((t+z),Q_Carrier*QC);//Q_Carrier * Q-PAM (Q Balance Modulator)
plot((t+z),QC,'r');//Q-PAM Output
subplot(3,1,2)//IC Plot
a=gca();
a.data_bounds=[0,-1.5;length(I_PNRZ),1.5];
a.x_location="origin";
a.grid=[1,1];
title('I-PAM')
plot((t+z),I_Carrier*IC);//I_Carrier * I-PAM (I Balance Modulator)
plot((t+z),IC,'r');//I-PAM Output
subplot(3,1,3)//8-QAM Plot
a=gca();
a.data_bounds=[0,-1.5;length(I_PNRZ),1.5];
a.x_location="origin";
a.grid=[1,1];
title('8-QAM')
plot((t+z),(I_Carrier*IC)+(Q_Carrier*QC));//I-PAM + Q-PAM (Adder)
plot((t+z),I_Carrier,'r');//I Carrier for reference
plot(((t/3)+z),Q_Bit,'c');//Q Bit for reference
plot(((t/3)+1+z),I_Bit,'b');//I Bit for reference
plot(((t/3)+2+z),C_Bit,'m');//C Bit for reference
z=z+3;//Move starting point of plot on x-axis by 3 bits (1 symbol) period
end

Communication System - QPSK

 clear ;

clc ;
close ;
M =4;
i = 1:M;
t = 0:0.001:1;
for i = 1:M
s1(i ,:) = cos (2* %pi *2* t)*cos ((2*i -1) *%pi /4) ;
s2(i ,:) = -sin (2* %pi *2* t)*sin ((2*i -1) *%pi /4) ;
end
S1 =[];
S2 = [];
S = [];
Input_Sequence=[0 ,1 ,1 ,0 ,1 ,0 ,0 ,0];
m = [3 ,1 ,1 ,2];
for i =1: length (m)
S1 = [S1 s1(m(i) ,:) ];
S2 = [S2 s2(m(i) ,:) ];
end
S = S1+S2;
figure
subplot (3 ,1 ,1)
a = gca ();
a.x_location = "origin";
plot (S1)
title ('Binary PSK wave o f Odd-numbered bits of input sequence')
subplot (3 ,1 ,2)
a = gca ();
a.x_location = "origin";
plot (S2)
title ('Binary PSK wave o f Even-numbered bits of input sequence')
subplot (3 ,1 ,3)
a = gca ();
a.x_location = "origin";
plot (S)
title ('QPSK waveform ')

Communication Systems - Hamming Code

 clc ;

clear all;

code1 = input ('Enter the 1st Code Word ') ;
code2 = input ('Enter the 2nd Code Word ') ;
Hamming_Distance = 0;
for i = 1:length (code1)
 Hamming_Distance = Hamming_Distance + bitxor(code1(i),code2(i)) ;
end
disp (Hamming_Distance, 'Hamming Distance')

Communication Systems - DPSK

 clc ;

bk = [1 ,0 ,0 ,1 ,0 ,0 ,1 ,1]; // input digital sequence
for i = 1: length (bk)
if(bk(i) ==1)
bk_not (i) =~1;
else
bk_not (i)= 1;
end
end
dk_1 (1) = bool2s ( 1&bk (1) ); // initial value of differential encoded sequence
dk_1_not (1) = bool2s (0&bk_not(1));
dk (1) = bitxor ( dk_1 (1) ,dk_1_not (1)) // f i r s t b i t o f dpsk encoder
for i =2: length (bk)
dk_1 (i) = dk(i -1);
dk_1_not (i) = ~dk(i -1);
dk(i) = bitxor ( bool2s ( dk_1 (i)&bk(i)),bool2s (dk_1_not (i)&bk_not (i)));
end
for i =1: length (dk)
if(dk(i) ==1)
dk_radians (i)=0;
elseif (dk(i) ==0)
dk_radians (i)=%pi;
end
end
disp (' Table 7.3 Illustrating the Generation of DPSK Signal')
disp ( '' )
disp (bk , '( bk )')
bk_not = bk_not ';
disp (bk_not , ' (bk not)')
dk = dk ';
disp (dk , 'Differentially encoded sequence (dk)')
dk_radians = dk_radians ';
disp (dk_radians, 'Transmitted phase in radians')

Communication Systems - PSK

 clc;clear all;clf;

t=[0:0.01:5*%pi];
A=5;
fc=2;

Vm=A.*squarewave(t);
Vc=A.*sin(fc.*t);
Vp= Vm.*Vc;

subplot(3,1,1);
plot(t,Vm, 'red');
xlabel("Time")
ylabel("Amplitude of Message Signal")

subplot(3,1,2);
plot(t,Vc, 'green');
xlabel("Time")
ylabel("Amplitude of Carrier Signal")

subplot(3,1,3);
plot(t,Vp, 'blue');
xlabel("Time")
ylabel("Amplitude of PSK Signal")

Communication Systems - ASK

 

clc;clear all;clf;
t=[0:0.02:5*%pi];
fc=10;
A=1;

Vm=squarewave(t,40);

Vc=A/2.*cos(fc.*t);
Va=(1+Vm).*(Vc);

subplot(3,1,1);
plot(t,Vm,'red');
xlabel("Time")
ylabel("Amplitude of Message Signal")

subplot(3,1,2);
plot(t,Vc, 'green');
xlabel("Time")
ylabel("Amplitude of Carrier Signal")

subplot(3,1,3);
plot(t,Va, 'blue');
xlabel("Time")
ylabel("Amplitude of ASK Signal")

Communication Systems - FSK

clc; clear all; clf;
t=[0:0.01:4.4*%pi];
A=5;
wc=5;

Vm=A.*squarewave(t);
Vc=A.*cos(wc.*t);
fc=wc/(2*%pi);

subplot(3,1,1);
plot(t,Vm, 'red');
xlabel("Time")
ylabel("Amplitude of Message Signal")

subplot(3,1,2);
plot(t,Vc, 'green');
xlabel("Time")
ylabel("Amplitude of Carrier Signal")

fd=0.5; //frequency deviation
subplot(3,1,3);
Vf=A.*cos(2.*%pi.*(fc+Vm.*fd).*t);
plot(t,Vf, 'blue');
xlabel("Time")
ylabel("Amplitude of FSK Signal")

Verilog Code for Universal Shift Register

  Universal Shift Register module universalshift (clr,clk,sel,in,out); input clr,clk; input [1:0]sel; input [3:0]parin; output reg[3:0]out; ...