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

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; ...