# 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:

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:

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)')


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

### 16 responses to “BPSK Modulation And Demodulation- Complete Matlab Code With Explanation”

1. Jo says:

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

2. Jo says:

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

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

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

4. Imran says:

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.

5. smokingRooster says:

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 says:

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

6. Mustafa says:

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.

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

8. hh says:

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

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

9. Arun says:

how to make bpsk program run repeatedly?

10. sam kumar says:

this code not giving the propar phase shift .

11. Devika says: