%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% preamble format = 0 time is 1ms time of duration is 800us max length
%%%% is 14.53km
%%%% UL/DL Configuration = 1 frame is DSUUD DSUUD
%%%% time delay evaluation have 1us error
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
clc;
close all;
clear all;
%%
format = 0; %preamble format
%PRACH Channel Configuration
prachConfig = 18; %prach-ConfigurationIndex
zeroConfig = 5; %zeroCorrelationZoneConfig
%Physical root sequence number
rootSeq = [129, 710, 140, 699, 120, 719, 210, 629, 168, 671, 84, 755, 105, 734, 93, 746, 70, 769, 60, 779, 2, 837, 1, 838, 56, 783, 112, 727, 148, 691, 80, 759, 42, 797, 40, 799, 35, 804, 73, 766, 146, 693, 31, 808, 28, 811, 30, 809, 27, 812, 29, 810, 24, 815, 48, 791, 68, 771, 74, 765, 178, 661, 136, 703]; %Physical root sequence number
highspeedflag = 0; %False
PRBOffset = 10; %prach-FrequencyOffset 0~94
TimeFre_usr2 = [0,0,1,1]; %same Time and Frequence , here's crash
t_id = 3; %in the 3th subframe of frame
zeroCorrelationZoneConfig = zeros(3,16);
for i = 1:1:16
zeroCorrelationZoneConfig(1,i) = i-1;
end
zeroCorrelationZoneConfig(2,:) = [0 13 15 18 22 26 32 38 46 59 76 93 119 167 279 419];
zeroCorrelationZoneConfig(3,:) = [64 1 2 2 2 2 3 3 4 5 6 8 10 13 22 32];
Nzc = 839; %ZC sequence number
N_RB_sc = 12; %subcarrier num per RB
N_UL_RB = 100; %uplink RB is 100
Ncs = zeroCorrelationZoneConfig(2,zeroConfig+1);
% SNR = 0; % > -15dB
P_ctrl = 10;
delay_time_court = 1500;%time delay court
error_num = zeros(1,16);
usr_info = zeros(4,64);
subframe_num = 200;
OFDM_RA_T_usr = zeros(1,27744);
UE_state = 1; %UE statment first time is state 1
% NodeB_state = 0; %if there is not preamble NodeB_state = 0
retran_Msg1 = 0; %retransmission times for Msg1
retran_Msg2 = 0; %retransmission times for Msg2
Max_retran_Msg1 = 3; %Max retransmission times for Msg1
Max_retran_Msg2 = 3; %Max retransmission times for Msg1
Msg1 = zeros(1,27744);
Msg2.PreambleID = zeros(1,64);
Msg2.C_RNTI = zeros(1,64);
Msg2.TimeAd = zeros(1,64); %time advanced
Msg2_right_num = 0;
Msg3.UE_ID = 0; %modification the same as IMSI there is 40 bit actually
Msg3.C_RNTI = 0;
Msg4.C_RNTI = 0;
Msg4.UE_ID = 0; %modification the same as IMSI there is 40 bit actually
Msg4.CompMsg = 0; %initial is 0 compete dissolved is 1 compete solved is 2
NodeB_Soc = zeros(1,10);
NodeB_sameSoc = 0;
NodeB_Soc_used = zeros(1,10);
[ZCSeq , ZCconfig] = ZCSequence( rootSeq , Nzc ); %create 64 ZC sequence this is the sequence of A cell
for SNR = -3
for subframe = 1:subframe_num
%%
%transmission
switch UE_state
case 1 %Msg1
if mod(subframe,10) == 3 %in the 3th subframe of one frame
if retran_Msg1 == 0 %first transmission no retransmission
fprintf('Ready to Random Access\n\n');
root_num = round(rand(1)*64)+1; %choose a ZC root number randomly
%time and frequency
[OFDM_RA_T_usr] = TFlayerMap( ZCSeq(root_num,:) , PRBOffset , Nzc , N_UL_RB , TimeFre_usr2 );
Msg1_variable = 1; %Msg1 has been sent
UE_state = 2; %change to state 2
retran_Msg1 = retran_Msg1 +1;
respon_window_len = 1;
fprintf('Start to transmission Msg1\n\n');
else %retransmission
[OFDM_RA_T_usr] = TFlayerMap( ZCSeq(root_num,:) , PRBOffset , Nzc , N_UL_RB , TimeFre_usr2 );
Msg1_variable = 1;
UE_state = 2;
retran_Msg1 = retran_Msg1 +1;
respon_window_len = 1;
fprintf('Start to retransmission Msg1\n\n');
end
end
case 2 %Msg2
if retran_Msg1 <= Max_retran_Msg1 %less than max retransmission times
if respon_window_len > 2 %for Msg2 respon window , I set it to listen when 2 subframe later than Msg1 sent
Msg2_right_num = 0; % see that if there's right Msg2 or not
for i = 1:64
if root_num == Msg2.PreambleID(1,i) && respon_window_len <= 12 % if Msg2 is correct
UE_state = 3; %change to state 3
retran_Msg1 = 0; %reset
respon_window_len = 1; %reset
Msg2_right_num = i; %the right location in struct Msg2
fprintf('Msg2 correct\n\n');
break;
end
end
if respon_window_len > 12 %beyond the window
UE_state = 1;
fprintf('None of correct Msg2 in 12 subframe\n\n');
elseif Msg2_right_num == 0 && respon_window_len <= 12;
respon_window_len = respon_window_len + 1;
fprintf('None of correct Msg2 in current subframe\n\n');
end
else %no correct Msg2 but not the limit of the window
respon_window_len = respon_window_len + 1;
fprintf('Wait 2 subframe to start response window\n\n');
end
else %faile to transimission
UE_state =1;
retran_Msg1 = 0;
fprintf('Random Access failed\n\n');
end
case 3 %Msg3
if retran_Msg2 == 0 %first ro transmission
Msg3.UE_ID = 123456789; %modification the same as IMSI
Msg3.C_RNTI = Msg2.C_RNTI(1,Msg2_right_num); %add some data in Msg2 to Msg3
respon_window_Msg4_len = 1;
UE_state = 4;
fprintf('Start to transmission Msg3\n\n');
else %retransmission
Msg3.UE_ID = 123456789; %modification
Msg3.C_RNTI = Msg2.C_RNTI(1,Msg2_right_num);
respon_window_Msg4_len = 1;
UE_state = 4;
fprintf('Start to retransmission Msg3\n\n');
end
case 4 %Msg4
if respon_window_Msg4_len > 12
if retran_Msg2 >= Max_retran_Msg2
UE_state = 1;
retran_Msg2 = 0;
fprintf('Retransmission failed\n\n');
fprintf('Random Access failed\n\n')
else
UE_state = 3;
retran_Msg2 = retran_Msg2 + 1;
end
else
if Msg4.UE_ID == Msg3.UE_ID
if Msg4.CompMsg == 2
fprintf('Receive compete solved Msg\n\n');
fprintf('Random Access succeed\n\n');
UE_state = 0;
elseif Msg4.CompMsg == 1
Msg4.CompMsg = 0;
fprintf('Receive collide Msg\n\n');
fprintf('Random Access failed\n\n');
UE_state = 1;
end
else
respon_window_Msg4_len = respon_window_Msg4_len + 1;
fprintf('None of correct Msg4 in current subframe\n\n');
end
end
end
%%
%channel
delay_time = 500; %actually delay time must < Ncs
Msg1 = awgn