Slotted_ALOHA.m
%Pure ALOHA
N1 = 8;
N2 = 4;
N3 = 3;
N4 = 2;
N5 = 1;
p = 0:0.01:1;
Throughput1 = N1*p.*(1-p).^(N1-1);
Throughput2 = N2*p.*(1-p).^(N2-1);
Throughput3 = N3*p.*(1-p).^(N3-1);
Throughput4 = N4*p.*(1-p).^(N4-1);
Throughput5 = N5*p.*(1-p).^(N5-1);
figure
%% 绘制图一
plot(p,Throughput1,p,Throughput2,p,Throughput2,p,Throughput3,p,Throughput4,p,Throughput5);
xlabel('Transmission probability p')
ylabel('Throughput')
title('Throughput of Slotted ALOHA method')
legend('N1 = 8','N2 = 4','N3 = 3','N4 = 2','N5 = 1')
N = 1:40;
p = 1./N;
Throughput = N.*(p.*(1-p).^(N-1));
figure
%% 绘制图二
plot(N,Throughput)
xlabel('Number of Nodes N')
ylabel('Throughput')
title('Maximum throughput of Slotted ALOHA method')
G = 0:0.1:5;
S = G.*exp(-G);
figure
%% 绘制图三
plot(G,S)
xlabel('Offered Load rate G')
ylabel('Throughput')
title('Throughput of Slotted ALOHA protocol')
Pure_ALOHA.m
%Pure ALOHA
N1 = 8;
N2 = 4;
N3 = 3;
N4 = 2;
N5 = 1;
p = 0:0.01:1;
Throughput1 = N1*p.*(1-p).^(2*N1-2);
Throughput2 = N2*p.*(1-p).^(2*N2-2);
Throughput3 = N3*p.*(1-p).^(2*N3-2);
Throughput4 = N4*p.*(1-p).^(2*N4-2);
Throughput5 = N5*p.*(1-p).^(2*N5-2);
figure
plot(p,Throughput1,p,Throughput2,p,Throughput2,p,Throughput3,p,Throughput4,p,Throughput5);
xlabel('传输概率p')
ylabel('吞吐量')
title(' Pure ALOHA 吞吐量')
legend('N1 = 8','N2 = 4','N3 = 3','N4 = 2','N5 = 1')
N = 1:40;
p = 1./(2*N-1);
Throughput = N.*(p.*(1-p).^(2*N-2));
figure
plot(N,Throughput)
xlabel('Number of Nodes N')
ylabel('吞吐量')
title('ALOHA协议的最大吞吐量')
G = 0:0.1:5;
S = G.*exp(-2*G);
figure
plot(G,S)
xlabel('负载率G')
ylabel('吞吐量')
title('ALOHA协议的吞吐量')
CSMA.m
close all;
clear all;
%% Unslotted 0-persistent CSMA 非时隙 0-持续CSMA
a0 = 0;
a1 = 0.001;
a2 = 0.01;
a3 = 0.1;
a4 = 1;
G = 0:10^-3:1000;
S0 = G.*exp(-a0*G)./(G.*(1+2*a0)+exp(-a0*G));
S1 = G.*exp(-a1*G)./(G.*(1+2*a1)+exp(-a1*G));
S2 = G.*exp(-a2*G)./(G.*(1+2*a2)+exp(-a2*G));
S3 = G.*exp(-a3*G)./(G.*(1+2*a3)+exp(-a3*G));
S4 = G.*exp(-a4*G)./(G.*(1+2*a4)+exp(-a4*G));
figure
semilogx(G,S0,G,S1,G,S2,G,S3,G,S4)
xlabel('提供的负载 (G)');
ylabel('吞吐量 (S)');
title('Throughput-Load of Unslotted Non-persistent CSMA');
legend('a = 0','a=0.001','a=0.01','a=0.1','a=1');
%% Slotted 0-persistent CSMA 非坚持型 CSMA 的理论吞吐量公式
a0 = 0;
a1 = 0.001;
a2 = 0.01;
a3 = 0.1;
a4 = 1;
G = 0:10^-3:1000;
S0 = G.*(a0*exp(-a0*G))./(1-exp(-a0*G)+a0);
S1 = G.*(a1*exp(-a1*G))./(1-exp(-a1*G)+a1);
S2 = G.*(a2*exp(-a2*G))./(1-exp(-a2*G)+a2);
S3 = G.*(a3*exp(-a3*G))./(1-exp(-a3*G)+a3);
S4 = G.*(a4*exp(-a4*G))./(1-exp(-a4*G)+a4);
figure
semilogx(G,S0,G,S1,G,S2,G,S3,G,S4)
xlabel('提供的负载 (G)');
ylabel('吞吐量 (S)');
title('时隙非持久的吞吐量负载 CSMA');
legend('a = 0','a=0.001','a=0.01','a=0.1','a=1');
%% Unslotted 1-persistent CSMA
a0 = 0;
a3 = 0.1;
a4 = 1;
G = 0:10^-2:100;
S0 = G.*exp(-G).*(1+G)./(G+exp(-G));
S3 = G.*(1+G+a3*G.*(1+G+a3*G/2)).*exp(-G*(1+2*a3))./(G*(1+2*a3)-(1-exp(-a3*G))+(1+a3*G).*exp(-G*(1+a3)));
S4 = G.*(1+G+a4*G.*(1+G+a4*G/2)).*exp(-G*(1+2*a4))./(G*(1+2*a4)-(1-exp(-a4*G))+(1+a4*G).*exp(-G*(1+a4)));
figure
semilogx(G,S0,G,S3,G,S4)
xlabel('Offered Load (G)');
ylabel('Throughput (S)');
title('Throughput-Load of Unslotted 1-persistent CSMA');
legend('a = 0','a=0.1','a=1');
%% Slotted 1-persistent CSMA 当站点发现信道空闲时,它将立即发送其帧(概率为1)。
a0 = 0;
a3 = 0.1;
a4 = 1;
G = 0:10^-2:100;
S0 = G.*exp(-G).*(1+G)./(G+exp(-G));
S3 = G.*(1+a3-exp(-a3*G)).*exp(-G*(1+a3))./((1+a3)*(1-exp(-a3*G))+a3*exp(-G*(1+a3)));
S4 = G.*(1+a4-exp(-a4*G)).*exp(-G*(1+a4))./((1+a4)*(1-exp(-a4*G))+a4*exp(-G*(1+a4)));
figure
semilogx(G,S0,G,S3,G,S4)
xlabel('Offered Load (G)');
ylabel('吞吐量 (S)');
title('时隙1-persistent的吞吐量负载CSMA');
legend('a = 0','a=0.1','a=1');
main.m
close all;
clear all;
%% Pure ALOHA vs Slotted ALOHA
G = 0:0.1:5;
S_slotted = G.*exp(-G);
S_pure = G.*exp(-2*G);
figure
plot(G,S_slotted,G,S_pure)
xlabel('Offered Load rate G')
ylabel('Throughput')
title('Throughput of Slotted and Pure ALOHA protocol')
legend('Slotted ALOHA', 'Pure ALOHA')
%% Unslotted and Slotted Non-persistent CSMA
a1 = 0.001;
a2 = 0.01;
G = 0:10^-3:10^4;
S1_u = G.*exp(-a1*G)./(G.*(1+2*a1)+exp(-a1*G));
S1_s = G.*(a1*exp(-a1*G))./(1-exp(-a1*G)+a1);
S2_u = G.*exp(-a2*G)./(G.*(1+2*a2)+exp(-a2*G));
S2_s = G.*(a2*exp(-a2*G))./(1-exp(-a2*G)+a2);
figure
semilogx(G,S1_u,G,S1_s,G,S2_u,G,S2_s)
xlabel('Offered Load (G)');
ylabel('Throughput (S)');
title('Comparing throughput-load of slotted and unslotted non-persistent CSMA');
legend('unslotted a=0.001','slotted a=0.001','unslotted a=0.01','slotteda=0.01');
%% Unslotted and Slotted 1-persistent CSMA
a3 = 0.1;
a4 = 1;
G = 0:10^-3:10^1;
S3_u = G.*(1+G+a3*G.*(1+G+a3*G/2)).*exp(-G*(1+2*a3))./(G*(1+2*a3)-(1-exp(-a3*G))+(1+a3*G).*exp(-G*(1+a3)));
S3_s = G.*(1+a3-exp(-a3*G)).*exp(-G*(1+a3))./((1+a3)*(1-exp(-a3*G))+a3*exp(-G*(1+a3)));
S4_u = G.*(1+G+a4*G.*(1+G+a4*G/2)).*exp(-G*(1+2*a4))./(G*(1+2*a4)-(1-exp(-a4*G))+(1+a4*G).*exp(-G*(1+a4)));
S4_s = G.*(1+a4-exp(-a4*G)).*exp(-G*(1+a4))./((1+a4)*(1-exp(-a4*G))+a4*exp(-G*(1+a4)));
figure
semilogx(G,S3_u,G,S3_s,G,S4_u,G,S4_s)
xlabel('Offered Load (G)');
ylabel('Throughput (S)');
title('Comparing throughput-load of Slotted and Unslotted 1-persistent CSMA');
legend('unslotted a=0.1','slotted a=0.1','unslotted a=1','slotteda=1');
%% Comparing between all the protocols
a = 0.8;
G = 0:10^-3:1000;
S_pure = G.*exp(-2*G);
S_slotted = G.*exp(-G);
S_u0 = G.*exp(-a*G)./(G.*(1+2*a)+exp(-a*G));
S_s0 = G.*(a*exp(-a*G))./(1-exp(-a*G)+a);
S_u1 = G.*(1+G+a*G.*(1+G+a*G/2)).*exp(-G*(1+2*a))./(G*(1+2*a)-(1-exp(-a*G))+(1+a*G).*exp(-G*(1+a)));
S_s1 = G.*(1+a-exp(-a*G)).*exp(-G*(1+a))./((1+a)*(1-exp(-a*G))+a*exp(-G*(1+a)));
figure
semilogx(G,S_pure,G,S_slotted,G,S_u0,G,S_s0,G,S_u1,G,S_s1)
xlabel('Offered Load (G)');
ylabel('Throughput (S)');
title('Throughput for the various access mode (a=0.8)');
legend('Pure ALOHA','slotted ALOHA','unslotted 0-persistent CSMA','slotted 0-persistent CSMA','unslotted 1-persistent CSMA','slotted 1-persistent CSMA');
%% CSMA/CA
这里主要是分享啊,有的博客是在是找不到了,浏览历史太多了,后期找到会及时补上代码和一些分析是从知网和GIthub 拿过来进行的仿真,这里做一个总结,文献pdf 的话可以百度呀,
博客:
- ALOHA 协议简介
- 1-CSMA 系统模型仿真
- CSMA 简介
文献引用
- 基于CSMA_CA的无线自组网MAC协议性能分析_胡剑
- ALOHA协议的性能分析_王秉钧