% This program simulates the simple example in ECE 587 notes
clear;clc;close all;
e1 = [-1 -1 -1 1 -1 -1 1 1 -1 1 -1 1 1 1 1];e2 = [-1 -1 -1 1 1 1 1 -1 1 -1 1 1 -1 -1 1]; % Spreading PN sequences
vmat = [-1 -1;-1 +1;+1 -1;+1 +1];v1 = -1;v2 = 1; % Probable message signals and actual Message signals of user1 and 2
Ep1 = 1;Ep2 = 1; %%% square root of epsilon1 and epsilon2
Rc = [e1*e1' e1*e2';e2*e1' e2*e2']; % Computation of crosscorellation matrix
%%%%%% Spread the signals add them and also add WGN
rt = Ep1*[-1.2 2.9 -0.5 0.5 1.3 0.7 -0.1 0.3 -1.5 2.8 -1.35 0.29 -1.1 0.4 -0.83] + ....
Ep2*[2.1 -1.4 0.8 1.1 -0.6 -0.1 0.2 -1.9 0.34 0.91 -0.65 2.3 -0.56 -0.1 -1.2];
%%%% Detection and Decision at Receiver
%%%% Note that matrix multiplication also performs the summation
r1 = rt*e1';r2 = rt*e2';rk = [r1;r2];
%%% Computation of C(r_k, a_k) values
Crkakm1m1 = 2*[-Ep1 -Ep2]*[r1;r2] - [-Ep1 -Ep2]*Rc*[-Ep1; -Ep2]; % Testing for the case of [-1 -1]
Crkakm1p1 = 2*[-Ep1 +Ep2]*[r1;r2] - [-Ep1 +Ep2]*Rc*[-Ep1; +Ep2]; % Testing for the case of [-1 +1]
Crkakp1m1 = 2*[+Ep1 -Ep2]*[r1;r2] - [+Ep1 -Ep2]*Rc*[+Ep1; -Ep2]; % Testing for the case of [+1 -1]
Crkakp1p1 = 2*[+Ep1 +Ep2]*[r1;r2] - [+Ep1 +Ep2]*Rc*[+Ep1; +Ep2]; % Testing for the case of [+1 +1]
Crkak = [Crkakm1m1 Crkakm1p1 Crkakp1m1 Crkakp1p1];[C, I] = max(Crkak);vdet = vmat(I,:);ak = sign(inv(Rc)*rk);
'According to multiuser optimum detection strategy'
vdet'
'were transmitted by user 1 and 2'
%%%%% Suboptimum detection excluding the sign
'According to multiuser suboptimum detection strategy'
ak
'were transmitted by user 1 and 2'