📄 sim_logic.m
字号:
%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 coarse_sync_index = 0; channel_mask = []; est_sig_level = 0; %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,CH_MODEL,CH_ATT_THLD_DB,CH_RNG_SEED,... 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 %the fine sync current_sample = sync_sample; state = CH_EST; sync_overlap = []; corr_overlap = []; coarse_sync_index = 0; fprintf('OK\n'); elseif(sync_ok) state = FINE_SYNC; %coarse sync ok, but fine sync not yet sync_overlap = []; corr_overlap = []; fprintf('OK\n'); else %update current_sample current_sample = current_sample + sync_block_size; fprintf('WAIT\n'); end case FINE_SYNC, fprintf('FINE_SYNC...'); %run the sync on the next block of data %get an rx signal block [pkts, rx_sig] = get_rx_signal(current_sample + sync_block_size,... current_sample + sync_block_size + fine_sync_block_size - 1,pkts,total_len,users,ep,symbol_start,TC,... FS_CONT,CH_MODEL,CH_ATT_THLD_DB,CH_RNG_SEED,... 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); fine_overlap = []; sync_sample = current_sample+sync_offset*samples_per_tint*N_INT; %the factor is because we only have this precision on %the fine sync current_sample = sync_sample; state = CH_EST; sync_overlap = []; %if we are in the begining corr_overlap = []; %what has to be remembered from the correlation in on step to next fine_overlap = []; coarse_sync_index = 0; fprintf('OK\n'); case CH_EST, fprintf('CH_EST...'); %check whether it makes sense to try if(sim_stop - current_sample < samples.packet) fprintf('OVER\n'); break; %it is over end %get an rx signal block to run channel estimation on [pkts, rx_sig] = get_rx_signal(current_sample,... current_sample + ch_est_block_size - 1,pkts,total_len,users,ep,symbol_start,TC,... FS_CONT,CH_MODEL,CH_ATT_THLD_DB,CH_RNG_SEED,... 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); %calculate thld for channel estimation chest_thld = noise_var * chi2inv(THLD_PROBA, (T_INT * 2* RX_BW) * ... N_INT * G_CHEST * non_zero_code_symbols); [channel_mask, est_sig_level] = channel_est(rx_down, 1, ... G_CHEST, abs(len31_preamble_code(users(1).code)), blocks_per_code_symbol, ... chest_thld, PLOT_DEBUG); %advance time current_sample = current_sample + ch_est_block_size; %go to sfd detection state = SFD; fprintf('OK\n'); case SFD, fprintf('SFD...'); %check whether it makes sense to try if(sim_stop - current_sample < samples.packet) fprintf('OVER\n'); break; %it is over end %get an rx signal block to run channel estimation on [pkts, rx_sig] = get_rx_signal(current_sample,... current_sample + sfd_block_size - 1,pkts,total_len,users,ep,symbol_start,TC,... FS_CONT,CH_MODEL,CH_ATT_THLD_DB,CH_RNG_SEED,... 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); %we are now ready to look for the sfd in the remaining part ch_taps = length(find(channel_mask>0));%no of taps that are non-zero est_noise_level = noise_var * (T_INT * 2* RX_BW) * N_INT * ... ch_taps * non_zero_code_symbols; sfd_thld = 2.5*(est_sig_level - est_noise_level); [sfd_index] = sfd_detection(rx_down,sfd_code,sfd_thld, ... 1-G_CHEST*code_len*blocks_per_code_symbol,channel_mask,G_CHEST,users(1).code,blocks_per_code_symbol,... PLOT_DEBUG); if(sfd_index) sfd_sync_sample = current_sample+(sfd_index-1)*samples_per_tint*N_INT; state = DATA; current_sample = sfd_sync_sample + NSFD * code_len*blocks_per_code_symbol*samples_per_tint*N_INT; fprintf('OK\n'); else fprintf('not found sfd'); state = SYNC; channel_mask = []; est_sig_level = 0; current_sample = current_sample + sfd_block_size; fprintf('FAILED\n'); end case DATA, fprintf('DATA...'); %check whether there is a data part of the UOI nearby. if %not it does not make sense to do the whole decoding stuff. [diff2closest, closest_idx] = min(abs(uoi_data_starts-current_sample)); %if the difference to the closest starting point of a data %sequence of the UOI is bigger than the length of the %channel mask, we will never be able to decode successfully if(diff2closest <= length(channel_mask)*N_INT*T_INT*FS_CONT) %get an rx signal block [pkts, rx_sig] = get_rx_signal(current_sample,... current_sample + samples.packet - 1,pkts,total_len,users,ep,symbol_start,TC,... FS_CONT,CH_MODEL,CH_ATT_THLD_DB,CH_RNG_SEED,... DATA_LENGTH,RSCODE,L,PACKET_LENGTH,BURST_LENGTH,... SYMBOL_LENGTH,RX_BW,noise_var,samples,PLOT_DEBUG); %pass it through the ed receiver demod_bit = ed_decoder(rx_sig, ep.ths, ... symbol_start, ... 0,samples.burst,samples.symbol, ... channel_mask', T_INT*N_INT, ... 0, ... PACKET_LENGTH, FS_CONT, T_INT, N_INT); if RSCODE == 1 % RS decoding dec_bit = rs_decoder_154a(demod_bit,ep.interleaver_seed); else dec_bit = demod_bit; end %get the corresponding data bits data_bit = pkts(uoi_pkt_idx(closest_idx,1)).data_bits; % Compute the BER num_err = sum(abs(data_bit-dec_bit)); ber_sim = num_err/DATA_LENGTH; %do the bookkeeping %test to be sure if(uoi_pkt_errors(1,uoi_pkt_idx(closest_idx,2))~=-1) error('sth went wrong...'); end uoi_pkt_errors(1,uoi_pkt_idx(closest_idx,2))=num_err; uoi_pkt_errors(2,uoi_pkt_idx(closest_idx,2))=ber_sim; uoi_pkt_errors(3,uoi_pkt_idx(closest_idx,2))=diff2closest; fprintf('OK\n'); fprintf('errors: %d, ber: %.5f\n',num_err, ber_sim); else fprintf('TOO FAR, Decoding aborted\n'); end current_sample = current_sample + samples.packet; state = SYNC; otherwise error('undefined state of simulation'); end %state switch end %main while loop tocend %sim chunksuoi_pkt_errorsfigurestem(timing.startTimes,ones(1,length(timing.startTimes)),'r')hold onstem(timing.stopTimes,ones(1,length(timing.stopTimes)),'b')stem(timing.packets(:,1),min(1,timing.packets(:,2)/2),'k.')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -