forked from Evrytania/LTE-Cell-Scanner
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecode_mib.m
206 lines (184 loc) · 5.43 KB
/
decode_mib.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
function peak_out=decode_mib(peak,tfg);
% Try various combinations of antenna ports and frame timings to attempt
% to decode the MIB.
% Copyright 2012 Evrytania LLC (http://www.evrytania.com)
%
% Written by James Peroulas <[email protected]>
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU Affero General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Affero General Public License for more details.
%
% You should have received a copy of the GNU Affero General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
peak_out=peak;
% Local shortcuts
n_id_1=peak.n_id_1;
n_id_2=peak.n_id_2;
cp_type=peak.cp_type;
% Derive some values
n_ofdm=size(tfg,1);
if (strcmpi(cp_type,'normal'))
n_symb_dl=7;
elseif (strcmpi(cp_type,'extended'))
n_symb_dl=6;
else
error('Check code...');
end
n_id_cell=n_id_2+3*n_id_1;
% Channel estimation
ce_tfg=NaN(n_ofdm,72,4);
[ce_tfg(:,:,1) np0]=chan_est(peak,tfg,0);
[ce_tfg(:,:,2) np1]=chan_est(peak,tfg,1);
[ce_tfg(:,:,3) np2]=chan_est(peak,tfg,2);
[ce_tfg(:,:,4) np3]=chan_est(peak,tfg,3);
% Try various numbers of ports and various frame timing offsets
%disp('Check code here!!!');
for frame_timing_guess=0:3
%for frame_timing_guess=3
%frame_timing_guess
% Extract the 4 frames that will be used to find the PBCH
%ofdm_sym_set=frame_timing_guess*10*2*n_symb_dl+1:10*2*n_symb_dl*(frame_timing_guess+4);
ofdm_sym_set=frame_timing_guess*10*2*n_symb_dl+1;
ofdm_sym_set=ofdm_sym_set:ofdm_sym_set+3*10*2*n_symb_dl+2*n_symb_dl-1;
tfg_try=tfg(ofdm_sym_set,:);
ce_try=ce_tfg(ofdm_sym_set,:,:);
% Extract symbols and channel estimates for the PBCH
[pbch_sym pbch_ce]=pbch_extract(peak,tfg_try,ce_try);
%load override_pbch_ce.mat
found=0;
%disp('Check code here!!!');
for n_ports=[1 2 4]
%for n_ports=2
%n_ports
if (n_ports==1)
np=np0;
gain=conj(pbch_ce(1,:))./absx2(pbch_ce(1,:));
syms=pbch_sym.*gain;
np=np*absx2(gain);
elseif (n_ports==2)
np_mean=mean([np0 np1]);
syms=NaN(1,length(pbch_sym));
np=NaN(1,length(pbch_sym));
for t=1:2:length(syms)
% http://en.wikipedia.org/wiki/Space-time_block_coding_based_transmit_diversity
h1=mean(pbch_ce(1,t:t+1));
h2=mean(pbch_ce(2,t:t+1));
x1=pbch_sym(t);
x2=pbch_sym(t+1);
scale=sum(absx2([h1 h2]));
syms(t)=(conj(h1)*x1+h2*conj(x2))/scale;
syms(t+1)=conj((-conj(h2)*x1+h1*conj(x2))/scale);
np(t)=(abs(h1)/scale)^2*np_mean+(abs(h2)/scale)^2*np_mean;
np(t+1)=np(t);
end
% 3dB factor comes from precoding for transmit diversity
syms=syms*sqrt(2);
elseif (n_ports==4)
np_mean=mean([np0 np1 np2 np3]);
syms=NaN(1,length(pbch_sym));
np=NaN(1,length(pbch_sym));
for t=1:2:length(syms)
% http://en.wikipedia.org/wiki/Space-time_block_coding_based_transmit_diversity
if (mod(t,4)==1)
h1=mean(pbch_ce(1,t:t+1));
h2=mean(pbch_ce(3,t:t+1));
else
h1=mean(pbch_ce(2,t:t+1));
h2=mean(pbch_ce(4,t:t+1));
end
x1=pbch_sym(t);
x2=pbch_sym(t+1);
scale=sum(absx2([h1 h2]));
syms(t)=(conj(h1)*x1+h2*conj(x2))/scale;
syms(t+1)=conj((-conj(h2)*x1+h1*conj(x2))/scale);
np(t)=(abs(h1)/scale)^2*np_mean+(abs(h2)/scale)^2*np_mean;
np(t+1)=np(t);
end
% 3dB factor comes from precoding for transmit diversity
syms=syms*sqrt(2);
else
error('Check code...');
end
% Extract the bits
e_est=deqam(syms,np,'QAM','LTE');
% Unscramble
scr=lte_pn(n_id_cell,length(e_est));
e_est(scr==1)=1-e_est(scr==1);
% Undo ratematching
d_est=lte_conv_deratematch(e_est,40);
% If identical CE smoothing is used, C code matches perfectly until
% here.
% Viterbi decode
c_est=lte_conv_decode(d_est);
% Calculate the received CRC
crc_est=lte_calc_crc(c_est(1:24),16);
%c_est(25:end)
% Apply CRC mask
if (n_ports==2)
crc_est=1-crc_est;
elseif (n_ports==4)
crc_est(2:2:end)=1-crc_est(2:2:end);
end
%crc_est
% Success?
if (all(crc_est==c_est(25:end)))
found=1;
break;
end
end
if (found==1)
break;
end
end
% Record the extracted data if the MIB was properly decoded
if (found)
disp('Found MIB!!!');
%frame_timing_guess
%n_ports
c_est(1:24)
% Record system BW
bw_packed=c_est(1)*4+c_est(2)*2+c_est(3);
switch (bw_packed)
case 0
n_rb_dl=6;
case 1
n_rb_dl=15;
case 2
n_rb_dl=25;
case 3
n_rb_dl=50;
case 4
n_rb_dl=75;
case 5
n_rb_dl=100;
otherwise
n_rb_dl=NaN;
end
peak_out.n_rb_dl=n_rb_dl;
% PHICH duration
if (c_est(4))
peak_out.phich_dur='extended';
else
peak_out.phich_dur='normal';
end
% PHICH res
phich_res=c_est(5)*2+c_est(6);
phich_res_map=[1/6 1/2 1 2];
peak_out.phich_res=phich_res_map(phich_res);
% Calculate the SFN of the first frame in the tfg variable.
sfn_bits=c_est(7:14);
sfn=0;
for t=1:length(sfn_bits)
sfn=2*sfn+sfn_bits(t);
end
sfn=sfn*4-frame_timing_guess;
sfn=mod(sfn,1024);
peak_out.sfn=sfn;
end