You are on page 1of 17

BPSK generation and detection

% <<<<<<<<<<<<<<<<<<< BPSK Modulation and


Demodulation >>>>>>>>>>>>>>>>>>>
clc, clear all, close all;
% ******************* Digital/Binary input
information ********************
x = input('Enter Digital Input Information
= '); % Binary information as stream of
bits (binary signal 0 or 1)
N = length(x);
Tb = 0.0001; %Data rate = 1MHz i.e., bit
period (second)
disp('Binary Input Information at
Transmitter: ');
disp(x);
% ************* Represent input information
as digital signal *************
nb = 100; % Digital signal per bit
digit = [];
for n = 1:1:N
if x(n) == 1;
sig = ones(1,nb);
else x(n) == 0;
sig = zeros(1,nb);
end
digit = [digit sig];
end
t1=Tb/nb:Tb/nb:nb*N*(Tb/nb); % Time
period
figure('Name','BPSK Modulation and
Demodulation','NumberTitle','off');
subplot(3,1,1);
plot(t1,digit,'lineWidth',2.5);
grid on;
axis([0 Tb*N -0.5 1.5]);
xlabel('Time(Sec)');
ylabel('Amplitude(Volts)');
title('Digital Input Signal');
% **************************** BPSK
Modulation ****************************
Ac = 10; % Carrier amplitude for
binary input
br = 1/Tb; % Bit rate
Fc = br*10; % Carrier frequency
Pc1 = 0; % Carrier phase for binary
input '1'
Pc2 = pi; % Carrier phase for binary
input '0'
t2 = Tb/nb:Tb/nb:Tb; % Signal time
mod = [];
for (i = 1:1:N)
if (x(i)==1)
y = Ac*cos(2*pi*Fc*t2+Pc1); %
Modulation signal with carrier signal 1
else
y = Ac*cos(2*pi*Fc*t2+Pc2); %
Modulation signal with carrier signal 2
end
mod=[mod y];
end
t3=Tb/nb:Tb/nb:Tb*N; % Time period
subplot(3,1,2);
plot(t3,mod);
xlabel('Time(Sec)');
ylabel('Amplitude(Volts)');
title('BPSK Modulated Signal');
% ********************* Transmitted signal
x ******************************
x = mod;
% ********************* Channel model h and
w *****************************
h = 1; % Signal fading
w = 0; % Noise
% ********************* Received signal y
*********************************
y = h.*x + w; % Convolution
% *************************** BPSK
Demodulation ***************************
s = length(t2);
demod = [];
for n = s:s:length(y)
t4 = Tb/nb:Tb/nb:Tb;
c = cos(2*pi*Fc*t4); % carrier
siignal
mm = c.*y((n-(s-1)):n); % Convolution
t5 = Tb/nb:Tb/nb:Tb;
z = trapz(t5,mm); % intregation
rz = round((2*z/Tb));
if(rz > Ac/2) % Logical
condition
a = 1;
else
a = 0;
end
demod = [demod a];
end
disp('Demodulated Binary Information at
Receiver: ');
disp(demod);
% ********** Represent demodulated
information as digital signal **********
digit = [];
for n = 1:length(demod);
if demod(n) == 1;
sig = ones(1,nb);
else demod(n) == 0;
sig = zeros(1,nb);
end
digit = [digit sig];
end
t5=Tb/nb:Tb/nb:nb*length(demod)*(Tb/nb);
% Time period
subplot(3,1,3)
plot(t5,digit,'LineWidth',2.5);grid on;
axis([0 Tb*length(demod) -0.5 1.5]);
xlabel('Time(Sec)');
ylabel('Amplitude(Volts)');
title('BPSK Demodulated Signal');
Input and Output:

Enter Digital Input Information = [1 0 1 0]

Binary Input Information at Transmitter:

1 0 1 0

Demodulated Binary Information at Receiver: 1 0 1 0


Wave forms:
FSK modulation and demodulation

>> MATLAB code for binary FSK modulation


and de-modulation >>>>>>>%
clc;
clear all;
close all;
x=[ 1 0 0 1 1 0 1];
% Binary Information
bp=.000001;
% bit period
disp(' Binary information at Trans
mitter :');
disp(x);
%XX representation of transmitting binary
information as digital signal XXX
bit=[];
for n=1:1:length(x)
if x(n)==1;
se=ones(1,100);
else x(n)==0;
se=zeros(1,100);
end
bit=[bit se];
end
t1=bp/100:bp/100:100*length(x)*(bp/100);
subplot(3,1,1);
plot(t1,bit,'lineWidth',2.5);grid on;
axis([ 0 bp*length(x) -.5 1.5]);
ylabel('amplitude(volt)');
xlabel(' time(sec)');
title('transmitting information as digital
signal');
%XXXXXXXXXXXXXXXXXXXXXXX Binary-FSK
modulation XXXXXXXXXXXXXXXXXXXXXXXXXXX%
A=5;
% Amplitude of carrier signal
br=1/bp;
% bit rate
f1=br*8; %
carrier frequency for information as 1
f2=br*2; %
carrier frequency for information as 0
t2=bp/99:bp/99:bp;
ss=length(t2);
m=[];
for (i=1:1:length(x))
if (x(i)==1)
y=A*cos(2*pi*f1*t2);
else
y=A*cos(2*pi*f2*t2);
end
m=[m y];
end
t3=bp/99:bp/99:bp*length(x);
subplot(3,1,2);
plot(t3,m);
xlabel('time(sec)');
ylabel('amplitude(volt)');
title('waveform for binary FSK modulation
coresponding binary information');
%XXXXXXXXXXXXXXXXXXXX Binary FSK
demodulation XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
mn=[];
for n=ss:ss:length(m)
t=bp/99:bp/99:bp;
y1=cos(2*pi*f1*t); %
carrier siignal for information 1
y2=cos(2*pi*f2*t); %
carrier siignal for information 0
mm=y1.*m((n-(ss-1)):n);
mmm=y2.*m((n-(ss-1)):n);
t4=bp/99:bp/99:bp;
z1=trapz(t4,mm)
% intregation
z2=trapz(t4,mmm)
% intregation
zz1=round(2*z1/bp)
zz2= round(2*z2/bp)
if(zz1>A/2) % logic lavel= (0+A)/2
or (A+0)/2 or 2.5 ( in this case)
a=1;
else(zz2>A/2)
a=0;
end
mn=[mn a];
end
disp(' Binary information at Reciver :');
disp(mn);
%XXXXX Representation of binary information
as digital signal which achived
%after demodulation
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXX
bit=[];
for n=1:length(mn);
if mn(n)==1;
se=ones(1,100);
else mn(n)==0;
se=zeros(1,100);
end
bit=[bit se];
end
t4=bp/100:bp/100:100*length(mn)*(bp/100);
subplot(3,1,3)
plot(t4,bit,'LineWidth',2.5);grid on;
axis([ 0 bp*length(mn) -.5 1.5]);
ylabel('amplitude(volt)');
xlabel(' time(sec)');
title('recived information as digital
signal after binary FSK demodulation');

Input:

Binary information at Trans mitter :

1 0 0 1 1 0 1

Output:

Binary information at Reciver :

1 0 0 1 1 0 1
Waveforms:

QPSK
%XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
%XXXX QPSK Modulation and Demodulation without consideration of noise XXXXX
%XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
clc;
clear all;
close all;
data=[0 1 0 1 1 1 0 0 1 1]; % information
%Number_of_bit=1024;
%data=randint(Number_of_bit,1);
figure(1)
stem(data, 'linewidth',3), grid on;
title(' Information before Transmiting ');
axis([ 0 11 0 1.5]);
data_NZR=2*data-1; % Data Represented at NZR form for QPSK modulation
s_p_data=reshape(data_NZR,2,length(data)/2); % S/P convertion of data
br=10.^6; %Let us transmission bit rate 1000000
f=br; % minimum carrier frequency
T=1/br; % bit duration
t=T/99:T/99:T; % Time vector for one bit information
% XXXXXXXXXXXXXXXXXXXXXXX QPSK modulatio XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
y=[];
y_in=[];
y_qd=[];
for(i=1:length(data)/2)
y1=s_p_data(1,i)*cos(2*pi*f*t); % inphase component
y2=s_p_data(2,i)*sin(2*pi*f*t) ;% Quadrature component
y_in=[y_in y1]; % inphase signal vector
y_qd=[y_qd y2]; %quadrature signal vector
y=[y y1+y2]; % modulated signal vector
end
Tx_sig=y; % transmitting signal after modulation
tt=T/99:T/99:(T*length(data))/2;
figure(2)
subplot(3,1,1);
plot(tt,y_in,'linewidth',3), grid on;
title(' wave form for inphase component in QPSK modulation ');
xlabel('time(sec)');
ylabel(' amplitude(volt0');
subplot(3,1,2);
plot(tt,y_qd,'linewidth',3), grid on;
title(' wave form for Quadrature component in QPSK modulation ');
xlabel('time(sec)');
ylabel(' amplitude(volt0');
subplot(3,1,3);
plot(tt,Tx_sig,'r','linewidth',3), grid on;
title('QPSK modulated signal (sum of inphase and Quadrature phase signal)');
xlabel('time(sec)');
ylabel(' amplitude(volt0');
% XXXXXXXXXXXXXXXXXXXXXXXXXXXX QPSK demodulation XXXXXXXXXXXXXXXXXXXXXXXXXX
Rx_data=[];
Rx_sig=Tx_sig; % Received signal
for(i=1:1:length(data)/2)
%%XXXXXX inphase coherent dector XXXXXXX
Z_in=Rx_sig((i-1)*length(t)+1:i*length(t)).*cos(2*pi*f*t);
% above line indicat multiplication of received & inphase carred signal

Z_in_intg=(trapz(t,Z_in))*(2/T);% integration using trapizodial rull


if(Z_in_intg>0) % Decession Maker
Rx_in_data=1;
else
Rx_in_data=0;
end

%%XXXXXX Quadrature coherent dector XXXXXX


Z_qd=Rx_sig((i-1)*length(t)+1:i*length(t)).*sin(2*pi*f*t);
%above line indicat multiplication ofreceived & Quadphase carred signal

Z_qd_intg=(trapz(t,Z_qd))*(2/T);%integration using trapizodial rull


if (Z_qd_intg>0)% Decession Maker
Rx_qd_data=1;
else
Rx_qd_data=0;
end

Rx_data=[Rx_data Rx_in_data Rx_qd_data]; % Received Data vector


end
figure(3)
stem(Rx_data,'linewidth',3)
title('Information after Receiveing ');
axis([ 0 11 0 1.5]), grid on;
% XXXXXXXXXXXXXXXXXXXXXXXXX end of program XXXXXXXXXXXXXXXXXXXXXXXXXX
Waveforms:
DPSK:

PCM
%% Pulse Code Modulation (PCM)
P=5; %percentage of errors in transmission
% sampling
t = [0:0.1:1*pi]; % Times at which to
sample the sine function
sig = 4*sin(t); % Original signal, a sine
wave
%sig=exp(-1/3*t);
% quantization
Vh=max(sig);Vl=min(sig);
N=3;M=2^N;S=(Vh-Vl)/M; %design N-bit
uniform quantizer with stepsize=S
partition = [Vl+S:S:Vh-S]; % Length M-1, to
represent M intervals
codebook = [Vl+S/2:S:Vh-S/2]; % Length M,
one entry for each interval
% partition = [-1:.2:1]; % Length 11, to
represent 12 intervals
% codebook = [-1.2:.2:1]; % Length 12, one
entry for each interval
[index,quantized_sig,distor] =
quantiz(sig,partition,codebook); %
Quantize.
% binary encoding
codedsig=de2bi(index,'left-
msb');codedsig=codedsig';
txbits=codedsig(:); %serial transmit
errvec=randsrc(length(txbits),1,[0 1;(1-
P/100) P/100]); %error vector
%rxbits=xor(txbits,errvec);
rxbits=rem(txbits+errvec,2); %bits received
rxbits=reshape(rxbits,N,length(sig));rxbits
=rxbits';
index1=bi2de(rxbits,'left-msb'); %decode
reconstructedsig=codebook(index1+1); %re-
quantize
%plot(t,sig,'x',t,quantized_sig,'.')
%plot(t,sig,'x-',t,quantized_sig,'.--',t,re
constructedsig,'d-')
%figure,stem(t,sig,'x-');hold;stem(t,quanti
zed_sig,'.--');stem(t,reconstructedsig,'d-'
);
figure,subplot(2,2,1); stem(t,sig);
xlabel('time'); title('original signal');
subplot(2,2,2); stem(t,quantized_sig);
xlabel('time'); title('quantized signal');
tt=[0:N*length(t)-1];
subplot(2,2,3);stairs(tt,txbits);
xlabel('time'); title('PCM waveform');
subplot(2,2,4);stem(t,reconstructedsig);
xlabel('time'); title('received signal');

Waveforms:

DM modulation and demodulation


%% Delta Modulation (DM)
%delta modulation = 1-bit differential
pulse code modulation (DPCM)
predictor = [0 1]; % y(k)=x(k-1)
%partition = [-1:.1:.9];codebook = [-
1:.1:1];
step=0.2; %SFs>=2pifA
partition = [0];codebook = [-1*step step];
%DM quantizer
t = [0:pi/20:2*pi];
x = 1.1*sin(2*pi*0.1*t); % Original signal,
a sine wave
%t = [0:0.1:2*pi];x = 4*sin(t);
%x=exp(-1/3*t);
%x = sawtooth(3*t); % Original signal
% Quantize x(t) using DPCM.
encodedx =
dpcmenco(x,codebook,partition,predictor);
% Try to recover x from the modulated
signal.
decodedx =
dpcmdeco(encodedx,codebook,predictor);
distor = sum((x-decodedx).^2)/length(x) %
Mean square error
% plots
figure,subplot(2,2,1);plot(t,x);xlabel('tim
e');title('original signal');
subplot(2,2,2);stairs(t,10*codebook(encoded
x+1),'--');xlabel('time');title('DM
output');
subplot(2,2,3);plot(t,x);hold;stairs(t,deco
dedx);grid;xlabel('time');title('received
signal');
Waveforms:

You might also like