forked from hassiweb/otfs-chan-est-and-eq
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOTFS.m
424 lines (376 loc) · 17.6 KB
/
OTFS.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
% Copyright (c) 2021, KDDI Research, Inc. and KDDI Corp. All rights reserved.
addpath('functions');
addpath('classes');
clear variables
rng('default');
% rng('shuffle');
warning ('on','all'); % Turn on when testing
%% %%%%%%%%%%%%%%%%%%%%%%% INPUT PARAMETERS %%%%%%%%%%%%%%%%%%%%%%% %%
% System Parameters
nSubcarriersPerBlock = 12; % Number of SCs / PRB
nSymbolsPerBlock = 14; % Number of symbols per frame
fSubcarrierSpacing = 15e3; % SC spacing in Hz
nFft = 256; % FFT size
nCyclicPrefix = floor(nFft * 0.067); % CP length in samples (6.7% is the calibration assumption defined in 3GPP R1-165989)
samplingRate = nFft * fSubcarrierSpacing; % Sampling rate
modOrder = 16; % 4 for QPSK, 16 for 16QAM, 64 for 64QAM, 256 for 256QAM
nTxAntennas = 1;
nRxAntennas = 1;
nSubcarriers = nFft;
nBitsPerBlock = nSymbolsPerBlock * nSubcarriers * log2(modOrder);
% Parameter for LTE Channel Coder
turboDecodingNumIterations = 8;
codeRate = 0.5; % Coding rate of source data bits over transmition redundant data bits
codedTransportBlockSize = nBitsPerBlock * codeRate;
crcBitLength = 24; % defined in 3GPP TS36.2xx
maxCodeBlockSize = 6144; % defined in 3GPP TS36.2xx
numCodeBlockSegments = ceil((codedTransportBlockSize - crcBitLength) / maxCodeBlockSize);
if numCodeBlockSegments > 1
tbs = codedTransportBlockSize-crcBitLength*(numCodeBlockSegments+1); % CRC24A + CRC24B*numSegments
else
tbs = codedTransportBlockSize-crcBitLength; % CRC24A only
end
% Parameters for Scrambler
rnti = hex2dec('003D');
cellId = 0;
frameNo = 0; % radio frame
codeword = 0;
% Parameter for OFDM Modulator
nGuardBands = [(nFft - nSubcarriers)/2; (nFft - nSubcarriers)/2]; % Number of guard band subcarriers in both sides
% Parameter for ChannelEstimator
activeSubcarrierIndices = 1+nGuardBands(1):nFft-nGuardBands(2);
% Parameters for Fading Channel
velocity_kmph = 500; % user velocity [km/h]
delaySpread_ns = 300; % rms delay spred for TDL channel model only, in nano second
fCenter = 0.8e9; % center carrier frequency [Hz]
velocity = velocity_kmph*1000/(60*60); % convert km/h -> m/s
waveLength = physconst('lightspeed')/fCenter;
fDoppler = velocity / waveLength;
txCorrMat = 1;
rxCorrMat = 1;
%% %%%%%%%%%%%%%%%%%%%%%%% ALGORITHM SELECTION %%%%%%%%%%%%%%%%%%%%%%% %%
% ----- Path Estimator -----
channelEstimationAlgorithm = 'Pilot-based iterative path estimation';
% channelEstimationAlgorithm = 'PN-based iterative estimation';
% channelEstimationAlgorithm = 'Ideal';
fprintf("Channel estimation: %s\n", channelEstimationAlgorithm);
% ----- Equalizer -----
% equalizerAlgorithm = 'Vectorized equalizer';
% eqAlgorithm = 'blockedMMSE';
equalizerAlgorithm = 'Deconvolutional equalizer';
eqAlgorithm = 'Wiener';
fprintf("Channel equalization: %s\n", equalizerAlgorithm);
% ----- Channel -----
channelModel = 'EVA';
fprintf("Channel profile: %s\n", channelModel);
%---- Channel Coding ----
channelCoding = 'LTE';
% channelCoding = 'None';
fprintf("Channel coding: %s\n", channelCoding);
%% %%%%%%%%%%%%%%%%%%%%%%% OBJECT GENERATION %%%%%%%%%%%%%%%%%%%%%%% %%
switch channelCoding
case 'None'
% Bit Generator
hBitGenerator = RandomBitGenerator('DataLength', nBitsPerBlock);
% Symbol Mapper
hSymbolMapper = QamMapperV2( ...
'ModOrder', modOrder, ...
'InputType', 'bit', ...
'OutputType', 'bit', ... % 'bit' for uncoded, 'llr' for coded
'UnitAveragePower', true, ...
'LLROverflowPrevention', true);
case 'LTE'
% Bit Generator
hBitGenerator = RandomBitGenerator('DataLength', tbs);
% Symbol Mapper
hSymbolMapper = QamMapperV2( ...
'ModOrder', modOrder, ...
'InputType', 'bit', ...
'OutputType', 'llr', ... % 'bit' for uncoded, 'llr' for coded
'UnitAveragePower', true, ...
'LLROverflowPrevention', true);
% Symbol Mapper for uncoded
hSymbolMapperUncoded = QamMapperV2( ...
'ModOrder', modOrder, ...
'InputType', 'bit', ...
'OutputType', 'bit', ... % 'bit' for uncoded, 'llr' for coded
'UnitAveragePower', true, ...
'LLROverflowPrevention', true);
% LTE Channel Coder
hChannelCoder = LteChannelCoder( ...
'TurboDecodingNumIterations', turboDecodingNumIterations, ...
'ModOrder', modOrder, ...
'NumLayers', 1, ...
'OutputLength', nBitsPerBlock, ...
'LinkDirection', 'Downlink', ...
'RedundancyVersion', 0, ...
'TBS', tbs);
% Scrambler
hScrambler = LteScrambler(rnti, cellId, frameNo, codeword);
end
% OFDM Modulator
hOfdmModulator = CpOfdmModulator(...
'FFTLength', nFft, ...
'NumGuardBandCarriers', nGuardBands, ...
'NumSymbols', nSymbolsPerBlock, ...
'CyclicPrefixLength', nCyclicPrefix, ...
'InsertDCNull', false, ...
'PilotInputPort', false, ...
'PilotOutputPort', false, ...
'NumTransmitAntennas', nTxAntennas, ...
'NumReceiveAntennas', nRxAntennas, ...
'SubcarrierIndexOrder', 'ZeroToFFTSize');
% OTFS Precoder
hPrecoder = OtfsPrecoder( ...
'NumDelayBins', nSubcarriers, ...
'NumDopplerBins', nSymbolsPerBlock, ...
'NumTransmitAntennas', nTxAntennas, ...
'NumReceiveAntennas', nRxAntennas);
% Path Estimator
switch channelEstimationAlgorithm
case 'Pilot-based iterative path estimation'
hEstimator = OtfsPilotResponseBasedPathParameterEstimator( ...
'DividingNumber', 10, ...
'CyclicPrefixLength', nCyclicPrefix, ... % Ncp
'NumDopplerBins', nSymbolsPerBlock, ... % N
'NumDelayBins', nSubcarriers, ... % M
'SamplingRate', samplingRate ...
);
threshAlpha = 1/50;
threshBeta = 1/10;
localUpdateThreshold = 0.01;
case 'PN-based iterative estimation'
% threshold = 1/150; % This is the best performance regardless the computation time
threshold = 1/25; % This is the same number of paths to be estimated with the proposed CE
hEstimator = OtfsPNSeqBasedPathParameterEstimator( ...
'DividingNumber', 10, ...
'CyclicPrefixLength', nCyclicPrefix, ... % Ncp
'NumDopplerBins', nSymbolsPerBlock, ... % N
'NumDelayBins', nSubcarriers, ... % M
'SamplingRate', samplingRate, ...
'Threshold', threshold);
case 'Ideal'
end
% OTFS Equalizer
switch equalizerAlgorithm
case 'Deconvolutional equalizer'
hEqualizer = OtfsDeconvolutionalEqualizer( ...
'NumSymbols', nSymbolsPerBlock, ... % N
'CyclicPrefixLength', nCyclicPrefix, ... % Ncp
'NumSubcarriers', nSubcarriers, ... % M
'SamplingRate', samplingRate, ...
'DividingNumber', 10, ...
'EqualizationAlgorithm', eqAlgorithm);
case 'Vectorized equalizer'
hEqualizer = OtfsVectorizedEqualizer( ...
'EqualizationAlgorithm', 'ZF', ...
'NumSymbols', nSymbolsPerBlock, ... % N
'CyclicPrefixLength', nCyclicPrefix, ... % Ncp
'NumSubcarriers', nSubcarriers, ... % M
'SamplingRate', samplingRate, ...
'OutputConditionNumber', false, ...
'EqualizationAlgorithm', eqAlgorithm);
end
% Fading Channel
hFading = SoSBasedChannel( ... % based on in-house implementation
'ChannelModel', channelModel, ...
'RMSDelaySpread', delaySpread_ns, ...
'SamplingRate', samplingRate, ...
'DopplerFrequency', fDoppler, ...
'NumTxAntennas', nTxAntennas, ...
'NumRxAntennas', nRxAntennas, ...
'TxCorrMatrix', txCorrMat, ...
'RxCorrMatrix', rxCorrMat, ...
'OutputCIR', true, ...
'CyclicPrefix', nCyclicPrefix, ...
'FFTSize', nFft, ...
'ImpulseSource', 'Input', ...
'SequentialOrRandomChannel', 'Sequential', ...
'FDFMethod', 'ApplyFDFToPathParameters');
% AWGN
hAwgn = AwgnChannel('N0');
%% %%%%%%%%%%%%%%%%%%%%%%% SIMULATION SETTINGS %%%%%%%%%%%%%%%%%%%%%%% %%
% Create an empty storage to store simulation status
snrRange = 10:2:30; % in dB
simIterations = 1000 * ones(1,length(snrRange));
simstatus = table; % use table-type variable since it's good to see in the workspace
simstatus.SNR = snrRange';
simstatus.BER = nan * zeros(length(snrRange),1);
simstatus.UncodedBER = nan * zeros(length(snrRange),1);
simstatus.BLER = nan * zeros(length(snrRange),1);
simstatus.TotalBitErrors = zeros(length(snrRange),1);
simstatus.TotalUncodedBitErrors = zeros(length(snrRange),1);
simstatus.TotalBlockErrors = zeros(length(snrRange),1);
simstatus.Iteration = zeros(length(snrRange),1);
simstatus.PathCounts = zeros(length(snrRange),1);
simstatus.ComputeTime = zeros(length(snrRange),1);
time = strrep(strrep(strrep(string(datetime),'/',''),' ',''),':','');
filename = strcat('Resume-', mfilename, '-', time);
%% Load data when finding files that are created when simulation was interrupted
resumefiles = ls(sprintf('Resume-%s-*.mat', mfilename));
if not(isempty(resumefiles))
fprintf("Found file(s) to resume a simulation:\n")
disp(resumefiles)
for file = resumefiles'
prompt = strcat('Do you want to resume the simulation using "', file', '"? [Y/n/F]:');
answer = input(prompt, 's');
if answer == 'Y'
disp("Now Loading...")
load(file, 'simstatus') % Load only the simulation status
snrRange = simstatus.SNR';
filename = file;
break;
elseif answer == 'F'
disp("Now Loading...")
load(file) % Load all parameters
snrRange = simstatus.SNR';
filename = file;
break;
end
end
end
%% %%%%%%%%%%%%%%%%%%%%%%% START SIMULATION %%%%%%%%%%%%%%%%%%%%%%% %%
% For ideal channel estimation
delayDopplerImpulse = zeros(nSubcarriers, nSymbolsPerBlock); delayDopplerImpulse(1,1)=sqrt(nSymbolsPerBlock*nSubcarriers);
ddImpulseInTFDomain = hPrecoder.encode(delayDopplerImpulse);
ddImpulseInTimeDomain = hOfdmModulator.modulate(ddImpulseInTFDomain);
% For PN-sequence based channel estimation
switch channelEstimationAlgorithm
case 'PN-based estimation'
txPNSeq = zeros((nCyclicPrefix+nFft)*nSymbolsPerBlock*10,1); % for long PN sequence
% txPNSeq = zeros((nCyclicPrefix+nFft)*nSymbolsPerBlock*1,1); % for short PN sequence
nPNSeq = 1023;
tmpSeq = repmat(genltegoldseq(nPNSeq, de2bi(31,31)), 1, ceil(length(txPNSeq)/nPNSeq)); % 1023-length with an initial value of 31
tmpSeq(tmpSeq==0) = -1; % map 0 or 1 bit into -1 or 1 bit
txPNSeq = tmpSeq(1:length(txPNSeq))';
case 'PN-based iterative estimation'
txPNSeq = zeros((nCyclicPrefix+nFft)*nSymbolsPerBlock,1);
nPNSeq = 1023;
tmpSeq = repmat(genltegoldseq(nPNSeq, de2bi(31,31)), 1, ceil(length(txPNSeq)/nPNSeq)); % 1023-length with an initial value of 31
tmpSeq(tmpSeq==0) = -1; % map 0 or 1 bit into -1 or 1 bit
txPNSeq = tmpSeq(1:length(txPNSeq))';
end
charCount = 49+17;
for snrdb = snrRange
rng('default');
rng(106);
fprintf('\nSNR = %2d dB \n', snrdb);
snrIndex = find(snrRange==snrdb);
snr = 10^(snrdb/10);
noiseVar = 1/snr; % Total power
N0 = noiseVar/nFft; % N0 is the power spectral density of noise per unit of bandwidth, which is band-limited.
totalBitErrors = 0;
fprintf(repmat(' ', 1, charCount)); % Print spaces in advance to avoid deleting the previously displayed characters
tic
for count = simstatus.Iteration(snrIndex)+1:simIterations(snrIndex)
%% Transmitter
% Bit Generation
txBits = hBitGenerator.generate();
% txBits = zeros(size(txBits)); % FOR TEST
switch channelCoding
case 'None'
txScrampledBits = txBits;
case 'LTE'
% Channel Encoding
txCodedBits = hChannelCoder.encode(txBits);
% Scrambler
txScrampledBits = hScrambler.scramble(txCodedBits);
end
% Symbol Mapping (QAM modulation)
txSymbols = hSymbolMapper.map(txScrampledBits);
% OTFS Modulation
txBlocks = reshape(txSymbols, nSubcarriers, nSymbolsPerBlock);
txPrecodedBlocks = hPrecoder.encode(txBlocks);
% OFDM Modulation
txSignals = hOfdmModulator.modulate(txPrecodedBlocks);
%% Channel
% Fading Channel
switch channelEstimationAlgorithm
case {'Pilot-based iterative path estimation', 'Ideal'}
hFading.initRayleighFading();
[distortedSignals, cir] = hFading.apply(txSignals, ddImpulseInTimeDomain);
case 'PN-based iterative estimation'
[distortedSignals, distortedPNSeq] = hFading.apply(txSignals, txPNSeq);
end
% AWGN Channel
[noisySignals, noise] = hAwgn.add(distortedSignals, N0);
%% Receiver
% OFDM Demodulator
rxPrecodedBlocks = hOfdmModulator.demodulate(noisySignals(1:(nFft+nCyclicPrefix)*nSymbolsPerBlock));
% OTFS Demodulator
rxBlocks = hPrecoder.decode(rxPrecodedBlocks);
% Equalization (Ideal)
switch channelEstimationAlgorithm
case 'Pilot-based iterative path estimation'
noisyCir = hAwgn.add(cir, N0);
chanEst = hPrecoder.decode(hOfdmModulator.demodulate(noisyCir(1:(nFft+nCyclicPrefix)*nSymbolsPerBlock)));
[estGains, estDopplers, estDelays, estOffsets] = hEstimator.estimate(chanEst, threshAlpha, threshBeta, noiseVar);
rxEqBlocks = hEqualizer.equalize(rxBlocks, estGains, estDopplers, estDelays, estOffsets, noiseVar, localUpdateThreshold);
case 'PN-based iterative estimation'
noisyPNSeq = hAwgn.add(distortedPNSeq, noiseVar*sqrt(nSubcarriers));
[estGains, estDopplers, estDelays, estOffsets] = hEstimator.estimateIteratively(noisyPNSeq, txPNSeq, fDoppler*2);
rxEqBlocks = hEqualizer.equalize(rxBlocks, estGains, estDopplers, estDelays, estOffsets, noiseVar, 0.01);
case 'Ideal'
chanEst = hPrecoder.decode(hOfdmModulator.demodulate(cir(1:(nFft+nCyclicPrefix)*nSymbolsPerBlock)));
idealGains = hFading.pathRayleighGains;
idealDopplers = hFading.pathDopplers;
idealDelays = hFading.pathDelays;
idealOffsets = hFading.pathOffsets;
rxEqBlocks = hEqualizer.equalize(rxBlocks, idealGains, idealDopplers, idealDelays, idealOffsets, noiseVar);
end
% figure(1); clf; plot(rxEqBlocks, '.'); grid on; grid minor; xlim([-1.5 1.5]); ylim([-1.5 1.5]); % hold on; plot(rxIdealEqBlocks, '.'); hold off;
rxSymbols = rxEqBlocks(:);
% Symbol Demapping
rxSoftBits = hSymbolMapper.demap(rxSymbols, noiseVar);
% Channel Decoding
switch channelCoding
case 'None'
rxUncodedHardBits = rxSoftBits;
rxHardBits = rxUncodedHardBits;
if sum(xor(txBits, rxHardBits)) > 0
blockError = 1;
else
blockError = 0;
end
case 'LTE'
rxUncodedHardBits = hSymbolMapperUncoded.demap(rxSymbols);
% Descrambling
rxDescrambledBits = hScrambler.descramble(rxSoftBits);
[rxHardBits, blockError] = hChannelCoder.decode(rxDescrambledBits);
end
%% Result
% Bit Error Rate
numBitErrors = sum(xor(txBits, rxHardBits));
simstatus.TotalBitErrors(snrIndex) = simstatus.TotalBitErrors(snrIndex) + numBitErrors;
simstatus.BER(snrIndex) = simstatus.TotalBitErrors(snrIndex)/(count*nBitsPerBlock);
simstatus.TotalBlockErrors(snrIndex) = simstatus.TotalBlockErrors(snrIndex) + blockError;
simstatus.BLER(snrIndex) = simstatus.TotalBlockErrors(snrIndex)/count;
numBitErrors = sum(xor(txScrampledBits, rxUncodedHardBits));
simstatus.TotalUncodedBitErrors(snrIndex) = simstatus.TotalUncodedBitErrors(snrIndex) + numBitErrors;
simstatus.UncodedBER(snrIndex) = simstatus.TotalUncodedBitErrors(snrIndex)/(count*length(txScrampledBits));
fprintf(repmat('\b',1, charCount)); % Delete the prevously displayed characters, and then update the display
fprintf('[%5d /%5d] BLER: %1.4f, BER: %1.7f (%9d/ %9d)', count, simIterations(snrIndex), simstatus.BLER(snrIndex), simstatus.UncodedBER(snrIndex), simstatus.TotalUncodedBitErrors(snrIndex), count*length(txScrampledBits));
if mod(count,100)==0
simstatus.Iteration(snrIndex) = count;
save(filename); % save all parameters to resume
end
pause(0.01)
end
computationTime = toc;
computationTimePerIteration = computationTime/simIterations(snrIndex);
fprintf(' Computation Time : %f sec. (as per iteration)\n', computationTimePerIteration);
simstatus.ComputeTime(snrIndex) = computationTimePerIteration;
end
%% Save results
save(filename)
%% Show results
if usejava('jvm') % if GUI is available
figure
semilogy(simstatus.SNR, simstatus.BER);
ylabel('BER')
xlabel('SNR (dB)')
figure
semilogy(simstatus.SNR, simstatus.BLER);
ylabel('BLER')
xlabel('SNR (dB)')
end