Code Matlab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

So sánh SC-NOMA và TDMA

clc; clear variables;


close all;

d1 = 10; d2 = 9;
d3 = 4; d4 = 3;

eta = 4;

N = 10^4;

h1 = sqrt(d1^-eta)*(randn(1,N)+1i*randn(1,N))/sqrt(2);
h2 = sqrt(d2^-eta)*(randn(1,N)+1i*randn(1,N))/sqrt(2);
h3 = sqrt(d3^-eta)*(randn(1,N)+1i*randn(1,N))/sqrt(2);
h4 = sqrt(d4^-eta)*(randn(1,N)+1i*randn(1,N))/sqrt(2);

g1 = (abs(h1)).^2;
g2 = (abs(h2)).^2;
g3 = (abs(h3)).^2;
g4 = (abs(h4)).^2;

SNR = 20:2:40;
snr = db2pow(SNR);

a1 = 0.7; a2 = 1-a1;

b1 = 0.7; b2 = (3/4)*(1-b1); b3 = (3/4)*(1-(b1+b2)); b4 = 1


- (b1+b2+b3);
T = 1;
epsilon = (2^T)-1;

for u = 1:length(snr)
% a1 = min(1,epsilon*((1/snr(u)) +
g1)./(g1*(1+epsilon)));
% a2 = 1 - a1;
%
% a1 = min(1,epsilon*((1/snr(u)) +
g1)./(g1*(1+epsilon)));
% a2 = 1 - a1;

%1-2 3-4 (n-n, f-f)


C1(u) = mean(log2(1 + a1.*g1*snr(u)./(a2.*g1*snr(u)+1)));
C2(u) = mean(log2(1 + a2.*g2*snr(u)));
C3(u) = mean(log2(1 + a1.*g3*snr(u)./(a2.*g3*snr(u)+1)));
C4(u) = mean(log2(1 + a2.*g4*snr(u)));

%1-4 2-3 (n-f)


C11(u) = mean(log2(1 + a1.*g1*snr(u)./(a2.*g1*snr(u)
+1)));
C44(u) = mean(log2(1 + a2.*g4*snr(u)));
C22(u) = mean(log2(1 + a1.*g2*snr(u)./(a2.*g2*snr(u)
+1)));
C33(u) = mean(log2(1 + a2.*g3*snr(u)));

%TDMA
t1(u) = 0.25*mean(log2(1 + g1*snr(u)));
t2(u) = 0.25*mean(log2(1 + g2*snr(u)));
t3(u) = 0.25*mean(log2(1 + g3*snr(u)));
t4(u) = 0.25*mean(log2(1 + g4*snr(u)));

% %NOMA
n1(u) = mean(log2(1 +
b1.*g1*snr(u)./((b2+b3+b4).*g1*snr(u)+1)));
n2(u) = mean(log2(1 + b2.*g2*snr(u)./((b3+b4).*g2*snr(u)
+1)));
n3(u) = mean(log2(1 + b3.*g3*snr(u)./(b4.*g3*snr(u)+1)));
n4(u) = mean(log2(1 + b4.*g4*snr(u)));
%
% %
end

R1 = 0.5*(C1+C2+C3+C4);
R11 = 0.5*(C11+C22+C33+C44);
t = (t1+t2+t3+t4);
n = n1+n2+n3+n4;
figure;
plot(SNR,R11,'o-b','linewidth',1.5);hold on; grid on;
plot(SNR,R1,'o-r','linewidth',1.5);
plot(SNR,n,'o-m','linewidth',1.5);
plot(SNR,t,'o-k','linewidth',1.5);
legend('Hybrid NOMA N-F pairing','Hybrid NOMA N-N,F-F
pairing','SC-NOMA','TDMA');
xlabel('SNR (dB)');
ylabel('Sum rate (bps/Hz)');
So sánh công suất của OMA và NOMA

clc; clear variables; close all;

%SNR range
Pt = -114:5:-54; %in dB
pt = db2pow(Pt); %in linear scale

N = 10^4;

d1 = 5; d2 = 3; d3 = 2; %Distance of users
eta = 4; %Path loss exponent

%Rayleigh fading coefficients of both users


h1 = (sqrt(d1^-eta))*(randn(N,1) + 1i*randn(N,1))/sqrt(2);
h2 = (sqrt(d2^-eta))*(randn(N,1) + 1i*randn(N,1))/sqrt(2);
h3 = (sqrt(d3^-eta))*(randn(N,1) + 1i*randn(N,1))/sqrt(2);

%Channel gains
g1 = (abs(h1)).^2;
g2 = (abs(h2)).^2;
g3 = (abs(h3)).^2;

BW = 10^9;
No = -174 + 10*log10(BW);
no = (10^-3)*db2pow(No);

%Power allocation coefficients


a1 = 0.75; a2 = 0.1825; a3 = 1-(a1+a2);

C_noma = zeros(1,length(pt));
C_oma = zeros(1,length(pt));
for u = 1:length(pt)

%NOMA capacity calculation


C_noma_1 = log2(1 + pt(u)*a1.*g1./(pt(u)*a2.*g1 +
pt(u)*a3.*g1 + no)); %User 1
C_noma_2 = log2(1 + pt(u)*a2.*g2./(pt(u)*a3.*g1+no));
%User 2
C_noma_3 = log2(1 + pt(u)*a3.*g2/no);
%User 3
C_noma_sum(u) = mean(C_noma_1 + C_noma_2 + C_noma_3);
%Sum capacity of NOMA

%OMA capacity calculation


C_oma_1 = (1/3)*log2(1 + pt(u)*g1/no); %User 1
C_oma_2 = (1/3)*log2(1 + pt(u)*g2/no); %User 2
C_oma_3 = (1/3)*log2(1 + pt(u)*g3/no); %User 3

C_oma_sum(u) = mean(C_oma_1 + C_oma_2 + C_oma_3); %Sum


capacity of OMA

end
SNR = Pt - No;
figure;
plot(SNR,C_noma_sum,'linewidth',2); hold on; grid on;
plot(SNR,C_oma_sum,'linewidth',2)
xlabel('SNR (dB)');
ylabel('Achievable rate (bps/Hz)');
legend('NOMA','OMA');
title('Capacity of NOMA vs OMA');
ylim([0 max(C_noma_sum)+1]);

Mô phỏng BER của NOMA trong kênh AWGN (Nhiễu Gausse trắng cộng sinh)
clc; clear variables; close all;

SNR = 0:30; %SNR range in dB


snr = db2pow(SNR); %SNR range in linear scale

%Tao chuoi nhi phan co do dai N


N = 10^5;

x1 = randi([0 1],1,N); %Du lieu bit cua user 1


x2 = randi([0 1],1,N); %Du lieu bit cua user 2

%Dieu che BPSK(Pha nhi phan)cho du lieu dc tao o tren


xmod1 = 2*x1 - 1;
xmod2 = 2*x2 - 1;

%Thiet lap trong so cong suat cho cac user


a1 = 0.75;
a2 = 0.25;

%Thuc hien ma hoa chong chat


x = sqrt(a1)*xmod1 + sqrt(a2)*xmod2; %sqrt là Ham khai can

%Them AWGN vào x(Truyen x qua kenh AWGN)


for u = 1:length(snr)
y1 = awgn(x,SNR(u),'measured'); %T/h cua user 1 bi tac
dong boi AWGN
y2 = awgn(x,SNR(u),'measured'); %T/h cua user 2 bi tac
dong boi AWGN

%AT USER 1
%Direct decoding of x from y1
x1_hat = ones(1,N); %just a buffer
x1_hat(y1 < 0) = 0;

%AT USER 2
%Direct decoding of x from y2
x11_est = ones(1,N); %just a buffer
x11_est(y2 < 0) = 0; %Estimate user 1's signal first
x11_est(x11_est == 0) = -1; %Remodulate user 1's signal
%Subtract remodulated x11_est component from y2
rem = y2 - sqrt(a1)*x11_est; % T/h dang o dang d/che
BPSK

%Decode x2 from rem


x2_hat = zeros(1,N);
x2_hat(rem>0) = 1;

%Estimate BER
ber1(u) = biterr(x1,x1_hat)/N;% = so bit loi/ tong so
bit
ber2(u) = biterr(x2,x2_hat)/N;
end

%plot BER curves


semilogy(SNR, ber1, 'linewidth', 1.5); hold on;
semilogy(SNR, ber2, 'linewidth', 1.5); grid on;
legend('User 1 \alpha_1 = 0.75','User 2 \alpha_2 = 0.25');
xlabel('SNR (dB)');
ylabel('BER');
title('BER graph for NOMA in AWGN channel');
Dung lượng và xác suất ngừng hoạt động
clc; clear variables; close all;
N = 10^5;

d1 = 1000; d2 = 500; %Distances of users from base


station (BS)
a1 = 0.75; a2 = 0.25; %Power allocation factors
eta = 4; %Path loss exponent

%Generate rayleigh fading coefficient for both users


h1 = sqrt(d1^-eta)*(randn(1,N)+1i*randn(1,N))/sqrt(2);
h2 = sqrt(d2^-eta)*(randn(1,N)+1i*randn(1,N))/sqrt(2);

g1 = (abs(h1)).^2;
g2 = (abs(h2)).^2;

Pt = 0:2:40; %Transmit power in dBm


pt = (10^-3)*10.^(Pt/10); %Transmit power in linear scale
BW = 10^6; %System bandwidth
No = -174 + 10*log10(BW); %Noise power (dBm)
no = (10^-3)*10.^(No/10); %Noise power (linear scale)

p = length(Pt);
p1 = zeros(1,length(Pt));
p2 = zeros(1,length(Pt));

rate1 = 1; rate2 = 2; %Target rate of users in bps/Hz


for u = 1:p
%Calculate SNRs
gamma_1 = a1*pt(u)*g1./(a2*pt(u)*g1+no);
gamma_12 = a1*pt(u)*g2./(a2*pt(u)*g2+no);
gamma_2 = a2*pt(u)*g2/no;

%Calculate achievable rates


R1 = log2(1+gamma_1);
R12 = log2(1+gamma_12);
R2 = log2(1+gamma_2);

%Find average of achievable rates


R1_av(u) = mean(R1);
R12_av(u) = mean(R12);
R2_av(u) = mean(R2);

%Check for outage


for k = 1:N
if R1(k) < rate1
p1(u) = p1(u)+1;
end
if (R12(k) < rate1)||(R2(k) < rate2)
p2(u) = p2(u)+1;
end
end
end

pout1 = p1/N;
pout2 = p2/N;

figure;
semilogy(Pt, pout1, 'linewidth', 1.5); hold on; grid on;
semilogy(Pt, pout2, 'linewidth', 1.5);
xlabel('Transmit power (dBm)');
ylabel('Outage probability');
legend('User 1 (far user)','User 2 (near user)');

figure;
plot(Pt, R1_av, 'linewidth', 1.5); hold on; grid on;
plot(Pt, R12_av, 'linewidth', 1.5);
plot(Pt, R2_av, 'linewidth', 1.5);
xlabel('Transmit power (dBm)');
ylabel('Achievable capacity (bps/Hz)');
legend('R_1','R_{12}','R_2')

Mô phỏng BER của NOMA trong kênh Rayleigh


clc; clear variables; close all;

N = 10^6;

d1 = 1000; d2 = 500; %Distances of users from base


station (BS)
a1 = 0.75; a2 = 0.25; %Power allocation factors
eta = 4; %Path loss exponent

%Generate rayleigh fading coefficient for both users


h1 = sqrt(d1^-eta)*(randn(1,N)+1i*randn(1,N))/sqrt(2);
h2 = sqrt(d2^-eta)*(randn(1,N)+1i*randn(1,N))/sqrt(2);

g1 = (abs(h1)).^2;
g2 = (abs(h2)).^2;
Pt = 0:2:40; %Transmit power in dBm
pt = (10^-3)*10.^(Pt/10); %Transmit power in linear scale
BW = 10^6; %System bandwidth
No = -174 + 10*log10(BW); %Noise power (dBm)
no = (10^-3)*10.^(No/10); %Noise power (linear scale)

%Generate noise samples for both users


w1 = sqrt(no)*(randn(1,N)+1i*randn(1,N))/sqrt(2);
w2 = sqrt(no)*(randn(1,N)+1i*randn(1,N))/sqrt(2);

%Generate random binary data for two users


data1 = randi([0 1],1,N); %Data bits of user 1
data2 = randi([0 1],1,N); %Data bits of user 2

%Do BPSK modulation of data


x1 = 2*data1 - 1;
x2 = 2*data2 - 1;

p = length(Pt);
for u = 1:p
%Do superposition coding
x = sqrt(pt(u))*(sqrt(a1)*x1 + sqrt(a2)*x2);
%Received signals
y1 = h1.*x + w1; %So phuc h1 * x + w1
y2 = h2.*x + w2;

%Equalize
eq1 = y1./h1; %So phuc y1 * x + w1
eq2 = y2./h2;

%AT USER 1--------------------


%Direct decoding of x1 from y1
x1_hat = zeros(1,N);
x1_hat(eq1>0) = 1;

%Compare decoded x1_hat with data1 to estimate BER


ber1(u) = biterr(data1,x1_hat)/N;

%----------------------------------

%AT USER 2-------------------------


%Direct decoding of x1 from y2
x12_hat = ones(1,N);
x12_hat(eq2<0) = -1;
y2_dash = eq2 - sqrt(a1*pt(u))*x12_hat;
x2_hat = zeros(1,N);
x2_hat(real(y2_dash)>0) = 1;

ber2(u) = biterr(x2_hat, data2)/N;


%-----------------------------------

gam_a =
2*((sqrt(a1*pt(u))-sqrt(a2*pt(u)))^2)*mean(g1)/no;
gam_b =
2*((sqrt(a1*pt(u))+sqrt(a2*pt(u)))^2)*mean(g1)/no;
ber_th1(u) = 0.25*(2 - sqrt(gam_a/(2+gam_a)) -
sqrt(gam_b/(2+gam_b)));

gam_c = 2*a2*pt(u)*mean(g2)/no;
gam_d = 2*((sqrt(a2) + sqrt(a1))^2)*pt(u)*mean(g2)/no;
gam_e = 2*((sqrt(a2) + 2*sqrt(a1))^2)*pt(u)*mean(g2)/no;
gam_f = 2*((-sqrt(a2) + sqrt(a1))^2)*pt(u)*mean(g2)/no;
gam_g = 2*((-sqrt(a2) +
2*sqrt(a1))^2)*pt(u)*mean(g2)/no;

gc = (1 - sqrt(gam_c/(2+gam_c)));
gd = (1-sqrt(gam_d/(2+gam_d)));
ge = (1-sqrt(gam_e/(2+gam_e)));
gf = (1-sqrt(gam_f/(2+gam_f)));
gg = (1-sqrt(gam_g/(2+gam_g)));

ber_th2(u) = 0.5*gc - 0.25*gd + 0.25*(ge+gf-gg);

gamma1(u) = a1*pt(u)*mean(g1)/(a2*pt(u)*mean(g1) + no);


%gamma = SNR
gamma2(u) = a2*pt(u)*mean(g2)/no;
end

semilogy(Pt, ber1,'r', 'linewidth',1.5); hold on; grid on;


semilogy(Pt, ber2,'b', 'linewidth',1.5);
semilogy(Pt, ber_th1, '*r','linewidth',1.5);
semilogy(Pt, ber_th2, '*b','linewidth',1.5);
xlabel('Transmit power (P in dBm)');
ylabel('BER');
legend('Sim. User 1/Far user','Sim. User 2/Near user','Theo.
User 1/Far user','Theo. User 2/Near user');
BER của NOMA khi phân bổ công suất cố định
clc; clear variables;

N = 5*10^5;
Pt = [40 20];
No = -114;

pt = (10^-3)*db2pow(Pt);
no = (10^-3)*db2pow(No);

a1 = 0:0.01:1;
a2 = 1 - a1;

d1 = 1000; d2 = 500;

eta = 4;
h1 = sqrt(d1^-eta)*(randn(1,N) + 1i*randn(1,N))/sqrt(2);
h2 = sqrt(d2^-eta)*(randn(1,N) + 1i*randn(1,N))/sqrt(2);

g1 = (abs(h1)).^2;
g2 = (abs(h2)).^2;

%Generate noise samples for both users


w1 = sqrt(no)*(randn(1,N)+1i*randn(1,N))/sqrt(2);
w2 = sqrt(no)*(randn(1,N)+1i*randn(1,N))/sqrt(2);

data1 = randi([0 1],1,N);


data2 = randi([0 1],1,N);

x1 = 2*data1 - 1;
x2 = 2*data2 - 1;
for v=1:2
for u = 1:length(a1)
x = sqrt(pt(v))*(sqrt(a1(u))*x1 + sqrt(a2(u))*x2);
y1 = x.*h1 + w1;
y2 = x.*h2 + w2;

%Equalize
eq1 = y1./h1;
eq2 = y2./h2;

%AT USER 1--------------------


%Direct decoding of x1 from y1
x1_hat = zeros(1,N);
x1_hat(eq1>0) = 1;

%Compare decoded x1_hat with data1 to estimate BER


ber1(u) = biterr(data1,x1_hat)/N;

%----------------------------------

%AT USER 2-------------------------


%Direct decoding of x1 from y2
x12_hat = ones(1,N);
x12_hat(eq2<0) = -1;

y2_dash = eq2 - sqrt(a1(u)*pt(v))*x12_hat;


x2_hat = zeros(1,N);
x2_hat(real(y2_dash)>0) = 1;

ber2(u) = biterr(x2_hat, data2)/N;


%-----------------------------------
end
if v==1
semilogy(a1, ber1,'-r', 'linewidth',1.5); hold on; grid
on;
semilogy(a1, ber2,'-b', 'linewidth',1.5);
xlabel('\alpha_1');
ylabel('BER');

end
if v==2
semilogy(a1, ber1,'--r', 'linewidth',1.5); hold on; grid
on;
semilogy(a1, ber2,'--b', 'linewidth',1.5);
xlabel('\alpha_1');
ylabel('BER');

end

end
legend('Far user p = 40','Near user p = 40','Far user p =
20','Near user p = 20');
So sánh phân bổ công suất cố định và công bằng
clc; clear variables; close all;

N = 10^5;
Pt = 30; %Max BS Tx power (dBm)
pt = (10^-3)*db2pow(Pt); %Max BS Tx power (Linear
scale)
No = -114; %Noise power (dBm)
no = (10^-3)*db2pow(No); %Noise power (Linear scale)

r = 0.5:0.5:10; %Far user target rate range


(R*)

df = 1000; dn = 500; %Distances

eta = 4; %Path loss exponent

p1 = zeros(1,length(r));
p2 = zeros(1,length(r));
pa1 = zeros(1,length(r));
pa2 = zeros(1,length(r));

af = 0.75; an = 0.25; %Fixed PA (for comparison)

hf = sqrt(df^-eta)*(randn(1,N) + 1i*randn(1,N))/sqrt(2);
hn = sqrt(dn^-eta)*(randn(1,N) + 1i*randn(1,N))/sqrt(2);

g1 = (abs(hf)).^2;
g2 = (abs(hn)).^2;

for u = 1:length(r)
epsilon = (2^(r(u)))-1; %Target SINR for far
user

%BASIC FAIR PA%


aaf = min(1,epsilon*(no +
pt*g1)./(pt*g1*(1+epsilon)));
aan = 1 - aaf;

% %IMPROVED FAIR PA%


% aaf = epsilon*(no + pt*g1)./(pt*g1*(1+epsilon));
% aaf(aaf>1) = 0;
% aan = 1 - aaf;

gamma_f = pt*af*g1./(pt*g1*an + no);


gamma_nf = pt*af*g2./(pt*g2*an + no);
gamma_n = pt*g2*an/no;

gamm_f = pt*aaf.*g1./(pt*g1.*aan + no);


gamm_nf = pt*aaf.*g2./(pt*g2.*aan + no);
gamm_n = pt*g2.*aan/no;

Cf = log2(1 + gamma_f);
Cnf = log2(1 + gamma_nf);
Cn = log2(1 + gamma_n);

Ca_f = log2(1 + gamm_f);


Ca_nf = log2(1 + gamm_nf);
Ca_n = log2(1 + gamm_n);

for k = 1:N
if Cf(k) < r(u)
p1(u) = p1(u) + 1;
end
if (Cnf(k)<r(u))||(Cn(k) < r(u))
p2(u) = p2(u) + 1;
end

if Ca_f(k) < r(u)


pa1(u) = pa1(u) + 1;
end
if aaf(k) ~= 0
if (Ca_n(k) < r(u)) || (Ca_nf(k) < r(u))
pa2(u) = pa2(u) + 1;
end
else
if Ca_n(k) < r(u)
pa2(u) = pa2(u) + 1;
end
end
end
end
pout1 = p1/N;
pout2 = p2/N;
pouta1 = pa1/N;
pouta2 = pa2/N;

figure;
plot(r,pout1,'--+r','linewidth',2); hold on; grid on;
plot(r,pout2,'--ob','linewidth',2);
plot(r,pouta1,'r','linewidth',2);
plot(r,pouta2,'b','linewidth',2);
xlabel('Target rate of far user (R*) bps/Hz');
ylabel('Outage probability');
xlim([r(1) r(end)]);
legend('Far user (fixed PA)','Near user (fixed PA)','Far
user (fair PA)','Near user (fair PA)');

Mã hóa chồng chất

clc; clear all; close all;

x1 = [1 0 1 0];
x2 = [0 1 1 0];

xmod1 = 2*x1-1;
xmod2 = 2*x2-1;

a1 = 0.75; a2 = 0.25;
x = sqrt(a1)*xmod1 + sqrt(a2)*xmod2;

%Plot figures

ay = -2:0.2:2;
ax = ones(1,length(ay));

figure;
subplot(2,1,1)
stairs([x1,x1(end)],'linewidth',2);
ylim([-2 2])
grid on; hold on;
title('Data of user 1 (x_1)')
for u = 1:3
plot(ax*(u+1),ay,':k','linewidth',2);
end
subplot(2,1,2)
stairs([x2,x2(end)],'m','linewidth',2);
ylim([-2 2])
grid on; hold on;
for u = 1:3
plot(ax*(u+1),ay,':k','linewidth',2);
end
title('Data of user 2 (x_2)')

figure;
subplot(2,1,1)
stairs([xmod1,xmod1(end)],'linewidth',2);
ylim([-2 2])
grid on; hold on;
title('Data of user 1 (x_1)')
for u = 1:3
plot(ax*(u+1),ay,':k','linewidth',2);
end
subplot(2,1,2)
stairs([xmod2,xmod2(end)],'m','linewidth',2);
ylim([-2 2])
grid on; hold on;
for u = 1:3
plot(ax*(u+1),ay,':k','linewidth',2);
end
title('Data of user 2 (x_2)');

t1 = sqrt(a1)*xmod1;
t2 = sqrt(a2)*xmod2;
figure;
subplot(2,1,1)
stairs([t1,t1(end)],'linewidth',2);
ylim([-2 2])
grid on; hold on;
title('Scaled data of user 1 ($$\sqrt{a_1}x_1$
$)','Interpreter','latex','FontSize',13)
for u = 1:3
plot(ax*(u+1),ay,':k','linewidth',2);
end
subplot(2,1,2)
stairs([t2,t2(end)],'m','linewidth',2);
ylim([-2 2])
title('Scaled data of user 2 ($$\sqrt{a_2}x_2$
$)','Interpreter','latex','FontSize',13)
grid on; hold on;

for u = 1:3
plot(ax*(u+1),ay,':k','linewidth',2);
end

figure;
stairs([x,x(end)],'r','linewidth',2);
grid on; hold on;
for u = 1:3
plot(ax*(u+1),ay,':k','linewidth',2);
end
title('Superposition coded signal')

Loại bỏ nhiễu liên tiếp (SIC)

clc; clear all; close all;

x1 = [1 0 1 0];
x2 = [0 1 1 0];

xmod1 = 2*x1-1;
xmod2 = 2*x2-1;

a1 = 0.75; a2 = 0.25;
x = sqrt(a1)*xmod1 + sqrt(a2)*xmod2;

xdec1 = ones(1,length(x1));
xdec1(x<0)=-1;

xrem = x - sqrt(a1)*xdec1;
xdec2 = zeros(1,length(x1));
xdec1(x<0)=0;
xdec2(xrem>0)=1;

%Plot figures

ay = -2:0.2:2;
ax = ones(1,length(ay));

figure;
subplot(2,1,1)
stairs([x1,x1(end)],'linewidth',2);
ylim([-2 2])
grid on; hold on;
title('Data of user 1 (x_1)')
for u = 1:3
plot(ax*(u+1),ay,':k','linewidth',2);
end
subplot(2,1,2)
stairs([x2,x2(end)],'m','linewidth',2);
ylim([-2 2])
grid on; hold on;
for u = 1:3
plot(ax*(u+1),ay,':k','linewidth',2);
end
title('Data of user 2 (x_2)')

figure;
subplot(2,1,1)
stairs([xmod1,xmod1(end)],'linewidth',2);
ylim([-2 2])
grid on; hold on;
title('Data of user 1 (x_1)')
for u = 1:3
plot(ax*(u+1),ay,':k','linewidth',2);
end
subplot(2,1,2)
stairs([xmod2,xmod2(end)],'m','linewidth',2);
ylim([-2 2])
grid on; hold on;
for u = 1:3
plot(ax*(u+1),ay,':k','linewidth',2);
end
title('Data of user 2 (x_2)');

t1 = sqrt(a1)*xmod1;
t2 = sqrt(a2)*xmod2;
figure;
subplot(2,1,1)
stairs([t1,t1(end)],'linewidth',2);
ylim([-2 2])
grid on; hold on;
title('Scaled data of user 1 ($$\sqrt{a_1}x_1$
$)','Interpreter','latex','FontSize',13)
for u = 1:3
plot(ax*(u+1),ay,':k','linewidth',2);
end
subplot(2,1,2)
stairs([t2,t2(end)],'m','linewidth',2);
ylim([-2 2])
title('Scaled data of user 2 ($$\sqrt{a_2}x_2$
$)','Interpreter','latex','FontSize',13)
grid on; hold on;

for u = 1:3
plot(ax*(u+1),ay,':k','linewidth',2);
end

figure;
stairs([x,x(end)],'r','linewidth',2);
grid on; hold on;
for u = 1:3
plot(ax*(u+1),ay,':k','linewidth',2);
end
title('Superposition coded signal')
plot(1:5,zeros(1,5),'k','linewidth',1.5)

figure;
stairs([xrem,xrem(end)],'r','linewidth',2);
grid on; hold on;
for u = 1:3
plot(ax*(u+1),ay,':k','linewidth',2);
end
title('Superposition coded signal')
plot(1:5,zeros(1,5),'k','linewidth',1.5)

You might also like