-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsrsOFDMDemodulatorUnittest.m
202 lines (171 loc) · 8.65 KB
/
srsOFDMDemodulatorUnittest.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
%srsOFDMDemodulatorUnittest Unit tests for OFDM demodulator functions.
% This class implements unit tests for the OFDM demodulator functions using the
% matlab.unittest framework. The simplest use consists in creating an object with
% testCase = srsOFDMDemodulatorUnittest
% and then running all the tests with
% testResults = testCase.run
%
% srsOFDMDemodulatorUnittest Properties (Constant):
%
% srsBlock - The tested block (i.e., 'ofdm_demodulator').
% srsBlockType - The type of the tested block, including layer
% (i.e., 'phy/lower/modulation').
%
% srsOFDMDemodulatorUnittest Properties (ClassSetupParameter):
%
% outputPath - Path to the folder where the test results are stored.
%
% srsOFDMDemodulatorUnittest Properties (TestParameter):
%
% numerology - Defines the subcarrier spacing (0, 1).
% DFTsize - Size of the DFT (128, 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096).
% CyclicPrefix - Cyclic prefix type ('normal', 'extended').
% NSlot - Slot index (0...15).
%
% srsOFDMDemodulatorUnittest Methods (TestTags = {'testvector'}):
%
% testvectorGenerationCases - Generates test vectors according to the provided
% parameters.
%
% srsOFDMDemodulatorUnittest 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, nrOFDMModulate and nrOFDMDemodulate.
% 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 srsOFDMDemodulatorUnittest < srsTest.srsBlockUnittest
properties (Constant)
%Name of the tested block.
srsBlock = 'ofdm_demodulator'
%Type of the tested block.
srsBlockType = 'phy/lower/modulation'
end
properties (ClassSetupParameter)
%Path to results folder (old 'ofdm_demodulator' tests will be erased).
outputPath = {['testOFDMdemodulator', char(datetime('now', 'Format', 'yyyyMMdd''T''HHmmss'))]}
end
properties (TestParameter)
%Defines the subcarrier spacing (0, 1, 2, 3, 4).
numerology = {0, 1, 2, 3}
%Size of the DFT (256, 512, 1024, 2048, 4096). Only standard values.
DFTsize = {256, 512, 1024, 2048, 4096}
%Cyclic prefix type ('normal', 'extended').
CyclicPrefix = {'normal', 'extended'}
%Slot index (0...15).
NSlot = num2cell(0:15)
end
methods (Access = protected)
function addTestIncludesToHeaderFile(obj, fileID)
%addTestIncludesToHeaderFile Adds include directives to the test header file.
addTestIncludesToHeaderFilePHYchmod(obj, fileID);
end
function addTestDefinitionToHeaderFile(~, fileID)
%addTestDetailsToHeaderFile Adds details (e.g., type/variable declarations) to the test header file.
fprintf(fileID, 'struct ofdm_demodulator_test_configuration {\n');
fprintf(fileID, 'ofdm_demodulator_configuration config;\n');
fprintf(fileID, 'uint8_t port_idx;\n');
fprintf(fileID, 'uint8_t slot_idx;\n');
fprintf(fileID, '};\n\n');
fprintf(fileID, 'struct test_case_t {\n');
fprintf(fileID, 'ofdm_demodulator_test_configuration test_config;\n');
fprintf(fileID, 'file_vector<cf_t> data;\n');
fprintf(fileID, ...
'file_vector<resource_grid_writer_spy::expected_entry_t> demodulated;\n');
fprintf(fileID, '};\n');
end
end % of methods (Access = protected)
methods (Test, TestTags = {'testvector'})
function testvectorGenerationCases(testCase, numerology, DFTsize, CyclicPrefix)
%testvectorGenerationCases Generates a test vector for the given numerology,
% DFTsize and CyclicPrefix. NSlot, port index, scale and center carrier
% frequency are randomly generated.
import srsLib.phy.helpers.srsRandomGridEntry
import srsTest.helpers.writeResourceGridEntryFile
import srsTest.helpers.writeComplexFloatFile
% Generate a unique test ID.
testID = testCase.generateTestID;
% Use a unique port index and scale for each test.
portIdx = randi([0, 15]);
scale = 2 * rand - 1;
nSlot = randi([0 pow2(numerology)-1]);
% Select a random carrier frequency from 0 to 3 GHz to avoid
% any multiple of the sampling rate. Granularity 100 kHz.
CarrierFrequency = round(rand() * 3e4) * 1e5;
% Current fixed parameter values.
nStartGrid = 0;
nFrame = 0;
% Calculate the number of RBs to be used.
nSizeGrid = floor(192 * (DFTsize / 4096));
% Skip those invalid configuration cases.
isCPTypeOK = ((numerology == 2) || strcmp(CyclicPrefix, 'normal'));
isNSizeGridOK = nSizeGrid > 0;
if isCPTypeOK && isNSizeGridOK
% Configure the carrier according to the test parameters.
subcarrierSpacing = 15 * (2 .^ numerology);
carrier = nrCarrierConfig( ...
SubcarrierSpacing=subcarrierSpacing, ...
NStartGrid=nStartGrid, ...
NSizeGrid=nSizeGrid, ...
NSlot=nSlot, ...
NFrame=nFrame, ...
CyclicPrefix=CyclicPrefix ...
);
% Generate the DFT input data and related indices.
[inputData, inputIndices] = srsRandomGridEntry(carrier, portIdx);
% Call the OFDM modulator MATLAB functions.
timeDomainData = nrOFDMModulate(carrier, reshape(inputData, [nSizeGrid * 12, carrier.SymbolsPerSlot]), ...
'Windowing', 0, 'CarrierFrequency', CarrierFrequency);
% Write the time-domain data into a binary file.
testCase.saveDataFile('_test_input', testID, ...
@writeComplexFloatFile, timeDomainData);
% Call the OFDM demodulator MATLAB functions.
demodulatedData = nrOFDMDemodulate(carrier, timeDomainData, ...
'CyclicPrefixFraction', 0, 'CarrierFrequency', CarrierFrequency);
% Apply the requested scale.
demodulatedData = demodulatedData * scale;
% Reshape the demodulated data and write it with its associated indices into a binary file.
demodulatedGrid = reshape(demodulatedData, [], 1);
testCase.saveDataFile('_test_output', testID, ...
@writeResourceGridEntryFile, demodulatedGrid, inputIndices);
cpString = ['cyclic_prefix::', upper(CyclicPrefix)];
% Select a DFT window offset between 0 to half cyclic
% prefix length.
DftWindowOffset = randi([0, (72 * DFTsize) / 2048]);
configCell = {
numerology, ... % numerology
nSizeGrid, ... % bw_rb
DFTsize, ... % dft_size
cpString, ... % cp
DftWindowOffset, ... % nof_samples_window_offset
scale, ... % scale
CarrierFrequency, ... % center_freq_hz
};
ofdmDemodulatorConfigCell = {
configCell, ... % config
portIdx, ... % port_idx
nSlot, ... % slot_idx
};
% Generate the test case entry.
testCaseString = testCase.testCaseToString(testID, ...
ofdmDemodulatorConfigCell, true, '_test_input', '_test_output');
% Add the test to the file header.
testCase.addTestToHeaderFile(testCase.headerFileID, testCaseString);
end
end % of function testvectorGenerationCases
end % of methods (Test, TestTags = {'testvector'})
end % of classdef srsOFDMDemodulatorUnittest