Académique Documents
Professionnel Documents
Culture Documents
BPSK MODULATOR:
%MATLAB Script for a Binary PSK with two Phases % Clear all variables and close all figures clear all; close all; % The number of bits to send - Frame Length N=input('enter the number of bits to be modulated : N = '); % Generate a random bit stream bit_stream = round(rand(1,N)); % Enter the two Phase shifts - in Radians % Phase for 0 bit P1 = 0; % Phase for 1 bit P2 = pi; % Frequency of Modulating Signal f = 2; %f --> time period % Sampling rate of sine wave - This will define the resolution fs = 100; % Time for one bit t = 0: 1/fs : 1; % This time variable is just for plot time = []; PSK_signal = []; Digital_signal = []; carrier_signal = []; for ii = 1: 1: length(bit_stream) % The Original Digital Signal bit0 = (bit_stream(ii)==0)*zeros(1,length(t)); bit1 = (bit_stream(ii)==1)*ones(1,length(t)); Digital_signal = [Digital_signal (bit0+bit1)]; % The FSK Signal bit0 = (bit_stream(ii)==0)*sin(2*pi*f*t + P1); bit1 = (bit_stream(ii)==1)*sin(2*pi*f*t + P2); PSK_signal = [PSK_signal (bit0+bit1)];
% Generating carrier wave carrier_signal = [carrier_signal (sin(2*pi*f*t))]; time = [time t]; t = t + 1; end % Plot the Original Digital Signal subplot(3,1,1); plot(time,Digital_signal,'r','LineWidth',2); xlabel('Time (bit period)'); ylabel('Amplitude'); title('Original Digital Signal'); axis([0 time(end) -0.5 1.5]); grid on; % Plot the carrier Signal subplot(3,1,2); plot(time,carrier_signal,'g','LineWidth',2); xlabel('Time (bit period)'); ylabel('Amplitude'); title('carrier Signal'); axis([0 time(end) -1.5 1.5]); grid on; % Plot the PSK Signal subplot(3,1,3); plot(time,PSK_signal,'LineWidth',2); xlabel('Time (bit period)'); ylabel('Amplitude'); title('PSK Signal with two Phase Shifts'); axis([0 time(end) -1.5 1.5]); grid on;
end time = [time t]; t = t + 1; end % Plot the input BPSK Signal subplot(3,1,1); plot(time,BPSK_signal,'LineWidth',2); xlabel('Time (bit period)'); ylabel('Amplitude'); title('BPSK Signal with two Phase Shifts'); axis([0 time(end) -2.5 2.5]); grid on; % Plot the carrier Signal subplot(3,1,2); plot(time,carrier_signal,'g','LineWidth',2); xlabel('Time (bit period)'); ylabel('Amplitude'); title('carrier Signal'); axis([0 time(end) -1.5 1.5]); grid on; %Plot the Original Digital Signal subplot(3,1,3); plot(time,Digital_signal,'r','LineWidth',2); xlabel('Time (bit period)'); ylabel('Amplitude'); title('Original Digital Signal'); axis([0 time(end) -0.5 1.5]); grid on;
QPSK MODULATION: Matlab code: %MATLAB Script for a Binary PSK with two Phases % Clear all variables and close all figures clear all; close all; % The number of bits to send - Frame Length N=input('enter the number of bits to be modulated : N = '); % Generate a random bit stream bit_stream = round(rand(1,N)); % 4 PHASE SHIFTS P1 = pi/4; %45degrees phase shift P2 = 3/4*pi; %135 degrees phase shift P3 = 5/4*pi; %225 degree phase shift P4 = 7/4*pi; %315 degree phase shift % Frequency of Modulating Signal f = 1; %f --> time period % Sampling rate of sine wave - This will define the resoultion fs = 100;
% Time for one bit t = 0: 1/fs : 1; % This time variable is just for plot time = []; QPSK_signal = []; Digital_signal = []; carrier_signal=[]; for ii = 1: 2: length(bit_stream) jj = ii + 1; %Code for generation of Original Digital Signal Digital_signal = [Digital_signal (bit_stream(ii)==0)*zeros(1,length(t)) + (bit_stream(jj)==1)*ones(1,length(t)) ]; %Code for generation of carrier signal carrier_signal=[carrier_signal (sin(2*pi*f*t))]; %Code for genearting QPSK signal modulated signal if bit_stream(ii)==0 if bit_stream(jj)==0 bit00 = (bit_stream(ii)==0)*sin(2*pi*f*t + P1); QPSK_signal = [QPSK_signal (bit00)];
else bit0 = (bit_stream(ii)==0)*sin(2*pi*f*t + P2); bit1 = (bit_stream(jj)==0)*sin(2*pi*f*t + P2); QPSK_signal = [QPSK_signal (bit0+bit1) ]; end end if bit_stream(ii)==1 if bit_stream(jj)==0 bit1 = (bit_stream(ii)==0)*sin(2*pi*f*t + P3); bit0 = (bit_stream(jj)==0)*sin(2*pi*f*t + P3); QPSK_signal = [QPSK_signal (bit1+bit0) ]; else bit11 = (bit_stream(jj)==1)*sin(2*pi*f*t + P4); QPSK_signal = [QPSK_signal (bit11) ]; end end time = [time t]; t = t + 1; end
% Plot the Original Digital Signal subplot(3,1,1); plot(time,Digital_signal,'r','LineWidth',2); xlabel('Time (bit period)'); ylabel('Amplitude'); title('Original Digital Signal'); axis([0 8 -0.5 1.5]); grid on; % Plot the carrier Signal subplot(3,1,2); plot(time,carrier_signal,'g','LineWidth',2); xlabel('Time (bit period)'); ylabel('Amplitude'); title('carrier Signal'); axis([0 time(end) -1.5 1.5]); grid on; % Plot the QPSK Signal subplot(3,1,3);
plot(time, QPSK_signal,'LineWidth',2); xlabel('Time (bit period)'); ylabel('Amplitude'); title('QPSK Signal with two Phase Shifts'); axis([0 8 -1.5 1.5]); grid on;
QPSK DEMODULATOR: Matlab code: %MATLAB Script for a Binary PSK with two Phases format long; % Clear all variables and close all figures clear all; close all; a=input('enter the number of elements: N = '); N=a; % Generate a random bit stream bit_stream =round(rand(1,N)); % 4 PHASE SHIFTS
P1 = pi/4; %45degrees phase shift P2 = 3/4*pi; %135 degrees phase shift P3 = 5/4*pi; %225 degree phase shift P4 = 7/4*pi; %315 degree phase shift % Frequency of Modulating Signal f = 2; %f --> time period
% Sampling rate of sine wave - This will define the resoultion fs = 100; % Time for one bit t = 0: 1/fs : 1; % This time variable is just for plot time = []; QPSK_signal = []; Digital_signal = []; carrier_signal=[]; for ii = 1: 2: length(bit_stream) jj=ii+1; % Checking for input and carrier wave phase
if bit_stream(ii)==0 if bit_stream(jj)==0 bit00 = (bit_stream(ii)==0)*sin(2*pi*f*t + P1); QPSK_signal = [QPSK_signal (bit00)]; else bit0 = (bit_stream(ii)==0)*sin(2*pi*f*t + P2); bit1 = (bit_stream(jj)==0)*sin(2*pi*f*t + P2); QPSK_signal = [QPSK_signal (bit0+bit1) ]; end end if bit_stream(ii)==1 if bit_stream(jj)==0 bit1 = (bit_stream(ii)==0)*sin(2*pi*f*t + P3); bit0 = (bit_stream(jj)==0)*sin(2*pi*f*t + P3); QPSK_signal = [QPSK_signal (bit1+bit0) ]; else bit11 = (bit_stream(jj)==1)*sin(2*pi*f*t + P4); QPSK_signal = [QPSK_signal (bit11) ]; end
end % Generating carrier wave bit00 = ((bit_stream(ii)==0)*sin(2*pi*f*t)); bit11 = ((bit_stream(ii)==1)*sin(2*pi*f*t )); carrier_signal=[carrier_signal (bit00 + bit11)]; %genearting Digital wave if (QPSK_signal (bit_stream(ii)==0)& carrier_signal (bit_stream(ii)==0)) Digital_signal = [Digital_signal (bit_stream(ii)==0)*zeros(1,length(t))]; else Digital_signal = [Digital_signal (bit_stream(ii)==1)*ones(1,length(t))]; end time = [time t]; t = t + 1; end %Plot the Original Digital Signal subplot(3,1,1); plot(time,Digital_signal,'r','LineWidth',2); xlabel('Time (bit period)'); ylabel('Amplitude');
title('Original Digital Signal'); axis([0 time(end) -0.5 1.5]); grid on; % Plot the carrier Signal subplot(3,1,2); plot(time,carrier_signal,'g','LineWidth',2); xlabel('Time (bit period)'); ylabel('Amplitude'); title('carrier Signal'); axis([0 time(end) -1.5 1.5]); grid on; % Plot the input BPSK Signal subplot(3,1,3); plot(time,QPSK_signal,'LineWidth',2); xlabel('Time (bit period)'); ylabel('Amplitude'); title('QPSK Signal with two Phase Shifts'); axis([0 time(end) -2.5 2.5]); grid on;
temp=temp+parity_column; end disp('parity matrix: ') disp(parity_matrix) %generating identity matrix identity_matrix = eye(block_size) %creating generator matrix g_matrix=[ ]; g_matrix= horzcat(parity_matrix, identity_matrix) %generating codeword codewd_matrix=[]; codewd_matrix=msg_vectors*g_matrix; for i=1:1:no_of_msg_vectors for j=1:1:n if mod(codewd_matrix(i,j),2)==0 codewd_matrix(i,j)=0; else codewd_matrix(i,j)=1; end end end codewd_matrix %transmission of a code word selected_word=input('Enter the codeword to be transmitted'); trans_word=codewd_matrix(selected_word,:) copy_trans=trans_word; e_pos=input('Enter the bit position to be changed '); if(copy_trans(1,e_pos)==0) copy_trans(1,e_pos)=1; else
copy_trans(1,e_pos)=0; end %decoding the received word received_word=copy_trans parity_t=parity_matrix' identity_mat1=eye(n-block_size) hamming_matrix=[identity_mat1,parity_t] %Error Detection syndrome=received_word*hamming_matrix'; disp('Transpose of Hamming Matrix: ') disp(hamming_matrix') hamming_transpose=hamming_matrix'; for i=1:1:parity_column if mod(syndrome(i),2)==0 syndrome(i)=0; else syndrome(i)=1; end end syndrome for i=1:1:n if syndrome(1,:)==hamming_transpose(i,:) e_bit =i; %errored bit end end disp('The corrupted bit is ') e_bit %Error Correction if received_word(1,e_bit)==1 received_word(1,e_bit)=0; else received_word(1,e_bit)=1;
OUTPUT: Enter the length of codeword: 7 Enter the length of parity stream: 12 Enter the bits (zeros and ones): [1 1 0 0 1 1 1 1 1 0 0 1] Enter the block size (factor of length): 4 no_of_msg_vectors: 16 message vectors are: 0000 0001 0010 0011 0100 0101 0110 0111 1000
identity_matrix = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
g_matrix = 1 0 1 1 0 1 1 0 0 1 0 0 0 0
1 0
1 0
1 1
0 0
0 0
1 0
0 1
codewd_matrix = 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
trans_word = 0 1 0 0 1 0 1
parity_t = 1 1 0 0 1 1 1 1 1 0 0 1
identity_mat1 = 1 0 0 0 1 0 0 0 1
hamming_matrix = 1 0 0 0 1 0 0 0 1 1 1 0 0 1 1 1 1 1 0 0 1
1 0 0 1 0 1 0
0 1 0 1 1 1 0
0 0 1 0 1 1 1
syndrome = 1 1 0
M=[M;dec_bin]; %convert binary message to polynomial expression and store it n array m m=[m;poly2sym(dec_bin)]; end disp('polynomial expression for message word'); m %generator polynomial disp('coefficients of generator polynomial') G=cyclpoly(n,k)%[1 0 1 1] disp('generator polynomial expression :') g=poly2sym(G)%1+x+x^3 % initialise array to store codeword C=[]; %generation of polynomial codeword for i=1:(2^k) %multiply the message word with generator polynomial polynomial_codeword=expand(m(i,:)*g(1,:));%c=m(x).g(x) %store the codeword in an array C=[C;polynomial_codeword]; end
disp('polynomial codeword'); C %array to store binary codeword c=[]; codeword_coefficients=[]; disp('coefficients of codeword (deci)') for i=1:1:(2^k) %extraction of coefficients from the polynomial codeword codeword_coefficients =sym2poly(C(i)); %append zeros ,to make codeword length =n if(length( codeword_coefficients)<n+1) code_length=n-length(codeword_coefficients); z=zeros(1,code_length); %append 0s to the codeword codeword_coefficients=[z codeword_coefficients]; end c=[c; codeword_coefficients]; end c disp('coefficients of codeword (bin)')
%modulo 2 operation for i=1:1:(2^k) for j=1:1:n if mod(c(i,j),2)==0 c(i,j)=0; else c(i,j)=1;
end end end c disp('******* TRANSMITTING **********'); t=input('enter the code to be transmitted '); R=c(t,:) original_codeword=R; modify_bit=input ('enter the bit to be modified '); if ((R(1,modify_bit))==0) R(1,modify_bit)=1; else
R(1,modify_bit)=0; end disp('modified codeword'); disp(R); disp(' ****** RECEIVING AND DECODING *******') %parity check matrix H disp('parity check matrix H'); H=cyclgen(n,G) disp('syndrome matrix'); Syndrome=R*H';
H_transpose=H' for i=1:n if((Syndrome(1,:))==(H_transpose(i,:))) error_bit=i; end end disp('error bit is:'); disp(error_bit); disp('error pattern matrix') E=eye(n)%generate identity matrix of order n*n ei=[]; disp('error vector from error pattern matrix '); ei=E(error_bit,:) original_codeword Corrected_codeword=xor(R,ei)%c=e xor r OUTPUT OF NON SYSTEMATIC CYCLIC CODES: Non-Systematic Cyclic Code Enter the codeword length: 7 Enter the message length: 4 polynomial expression for message word
m= 0 1 x x+1 x^2 x^2+1 x^2+x x^2+x+1 x^3 x^3+1 x^3+x x^3+x+1 x^3+x^2 x^3+x^2+1 x^3+x^2+x x^3+x^2+x+1
generator polynomial expression : g= x^3+x+1 polynomial codeword C= 0 x^3+x+1 x^4+x^2+x x^4+x^2+2*x+x^3+1 x^5+x^3+x^2 x^5+2*x^3+x^2+x+1 x^5+x^3+2*x^2+x^4+x x^5+2*x^3+2*x^2+x^4+2*x+1 x^6+x^4+x^3 x^6+x^4+2*x^3+x+1 x^6+2*x^4+x^3+x^2+x x^6+2*x^4+2*x^3+x^2+2*x+1 x^6+x^4+2*x^3+x^5+x^2 x^6+x^4+3*x^3+x^5+x^2+x+1
syndrome matrix 0 1 1
H_transpose = 1 0 0 1 1 0 1 0 0 1 0 0 1 1 1
1 0
1 1
0 1
original_codeword = 0 1 0 1 1 0 0
Corrected_codeword = 0 1 0 1 1 0 0
clc; n = input('Enter the codeword length: '); disp (n); k=input('Enter the message length: '); disp (k ); M=[]; m=[]; %array to store binary message word %array to store polynomial message word % get message length % get codeword length
%get message word for i=0:(2^k-1) decimal_to_binary=de2bi(i,4,'left-msb'); %convert binary to decimal M=[M;decimal_to_binary]; %store the binary message word m=[m;poly2sym(decimal_to_binary)]; %store the polynomial message word end
disp('polynomial messageword'); m %X term.. x=[1 0 0 0];% x^(n-k) disp('numerator X term'); X=poly2sym(x); %generator polynomial-denominator Generator_polynomial=cyclpoly(n,k); disp('generator polynomial'); g=poly2sym(Generator_polynomial) numerator_polynomial=[]; %array to store polynomial numerator %numerator in polynomial for i=1:(2^k) %multiply the message word with X^(n-k) num=expand(m(i,:)*X(1,:));%c=m(x).X(x) numerator_polynomial=[numerator_polynomial;num]; %store the codeword in an array end numerator_polynomial c=[]; %array to store numerator inbinary
numerator=[]; num_coefficient=[]; for i=1:1:(2^k) %extraction of coefficients from the polynomial numerator num_coefficient =sym2poly(numerator_polynomial(i)); %append zeros ,to make numerator length =n if(length(num_coefficient)<n+1) code_length=n-length(num_coefficient); z=zeros(1,code_length); num_coefficient=[z num_coefficient]; end numerator=[numerator;num_coefficient];%store the coefficients in an array end numerator g=cyclpoly(n,k); %generator polynomial-denominator disp('generator polynomial-denominator'); Generator_polynomial=poly2sym(g) b=[]; %array to store the remainder %division for i=1:2^k
[q,r]=deconv(numerator(i,:),g); % divide X(x).m(x)/g(x) b=[b;r]; end %modulo -2-operation for i=1:2^k for j=1:1:7 if mod(b(i,j),2)==0 b(i,j)=0; else b(i,j)=1; end end end disp('b(x)'); b %add b(x) with X(x).M(x) disp('codeword'); c=[]; for i=1:2^k codeword=xor(numerator(i,:),b(i,:)); %append the b(x) with m(x).g(x)
c=[c;codeword]; end c disp('****TRANSMITTING****') t=input('enter the code to be transmitted'); R=c(t,:) r=R; b=input ('enter the bit to be modified'); if ((R(1,b))==0) R(1,b)=1; else R(1,b)=0; end disp('modified codeword'); disp(R); disp('****RECEIVING AND DECODING****') disp('parity check matrix '); H=cyclgen(n,g) %syndrome matrix S=R*H';
for i=1:(n-k) if mod(S(i),2)==0 S(i)=0; else S(i)=1; end end disp('syndrome'); disp(S); disp('transpose(H)'); h=H' for i=1:n if((S(1,:))==(h(i,:))) e=i; end end disp('error bit is:'); disp(e); disp('error pattern matrix') E=eye(n)
ei=[]; ei=E(e,:) disp('original codeword') r disp('corrected codeword') C1=xor(R,ei) OUTPUT: Enter the codeword length: 7 7 Enter the message length: 4 4 polynomial messageword m= 0 1 x x+1 x^2 x^2+1 x^2+x
x^2+x+1 x^3 x^3+1 x^3+x x^3+x+1 x^3+x^2 x^3+x^2+1 x^3+x^2+x x^3+x^2+x+1 numerator X term generator polynomial g= x^3+x+1 numerator_polynomial = 0 x^3 x^4 x^4+x^3 x^5 x^5+x^3
x^5+x^4 x^5+x^4+x^3 x^6 x^6+x^3 x^6+x^4 x^6+x^4+x^3 x^6+x^5 x^6+x^5+x^3 x^6+x^5+x^4 x^6+x^5+x^4+x^3 numerator = 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1 0 0 0 1 1 0 0 1 1 0 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1
0 0 0 1 1 1 1
0 1 1 0 0 1 1
1 0 1 0 1 0 1
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1 1 0 0 0 0 1 1
0 1 1 0 1 0 0 1
1 0 1 0 0 1 0 1
codeword c= 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 0 0 1 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 1 1 1 1 0 0 1 1 0 1 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0
1 1 1 1 1 1
0 0 1 1 1 1
1 1 0 0 1 1
0 1 0 1 0 1
0 0 0 0 1 1
1 0 1 0 0 1
1 0 0 1 0 1
syndrome 1 1 1
transpose(H) h =1 0 0 1 1 1 0 0 1 0 0 1 1 1 0 0 1 1 1 0 1
original codeword r= 0 1 1 0 0 0 1
corrected codeword C1 = 0 1 1 0 0 0 1