* INTC Contribution to the O-RAN F Release for O-DU Low
[o-du/phy.git] / fhi_lib / app / gen_test.m
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 %
3 % Copyright (c) 2021 Intel.
4
5 % Licensed under the Apache License, Version 2.0 (the "License");
6 % you may not use this file except in compliance with the License.
7 % You may obtain a copy of the License at
8
9 % http://www.apache.org/licenses/LICENSE-2.0
10
11 % Unless required by applicable law or agreed to in writing, software
12 % distributed under the License is distributed on an "AS IS" BASIS,
13 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 % See the License for the specific language governing permissions and
15 % limitations under the License.
16
17 %
18 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
19
20 %This script was tested with GNU Octave, version 3.8.2 or Matlab 9.2.0.538062 (R2017a) 
21
22 close all;
23 clear all;
24
25      %  5MHz    10MHz   15MHz   20 MHz  
26 nLteNumRbsPerSymF1 = ...
27 [
28      %  5MHz    10MHz   15MHz   20 MHz  
29         [25,    50,     75,     100]         % Numerology 0 (15KHz)
30 ];
31
32      %  5MHz    10MHz   15MHz   20 MHz  25 MHz  30 MHz  40 MHz  50MHz   60 MHz  70 MHz  80 MHz   90 MHz  100 MHz
33 nNumRbsPerSymF1 = ...
34 [
35      %  5MHz    10MHz   15MHz   20 MHz  25 MHz  30 MHz  40 MHz  50MHz   60 MHz  70 MHz  80 MHz   90 MHz  100 MHz
36         [25,    52,     79,     106,    133,    160,    216,    270,    0,         0,      0,      0,      0]         % Numerology 0 (15KHz)
37         [11,    24,     38,     51,     65,     78,     106,    133,    162,       0,    217,    245,    273]         % Numerology 1 (30KHz)
38         [0,     11,     18,     24,     31,     38,     51,     65,     79,        0,    107,    121,    135]         % Numerology 2 (60KHz)
39 ];
40
41 nNumRbsPerSymF2 = ...
42 [
43     %  50Mhz  100MHz  200MHz   400MHz
44         [66,    132,    264,     0]        % Numerology 2 (60KHz)
45         [32,    66,     132,     264]      % Numerology 3 (120KHz)
46 ];
47
48 % total number of tests
49 tests_total = 15
50 tech_all = ... % 0 - NR 1- LTE
51     [ 
52       0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2
53     ]
54
55 sub6_all = ...
56     [ 
57       true, true, true, true, false, true, true, true, true, true, true, true, true, true, true
58     ]
59
60 mu_all = ...
61     [
62       0, 0, 0, 1, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
63     ]
64
65 bw_all = ...
66     [
67       5, 10, 20, 100, 100, 100, 20, 10, 5, 20, 10, 5, 20, 10, 5
68     ]
69
70 ant_num_all = ...
71     [
72       4, 4, 4, 4, 4, 8, 4, 4, 4, 8, 8, 8, 4, 4, 4
73     ]
74
75 bfw_gen_all = ...
76     [
77       false, false, false, false, false, true, false, false, false, true, true, true, false, false, false
78     ]
79
80 trx_all = ...
81     [ 
82       32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
83     ]
84 path_to_usecase_all = ... 
85     [
86       "./usecase/cat_a/mu0_5mhz/";
87       "./usecase/cat_a/mu0_10mhz/";
88       "./usecase/cat_a/mu0_20mhz/";
89       "./usecase/cat_a/mu1_100mhz/";
90       "./usecase/cat_a/mu3_100mhz/";
91       "./usecase/cat_b/mu1_100mhz/";
92       "./usecase/lte_a/mu0_20mhz/";
93       "./usecase/lte_a/mu0_10mhz/";
94       "./usecase/lte_a/mu0_5mhz/";
95       "./usecase/lte_b/mu0_20mhz/";
96       "./usecase/lte_b/mu0_10mhz/";
97       "./usecase/lte_b/mu0_5mhz/";      
98       "./usecase/dss/mu0_20mhz/";
99       "./usecase/dss/mu0_10mhz/";
100       "./usecase/dss/mu0_5mhz/";
101     ]
102
103 path_to_usecase_all = cellstr(path_to_usecase_all) 
104
105 nSlots_all = ...
106     [
107        20,20,20,20,20,20,20,20,20,10,10,10,20,20,20
108     ]
109
110 %select mu and bw to generate test files
111 for test_num =(1:1:tests_total)
112     test_num
113     isLte=tech_all(test_num)  %false 
114     sub6=sub6_all(test_num)  %false
115     mu=mu_all(test_num) % 0,1, or 3
116     bw=bw_all(test_num) %5,10,20,100 MHz
117     ant_num = ant_num_all(test_num)
118     bfw_gen=bfw_gen_all(test_num)
119     trx = trx_all(test_num)
120     size(path_to_usecase_all)
121     path_to_usecase = path_to_usecase_all(test_num)
122
123     nSlots=nSlots_all(test_num) % any 40 and 160
124
125     if isLte
126         disp('LTE')
127         if mu < 3
128             nNumerology = mu+1;
129             switch (bw)
130                 case {5}
131                     numRBs = nLteNumRbsPerSymF1(nNumerology,0+1);
132                 case {10}
133                     numRBs = nLteNumRbsPerSymF1(nNumerology,1+1);
134                 case {15}
135                     numRBs = nLteNumRbsPerSymF1(nNumerology,2+1);
136                 case {20}
137                     numRBs = nLteNumRbsPerSymF1(nNumerology,3+1);
138             end
139         end
140     else
141         if sub6
142             disp('Sub6')
143             if mu < 3
144                 nNumerology = mu+1;
145                 switch (bw)
146                     case {5}
147                         numRBs = nNumRbsPerSymF1(nNumerology,0+1);
148                     case {10}
149                         numRBs = nNumRbsPerSymF1(nNumerology,1+1);
150                     case {15}
151                         numRBs = nNumRbsPerSymF1(nNumerology,2+1);
152                     case {20}
153                         numRBs = nNumRbsPerSymF1(nNumerology,3+1);
154                     case {25}
155                         numRBs = nNumRbsPerSymF1(nNumerology,4+1);
156                     case {30}
157                         numRBs = nNumRbsPerSymF1(nNumerology,5+1);
158                     case {40}
159                         numRBs = nNumRbsPerSymF1(nNumerology,6+1);
160                     case {50}
161                         numRBs = nNumRbsPerSymF1(nNumerology,7+1);
162                     case {60}
163                         numRBs = nNumRbsPerSymF1(nNumerology,8+1);
164                     case {70}
165                         numRBs = nNumRbsPerSymF1(nNumerology,9+1);
166                     case {80}
167                         numRBs = nNumRbsPerSymF1(nNumerology,10+1);
168                     case {90}
169                         numRBs = nNumRbsPerSymF1(nNumerology,11+1);
170                     case {100}
171                         numRBs = nNumRbsPerSymF1(nNumerology,12+1);
172                     otherwise
173                         disp('Unknown BW && mu')
174                 end
175             end
176         else
177             disp('mmWave')
178             if  (mu >=2) && (mu <= 3)
179                 nNumerology = mu;
180                 switch (bw)
181                     case {50}
182                         numRBs = nNumRbsPerSymF2(nNumerology-1,0+1);
183                     case {100}
184                         numRBs = nNumRbsPerSymF2(nNumerology-1,1+1);
185                     case {200}
186                         numRBs = nNumRbsPerSymF2(nNumerology-1,2+1);
187                     case {400}
188                         numRBs = nNumRbsPerSymF2(nNumerology-1,3+1);
189                     otherwise
190                         disp('Unknown BW && mu')
191                 end
192             end
193         end
194     end
195     if numRBs ==0
196         disp('Incorrect Numerology and BW combination.')
197         return
198     end
199
200     bw
201     numRBs
202     nSlots
203
204     %use file as input
205     %ifft_in = load('ifft_in.txt')
206
207     %gen IQs
208     ifft_in = [[1:1:(numRBs*12)]', [1:1:(numRBs*12)]'];
209
210     ant_c = ifft_in;
211     for (i=1:1:nSlots*14-1)
212         ifft_in_1 = ifft_in + i;
213         ant_c = [ant_c; ifft_in_1];
214     end
215
216     %write files for IQ samples 
217     for ant = 1:1:ant_num
218         antX=ant_c*(ant*10);
219         antX_16=int16(antX.');
220         file_name = strcat(path_to_usecase,"ant_", num2str(ant-1),".bin");
221         disp(file_name)
222         fileID = fopen(file_name,'w');
223         fwrite(fileID, antX_16, 'int16');
224         fclose(fileID);    
225     end
226
227     if bfw_gen
228         disp('Generate BF Weights per RB')
229
230         %seed to make it repeatable 
231         rand('seed',47)
232
233         %random channel matrix for single sym on syngle RB 
234         H = (rand(trx,ant_num) + 1j*rand(trx,ant_num));
235
236         %calculate weights 
237         % W_dl = H^*(H^TH^*)^-1
238         % W_ul =  ((H^H*H)^-1)H^H
239         % where H^* - conjugate 
240         %       H^T - transpose
241         %       H^H - conjugate transpose 
242         W_dl = conj(H)*(transpose(H)*conj(H))^-1; %weights for DL
243         W_ul = ((ctranspose(H)*H)^-1)*ctranspose(H); %weights for UL
244
245         W_ul = W_ul.';
246
247         for ant = 1:1:ant_num
248             %DL 
249
250             bfw_per_sym = [];
251             % adjust channel per each RB 
252             for iPrb = 1:1:numRBs
253                bfw_per_sym = [ bfw_per_sym, [real((W_dl(:, ant).'))*iPrb; imag((W_dl(:, ant).'))*iPrb]];
254             end
255
256             bfw_all_slots = [];
257             %reuse channel for all symbols  
258             for (slot_idx=1:1:nSlots*14)
259                 bfw_all_slots = [bfw_all_slots, bfw_per_sym];
260             end
261
262             bfw_all_slots_int = int16(bfw_all_slots./max(max(abs((bfw_all_slots.')))).*2^15);
263
264             %write files for IQ samples 
265             antX_16=bfw_all_slots_int.';
266             file_name = strcat(path_to_usecase,"dl_bfw_ue_", num2str(ant-1),".bin");
267             disp(file_name)
268             fileID = fopen(file_name,'w');
269             fwrite(fileID,antX_16, 'int16');
270             fclose(fileID);    
271
272             %UL 
273             bfw_per_sym = [];
274             % adjust channel per each RB 
275             for iPrb = 1:1:numRBs
276                bfw_per_sym = [ bfw_per_sym, [real((W_ul(:, ant).'))*iPrb; imag((W_ul(:, ant).'))*iPrb]];
277             end
278
279             bfw_all_slots = [];
280             %reuse channel for all symbols  
281             for (slot_idx=1:1:nSlots*14)
282                 bfw_all_slots = [bfw_all_slots, bfw_per_sym];
283             end
284
285             bfw_all_slots_int = int16(bfw_all_slots./max(max(abs((bfw_all_slots.')))).*2^15);
286
287             %write files for IQ samples 
288             antX_16=bfw_all_slots_int.';
289             file_name = strcat(path_to_usecase,"ul_bfw_ue_", num2str(ant-1),".bin");
290             disp(file_name)
291             fileID = fopen(file_name,'w');
292             fwrite(fileID,antX_16, 'int16');
293             fclose(fileID);    
294         end  
295     end
296 end
297
298 %% generate IQ file with valid constellation, for DL modulation compression
299 % only in mu1_100mhz
300 %constellation = [4096, -4096];
301 %constellation = [2590, 7770, -7770, -2590];
302 %constellation = [633, 1897, 3161, 4425, -4424, -3160, -1897, -633];
303 constellation_all = [628, 1885, 3141, 4398, 5654, 6911, 8167, 9424, -9424, -8167, -6911, -5654, -4398, -3141, -1885, -628;
304     633, 1897, 3161, 4425, -4424, -3160, -1897, -633, 633, 1897, 3161, 4425, -4424, -3160, -1897, -633;
305     2590, 7770, -7770, -2590, 2590, 7770, -7770, -2590, 2590, 7770, -7770, -2590, 2590, 7770, -7770, -2590;
306     4096, -4096, 4096, -4096, 4096, -4096, 4096, -4096, 4096, -4096, 4096, -4096, 4096, -4096, 4096, -4096;
307     ];
308
309 numRBs = 273
310 nSlots = 20
311 path_all = ...
312     [
313       "./usecase/cat_a/mu1_100mhz/";
314       "./usecase/cat_b/mu1_100mhz/";
315       "./usecase/cat_b/mu1_100mhz/";
316       "./usecase/cat_b/mu1_100mhz/";
317     ]
318 path_all = cellstr(path_all)
319 modtype_all = ...
320     [
321       "256qam_ant_";
322       "64qam_ant_";
323       "16qam_ant_";
324       "qpsk_ant_";
325     ]
326 modtype_all = cellstr(modtype_all)
327
328 for test_num = 1:4
329     path = path_all(test_num);
330     constellation=constellation_all(test_num,:);
331     modtype = modtype_all(test_num);
332     for ant = 1:4
333         ant_in = rand(2*12*numRBs*14*nSlots,1); % random constellation
334         ant_in = 1+round(15 * ant_in);
335         ant_out = constellation(ant_in);
336         file_name = strcat(path,modtype, num2str(ant-1),".bin");
337         disp(file_name)
338         fileID = fopen(file_name,'w');
339         fwrite(fileID, ant_out, 'int16');
340         fclose(fileID);
341     end
342 end
343
344