仅作为学习Gilbert-Elliot模型的参考,直接上代码:
%If p is the probability of transferring from Good State to the bad state
%and if r is the probability of transferring from the bad state to the Good
%state, given the p and r values, this code will generate a packet loss
%pattern (with burst losses) and save it to a file named “Loss_Pattern.txt”
clc
p = 0.02777; % probability of transferring from Good State to the bad
r = 0.25; % probability of transferring from bad state to the Good
total_packs = 10000; %包大小
check = 100;
while check >= 10 %当理论与实际的误差 >= 10% 时,再次执行
good = 1; %good = 1表示good state; good = 0表示bad state
packets = [];
size = 1;
while size <= total_packs
if good == 1
packets = [packets good]; %元素为1表示准确接收到了包,为0则表示丢包
good = rand(1) > p;
elseif good == 0
packets = [packets good];
good = rand(1) > (1-r);
else
fprintf('error\n');
break;
end
size = size + 1;
end
fid = fopen('Loss_Pattern.txt','w');
fprintf(fid, '%d ', packets);
fclose(fid);
received_packs = nnz(packets); %准确接收到的包
%假定好状态时不发生丢包;bad状态时必发生丢包
theo_pack_loss_rate = 1 - r / (p+r); %理论丢包率
act_pack_loss_rate = 1 - received_packs/total_packs; %实际丢包率
check = abs(theo_pack_loss_rate - act_pack_loss_rate) / theo_pack_loss_rate * 100;
end
save('packets','packets'); %保存为mat文件
theo_pack_loss_rate = p / (p+r)
act_pack_loss_rate = 1 - received_packs/total_packs
注:参考
https://www.mathworks.com/matlabcentral/fileexchange/38554-generate-packet-loss-patters-using-gilbert-elliot-model
https://blog.csdn.net/u010643777/article/details/89194456
http://www.ohohlfeld.com/paper/hasslinger_hohlfeld-mmb_2008.pdf