📄 ed_sim_mui.m
字号:
function ed_sim_mui(TC,FS_CONT,L,CODE_ID,CH_MODEL,CH_ATT_THLD_DB,... SNR_DB,NN,IF_PWR_DB,IF_CODE_IDS,LAMBDA,BURST_LENGTH,... SYMBOL_LENGTH,PACKET_LENGTH,RSCODE,RAND_THS,RX_BW,T_INT,N_INT,G,THLD_PROBA,... N,G_CHEST,RUN,PKTS_PER_RUN,PLOT_DEBUG,CLUSTER,OUT_FILE,LOG_FILE)%set this to run, the actual channels are further dependend on rand...RNG_SEED = RUN;%set the random number generator to another state each timerand('state',RNG_SEED); %same for same runrandn('state',RNG_SEED); %same for same runCH_RNG_SEED = RNG_SEED;%determine packet length based on whether we do codingif RSCODE == 1 DATA_LENGTH = 1014;%870; fullblock = floor(DATA_LENGTH/330); PACKET_LENGTH = fullblock*378 + (DATA_LENGTH-fullblock*330) + 48;else DATA_LENGTH = PACKET_LENGTH;endif(CLUSTER) %add path to copied toolboxes, so that we do not get trouble with the %licence mananger addpath ../../../toolbox_optim; addpath ../../../toolbox_shared; addpath ../../../toolbox_signal; addpath ../../../toolbox_comm; addpath ../../../toolbox_stats;end%add paths to includeaddpath ./rng;addpath ./util;addpath ./signal;addpath ./receiver;addpath ./preamble_signal;addpath ./data_signal;addpath ../src/channel;addpath ../src/scrambling_timehopping;addpath ./mui;addpath ./reed_solomon;%group all the parameters corresponding to the channelchannel_p = group_channel_params(CH_RNG_SEED,CH_MODEL,CH_ATT_THLD_DB,0.5,1e-9);%lengths in samplessamples = get_samples_values(TC,FS_CONT, ... BURST_LENGTH, ... SYMBOL_LENGTH, ... PACKET_LENGTH);%for the time being we set noise level herenoise_var = 1 / 10^(SNR_DB/10) / 2; %EbN0%create UOIusers(1).pwr = 0;users(1).code = CODE_ID;%create interferersfor i=2:NN users(i).pwr = IF_PWR_DB(i-1); users(i).code = IF_CODE_IDS(i-1);end%precompute some stuff[ep] = get_ep(PACKET_LENGTH, ... SYMBOL_LENGTH, ... BURST_LENGTH, ... samples);%generate one THS per userNHOP = SYMBOL_LENGTH/BURST_LENGTH/4;for i=1:NN if(RAND_THS) users(i).ths = floor(NHOP*rand(1,PACKET_LENGTH)); else %they all have the same one users(i).ths = ep.ths; endend%get preamble values[uoi_pre sfd_code NSYNC NSFD] = generate_preamble(L,users(1).code);non_zero_code_symbols = sum(abs(len31_preamble_code(users(1).code)));code_len = length(len31_preamble_code(users(1).code));%create the packetsack_gap = (NSYNC + NSFD) * code_len * L * TC + 0 * SYMBOL_LENGTH * TC; %secondsWARMUP = 500; %number of warmup packets to pass transient phase%get the sim scenario[timing] = get_packet_timing(NN,PKTS_PER_RUN,LAMBDA,ack_gap,WARMUP,NSYNC,NSFD,... TC,L,G,PACKET_LENGTH,SYMBOL_LENGTH,code_len,PLOT_DEBUG); sync_len = NSYNC*code_len*L*samples.chip;sfd_len = NSFD*code_len*L*samples.chip;pre_len = sync_len + sfd_len; %length of preamble in samplestotal_len = pre_len + samples.packet; %length of total packet in samples%the different block sizes the simulator operates onsync_block_size = pre_len/4;% make sure the block size is divisible by % samples_per_tint * N_INT, so that we don't have to pad zeros in the ed receiversamples_per_tint = T_INT * FS_CONT;divider = samples_per_tint * N_INT;no_times = ceil(sync_block_size / divider);no_pad = no_times * divider - sync_block_size;sync_block_size = sync_block_size + no_pad;blocks_per_code_symbol = L * TC / (T_INT * N_INT);corr_tmpl_len = G*31*blocks_per_code_symbol;fine_sync_block_size = 3*blocks_per_code_symbol+corr_tmpl_len*samples_per_tint*N_INT;ch_est_block_size = G_CHEST*code_len*blocks_per_code_symbol*samples_per_tint*N_INT;sfd_block_size = (NSYNC+NSFD-G_CHEST)*code_len*blocks_per_code_symbol*samples_per_tint*N_INT;%best we can do is a precision of 1/FS_CONT -> convert cont. times to this%precisiontiming.startTimes = round(timing.startTimes*FS_CONT); timing.stopTimes = round(timing.stopTimes*FS_CONT); timing.packets(:,1) = round(timing.packets(:,1)*FS_CONT); %add some random samples at the beginning so that we are not always well%aligned with a chip -> effect of arbitrary sampling starttiming.startTimes = timing.startTimes - floor(samples.chip*rand(1,length(timing.startTimes)));symbol_start = get_symbol_start(PACKET_LENGTH,samples);% %just to make sure, recount quickly UOI packets% num_uoi_pkts = length(timing.packets(timing.packets(:,2)==1,2));% if(num_uoi_pkts ~= PKTS_PER_RUN)% num_uoi_pkts% PKTS_PER_RUN% keyboard% error('uoi pkts do not match...');% end%initialize stuff needed for bookkeepinguoi_pkt_errors = -1 * ones(3,PKTS_PER_RUN); %this is where we do the bookkeepingnum_uoi_pkts_seen = 0; %counter%now, let's start with the simulation. first of all we simulate by chunkstot_pkts = size(timing.packets,1)num_sim_chunks = length(timing.startTimes)%write log to log file%events.decoding = []; %the time instances at which we started decodingfidlog = fopen( LOG_FILE, 'w' );fprintf(fidlog,'%20.0f:0 Simulation Start\n',timing.startTimes(1));for chunk=1:num_sim_chunks %tic chunk %get start/stop time start_samples = timing.startTimes(chunk); stop_samples = timing.stopTimes(chunk); pkts = []; %all the packets relevant for this chunk uoi_data_starts = []; %starting times of data parts of UOI uoi_pkt_idx = []; %index into uoi_pkt_errors/pkts of the corresponding packet %determine which of the packets belong to this chunk num_pkts = 0; %number of packets in this chunk for i=1:tot_pkts is_part = false; pkt_start = timing.packets(i,1); pkt_user = timing.packets(i,2); if(chunk==1 && pkt_start < start_samples)%the first chunk sometimes has %packets preceeding it, if we had a forced stop when creating the timing is_part = true; elseif(pkt_start >= start_samples && pkt_start <= stop_samples) %check whether it fall into the boundary is_part = true; end if(is_part) num_pkts = num_pkts + 1; pkts(num_pkts).start = pkt_start-start_samples+1; pkts(num_pkts).user = pkt_user; pkts(num_pkts).channel = []; pkts(num_pkts).ch_len = 0; pkts(num_pkts).tx_bits = []; pkts(num_pkts).data_bits = []; if(pkts(num_pkts).user == 1) %don't consider packets that preceed chunk 1 if(~(chunk == 1 && pkt_start < start_samples)) num_uoi_pkts_seen = num_uoi_pkts_seen + 1; uoi_data_starts = [uoi_data_starts pkts(num_pkts).start+pre_len]; uoi_pkt_idx = [uoi_pkt_idx; num_pkts, num_uoi_pkts_seen]; end end end end %note we rescale so that the first sample of the chunk is sample number 1 sim_stop = stop_samples-start_samples+1; %these are the states our simulater can be in, names are pretty %self-explanatory SYNC = 1; FINE_SYNC = 2; CH_EST = 3; SFD = 4; DATA = 5; %initialize values for this chunk current_sample = 1; %this is the clock of the simulator, first sample is 1 state = SYNC; %we start by trying to synchronize sync_overlap = []; %what has to be remembered from the rx signal from one step to next corr_overlap = []; %what has to be remembered from the correlation in one step to next fine_overlap = []; %same for the fine sync sfd_overlap = []; %same for sfd coarse_sync_index = 0; channel_mask = []; est_sig_level = 0; fprintf(fidlog,'%20.0f:0 Sync Start\n',current_sample+start_samples-1); %this is the main loop of our sim while(true) switch state case SYNC, fprintf('SYNC...'); %check whether it makes sense to try if(sim_stop - current_sample < samples.packet) fprintf('OVER\n'); break; %it is over end %run the sync on the next block of data %get an rx signal block [pkts, rx_sig] = get_rx_signal(current_sample,... current_sample + sync_block_size - 1,pkts,total_len,users,ep,symbol_start,TC,... FS_CONT,channel_p,... DATA_LENGTH,RSCODE,L,PACKET_LENGTH,BURST_LENGTH,... SYMBOL_LENGTH,RX_BW,noise_var,samples,PLOT_DEBUG); %pass it through the ed receiver [rx_down,samples_per_tint] = ed_receiver(rx_sig,T_INT,FS_CONT,N_INT,0); %run synchronization [sync_overlap, corr_overlap, fine_overlap, sync_ok, sync_offset, coarse_sync_index] = run_sync(rx_down, noise_var, sync_overlap, corr_overlap, fine_overlap, coarse_sync_index, users, L, TC, T_INT,... N_INT,G,RX_BW,THLD_PROBA,N,PLOT_DEBUG); %[current_sample, sync_ok, sync_overlap, corr_overlap] = run_sync(current_sample,sync_overlap,corr_overlap); if(sync_ok && isempty(fine_overlap))%coarse and fine sync done sync_sample = current_sample+sync_offset*samples_per_tint*N_INT; %the factor is because we only have this precision on
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -