⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classify_packets.m

📁 一个UWB仿真程序包
💻 M
📖 第 1 页 / 共 2 页
字号:
%%% parameters %%%
function [uoi_pkts] = classify_packets(LOG)

%events
SYNC_OK = 1;
SFD_OK = 3;
SFD_LATE = 7;
SFD_EARLY = 8;
SYNC_START = 0;
CHEST_DONE = 2;
MASK_LEN = 99;
%%% parse the log file %%%

fid = fopen(LOG);
log = true;
timing = false;
events = [];
pkts = [];
mask_len = [];

while 1
    tline = fgetl(fid);
    if ~ischar(tline),   break,   end
    if(log) %we are in the log part
        if(isempty(strmatch('Packet',tline)))
            %find semicolon
            k = strfind(tline, ':');
            %register event
            events = [events; str2num(tline(1:k-1)) str2num(tline(k+1:k+2))];
            %check whether mask len event
            if(str2num(tline(k+1:k+2))==MASK_LEN)
              time = str2num(tline(1:k-1));
              k = strfind(tline, '=');
              mlen = str2num(tline(k+1:end));
              mask_len = [mask_len; time mlen];
            end
        else
            log = false;
            timing = true;
        end
    elseif(timing) %we are in the timing part
        if(isempty(strmatch('Parameters',tline)))
            %find semicolon
            k = strfind(tline, ':');
            %register pkt
            pkts = [pkts; str2num(tline(1:k-1)) str2num(tline(k+1:end))];
        else
            timing = false;
        end
    else %we are in the Parameter part
        if(~isempty(strmatch('sync_len',tline)))
            %find semicolon
            k = strfind(tline, ':');
            sync_len = str2num(tline(k+1:end));
        elseif(~isempty(strmatch('sfd_len',tline)))
            %find semicolon
            k = strfind(tline, ':');
            sfd_len = str2num(tline(k+1:end));
        elseif(~isempty(strmatch('packet_len',tline)))
            %find semicolon
            k = strfind(tline, ':');
            packet_len = str2num(tline(k+1:end));
        end
    end
end
fclose(fid);

%%% do the processing %%%
pre_len = sync_len + sfd_len;
total_len = pre_len + packet_len;


%get pkts of UOI
%only take those after the simulation start event
sim_start = events(1,1)
pkts_after_sim_start_idx = find(pkts(:,1)>=sim_start);

uoi_pkts_idx = find(pkts(pkts_after_sim_start_idx,2)==1);
uoi_pkts_start = pkts(uoi_pkts_idx,1);
uoi_pkts_sfd_start = uoi_pkts_start + sync_len;
uoi_pkts_data_start = uoi_pkts_start + pre_len;
uoi_pkts = zeros(length(uoi_pkts_start),23);
uoi_pkts(:,1) = [1:length(uoi_pkts_start)]';
uoi_pkts(:,2) = uoi_pkts_start;

%get pkts of IFs
if_pkts_idx = find(pkts(:,2)~=1);
if_pkts_start = pkts(if_pkts_idx,1);

%for each uoi pkts check whether if present during each phase
for i=1:size(uoi_pkts,1)
   %sync phase
   stop = uoi_pkts_sfd_start(i);
   start = stop - sync_len - total_len;
   %count how many packets of ifs are btw start and stop
   temp = if_pkts_start;
   temp(temp>stop)=0;
   temp(temp<start)=0;
   if(sum(temp))
      uoi_pkts(i,3) = 1; 
   end
   
   %sfd phase
   stop = uoi_pkts_data_start(i);
   start = stop - sfd_len - total_len;
   %count how many packets of ifs are btw start and stop
   temp = if_pkts_start;
   temp(temp>stop)=0;
   temp(temp<start)=0;
   if(sum(temp))
      uoi_pkts(i,4) = 1; 
   end
   
   %if interference present during preamble, check percentage of preamble
   %that saw interference from data, preamble
   if(uoi_pkts(i,3) == 1 || uoi_pkts(i,4) == 1)
       stop = uoi_pkts_data_start(i);
       %check for interfering preambles first
       start = stop-2*pre_len;
       temp = if_pkts_start;
       temp(temp>stop)=0;
       temp(temp<start)=0;
       if_pre_start_times = nonzeros(temp); %the starting times of interfering preamble
       for j=1:length(if_pre_start_times)
          %calculate the percentage of our packet affected
          overlap_samples = min(stop,if_pre_start_times(j)+pre_len)- ...
              max(stop-pre_len,if_pre_start_times(j));
          uoi_pkts(i,11) = overlap_samples/pre_len;
       end
       
       %now the same for interfering data parts
       %check for interfering preambles first
       start = stop-pre_len-packet_len;
       temp = if_pkts_start+pre_len;
       temp(temp>stop)=0;
       temp(temp<start)=0;
       if_data_start_times = nonzeros(temp); %the starting times of interfering preamble
       for j=1:length(if_data_start_times)
          %calculate the percentage of our packet affected
          overlap_samples = min(stop,if_data_start_times(j)+packet_len)- ...
              max(stop-pre_len,if_data_start_times(j));
          uoi_pkts(i,12) = overlap_samples/pre_len;
       end  
   end
   
   %data phase
   stop = uoi_pkts_data_start(i)+ packet_len;
   start = stop - packet_len - total_len;
   %count how many packets of ifs are btw start and stop
   temp = if_pkts_start;
   temp(temp>stop)=0;
   temp(temp<start)=0;
   if(sum(temp))
      uoi_pkts(i,5) = 1; 
   end
   
   %if interference present during data, check percentage of preamble
   %that saw interference from data, preamble
   if(uoi_pkts(i,5) == 1)
       stop = uoi_pkts_data_start(i)+packet_len;
       %check for interfering preambles first
       start = uoi_pkts_data_start(i) - pre_len;
       temp = if_pkts_start;
       temp(temp>stop)=0;
       temp(temp<start)=0;
       if_pre_start_times = nonzeros(temp); %the starting times of interfering preamble
       for j=1:length(if_pre_start_times)
          %calculate the percentage of our packet affected
          overlap_samples = min(stop,if_pre_start_times(j)+pre_len)- ...
              max(uoi_pkts_data_start(i),if_pre_start_times(j));
          uoi_pkts(i,13) = uoi_pkts(i,13) + overlap_samples/packet_len;
       end
       
       %now the same for interfering data parts
       start = uoi_pkts_data_start(i) - packet_len;
       temp = if_pkts_start+pre_len;
       temp(temp>stop)=0;
       temp(temp<start)=0;
       if_data_start_times = nonzeros(temp); %the starting times of interfering preamble
       for j=1:length(if_data_start_times)
          %calculate the percentage of our packet affected
          overlap_samples = min(stop,if_data_start_times(j)+packet_len)- ...
              max(uoi_pkts_data_start(i),if_data_start_times(j));
          uoi_pkts(i,14) = uoi_pkts(i,14) + overlap_samples/packet_len;
       end
   end
end

%now check which packets were found, others were missed
times_sfd_ok = events(events(:,2)==SFD_OK,1);
times_sync_ok = events(events(:,2)==SYNC_OK,1);
times_sync_start = events(events(:,2)==SYNC_START,1);
times_chest_done = events(events(:,2)==CHEST_DONE,1);
times_sfd_fae = events(events(:,2)==SFD_EARLY,1);
times_sfd_fal = events(events(:,2)==SFD_LATE,1);
times_sfd_fa = sort([times_sfd_fae; times_sfd_fal]);
%must have been the packet with the closest data_start
for i=1:length(times_sfd_ok)
   [diff2closest, closest_idx] = min(abs(uoi_pkts_data_start-times_sfd_ok(i)));
   uoi_pkts(closest_idx,6) = 1;
end

%for the packets missed, why were they missed?
pkts_missed_idx = find(uoi_pkts(:,6)==0);
for i=1:length(pkts_missed_idx)
    pkt_num = pkts_missed_idx(i);
    
    %did we sync?
    stop = uoi_pkts_sfd_start(pkt_num);
    start = uoi_pkts_start(pkt_num);
    %count how sync_oks are btw start and stop
    temp = times_sync_ok;
    temp(temp>stop)=0;
    temp(temp<start)=0;
    if(sum(temp))
        uoi_pkts(pkt_num,7) = 1;
    end
    
    %did we find an sfd false alarm?
    stop = uoi_pkts_data_start(pkt_num);
    start = uoi_pkts_start(pkt_num);
    %count how sync_oks are btw start and stop
    temp = times_sfd_fa;
    temp(temp>stop)=0;
    temp(temp<start)=0;
    if(sum(temp))
        uoi_pkts(pkt_num,8) = 1;
    end
    
    %were we already receiving sth?
    stop = uoi_pkts_data_start(pkt_num);
    start = uoi_pkts_start(pkt_num)-packet_len;
    %count how sync_oks are btw start and stop
    temp = times_sfd_fa;
    temp(temp>stop)=0;
    temp(temp<start)=0;
    if(sum(temp))
        uoi_pkts(pkt_num,9) = 1;
    end
    %why was there a false alarm?
    if(sum(temp))
        %get the sfd fas
        sfd_fa = nonzeros(temp);
        %loop through all of these guys
        for j=1:length(sfd_fa)
           %get the corresponding sync ok
           sync_ok_before = times_sync_ok(times_sync_ok<sfd_fa(j));
           sync_ok = sync_ok_before(end);
           %get the corresponding sync start
           sync_start_before = times_sync_start(times_sync_start<sync_ok);
           sync_start = sync_start_before(end);
           %get the corresponding chest done
           chest_done_before = times_chest_done(times_chest_done<sfd_fa(j));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -