% COMMGETTINGSTARTED Simple communications example.
%
%   This script creates a random digital message signal
%   consisting of integers between 0 and 8, and modulates
%   it using two varieties of 8-ary quadrature amplitude 
%   shift-key (QASK) modulation.  Both varieties use square
%   constellations with the same points, but one variety labels 
%   the constellation points so as to implement Gray code and 
%   the other does not.  After modulating, this script adds 
%   noise to both modulated signals, demodulates both noisy 
%   signals, and computes the bit error rates in the two cases.
%   It outputs the two bit error rates and also plots the 
%   signal constellation for the Gray code case.
%
%   A more detailed explanation appears in Chapter 1 of
%   the Communications Toolbox User's Guide.

%   Copyright 1996-2000 The MathWorks, Inc.
%   $Revision: 1.4 $ $Date: 2000/05/19 14:06:42 $

% 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.

% Create a signal.
signal = randint(len,1,M); % Random digital message
% consisting of integers between 0 and M-1

% 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);

% 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);

% 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);

% 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))])

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

% [EOF] commgettingstarted.m
