BPSK Simulation in MATLAB

Actually flicked the program from somewhere in a forum.

Took some time to understand it first. Few months back.

Now i’ve coded it better with some tweaks. So that you understand it sooner than me. 🙂

 

Here’s the code, I’ve copied and changed it to Italics, copy, paste, save and run.
Post your doubts in the comments 

clear all;
close all;

f = 2; %frequency of sine wave
fs = 100; %sampling period of the sine wave
t = 0:1/fs:1; %splitting time into segments of 1/fs
%setting the phase shifts for the different BPSK signals
p1 = 0;
p2 = pi;
%getting the number bits to be modulated
N = input(‘enter the number of bits to be modulated: N = ‘);
%generating the random signal
bit_stream=round(rand(1,N));
%allocating the dynamic variables
time = [];
digital_signal = [];
PSK = [];
carrier_signal = [];

%GENERATING THE SIGNALS
for ii = 1:1:N
%the original digital signal is
if bit_stream(ii) == 0
bit = zeros(1,length(t));
else
bit = ones(1,length(t));
end
% bit0 = (bit_stream(ii)==0)*zeros(1,length(t));
% bit1 = (bit_stream(ii)==1)*ones(1,length(t));
digital_signal = [digital_signal bit];

%Generating the BPSK signal
if bit_stream(ii) == 0
bit = sin(2*pi*f*t+p1);
else
bit = sin(2*pi*f*t+p2);
end
PSK = [PSK bit];

%Generating the carrier wave
carrier = sin(2*f*t*pi);
carrier_signal = [carrier_signal carrier];

time = [time t];
t = t + 1;

end

subplot(3,1,1);
plot(time,digital_signal,’r’);
grid on;
axis([0 time(end) -0.5 1.5]);

subplot(3,1,2);
plot(time,PSK);
grid on;
% axis([0 time(end) -2 2]);
axis tight;

subplot(3,1,3);
plot(time,carrier_signal);
grid on;
axis tight;

 

 

The output should look something like this … 
Image

 

12 thoughts on “BPSK Simulation in MATLAB

  1. sorry in my hurry i did not erase some of the commands which i ‘commented’
    Please ignore those…
    (the lines starting with % can be ignored totally, theyre just comments, theyre either to make u understand better or to confuse u.. IGNORE)

    • Hello wafik… It stands for “red” … It plots that particular variable in Red.. See the digital signal plot. Just to show some difference. Nothing technical about it.

  2. the program is working but the it is not giving the expected out. input bitstream is not showing any up down, its coming a straightline whether i change how many times 1 to 0.

  3. I’m soo new to matlab. this section below is giving me syntax error. I change to (8).
    %getting the number bits to be modulated
    N = input(‘enter the number of bits to be modulated: N = ‘);

Leave a reply to sitiagel Cancel reply