# BPSK Modulation And Demodulation- Complete Matlab Code With Explanation

Binary Phase Shift Keying (BPSK) is a type of digital modulation technique in which we are sending one bit per symbol i.e., ‘0’ or a ‘1’. Hence, the bit rate and symbol rate are the same. Depending upon the message bit, we can have a phase shift of 0o or 180o with respect to a reference carrier as shown in the figure above.

For example, we can have the following transmitted band-pass symbols:

$S_1=\sqrt{\frac{2E}{T}}\cos{(2\pi f t)}\rightarrow represents \mbox{ }'1'$ $S_2=\sqrt{\frac{2E}{T}}\cos{(2\pi f t+\pi)}\rightarrow represents \mbox{ }'0'$

or

$S_2=-\sqrt{\frac{2E}{T}}\cos{(2\pi f t)}\rightarrow represents \mbox{ }'0'$

Where ‘E’ is the symbol energy, ‘T’ is the symbol time period, f is the frequency of the carrier. Using Gram-schmidt orthogonalization, we get a single orthonormal basis function, given as:

$\psi_1=\sqrt{\frac{2}{T}}\cos{(2\pi f t)}$

Hence, the resulting constellation diagram can be given as:

Constellation Diagram Of BPSK Signal

There are only two in-phase components and no quadrature component.

Now, we can easily see that the two waveform of So and S1 are inverted with respect to one another and we can use following scheme to design a BPSK modulator:

BPSK modulator

First the NRZ encoder converts these digital bits into impulses to add a notion of time into them. Then NRZ waveform is generated by up-sampling these impulses. Afterwards, multiplication with the carrier (orthonormal basis function) is carried out to generate the modulated BPSK waveform.

Demodulator Design:

We do coherent demodulation of the BPSK signal at the receiver. Coherent demodulation requires the received signal to be multiplied with the carrier having the same frequency and phase as at the transmitter. The phase synchronization is normally achieved using Phase Locked Loop (PLL) at the receiver. PLL implementation is not done here, rather we assume perfect phase synchronization. Block diagram of BPSK modulator is shown in the figure below.  After the multiplication with the carrier (orthonormal basis function), the signal is integrated over the symbol duration ‘T’ and sampled. Then thresholding is applied to determine if a ‘1’ was sent (+ve voltage) or a ‘0’ was sent (-ve voltage).

The Matlab simulation code is given below. Here for the sake of simplicity,  the bit rate is fixed to 1 bit/s (i.e., T=1 second). It is also assumed that Phased Locked Loop (PLL) has already achieved exact phase synchronization.

clear all;
close all;
%Nb is the number of bits to be transmitted
T=1;%Bit rate is assumed to be 1 bit/s;
%bits to be transmitted
b=[1 0 1 0 1]
%Rb is the bit rate in bits/second

NRZ_out=[];

%Vp is the peak voltage +v of the NRZ waveform
Vp=1;
%Here we encode input bitstream as Bipolar NRZ-L waveform
for index=1:size(b,2)
if b(index)==1
NRZ_out=[NRZ_out ones(1,200)*Vp];
elseif b(index)==0
NRZ_out=[NRZ_out ones(1,200)*(-Vp)];
end
end

%Generated bit stream impulses
figure(1);
stem(b);
xlabel('Time (seconds)-->')
ylabel('Amplitude (volts)-->')
title('Impulses of bits to be transmitted');
figure(2);
plot(NRZ_out);
xlabel('Time (seconds)-->');
ylabel('Amplitude (volts)-->');
title('Generated NRZ signal');

t=0.005:0.005:5;
%Frequency of the carrier
f=5;
%Here we generate the modulated signal by multiplying it with
%carrier (basis function)
Modulated=NRZ_out.*(sqrt(2/T)*cos(2*pi*f*t));
figure;
plot(Modulated);
xlabel('Time (seconds)-->');
ylabel('Amplitude (volts)-->');
title('BPSK Modulated signal');

y=[];
%We begin demodulation by multiplying the received signal again with
%the carrier (basis function)
demodulated=Modulated.*(sqrt(2/T)*cos(2*pi*f*t));
%Here we perform the integration over time period T using trapz
%Integrator is an important part of correlator receiver used here
for i=1:200:size(demodulated,2)
y=[y trapz(t(i:i+199),demodulated(i:i+199))];
end
figure;
xlabel('Time (seconds)-->');
ylabel('Amplitude (volts)')


Impulses of bits to be transmitted

Generated NRZ signal

BPSK Modulated Signal

If you have any comments or questions, you can discuss them below.

• Jo August 13, 2016, 8:04 am

scatter(); %error!! -> Not enough input arguments……

• Jo August 13, 2016, 8:13 am

Thank you very much, that is very helpful!
but, there is one problem….

scatter(); % error !! -> Not enough input arguments……..

• Android file Recovery October 20, 2016, 1:37 pm

This device can also be fight friendly this means no knife blade to slow
you down at airport security. If the integrity of the own servers is compromised with a fire inside your
workplace, an electric surge, or one thing else, the files might be destroyed.
Cloud figuring out structure enables entire discretion of users data.

• Imran January 18, 2017, 11:42 am

Hi Dr. Moazzam,

First of all, thanks for this. I was looking for an easy to understand BPSK Matlab implementation so was glad I found this. I just have a question on the NRZ_out and Modulated plots. Shouldn’t the xlabel be samples instead of time? Because I don’t think the NRZ stream could have changed the frequency of the carrier which is what the plot seems to suggest. Hope to hear from you and thanks again.

• smokingRooster March 13, 2017, 9:44 pm

Hi, quick question.

You specify a frequency of 5hz…but your graph shows one of about 50. Could you explain why that is?
thank you

• sepiatone June 18, 2021, 10:05 am

That’s just an artifact of the plotting – plot(t, Modulated) and you would see a frequency of 5Hz.

• Mustafa April 28, 2017, 7:05 pm

Problem 1.
I want to Write a code in Matlab which creates a constant envelop PSK signal waveform that generate for M=8 (M stands for modulation), so that amplitude of the signal can reach up to sqrt(2). I want to Plot a graph which showing that there is no difference except in their phases

Problem 2.
I want to Write a code in Matlab which will generate a 500 random numbers to represent our symbols; and then divide them into 4 intervals. Whereby each interval corresponds to a symbol A0, A1, A2, A3, then plot a stem of 50 random symbols generated in accordance to the interval division.

• sudar May 25, 2017, 10:51 am

i have error in scatter plot. while BPSK modulation. whether anyone help me to correct scatterplot.

• hh December 8, 2017, 4:08 pm

i don’t understand this part
for i=1:200:size(demodulated,2)
y=[y trapz(t(i:i+199),demodulated(i:i+199))];
end

• Qadeer June 14, 2018, 10:25 pm

The symbol duration is 200, here we integrate the demodulated signal with respect to i each of size 200. hope that helps!

• Arun March 8, 2018, 9:02 am

how to make bpsk program run repeatedly?

• sam kumar October 25, 2018, 3:14 pm

this code not giving the propar phase shift .

• Devika November 25, 2018, 8:08 am

• MR PAWEL J PASZYNSKI January 28, 2019, 8:36 pm

Hi Moazzam I love the code it is Tidy and very well commented, my question is: Since C++ is included within the usual MATLAB commands code how can the corresponding Simulink Block Model can be generated is there a simple way of achieving the Block Diagram at all??
Thank You

• sushil May 6, 2019, 8:33 am

Thank you so much… I was searching for a code …. I finally found it

• ritu May 7, 2019, 7:31 am