Write a MATLAB program to Simulate(modulate) BPSK signals for the data {10000111} and demodulate it using MATLAB. Plot all waveforms. Obtain a bit error rate graph.

93 visualizzazioni (ultimi 30 giorni)
% This is for the modulation and Demodulation of BPSK for the data [ 1 0 0 0 0 1 1 1]
% If you obtain the Bit error rate for the given data please post it in the comments
% ********************* BPSK modulation and de-modulation ****************%
clc;
clear all;
close all;
% ********************* Define transmitted signal *************************
x_inp = [1 0 0 0 0 1 1 1]; % Binary information as stream of bits (binary signal 0 or 1)
N = length(x_inp);
Tb = 0.0001; %Data rate = 1MHz i.e., bit period (second)
disp('Binary Input Information at Transmitter: ');
disp(x_inp);
% ********************* Represent input signal as digital signal ****
x_bit=[];
nb=100; % Digital signal per bit
for n=1:1:N
if x_inp(n)==1;
x_bitt=ones(1,nb);
else x_inp(n)==0;
x_bitt=zeros(1,nb);
end
x_bit=[x_bit x_bitt];
end
t1=Tb/nb:Tb/nb:nb*N*(Tb/nb); % time of the signal
f1 = figure(1);
set(f1,'color',[1 1 1]);
subplot(3,1,1);
plot(t1,x_bit,'lineWidth',2);grid on;
axis([ 0 Tb*N -0.5 1.5]);
ylabel('Amplitude(volt)');
xlabel(' Time(sec)');
title('Input signal as digital signal');
% ********************* Define BPSK Modulation ****************************
Ac=5; % Amplitude of carrier signal
mc=4; % fc>>fs fc=mc*fs fs=1/Tb
fc=mc*(1/Tb); % carrier frequency for bit 1
fi1=0; % carrier phase for bit 1
fi2=pi; % carrier phase for bit 0
t2=Tb/nb:Tb/nb:Tb;
t2L=length(t2);
x_mod=[];
for (i=1:1:N)
if (x_inp(i)==1)
x_mod0=Ac*cos(2*pi*fc*t2+fi1);%modulation signal with carrier signal 1
else
x_mod0=Ac*cos(2*pi*fc*t2+fi2);%modulation signal with carrier signal 2
end
x_mod=[x_mod x_mod0];
end
t3=Tb/nb:Tb/nb:Tb*N;
subplot(3,1,2);
plot(t3,x_mod);
xlabel('Time(sec)');
ylabel('Amplitude(volt)');
title('Signal of BPSK modulation ');
% ********************* Transmitted signal x ******************************
x=x_mod;
% ********************* Channel model h and w *****************************
h=1; % Fading
w=0; % Noise
% ********************* Received signal y *********************************
y=h.*x+w;
% ********************* Define BPSK Demodulation **************************
y_dem=[];
for n=t2L:t2L:length(y)
t=Tb/nb:Tb/nb:Tb;
c=cos(2*pi*fc*t); % carrier siignal
y_dem0=c.*y((n-(t2L-1)):n);
t4=Tb/nb:Tb/nb:Tb;
z=trapz(t4,y_dem0); % intregation
A_dem=round((2*z/Tb));
if(A_dem>Ac/2) % logic level = Ac/2
A=1;
else
A=0;
end
y_dem=[y_dem A];
end
x_out=y_dem; % output signal;
% *************** Represent output signal as digital signal ***************
xx_bit=[];
for n=1:length(x_out);
if x_out(n)==1;
xx_bitt=ones(1,nb);
else x_out(n)==0;
xx_bitt=zeros(1,nb);
end
xx_bit=[xx_bit xx_bitt];
end
t4=Tb/nb:Tb/nb:nb*length(x_out)*(Tb/nb);
subplot(3,1,3)
plot(t4,xx_bit,'LineWidth',2);grid on;
axis([ 0 Tb*length(x_out) -0.5 1.5]);
ylabel('Amplitude(volt)');
xlabel(' Time(sec)');
title('Output signal as digital signal');

Risposta accettata

Nikshith Shetty
Nikshith Shetty il 15 Dic 2020
Modificato: Nikshith Shetty il 15 Dic 2020
% This is for the modulation and Demodulation of BPSK for the data [ 1 0 0 0 0 1 1 1]
% If you obtain the Bit error rate for the given data please post it in the comments
% ********************* BPSK modulation and de-modulation ****************%
clc;
clear all;
close all;
% ********************* Define transmitted signal *************************
x_inp = [1 0 0 0 0 1 1 1]; % Binary information as stream of bits (binary signal 0 or 1)
N = length(x_inp);
Tb = 0.0001; %Data rate = 1MHz i.e., bit period (second)
disp('Binary Input Information at Transmitter: ');
disp(x_inp);
% ********************* Represent input signal as digital signal ****
x_bit=[];
nb=100; % Digital signal per bit
for n=1:1:N
if x_inp(n)==1;
x_bitt=ones(1,nb);
else x_inp(n)==0;
x_bitt=zeros(1,nb);
end
x_bit=[x_bit x_bitt];
end
t1=Tb/nb:Tb/nb:nb*N*(Tb/nb); % time of the signal
f1 = figure(1);
set(f1,'color',[1 1 1]);
subplot(3,1,1);
plot(t1,x_bit,'lineWidth',2);grid on;
axis([ 0 Tb*N -0.5 1.5]);
ylabel('Amplitude(volt)');
xlabel(' Time(sec)');
title('Input signal as digital signal');
% ********************* Define BPSK Modulation ****************************
Ac=5; % Amplitude of carrier signal
mc=4; % fc>>fs fc=mc*fs fs=1/Tb
fc=mc*(1/Tb); % carrier frequency for bit 1
fi1=0; % carrier phase for bit 1
fi2=pi; % carrier phase for bit 0
t2=Tb/nb:Tb/nb:Tb;
t2L=length(t2);
x_mod=[];
for (i=1:1:N)
if (x_inp(i)==1)
x_mod0=Ac*cos(2*pi*fc*t2+fi1);%modulation signal with carrier signal 1
else
x_mod0=Ac*cos(2*pi*fc*t2+fi2);%modulation signal with carrier signal 2
end
x_mod=[x_mod x_mod0];
end
t3=Tb/nb:Tb/nb:Tb*N;
subplot(3,1,2);
plot(t3,x_mod);
xlabel('Time(sec)');
ylabel('Amplitude(volt)');
title('Signal of BPSK modulation ');
% ********************* Transmitted signal x ******************************
x=x_mod;
% ********************* Channel model h and w *****************************
h=1; % Fading
w=0; % Noise
% ********************* Received signal y *********************************
y=h.*x+w;
% ********************* Define BPSK Demodulation **************************
y_dem=[];
for n=t2L:t2L:length(y)
t=Tb/nb:Tb/nb:Tb;
c=cos(2*pi*fc*t); % carrier siignal
y_dem0=c.*y((n-(t2L-1)):n);
t4=Tb/nb:Tb/nb:Tb;
z=trapz(t4,y_dem0); % intregation
A_dem=round((2*z/Tb));
if(A_dem>Ac/2) % logic level = Ac/2
A=1;
else
A=0;
end
y_dem=[y_dem A];
end
x_out=y_dem; % output signal;
% *************** Represent output signal as digital signal ***************
xx_bit=[];
for n=1:length(x_out);
if x_out(n)==1;
xx_bitt=ones(1,nb);
else x_out(n)==0;
xx_bitt=zeros(1,nb);
end
xx_bit=[xx_bit xx_bitt];
end
t4=Tb/nb:Tb/nb:nb*length(x_out)*(Tb/nb);
subplot(3,1,3)
plot(t4,xx_bit,'LineWidth',2);grid on;
axis([ 0 Tb*length(x_out) -0.5 1.5]);
ylabel('Amplitude(volt)');
xlabel(' Time(sec)');
title('Output signal as digital signal');

Più risposte (1)

Idin Motedayen-Aval
Idin Motedayen-Aval il 18 Nov 2024
The question, as stated, doesn't seem to be specific enough.
The given code plots 100 points for each bit of data, and shows the BPSK modulation on a carrier signal. That all seems to be fine.
Bit since this page is getting a fair number of views, it seems BPSK mod/demod is a popular topic. I am going to give some answers to the question in the title (ignoring the MATLAB code given above).
Modulating/demodulating a BPSK signal using Communications Toolbox is almost trivial:
x = [1 0 0 0 0 1 1 1]; % data
x_mod = pskmod(x,2); % BPSK Modulation
x_hat = pskdemod(x_mod,2); % BPSK Demodulation
% Plots (use "stairs" to give the stair-step shape)
subplot(3,1,1)
stairs(x)
subplot(3,1,2)
stairs(real(mod_x))
subplot(3,1,3)
stairs(x_hat)
To generate a bit error rate (BER) graph (as the last part of the title says), it is not enough to simulate 8 bits! One must simulate many more bits. Below is a very simple version of such a code (there are many more elaborate examples given in MATLAB Answers).
% To get bit error rate curve
% one needs many more bits (generated at random)
N = 1e6; % number of bits to simulate
x = randi([0,1],[N,1]); % generate N bits (1 million in this case)
x_mod = pskmod(x,2); % BPSK modulate
snr_vec = 0:9;
ber = zeros(size(snr_vec)); % pre-allocate BER vector
for idx = 1:length(snr_vec)
y = awgn(x_mod,snr_vec(idx)); % Add noise
x_hat = pskdemod(y,2); % demod received signal
errs = sum(x_hat~=x); % count errors
ber(idx) = errs/N;
end
figure % create new figure window
semilogy(snr_vec,ber,'-*')
Hope this helps someone.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by