%% Setup : Define parameters
% Modified Plot, With Gray Coding M = 16; % Number of points in constellation k = log2(M); % Number of bits per symbol n = 5e5; % Number of bits to process nsamp = 4; % Oversampling rate hMod = modem.qammod('M',M,'SymbolOrder','Gray'); % Modulator object mapping = hMod.SymbolMapping; % Symbol mapping vector pt = hMod.Constellation; % Vector of all points in constellation scatterplot(pt); % Plot the constellation.
% Include text annotations that number the points. text(real(pt)+0.1,imag(pt),dec2bin(mapping)); axis([-4 4 -4 4]); % Change axis so all labels fit in plot.
%% Signal Source : Create
a binary data stream as a column vector.
x = randint(n,1); % Random binary data
stream
%% Encoder : Define
a convolutional coding trellis and use it to encode the binary data.
t =
poly2trellis([5 4],[23 35 0; 0 5 13]); % Trellis
code =
convenc(x,t); % Encode.
coderate = 2/3;
%% Bit-to-Symbol Mapping : Convert
the bits in x into k-bit symbols.
% B. Do ordinary
binary-to-decimal mapping.
xsym =
bi2de(reshape(code,k,length(code)/k).','left-msb');
%% Modulation
y = modulate(modem.qammod(M),xsym); %
Modulate using 16-QAM.
%% Filter
Definition : Define filter-related parameters.
filtorder = 40; %
Filter order
delay =
filtorder/(nsamp*2); % Group delay (# of input samples)
rolloff = 0.25; %
Rolloff factor of filter
% Create a square
root raised cosine filter.
rrcfilter =
rcosine(1,nsamp,'fir/sqrt',rolloff,delay);
% Plot impulse
response.
figure;
impz(rrcfilter,1);
%% Transmitted
Signal : Upsample and apply square root raised
cosine filter.
ytx =
rcosflt(y,1,nsamp,'filter',rrcfilter);
% Create eye
diagram for part of filtered signal.
eyediagram(ytx(1:2000),nsamp*2);
%% Channel
: Send signal over an AWGN
channel.
EbNo = 10; % In dB
snr = EbNo +
10*log10(k*coderate)-10*log10(nsamp);
ynoisy = awgn(ytx,snr,'measured');
%% Received
Signal : Filter received signal using square root
raised cosine filter.
yrx = rcosflt(ynoisy,1,nsamp,'Fs/filter',rrcfilter);
yrx =
downsample(yrx,nsamp); % Downsample.
yrx =
yrx(2*delay+1:end-2*delay); % Account for delay.
%% Scatter Plot : Create
scatter plot of received signal before and after filtering.
h =
scatterplot(sqrt(nsamp)*ynoisy(1:nsamp*5e3),nsamp,0,'g.');
hold on;
scatterplot(yrx(1:5e3),1,0,'kx',h);
title('Received
Signal, Before and After Filtering');
legend('Before
Filtering','After Filtering');
axis([-5 5 -5 5]); % Set axis ranges.
%% Demodulation : Demodulate
signal using 16-QAM.
zsym = demodulate(modem.qamdemod(M),yrx);
%% 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.',numel(z),1);
%% Decoder : Decode
the convolutional code.
tb = 16; %
Traceback length for decoding
z =
vitdec(z,t,tb,'cont','hard'); % Decode.
%% BER
Computation : Compare x and z to obtain the number of
errors and the bit error rate. Take the decoding delay into account.
decdelay = 2*tb;
% Decoder delay, in bits
[number_of_errors,bit_error_rate]
= biterr(x(1:end-decdelay),z(decdelay+1:end))
|