Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

2/10/21 12:35 PM F:\bpsk.

m 1 of 2

clc; clear all; close all;

Nbits=10; % Number of bits in simulation


Rb=1e3; % bit rate
Eb=1e-3; % Bit energy in J/bit
fc=4e3; % carrier frequency

OSR=16; % Over sampling rate


fs=OSR*fc; % Sampling frequency
Tb=1/Rb; % bit period

%BPSK GENERATION
%bs=randint(1, Nbits, 2); % bit generation
bs=randi([0 1], Nbits, 1);

Ns=Nbits*(fc/Rb)*OSR; % number of samples in the simulation

pulse=ones(1,(fc/Rb)*OSR); % pulse for one period


bs(bs==0)=-1; % mapping to BPSK symbol constellation
bt=zeros(1, (Nbits-1)*(fc/Rb)*OSR+1); %generating impulse train
bt(1:(fc/Rb)*OSR:end)=bs;
bt=conv(pulse, bt); % getting Polar NRZ wave

n=0:(Ns-1);
ct=sqrt(2*Eb/Tb)*cos(2*pi*fc*n/fs); % generating carrier

st=bt.*ct; % Generating BPSK signal

%BPSK Receiver section


N1=length(n);
for j=1:N1
if (st(j)==ct(j))
y2(j)=1;
else
y2(j)=-1;
end
end

%BPSK COHERENT DETECTION


x=st.*ct;
for ii=1:Nbits
range=(ii-1)*(fc/Rb)*OSR+1 : ii*(fc/Rb)*OSR;
temp=x(range);
y(ii)=sum(temp);
if(y(ii)>=0)
bcat(ii)=1;

else
bcat(ii)=0;
end
end

bs(bs==-1)=0;
2/10/21 12:35 PM F:\bpsk.m 2 of 2

disp('The input and output bits are')


[bs.';bcat]

subplot(3,2,1); stem([0:Nbits-1], bs); axis([0 Nbits -0.1 1.1])


title('Input bits in Polar format');
xlabel('bit number'); ylabel('amplitude');grid on
subplot(3,2,2); plot(n/fs, bt); axis([0 Ns/fs -1.1 1.1])
title('Polar NRZ wave');
xlabel('time in seconds'); ylabel('amplitude');grid on
subplot(3,2,3); plot(n/fs, ct);
title('Carrier signal');
xlabel('time in seconds'); ylabel('amplitude');grid on
subplot(3,2,4); plot(n/fs, st);
title('BPSK'); xlabel('time in seconds'); ylabel('amplitude');grid on
subplot(3,2,5);
plot(n/fs, y2); axis([0 Ns/fs -1.1 1.1]);
title('Demodulated BPSK signal');
xlabel('Time');
ylabel('Amplitude');
grid on

subplot(3,2,6); stem([0:Nbits-1], bcat); axis([0 Nbits -0.1 1.1])


title('Output bits in Polar format');
xlabel('bit number'); ylabel('amplitude');grid on

The input and output bits are

ans =

001110 1000
0011101000

You might also like