Vous êtes sur la page 1sur 11

King Saud University

College of Computer and Information Sciences


Department of Computer Engineering
Course: CEN344
Prepared by: Dr. Abdulmohsen Mutairi

LAB-8: BPSK Modulation and Demodulation


Objectives: Learn to the basic operation of a BPSK modem and see its performance over a noisy
channel.

In this LAB, you will simulate a basic BPSK transmission system and demonstrate the transmission of text data over a
noisy channel with different noise levels. The basic components of the simulated system are illustrated in the following
figure. There are four parts in the LAB which need to be done in sequence.

Page 1 of 11
Part 1: BPSK Modulation

In this part, you are going to simulate a BPSK modulator in MATLAB and see how it works. In Phase Shift Keying (PSK)
modulation, the phase of the carrier signal is shifted between discrete values to represent binary data. In Binary PSK,
two phases represent the two binary digits as in the following equation:

cos(2 ) , binary 1
() = {
cos(2 + ) = cos(2 ) , binary 0

where is the carrier signal frequency and is its amplitude. In BPSK, input data bits are first encoded using NRZ-L
(+1 and -1) to produce a baseband signal (). The baseband signal is next multiplied by the carrier signal and the
output signal () is transmitted on the channel. The BPSK modulator is illustrated in the following figure.

In this part, you will use a fixed sequence of data bits; later on you will read your data from a text file.

1.1. First, generate some binary data bits in MATLAB. Create a fixed pattern of 5 or 6 bits, e.g. 11010 and replicate it
up the desired number of bits (use 10000 bits).
%
Num_bits= ; % number of bits to be generated in the simulation
pattern=[ ? ? ]; % deterministic bit sequence
% replicate this pattern up to Num_bits:
tx_databits=repmat(pattern,1,floor(Num_bits/length(pattern)));
% print the first 20 data bits on the screen:
disp(['Transmitted data bits (first 20 bits) = ' num2str(tx_databits(1:20))]);
%

1.2. Second, configure BPSK parameters. Use a data rate of 1Kbps and a carrier signal with an amplitude of 2/
where is the baseband symbol time. In practice, the carrier frequency is much higher than the modulation
rate, but to speed up the simulation, set the carrier frequency to 4 times the modulation rate (symbol rate). For
NRZ-L, use two pulse levels: +1 and -1.
%
M=2; % BPSK is a binary signaling scheme (two symbols)
R= ; % Data rates (bits/sec)
Rs=R/log2(M); % symbol rate (or modulation rate D) in symbols/sec (bauds)
T=1/Rs; % symbol time (equals bit time in Binary signaling e.g. BPSK)
A= ; % baseband (NRZ-L) pulse amplitude ;
E=A^2*T; % Energy per symbol
fc= ; % carrier frequency
Ac= ; % carrier amplitude
fs=30*fc; % sampling frequency of the continuous time signal

Page 2 of 11
Ts=1/fs; % time between samples of the continues time signal
Num_symbols=Num_bits/log2(M); % in binary signaling, each symbol carries 1 bit
% Generate time points:
MaxTime=Num_symbols*T; % total time needed to transmit Num_symbols
num_samples_per_symbol=T/Ts;
t=0:Ts:MaxTime;
t(end)=[]; % delete the last time point because we started from t=0
%

1.3. Third, encode each bit using NRZ-L to generate the baseband signal (). Basically, create two NRZ-L symbols
which are the square pulses 1 () = +, 0 < < to represent binary 1 and 2 = , 0 < < to
represent binary 0. Then map each bit to a symbol.
%
% create two baseband symbols. Width of each symbol is no_samples_per_symbol
% the factor sqrt(T) is needed to scale the amplitude of the BPSK signal to 1
g_1= +A*sqrt(T)*ones(1,num_samples_per_symbol); % NRZ-L for binary 1
g_2= -A*sqrt(T)*ones(1,num_samples_per_symbol); % NRZ-L for binary 0
g=[]; % declare an array for the output baseband signal
for l=1:length(tx_databits) % map each bit into a baseband symbol
current_bit=tx_databits(l);
if current_bit==1
output_pulse= ; % if bit=1 use the symbol for binary 1
else
output_pulse= ; % if bit=0 use the symbol for binary 0
end
g=[g output_pulse]; % append this symbol to the output signal
end
%

1.4. Next, plot the first 5 bits of the baseband signal () to verify the NRZ-L encoding step.
% plot baseband signal
Num_plot_bits= ; % number of bits to show on the figure
MaxPlotTime=Num_plot_bits*T;
figure(1);
subplot(3,1,1);
plot(t,g);
axis([0 MaxPlotTime -.1 .1]); % this just modify the scale of the range of
values shown on the axis (play with it until you can see the full signal range)
grid on;
xlabel('time'); ylabel('g(t)');
title('Baseband signal g(t)')
%

1.5. Fourth, generate the carrier signal with frequency and amplitude as given above. Plot the carrier on the
same figure.
%
% generate carrier sinusoid
s_c= ; % the carrier signal
% plot carrier wave:
subplot(3,1,2);
plot(t,s_c);
axis([0 MaxPlotTime -50 50]);
grid on;
xlabel('time'); ylabel('s_c(t)');
title('Carrier sinusoid (un-modulated)');
Page 3 of 11
%

1.6. Fifth, modulate the carrier signal () using the baseband signal () using BPSK. As described above, the
output BPSK signal is () = () (). In MATLAB, use element-by-element multiplication between the two
signal arrays g and s_c to create the BPSK signal array s.
%
% Bandpass BPSK modulation:
s = ; % element by element multiplication (not vector multiplication)
%

1.7. Finally, plot the output BPSK signal and check your result.
%
% plot the resultant BPSK signal
subplot(3,1,3);
plot(t,s);
axis([0 MaxPlotTime -2 2]);
grid on;
xlabel('time'); ylabel('s(t)');
title('Bandpass BPSK signal s(t)');
%

Page 4 of 11
Part 2: BPSK Demodulation

In this part, you are going to simulate a BPSK demodulator in MATLAB and see how it works. BPSK demodulation
converts the received signal into binary bits. The process consists of two steps as illustrated in the following figure. In
the first step, the incoming waveform signal is transformed into a sample and in the second step a decision is made
regarding what this sample means (0 or 1).

Step 1 starts by converting the bandpass signal () into a baseband signal () by multiplying it by the carrier signal.
This process is called frequency down-conversion because it shifts the bandwidth of the signal from around back to
baseband (around DC). Then, the baseband signal () is filtered in a receiving filter and sampled to produce a sample .
In practice, the receiving filter consists of many components to clean and enhance the signal in order to produce the
best sample possible (with highest SNR). In this simulation, the receiving filter is a simple correlation filter that maximize
the SNR in the sample to minimize the errors in the subsequent decision-making step. In MATLAB, we implement this
filter by integrating () over each symbol duration:

= () = () ()
0 0

Step 2 compares the value of to a threshold to decide which binary bit should be produced at the output of the
receiver. For BPSK, the optimum threshold is 0 so if Z is positive a binary 1 is produced otherwise a binary 0 is produced.

The two steps are done in order for every symbol duration. In MATLAB, we take the received signal array and divide it
into symbols and run the two steps on each symbol.

2.1. Use the code you created in Part 1 to build the BPSK modulation part and then add to it the following steps.
Make sure you modify the sample code below based on the description above.
2.2. First, set the received signal to the transmitted signal () = () which means there is no channel noise
(perfect channel). Plot the received signal to verify.
%
r= ; % received signal array
% plot the received bandpass signal.
subplot(4,1,4);
plot(t,r);
axis([0 MaxPlotTime min(r) max(r)]);
grid on;
xlabel('time'); ylabel('r(t)');

Page 5 of 11
title('Received Bandpass BPSK signal r(t)');
%

2.3. Second, multiply the received signal () by the carrier signal () to perform the frequency down-conversion
step to produce the baseband signal ().
%
v= ; % element by element multiplication (not vector multiplication)
%

2.4. Third, pass the resulting signal () into the correlation receiver to produce the sample . This is basically an
integration over each symbol duration. To do so, we will use some MATLAB tricks as follows:
%
% first we arrange the array v (which is a row vector) into a matrix where each
% column corresponds to one symbol (i.e. each column consists of time samples % %
of each received symbol)
tmp=reshape(v,num_samples_per_symbol,Num_symbols);
% next, we sum up each column (which is equivalent to integration).
Z= sum(tmp)/num_samples_per_symbol*T;
%

Note that the array Z consists of a sample for each symbol. The value Z(1) is the optimum sample for the first
received symbol, value Z(2) is for the second received symbol and so on. The length of the array Z is
Num_symbols.

2.5. Fourth, perform the decision-making (detection) step by comparing each sample in the array Z to the threshold.
The output of the comparison is the received binary bit. In MATLAB, the logical statement {X > a} produces 1 if
true and 0 if false.
%
% Step 2 Detection: compare the output of the correlation filter to the
% threshold
threshold = ; % set the optimum threshold value
rx_databits = Z > threshold; % if Z>threshold ==> binary 1 otherwise binary 0
%

2.6. Finally, print the first 20 bits of the received data bits and compare to the transmitted data bits.
%
% print the first 20 received bits
disp(['Received data bits (first 20 bits) = ' num2str(rx_databits(1:20))]);
%

Page 6 of 11
Part 3: BPSK Demodulation over a Noisy Channel

In this part, you are going to simulate BPSK demodulation in a noisy channel. You will repeat Part 2 above adding a noisy
channel in between the transmitter and receiver as illustrated in the following figure.

The noisy channel to be used here is an Additive White Gaussian Noise (AWGN) channel which is a common channel
model that is used to evaluate digital communication systems. White Gaussian noise is a good model for Thermal noise.
Discrete samples of AWGN can be generated in MATLAB by generating a sequence of random numbers from the Normal
(Gaussian) probability distribution using the function randn.

The most important factor in the performance of digital communication over noisy channels is the Signal to Noise Ratio
(SNR) which is the ratio of the received Signal power to Noise power. The probability of the receiver making a bit error
increases when the signal level is very close to the noise level (which means SNR is close to 1). Increasing the signal level
compared to noise reduces the probability of bit errors. The probability of bit error, also known as the Bit Error Rate
(BER), in BPSK over AWGN is related to the normalized bit SNR which is /0 as follows:

= (2 /0 )

Where the -function is the complementary Gaussian CDF.

In this simulation, you are going to simulate a channel with a noise power which is chosen carefully such that the
system have a desired target SNR. In practice, the noise power is fixed and we increase the signal power to increase SNR,
but here we fix the signal power and change the noise power to simplify the simulation code. Here, we will set a target
/0 and from it we will calculate the noise power according to the following equation:
/ log 2
=
0 /
Where is the energy per symbol and is the number of bits per symbol. We can set the bandwidth based on the
sampling frequency as = /2. Therefore, the noise power can be computed as follows

=
2 log 2 /0
The noise power is the same as the variance of the Gaussian random variable.

3.1. Re-use the code you created in Part 1 for BPSK modulation and then add the following steps. Make sure you
modify the sample code below based on the description above.

Page 7 of 11
3.2. First, specify the target /0 that needs to be simulated and from it compute the noise power. Start with a
value of / = dB. You are required to simulate multiple values of /0 later.
%
Eb_N0_dB = ; % Normalized bit SNR in dB.
Eb_N0=10^(Eb_N0_dB/10); % convert the given Eb_No_dB to linear scale
% compute the noise variance which is the same as noise power
noiseVariance=E*fs/(2*log2(M)*Eb_N0);
sigma_n=sqrt(noiseVariance); % standard deviation of noise
%

3.3. Second, generate the noise signal () based on the computed noise variance above. The length of the array
must be the same as the transmitted signal ().
%
n = sigma_n*randn(1,length(s)); % generate noise samples
%

3.4. Third, add the noise signal to the transmitted signal to get the noisy received signal () = () + () to
simulate passing the signal over a noisy channel. Plot the received noisy signal to verify.
%
r = ; % received noisy signal array
% plot the received bandpass signal.
subplot(4,1,4);
plot(t,r);
axis([0 MaxPlotTime min(r) max(r)]);
grid on;
xlabel('time'); ylabel('r(t)');
title('Received Bandpass BPSK signal r(t)');
%

3.5. Re-use the code you created in Part 2 for BPSK demodulation. Only use the steps 2.3 2.6.
3.6. Add the following code at the end of your code to compute the Bit Error Rate (BER) of BPSK demodulation and
compare it to the theoretical BER as calculated above.
% Compute the number of bit errors in the received data bits
c=xor(rx_databits,tx_databits);
num_bit_errors=sum(c);
disp(['Number of bits errors= ' num2str( num_bit_errors )]);
BER_simulation=num_bit_errors/Num_bits;
disp(['Simulation BER= ' num2str( BER_simulation )]);
BER_theoretical=qfunc(sqrt(2*Eb_N0));
disp(['Theorectial BER= ' num2str( BER_theoretical )]);
%
%

3.7. Repeat the simulation using / = dB , / = dB and / = dB.


3.8. What can you say about the effect of /0 on the demodulation error performance?

Page 8 of 11
Part 4: Sending text data using BPSK over a Noisy Channel

In this part, you are going to simulate transmitting text data using BPSK over a noisy channel. You will repeat Part 3
above using text data instead of a fixed binary pattern. For text data, you will need to convert the transmitted text
characters into binary bits and the received bits back to text characters using ASCII encoding. Two functions to make this
conversions are provided at the end of this document.

For the experiment, you will need to create a text file with the name data.txt and write some text message in it. Then,
the text file will be read by MATLAB and then transmitted and received using BPSK. The received message will be written
into another text file named data_received.txt. You are required to run the experiment for multiple values of noise
levels and see its effect on the received message.

4.1. Create a text message that contains at least 200 letters (> 50 English words) and save with name data.txt in
the same folder of your MATLAB scripts. For example, you may use the following message.

Hello Digital Communication World!


This is my first simulation of digital communication systems. This message has been transmitted over a noisy
channel and received using BPSK modulation/demodulation.

4.2. Re-use the full code that you created in Part 3 but modify it as in the following steps.
4.3. At the beginning of your MATLAB script, replace the code for generating the fixed binary pattern (Part 1) to the
following code which reads text characters from the input text file data.txt and convert them into binary bits.
%
% Read input text data from a text file
text_data = fileread('data.txt');
% then format the data into binary 1's and 0's using ASCII encoding:
tx_databits=FormatText2Bits(text_data); % look at the this function to
% % see how it works
Num_bits=length(tx_databits);
% print the first 20 data bits
disp(['Transmitted data bits (first 20 bits) = ' num2str(tx_databits(1:20))]);
%

4.4. At the end of your MATLAB script, replace the code for printing the received binary bits to the following code
which convert them into text characters and save them to the text file data_received.txt.
%
% print the first 20 received bits
disp(['Received data bits (first 20 bits) = ' num2str(rx_databits(1:20))]);
%
% Format received bits into text using ASCII decoding:
received_text=FormatBits2Text(rx_databits);
% write received data to a text file:
fid = fopen('data_received.txt','wt');
fprintf(fid, '%s', received_text);
fclose(fid);
%

4.5. Before, you run the simulation, you need to provide the formatting functions FormatText2Bits and
FormatBits2Text which are needed for binary to text conversion. The code for each function is provided in

Page 9 of 11
the last page of this document. Copy each code in a separate MATLAB m-file and save them as
FormatText2Bits.m and FormatBits2Text.m accordingly.
4.6. Run the simulation using a very low noise level , e.g. / = dB.
4.7. Look at the received text message in the output text file.
4.8. Repeat with different values of noise level, e.g. / = dB , / = dB and / = dB
4.9. What can you say about the effect of /0 on the accuracy of the received error performance?

Page 10 of 11
Formatting functions:
1. Function to convert text characters to binary bits (save the following code in an m-file FormatText2Bits.m

%
function output = FormatText2Bits (input_text)
out1=dec2bin(input_text,8);
out2=out1';
out3=out2(:)-'0';
output=out3(1:end)';
end
%

2. Function to convert binary bits into text characters (save the following code in an m-file FormatBits2Text.m

%
function output = FormatBits2Text (input_bits)
z=double(input_bits);
w=reshape(z',8,[])';
d=bi2de(w,'left-msb');
output=char(d)';
end
%

Page 11 of 11

Vous aimerez peut-être aussi