📄 acq_sim.m
字号:
function [sfd_ok, sync_ok, delta_samples, channel_mask, compound_channel, ...
rx_pre_overlap, max_delta_samples] = acq_sim(...
TC,FS_CONT,L,CODE_ID,channel_p,noise_var,...
RX_BW,T_INT,N_INT,G,THLD_PROBA,N,G_CHEST,PLOT_DEBUG);
% script to simulate acquisition in 802.15.4a
%PLOT_DEBUG = false; %if set to true, debug output will be plotted
%%%%%%%%%%%%%%%%%%%%%%%% define parameters %%%%%%%%%%%%%%%%%%%%%%%%
%Timing
%TC = 2e-9; %chip duration in s
%FS_CONT = 12e9; %sampling frequency in Hz to get cont time signal
%Preamble
%L = 64; %spreading factor
%CODE_ID = 5; %which one of the 802.15.4a ternary codes to use
%Channel Model
%CH_MODEL = 1;
%CH_ATT_THLD_DB = 15; %rays with bigger attenuation are considered 0
%Noise
%SNR_DB = 13; %Eb/N0
%Receiver
%RX_BW = 2e9; %BW of the receiver determines Nyquist = 2*RX_BW
%T_INT = 1e-9; %integration length
%N_INT = 8; %how many blocks of length T_INT to sum
%G = 4; %how many repetitions of code in correlation
%THLD_PROBA = 0.9999; %threshold for correlation peaks
%N = 6; %number of consecutive corr peaks needed
%G_CHEST = 8; %number of len 31 symbols we accumulate for channel mask
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%rand('state',sum(100*clock));
%randn('state',sum(100*clock));
%rand('state',2121);
%randn('state',2121);
samples_per_chip = TC * FS_CONT;
fs_cont_GHz = FS_CONT / 1e9;
%create preamble for user of interest
[uoi_pre sfd_code N_sync N_sfd] = generate_preamble(L,CODE_ID);
non_zero_code_symbols = sum(abs(len31_preamble_code(CODE_ID)));
code_len = length(len31_preamble_code(CODE_ID));
%get the channel
[compound_channel,ch_len] = get_compound_channel(TC,FS_CONT,...
channel_p,PLOT_DEBUG);
%upsample, apply pulse shape and multipath channel
[rx_uoi] = get_preamble_signal(uoi_pre,compound_channel,...
ch_len, channel_p.chip_span, TC,...
FS_CONT,PLOT_DEBUG);
ch_len
%add interference (for this we need the traffic models first)
%add noise
% Parameters for the generation of the Gaussian noise
%add that much noise in the beginning of the sequence
num_noise_samples = ceil(length(rx_uoi)*rand);
noise = [];
n_run = 5;
ALPHA_N = ceil((length(rx_uoi)+num_noise_samples)/2/samples_per_chip/n_run); %make it integer multiple
T = TC;
Nt = T*FS_CONT;
Scale = (2*RX_BW)/FS_CONT;
%noise_var = 1 / 10^(SNR_DB/10) / 2; %EbN0
for i=1:n_run
%noise = sqrt(noise_var)*(randn(1,length(rx_uoi)));
[noise_r noise_i] = ...
randn_bl(ALPHA_N,T,Nt,RX_BW,noise_var*Scale);
noise = [noise noise_r noise_i];
clear noise_r;
clear noise_i;
end
more_noise = noise(1:num_noise_samples);
t0 = (num_noise_samples + 1) / fs_cont_GHz;
rx_uoi = rx_uoi + noise(num_noise_samples+1:num_noise_samples+length(rx_uoi));
clear noise; %free memory
rx_uoi = [more_noise rx_uoi];
len_more_noise = length(more_noise);
clear more_noise; %free memory
% if(PLOT_DEBUG)
% figure
% plot(rx_uoi)
% end
%num_noise_samples = 0;
%more_noise = sqrt(noise_var)*randn(1,num_noise_samples);
%%%%%%%%%%%%%%%%%%% do acquisition %%%%%%%%%%%%%%%%%%%%%%%%%%%
%calculate quantization thld
%TBD
%get the end of the rx_uoi signal for the data reception part
overlap_samples = code_len*L*samples_per_chip + length(compound_channel) -1;
rx_pre_overlap = rx_uoi(end-overlap_samples+1:end);
[rx_down,samples_per_tint] = ed_receiver(rx_uoi,T_INT,FS_CONT,N_INT,0);
clear rx_uoi; %free memory
%do coarse syncronization
%generate sequence to correlate with
blocks_per_code_symbol = L * TC / (T_INT * N_INT );
corr_template = reshape( ...
repmat(abs(len31_preamble_code(CODE_ID)),blocks_per_code_symbol, 1), ...
1,31*blocks_per_code_symbol);
%repeat correlation template G times
corr_template = repmat(corr_template,1,G);
%analytical calculation of thld and noise floor, in a real system the noise
%comes from our components, so we know the noise_var and can thus always compute
%these thresholds
noise_floor = noise_var * T_INT * 2 * RX_BW * N_INT * G * ...
blocks_per_code_symbol * non_zero_code_symbols;
coarse_thld = noise_var * chi2inv(THLD_PROBA, (T_INT * 2* RX_BW) * N_INT * G * ...
blocks_per_code_symbol * non_zero_code_symbols);
[coarse_sync_index] = coarse_sync(rx_down,corr_template,coarse_thld,...
blocks_per_code_symbol,N,PLOT_DEBUG)
%check whether coarse sync succeeded
if(coarse_sync_index ~= 0)
%change the template wrt coarse_sync
corr_template = upsample(abs(len31_preamble_code(CODE_ID)),blocks_per_code_symbol);
%repeat correlation template G times
corr_template = repmat(corr_template,1,G);
%calculate thld for new template
fine_thld = noise_var * chi2inv(THLD_PROBA, (T_INT * 2* RX_BW) * ...
N_INT * G * non_zero_code_symbols);
%run fine sync
[fine_sync_index] = fine_sync(rx_down,corr_template,fine_thld,...
blocks_per_code_symbol,code_len,coarse_sync_index,PLOT_DEBUG);
%indicates the correct values of t0
rx_t0 = [zeros(1,len_more_noise) ...
repmat([max(rx_down) zeros(1,L*samples_per_chip-1) ...
zeros(1,L*samples_per_chip*30)],1,64)];
%do the integration
rx_t0 = zero_pad_reshape(rx_t0,samples_per_tint*N_INT);
rx_t0_down = sum(reshape(rx_t0, samples_per_tint*N_INT, ...
length(rx_t0) / samples_per_tint / N_INT));
clear rx_t0; %free memory
if(PLOT_DEBUG)
figure
plot(rx_down)
hold on
plot(rx_t0_down,'x-r')
line([coarse_sync_index coarse_sync_index],[0 max(rx_down)],'Color','y')
line([fine_sync_index fine_sync_index],[0 max(rx_down)],'Color','g')
% line([fine_sync_index2 fine_sync_index2],[0 max(rx_down)],'Color','m')
%
% figure
% plot(corr2)
% hold on
% plot(thld4*ones(1,length(corr2)),'g')
% line([corr_coarse_sync_index corr_coarse_sync_index],[0 max(corr2)],'Color','g')
% line([corr_fine_sync_index corr_fine_sync_index],[0 max(corr2)],'Color','y')
% end
end
%if we have fine sync, we can go on with channel estimation
%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, fine_sync_index, ...
G_CHEST, abs(len31_preamble_code(CODE_ID)), blocks_per_code_symbol, ...
chest_thld, PLOT_DEBUG);
%couldn't do channel est -> too late
if(isempty(channel_mask))
sfd_index = 0;
else
%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, ...
fine_sync_index,channel_mask,G_CHEST,CODE_ID,blocks_per_code_symbol,...
PLOT_DEBUG);
end
if(sfd_index ~= 0)
if(PLOT_DEBUG)
figure(5)
%code_len = length(len31_preamble_code(CODE_ID));
samples_per_code = code_len * blocks_per_code_symbol;
line([sfd_index sfd_index],[0 max(rx_down)],'Color','c')
line([sfd_index+samples_per_code sfd_index+samples_per_code],...
[0 max(rx_down)],'Color','c')
line([sfd_index+2*samples_per_code sfd_index+2*samples_per_code],...
[0 max(rx_down)],'Color','c')
end
delta_samples = sfd_index - length(rx_t0_down)
else
delta_samples = 999999;
%warning('sfd not found');
end
else
%warning('synchronization failed');
sfd_index = 0;
delta_samples = 999999;
end
%fill in return values
sfd_ok = sfd_index > 0;
sync_ok = coarse_sync_index > 0;
%code_len = length(len31_preamble_code(CODE_ID));
samples_per_code = code_len * blocks_per_code_symbol;
if(delta_samples > samples_per_code || delta_samples < -samples_per_code)
sfd_ok = 0;
end
max_delta_samples = samples_per_code;
clear rx_down;
if(~sfd_ok)
channel_mask = 0;
compound_channel = 0;
end
return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -