Communications Toolbox |

**How the Example Works**

This section displays and explains the example code, piece by piece.

**Setting Up Parameters**

The first part of the example defines variables that the rest of the example will use. The symbol alphabet has `M`

different symbols, namely, the integers between 0 and `M-1`

. The message will be a column vector having `len`

entries, each of which is chosen from the symbol alphabet.

The variables `Fd`

and `Fs`

refer to the relative sampling rates for the modulation scheme. They would be more meaningful if the example were sampling a real signal that had a natural notion of time. However, since this example uses a random signal that does not have a built-in notion of time, the main purpose of `Fd`

and `Fs`

is to indicate that the modulated signal has three entries for every one entry of the original signal.

% Set up parameters. M = 8; % Number of symbols in alphabet len = 10000; % Number of symbols in the original message Fd = 1; % Assume the original message is sampled % at a rate of 1 sample per second. Fs = 3; % The modulated signal will be sampled % at a rate of 3 samples per second.

**Creating the Signal**

The variable `signal`

is a `len`

-by-1 matrix, that is, a column vector of length `len`

, whose entries are randomly chosen integers between 0 and `M-1`

. This is the signal that the example will modulate. The `randint`

function is part of this toolbox.

```
% Create a signal.
signal =
````randint`

(len,1,M); % Random digital message
% consisting of integers between 0 and M-1

**Modulating the Signal**

This part of the example modulates the data in the column vector `signal`

in two different ways. The `dmodce`

function performs both modulations and puts the results into the two-column matrix `modsignal`

.

The first call to `dmodce`

, which creates the first column of `modsignal`

, tells `dmodce`

to use QASK modulation on `M`

symbols. The string `'qask'`

indicates the QASK method as well as the default square constellation configuration. In this case, the configuration implements Gray code.

The second call to `dmodce`

, which creates the second column of `modsignal`

, tells `dmodce`

to use QASK modulation with a signal constellation whose configuration is represented in the vectors `inphase`

and `quad`

. The variables `inphase`

and `quad`

are length-`M`

vectors that list the in-phase and quadrature components, respectively, of the points in the signal constellation. The points are listed in sequence, to associate a message symbol of k with the (k+1)st elements in `inphase`

and `quad`

. Whereas Gray code labels the constellation points in a special way, this configuration lists points in a sequence that is merely convenient for creating `inphase`

and `quad`

.

These lines also illustrate some common ways to manipulate matrices in MATLAB. If you are not familiar with MATLAB's colon notation or with functions like `ones`

and `zeros`

, then you should consult the MATLAB documentation set.

% Use M-ary QASK modulation with two different labeled % square constellations. modsignal(:,1) =`dmodce`

(signal,Fd,Fs,'qask',M); inphase = [-3:2:3 -3:2:3]; quad = [ones(1,4), -1*ones(1,4)]; modsignal(:,2) =`dmodce`

(signal,Fd,Fs,'qask/arb',inphase,quad);

**Adding Noise**

According to the definition of baseband QASK modulation, `modsignal`

is a *complex* matrix having `len*Fs/Fd`

rows and two columns. The command below adds normally distributed random numbers to the real and imaginary parts of `modsignal`

, to produce a noisy signal `noisy`

. The `randn`

function is a built-in MATLAB function.

Notice that the command adds to `modsignal`

an entire real matrix of the appropriate size and an entire imaginary matrix of the appropriate size. Using a loop to add noise to individual scalar entries of `modsignal`

would be less efficient, since MATLAB is optimized for matrix operations.

% Add noise to real and imaginary parts of the modulated signal. noisy = modsignal+.5*`randn`

(len*Fs/Fd,2)... +j*.5*`randn`

(len*Fs/Fd,2);

**Demodulating the Signal**

This part of the example demodulates the noisy modulated signal, `noisy`

, in two different ways. The `ddemodce`

function performs both demodulations by operating on each column of `noisy`

separately. In each case, `ddemodce`

puts the results into the two-column matrix `newsignal`

.

% Demodulate to recover the message. newsignal(:,1) = ddemodce(noisy(:,1),Fd,Fs,'qask',M); newsignal(:,2) = ddemodce(noisy(:,2),Fd,Fs,... 'qask/arb',inphase,quad);

**Computing and Displaying Bit Error Rates**

The `biterr`

function compares each demodulated signal (that is, each column of `newsignal`

) to the original signal. Then `biterr`

computes the number of bit errors, as well as the rate or fraction of bit errors. The built-in MATLAB function `disp`

displays the two bit error rates in the command window.

% Check whether Gray code resulted in fewer bit errors. % Compare signal with each column of newsignal. [num,rate] = biterr(newsignal,signal); disp('Bit error rates for the two constellations used here') disp('----------------------------------------------------') disp(['Gray code constellation: ', num2str(rate(1))]) disp(['Non-Gray code constellation: ', num2str(rate(2))])

**Plotting a Signal Constellation**

The `modmap`

function plots and labels the default square signal constellation having `M`

points. The constellation that `inphase`

and `quad`

determine looks the same, except that the points are labeled from left to right across each row in the diagram, starting with the upper row.

% Plot signal constellations with Gray code labeling. modmap('qask',M);

Where to Find the Example | Output from the Example |