Modulating a Random Signal

This first example addresses the following problem:

The table below indicates the key tasks in solving the problem, along with relevant functions from the Communications Toolbox. The solution arbitrarily chooses baseband 16-QAM (quadrature amplitude modulation) as the modulation scheme and AWGN (additive white Gaussian noise) as the channel model.

TaskFunction
Generate a random binary data streamrandint
Modulate using 16-QAMqammod
Add white Gaussian noiseawgn
Create a scatter plotscatterplot
Demodulate using 16-QAMqamdemod
Compute the system's BERbiterr

Solution of Problem

The discussion below describes each step in more detail, introducing M-code along the way. To view all the code in one editor window, enter the following in the MATLAB Command Window.

edit commdoc_mod

1. Generate a Random Binary Data Stream.   The conventional format for representing a signal in MATLAB is a vector or matrix. This example uses the randint function to create a column vector that lists the successive values of a binary data stream. The length of the binary data stream (that is, the number of rows in the column vector) is arbitrarily set to 30,000.

The code below also creates a stem plot of a portion of the data stream, showing the binary values. Your plot might look different because the example uses random numbers. Notice the use of the colon (:) operator in MATLAB to select a portion of the vector. For more information about this syntax, see The Colon Operator in the MATLAB documentation set.

%% Setup
% Define parameters.
M = 16; % Size of signal constellation
k = log2(M); % Number of bits per symbol
n = 3e4; % Number of bits to process
nsamp = 1; % Oversampling rate

%% Signal Source
% Create a binary data stream as a column vector.
x = randint(n,1); % Random binary data stream

% Plot first 40 bits in a stem plot.
stem(x(1:40),'filled');
title('Random Bits');
xlabel('Bit Index'); ylabel('Binary Value');

2. Prepare to Modulate.   The qammod function implements a 16-QAM modulator. However, it expects to receive integers between 0 and 15 rather than 4-tuples of bits. Therefore, you must preprocess the binary data stream x before invoking qammod. In particular, you arrange each 4-tuple of values from x across a row of a matrix, using the reshape function in MATLAB, and then apply the bi2de function to convert each 4-tuple to a corresponding integer. (The .' characters after the reshape command form the unconjugated array transpose operator in MATLAB. For more information about this and the similar ' operator, see Reshaping a Matrix in the MATLAB documentation set.)

%% Bit-to-Symbol Mapping
% Convert the bits in x into k-bit symbols.
xsym = bi2de(reshape(x,k,length(x)/k).','left-msb');

%% Stem Plot of Symbols 
% Plot first 10 symbols in a stem plot.
figure; % Create new figure window.
stem(xsym(1:10));
title('Random Symbols');
xlabel('Symbol Index'); ylabel('Integer Value');

3. Modulate Using 16-QAM.   Having defined xsym as a column vector containing integers between 0 and 15, you can use qammod to modulate xsym using the baseband representation. Recall that M is 16, the alphabet size.

%% Modulation
% Modulate using 16-QAM.
y = qammod(xsym,M);

The result is a complex column vector whose values are in the 16-point QAM signal constellation. A later step in this example will show what the constellation looks like.

To learn more about modulation functions, see Modulation. Also, note that the qammod function does not apply any pulse shaping. To extend this example to use pulse shaping, see Pulse Shaping Using a Raised Cosine Filter. For an example that uses rectangular pulse shaping with PSK modulation, see basicsimdemo.

4. Add White Gaussian Noise.   Applying the awgn function to the modulated signal adds white Gaussian noise to it. The ratio of bit energy to noise power spectral density, Eb/N0, is arbitrarily set at 10 dB.

The expression to convert this value to the corresponding signal-to-noise ratio (SNR) involves k, the number of bits per symbol (which is 4 for 16-QAM), and nsamp, the oversampling factor (which is 1 in this example). The factor k is used to convert Eb/N0 to an equivalent Es/N0, which is the ratio of symbol energy to noise power spectral density. The factor nsamp is used to convert Es/N0 in the symbol rate bandwidth to an SNR in the sampling bandwidth.

%% Transmitted Signal
ytx = y;

%% Channel
% Send signal over an AWGN channel.
EbNo = 10; % In dB
snr = EbNo + 10*log10(k) - 10*log10(nsamp);
ynoisy = awgn(ytx,snr,'measured');

%% Received Signal
yrx = ynoisy;

To learn more about awgn and other channel functions, see Channels.

5. Create a Scatter Plot.   Applying the scatterplot function to the transmitted and received signals shows what the signal constellation looks like and how the noise distorts the signal. In the plot, the horizontal axis is the in-phase component of the signal and the vertical axis is the quadrature component. The code below also uses the title, legend, and axis functions in MATLAB to customize the plot.

%% Scatter Plot
% Create scatter plot of noisy signal and transmitted
% signal on the same axes.
h = scatterplot(yrx(1:nsamp*5e3),nsamp,0,'g.');
hold on;
scatterplot(ytx(1:5e3),1,0,'k*',h);
title('Received Signal');
legend('Received Signal','Signal Constellation');
axis([-5 5 -5 5]); % Set axis ranges.
hold off;

To learn more about scatterplot, see Scatter Plots.

6. Demodulate Using 16-QAM.   Applying the qamdemod function to the received signal demodulates it. The result is a column vector containing integers between 0 and 15.

%% Demodulation
% Demodulate signal using 16-QAM.
zsym = qamdemod(yrx,M);

7. Convert the Integer-Valued Signal to a Binary Signal.   The previous step produced zsym, a vector of integers. To obtain an equivalent binary signal, use the de2bi function to convert each integer to a corresponding binary 4-tuple along a row of a matrix. Then use the reshape function to arrange all the bits in a single column vector rather than a four-column matrix.

%% Symbol-to-Bit Mapping
% Undo the bit-to-symbol mapping performed earlier.
z = de2bi(zsym,'left-msb'); % Convert integers to bits.
% Convert z from a matrix to a vector.
z = reshape(z.',prod(size(z)),1);

8. Compute the System's BER.   Applying the biterr function to the original binary vector and to the binary vector from the demodulation step above yields the number of bit errors and the bit error rate.

%% BER Computation
% Compare x and z to obtain the number of errors and
% the bit error rate.
[number_of_errors,bit_error_rate] = biterr(x,z)

The statistics appear in the MATLAB Command Window. Your results might vary because the example uses random numbers.

number_of_errors =

    71


bit_error_rate =

    0.0024

To learn more about biterr, see Performance Results via Simulation.


© 1994-2005 The MathWorks, Inc.