【Aloha通信】基于Matlab模拟时隙 Aloha 协议

✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。

🍎个人主页:Matlab科研工作室

🍊个人信条:格物致知。

更多Matlab仿真内容点击👇

智能优化算法       神经网络预测       雷达通信       无线传感器        电力系统

信号处理              图像处理               路径规划       元胞自动机        无人机 

⛄ 内容介绍

​时隙ALOHA使用了各种控制算法以保证系统的稳定性,在应用控制算法前,必须对算法的性能进行前期仿真测试.在比较三种常用网络仿真工具的基础上,分析了MATLAB离散事件仿真原理及基于蒙特卡洛法的随机数生成原理.给出了MATLAB仿真平台下实现时隙ALOHA控制算法的流程图,并依据该流程编写了MATLAB代码对时隙ALOHA的伪贝叶控制算法性能进行测试.仿真结果表示,MATLAB能很好的实现对时隙ALOHA控制算法的仿真.

⛄ 完整代码

clear all; close all; clc;

i=1;

for sourceNumber=[10, 25, 50]

    j=1;

    for lambda=[0:0.2:8]

        simulationTime=1e5;

        sourceStatus = zeros(1,sourceNumber);

        % 0: idle source

        % 1: active

        attemptSource = 0;

        pcktTransmissionAttempts = 0;

        %nodeDelay = zeros(1, sourceNumber);

        %sumDelay=0;

        ackdPacketCount = 0;

        pcktCollisionCount = 0;

        currentSlot = 0;

        pr=lambda/sourceNumber;

        %fileID = fopen('lambda5.txt','w');

        

        while currentSlot < simulationTime

            currentSlot = currentSlot + 1;

            %fprintf(fileID, 'slot = %i \n', currentSlot);

            transmissionAttemptsEachSlot = 0;

            for source = 1:sourceNumber

                if sourceStatus(source) == 0 && rand(1) <= pr % new packet

                    sourceStatus(source) = 1;

                    %nodeDelay(source)=0;

                    transmissionAttemptsEachSlot = transmissionAttemptsEachSlot+1;

                    pcktTransmissionAttempts = pcktTransmissionAttempts+1;

                    attemptSource = source;

                    %fprintf(fileID, 'station %d is transmitting new packet \n', source);

                elseif sourceStatus(source)==1 % active packet

                    %nodeDelay(source) = nodeDelay(source)+1;

                    if rand(1) <= pr

                        transmissionAttemptsEachSlot = transmissionAttemptsEachSlot+1;

                        pcktTransmissionAttempts = pcktTransmissionAttempts+1;

                        attemptSource = source;

                        %fprintf(fileID, 'station %d is transmitting backlogged packet \n', source);

                    end

                end

            end

            

            if transmissionAttemptsEachSlot == 1

                ackdPacketCount = ackdPacketCount + 1;

                %sumDelay = sumDelay+nodeDelay(attemptSource);

                sourceStatus(attemptSource) = 0;

                %fprintf(fileID, 'station %d packet is successfull with delay %d \n', attemptSource, nodeDelay(attemptSource));

            elseif transmissionAttemptsEachSlot>1

                pcktCollisionCount = pcktCollisionCount+1;

                %fprintf(fileID, 'COLLISION Happens \n');

            end

        end

        

        trafficOffered(i,j) = pcktTransmissionAttempts / currentSlot;

        %     if ackdPacketCount == 0

        %         meanDelay = simulationTime; % theoretically, if packets collide continously, the delay tends to infinity

        %     else

        %         meanDelay = sumDelay/ackdPacketCount;

        %     end

        throughput(i,j) = ackdPacketCount / currentSlot;

        pcktCollisionProb(i,j) = pcktCollisionCount / currentSlot;

        %fclose(fileID);

        j=j+1

    end

    i=i+1

end

%% plot

figure(1)

plot(trafficOffered(1,:),throughput(1,:), '-x')

title('Average Throughput of Finite-Station Slotted ALOHA')

xlabel('G (Offered Traffic)')

ylabel('Average Throughput')

hold on;

G=[0:0.2:8];

S=G.*(1-G/10).^(10-1);

plot(G,S);

hold on;

plot(trafficOffered(2,:),throughput(2,:), '-s')

hold on;

S=G.*(1-G/25).^(25-1);

plot(G,S);

hold on;

plot(trafficOffered(3,:),throughput(3,:), '-o')

hold on;

S=G.*(1-G/50).^(50-1);

plot(G,S);

legend('simulation, M=10', 'analytical, M=10','simulation, M=25', 'analytical, M=25', 'simulation, M=50', 'analytical, M=50');

grid on

figure(2)

plot(trafficOffered(1,:),pcktCollisionProb(1,:), '-x')

title('Collision Probability of Finite-Station Slotted ALOHA')

xlabel('G (Offered Traffic)')

ylabel('Collision Prob')

hold on;

G=[0:0.2:8];

S=1-G.*(1-G/10).^(10-1)-(1-G/10).^(10);

plot(G,S);

hold on;

plot(trafficOffered(2,:),pcktCollisionProb(2,:), '-s')

hold on;

S=1-G.*(1-G/25).^(25-1)-(1-G/25).^(25);

plot(G,S);

hold on;

plot(trafficOffered(3,:),pcktCollisionProb(3,:), '-o')

hold on;

S=1-G.*(1-G/50).^(50-1)-(1-G/50).^(50);

plot(G,S);

legend('simulation, M=10', 'analytical, M=10','simulation, M=25', 'analytical, M=25', 'simulation, M=50', 'analytical, M=50');

grid on

⛄ 运行结果

⛄ 参考文献

[1]方飞, 蒋猛. 时隙ALOHA在MATLAB中的仿真[J]. 内江师范学院学报, 2012, 27(8):5.

❤️部分理论引用网络文献,若有侵权联系博主删除

❤️ 关注我领取海量matlab电子书和数学建模资料

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

matlab科研助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值