-
Notifications
You must be signed in to change notification settings - Fork 14
/
srsPUCCHDetectorFormat1Unittest.m
383 lines (318 loc) · 15.9 KB
/
srsPUCCHDetectorFormat1Unittest.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
%srsPUCCHDetectorFormat1Unittest Unit test for PUCCH Format1 detector.
% This class implements unit tests for the PUCCH Format1 detector using the
% matlab.unittest framework. The simplest use consists in creating an object
% with
% testCase = srsPUCCHDetectorFormat1Unittest
% and then running all the tests with
% testResults = testCase.run
%
% srsPUCCHDetectorFormat1Unittest Properties (Constant):
%
% srsBlock - The tested block (i.e., 'pucch_detector').
% srsBlockType - The type of the tested block, including layer
% (i.e., '/phy/upper/channel_processors').
%
% srsPUCCHDetectorFormat1Unittest Properties (ClassSetupParameter):
%
% outputPath - Path to the folder where the test results are stored.
%
% srsPUCCHDetectorFormat1Unittest Properties (TestParameter):
%
% numerology - Numerology index (0, 1).
% NumRxPorts - Number of Rx antenna ports (1, 2, 4).
% SymbolAllocation - PUCCH symbol allocation.
% FrequencyHopping - Frequency hopping type ('neither', 'intraSlot').
% ackSize - Number of HARQ-ACK bits (0, 1, 2).
% srSize - Number of SR bits (0, 1).
%
% srsPUCCHDetectorFormat1Unittest Methods (TestTags = {'testvector'}):
%
% testvectorGenerationCases - Generates a test vector for the given numerology,
% number of Rx antenna ports, symbol allocation,
% frequency hopping, number of ACK and SR bits.
%
% srsPUCCHDetectorFormat1Unittest Methods (TestTags = {'testmex'}):
%
% mexTest - Tests the MEX-based implementation of the PUCCH detector for Format 1.
%
% srsPUCCHDetectorFormat1Unittest Methods (Access = protected):
%
% addTestIncludesToHeaderFile - Adds include directives to the test header file.
% addTestDefinitionToHeaderFile - Adds details (e.g., type/variable declarations)
% to the test header file.
%
% See also matlab.unittest, nrPUCCH1, nrPUCCH.
% Copyright 2021-2024 Software Radio Systems Limited
%
% This file is part of srsRAN-matlab.
%
% srsRAN-matlab is free software: you can redistribute it and/or
% modify it under the terms of the BSD 2-Clause License.
%
% srsRAN-matlab 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
% BSD 2-Clause License for more details.
%
% A copy of the BSD 2-Clause License can be found in the LICENSE
% file in the top-level directory of this distribution.
classdef srsPUCCHDetectorFormat1Unittest < srsTest.srsBlockUnittest
properties (Constant)
%Name of the tested block.
srsBlock = 'pucch_detector'
%Type of the tested block, including layers.
srsBlockType = 'phy/upper/channel_processors/pucch'
end
properties (ClassSetupParameter)
%Path to results folder (old 'pucch_detector' tests will be erased).
outputPath = {['testPUCCHdetector', char(datetime('now', 'Format', 'yyyyMMdd''T''HHmmss'))]}
end
properties (TestParameter)
%Numerology index (0, 1).
% Allows to compute the subcarrier spacing in kilohertz as 15 * 2^numerology.
% Note: Higher numerologies are currently not considered.
numerology = {0, 1}
%Number of Rx antenna ports (1, 2, 4).
NumRxPorts = {1, 2, 4}
%PUCCH symbol allocation.
% The symbol allocation is described by a two-element row array with,
% in order, the first allocated symbol and the number of allocated
% symbols.
SymbolAllocation = {[0, 14], [1, 13], [5, 5], [10, 4]}
%Frequency hopping type ('neither', 'intraSlot').
% Note: Interslot frequency hopping is currently not considered.
FrequencyHopping = {'neither', 'intraSlot'}
%Number of HARQ-ACK bits (0, 1, 2).
ackSize = {0, 1, 2}
%Number of SR bits (0, 1).
% Note: No SR bit is sent if ackSize > 0. Also, no PUCCH is sent if ackSize == 0
% and the SR is negative (i.e., the SR bit is set to 0).
srSize = {0, 1}
end
methods (Access = protected)
function addTestIncludesToHeaderFile(~, fileID)
%addTestIncludesToHeaderFile(OBJ, FILEID) adds include directives to
% the header file pointed by FILEID, which describes the test vectors.
fprintf(fileID, '#include "../../../support/resource_grid_test_doubles.h"\n');
fprintf(fileID, '#include "srsran/phy/upper/channel_processors/pucch/pucch_detector.h"\n');
fprintf(fileID, '#include "srsran/ran/cyclic_prefix.h"\n');
fprintf(fileID, '#include "srsran/ran/pucch/pucch_mapping.h"\n');
fprintf(fileID, '#include "srsran/support/file_vector.h"\n');
end
function addTestDefinitionToHeaderFile(~, fileID)
%addTestDefinitionToHeaderFile(OBJ, FILEID) adds test details (e.g., type
% and variable declarations) to the header file pointed by FILEID, which
% describes the test vectors.
fprintf(fileID, 'struct test_case_t {\n');
fprintf(fileID, 'pucch_detector::format1_configuration cfg = {};\n');
fprintf(fileID, 'float noise_var = 0;\n');
fprintf(fileID, 'std::vector<uint8_t> sr_bit;\n');
fprintf(fileID, 'std::vector<uint8_t> ack_bits;\n');
fprintf(fileID, 'file_vector<resource_grid_reader_spy::expected_entry_t> received_symbols;\n');
fprintf(fileID, 'file_vector<resource_grid_reader_spy::expected_entry_t> ch_estimates;\n');
fprintf(fileID, '};\n');
end
end % of methods (Access = protected)
methods (Test, TestTags = {'testvector'})
function testvectorGenerationCases(obj, numerology, NumRxPorts, SymbolAllocation, ...
FrequencyHopping, ackSize, srSize)
%testvectorGenerationCases generates a test vector for the given numerology,
% number of Rx antenna ports, symbol allocation, frequency hopping, number of
% ACK and SR bits.
import srsTest.helpers.matlab2srsCyclicPrefix
import srsTest.helpers.matlab2srsPUCCHGroupHopping
import srsTest.helpers.writeResourceGridEntryFile
% Generate a unique test ID.
testID = obj.generateTestID('_test_received_symbols');
[rxSymbols, ack, sr, channelCoefs, configuration] = ...
generateSimData(numerology, NumRxPorts, SymbolAllocation, FrequencyHopping, ...
ackSize, srSize);
nREs = size(configuration.Indices, 1);
indices = nan(NumRxPorts * nREs, 3);
for iPort = 0:(NumRxPorts - 1)
ix = iPort * nREs + (1:nREs);
indices(ix, 1:2) = configuration.Indices(:, 1:2);
indices(ix, 3) = iPort;
end
obj.saveDataFile('_test_received_symbols', testID, ...
@writeResourceGridEntryFile, rxSymbols(:), indices);
obj.saveDataFile('_test_ch_estimates', testID, ...
@writeResourceGridEntryFile, channelCoefs(:), indices);
cyclicPrefixConfig = matlab2srsCyclicPrefix(configuration.CyclicPrefix);
groupHoppingConfig = matlab2srsPUCCHGroupHopping(configuration.GroupHopping);
if NumRxPorts == 1
ports = {0};
else
ports = 0:(NumRxPorts - 1);
end
betaPUCCH = 1;
pucch = configuration.PUCCH;
% Generate PUCCH Format 1 configuration.
pucchF1Config = {...
{numerology, configuration.NSlot}, ... % slot
cyclicPrefixConfig, ... % cp
configuration.PRBSet, ... % starting_prb
configuration.SecondHopConfig, ... % second_hop_prb
pucch.SymbolAllocation(1), ... % start_symbol_index
pucch.SymbolAllocation(2), ... % nof_symbols
groupHoppingConfig, ... % group_hopping
ports, ... % ports
betaPUCCH, ... % beta_pucch
pucch.OCCI, ... % time_domain_occ
pucch.InitialCyclicShift, ... % initial_cyclic_shift
configuration.NCellID, ... % n_id
ackSize, ... % nof_harq_ack
};
% Generate the test case entry.
testCaseString = obj.testCaseToString(testID, {pucchF1Config, ...
configuration.NoiseVar, num2cell(sr), num2cell(ack)}, ...
false, '_test_received_symbols', '_test_ch_estimates');
% Add the test to the file header.
obj.addTestToHeaderFile(obj.headerFileID, testCaseString);
end % of function testvectorGenerationCases(...)
end % of methods (Test, TestTags = {'testvector'})
methods (Test, TestTags = {'testmex'})
function mexTest(obj, numerology, NumRxPorts, SymbolAllocation, FrequencyHopping, ackSize, srSize)
%mexTest Tests the mex wrapper of the srsRAN PUCCH detector for Format 1.
% mexTest(testCase, numerology, SymbolAllocation, FrequencyHopping, ackSize, srSize)
% runs a short simulation with a PUCCH transmission specified by the given
% numerology, symbol allocation, frequency hopping, number of ACK and SR bits.
import srsMEX.phy.srsPUCCHDetector
[rxSymbols, ack, sr, channelCoefs, configuration] = ...
generateSimData(numerology, NumRxPorts, SymbolAllocation, FrequencyHopping, ...
ackSize, srSize);
% Create a PUCCH Format 1 detector.
srspucch = srsPUCCHDetector;
carrier = configuration.Carrier;
% Copy the received signal into a resource grid.
rxGrid = nrResourceGrid(carrier, NumRxPorts);
nREs = size(configuration.Indices, 1);
indices = nan(NumRxPorts * nREs, 1);
indices(1:nREs) = sub2ind(size(rxGrid), configuration.Indices(:, 1) + 1, ...
configuration.Indices(:, 2) + 1, ones(nREs, 1));
for iPort = 2:NumRxPorts
ix = (1:nREs) + (iPort - 1) * nREs;
indices(ix) = sub2ind(size(rxGrid), configuration.Indices(:, 1) + 1, ...
configuration.Indices(:, 2) + 1, ones(nREs, 1) * iPort);
end
rxGrid(indices) = rxSymbols;
% Copy the estimated channel coefficients into a resource grid.
chGrid = nrResourceGrid(carrier, NumRxPorts);
chGrid(indices) = channelCoefs;
% Run the detector.
uci = srspucch(carrier, configuration.PUCCH, ackSize, rxGrid, chGrid, configuration.NoiseVar * ones(NumRxPorts, 1));
if (ackSize == 0)
if (srSize == 0)
assertFalse(obj, uci.isValid, 'An empty PUCCH occasion should return an ''invalid'' UCI.');
return;
end
if (sr == 1)
assertTrue(obj, uci.isValid, 'A positive SR-only PUCCH should return a ''valid'' UCI.');
return;
end
assertFalse(obj, uci.isValid, 'A negative SR-only PUCCH should return an ''invalid'' UCI.');
return;
end
assertTrue(obj, uci.isValid, 'An ACK-carrying PUCCH should return a ''valid'' UCI.');
assertLength(obj, uci.HARQAckPayload, ackSize, 'Wrong number of ACK bits.');
assertEqual(obj, uci.HARQAckPayload, int8(ack), 'HARQ-ACK bits do not match.');
end % of function mexTest(obj, numerology, SymbolAllocation, ...
end % of methods (Test, TestTags = {'testmex'})
end % of srsPUCCHDetectorFormat1Unittest < srsTest.srsBlockUnittest
%Generates simulation data (modulated symbols, ACK and SR values, channel coefficients and configuration objects).
function [rxSymbols, ack, sr, channelCoefs, configuration] = generateSimData(numerology, ...
nPorts, symbolAllocation, frequencyHopping, ackSize, srSize)
import srsLib.phy.upper.channel_processors.srsPUCCH1
% Generate random cell ID and slot number.
nCellID = randi([0, 1007]);
if numerology == 0
nSlot = randi([0, 9]);
else
nSlot = randi([0, 19]);
end
% Fix BWP size and start as well as the frame number, since they
% are irrelevant for the test.
NSizeBWP = 51;
NStartBWP = 1;
nSizeGrid = NSizeBWP + NStartBWP;
nStartGrid = 0;
nFrame = 0;
% Cyclic prefix can only be normal in the supported numerologies.
cyclicPrefix = 'normal';
% Configure the carrier according to the test parameters.
subcarrierSpacing = 15 * (2 .^ numerology);
carrier = nrCarrierConfig( ...
NCellID=nCellID, ...
SubcarrierSpacing=subcarrierSpacing, ...
NSizeGrid=nSizeGrid, ...
NStartGrid=nStartGrid, ...
NSlot=nSlot, ...
NFrame=nFrame, ...
CyclicPrefix=cyclicPrefix ...
);
% PRB assigned to PUCCH Format 1 within the BWP.
PRBSet = randi([0, NSizeBWP - 1]);
if strcmp(frequencyHopping, 'intraSlot')
% When intraslot frequency hopping is enabled, the OCCI value must be less
% than one fourth of the number of OFDM symbols allocated for the PUCCH.
maxOCCindex = max([floor(symbolAllocation(2) / 4) - 1, 0]);
secondHopStartPRB = randi([1, NSizeBWP - 1]);
secondHopConfig = {secondHopStartPRB};
else
% When intraslot frequency hopping is disabled, the OCCI value must be less
% than one half of the number of OFDM symbols allocated for the PUCCH.
maxOCCindex = max([floor(symbolAllocation(2) / 2) - 1, 0]);
secondHopStartPRB = 0;
secondHopConfig = {};
end % of if strcmp(frequencyHopping, 'intraSlot')
occi = randi([0, maxOCCindex]);
% We don't test group hopping or sequence hopping.
groupHopping = 'neither';
% The initial cyclic shift can be set randomly.
possibleShifts = 0:3:9;
initialCyclicShift = possibleShifts(randi([1, 4]));
% Configure the PUCCH.
pucch = nrPUCCH1Config( ...
SymbolAllocation=symbolAllocation, ...
PRBSet=PRBSet, ...
FrequencyHopping=frequencyHopping, ...
GroupHopping=groupHopping, ...
SecondHopStartPRB=secondHopStartPRB, ...
InitialCyclicShift=initialCyclicShift, ...
OCCI=occi ...
);
ack = randi([0, 1], ackSize, 1);
sr = randi([0, 1], srSize, 1);
% Generate PUCCH Format 1 symbols.
[symbols, indices] = srsPUCCH1(carrier, pucch, ack, sr);
if isempty(symbols)
symbols = complex(zeros(size(indices, 1), 1));
end
nSymbols = length(symbols);
rxSymbols = complex(nan(nSymbols, nPorts));
channelCoefs = complex(nan(nSymbols, nPorts));
for iPort = 1:nPorts
channelTmp = randn(length(symbols), 2) * [1; 1j] / sqrt(2);
% Ensure no channel is very small.
channelTmpAbs = abs(channelTmp);
mask = (channelTmpAbs < 0.1);
channelTmp(mask) = channelTmp(mask) ./ channelTmpAbs(mask) * 0.1;
% AWGN.
snrdB = 20;
noiseVar = 10^(-snrdB / 10);
noiseSymbols = randn(length(symbols), 2) * [1; 1j] * sqrt(noiseVar / 2);
channelCoefs(:, iPort) = channelTmp;
rxSymbols(:, iPort) = symbols .* channelTmp + noiseSymbols;
end
configuration = struct();
configuration.Indices = indices;
configuration.CyclicPrefix = cyclicPrefix;
configuration.GroupHopping = groupHopping;
configuration.NSlot = nSlot;
configuration.PRBSet = PRBSet;
configuration.SecondHopConfig = secondHopConfig;
configuration.PUCCH = pucch;
configuration.Carrier = carrier;
configuration.NCellID = nCellID;
configuration.NoiseVar = noiseVar;
end