Repository navigation
Expand file tree
/
Copy pathsimulation_OFDM_MIMO.m
More file actions
158 lines (129 loc) · 4.55 KB
/
Copy pathsimulation_OFDM_MIMO.m
File metadata and controls
158 lines (129 loc) · 4.55 KB
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% MIMO (multiplexer) IEEE 802.11n/ac simulation
%
% Copyright (C) 2022 Shiyue He (hsy1995313@gmail.com)
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU 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 General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear; close all;
%% Variables
global MCS_TAB
global N_CP N_LTF N_FFT N_LTFN N_DATA
MHz = 1e6; % 1MHz
Hz = 1;
Nbits = 8192;
MCSi = 4;
Ntxs = 1;
Nrxs = Ntxs;
Mod = MCS_TAB.mod(MCSi);
Nbps = log2(Mod) * N_DATA;
NO_CHANNEL = false;
%% Channel model
BW = 20; % Bandwidth (20 MHz)
doppler = 90 * Hz; % Doppler shift is around 5 Hz
path_delays = [0] / MHz;
avg_gains = [0]; % Average path gain in dB
mimochannel = comm.MIMOChannel(...
'SampleRate', BW * MHz,...
'PathDelays', path_delays,...
'AveragePathGains', avg_gains,...
'NormalizePathGains', true,...
'MaximumDopplerShift', doppler,...
'SpatialCorrelationSpecification','None', ...
'NumTransmitAntennas',Ntxs, ...
'NumReceiveAntennas',Nrxs);
%% Transmitter
TxBits = randi(2, [Nbits, Ntxs]) -1;
Npad = Nbps - mod(Nbits, Nbps);
TxPadBits = [TxBits; zeros(Npad, Ntxs)];
[STF, LTF, DLTF] = OFDM_PreambleGenerator(Ntxs);
TxModData = qammod(TxPadBits, Mod, 'InputType', 'bit', 'UnitAveragePower',true);
Payload_t = OFDM_Modulator(TxModData);
TxFrame = [STF; LTF; DLTF; Payload_t];
%% Channel model
if NO_CHANNEL
RxFrame = TxFrame;
else
RxFrame = mimochannel(TxFrame);
end
%% Receiver
if NO_CHANNEL
LTF_index = N_LTF * 2;
else
[sync_results, LTF_index] = OFDM_SymbolSync(RxFrame, LTF(2*N_CP +1: end, Nrxs), true);
end
if LTF_index == N_LTF * 2 % If sync is correct
RxDLTF = RxFrame(LTF_index +1: LTF_index + (N_CP + N_FFT) * N_LTFN, :);
RxPayload_t = RxFrame(LTF_index + (N_CP + N_FFT) * N_LTFN +1: end, :);
CSI = OFDM_ChannelEstimator(RxDLTF, Ntxs, Nrxs);
RxModData = OFDM_Demodulator(RxPayload_t, CSI);
RxPadBits = qamdemod(RxModData, Mod, 'OutputType', 'bit', 'UnitAveragePower',true);
RxBits = RxPadBits(1: end - Npad, :);
end
%% Transmission result
if LTF_index == N_LTF * 2 % If sync is correct
% BER
error_bits = xor(RxBits, TxBits);
BER = sum(sum(error_bits)) / Nbits;
clc;
disp(['*********MIMO Channel Model********']);
disp([' Path delays: [' num2str(path_delays) '] s']);
disp([' Path Gains: [' num2str(avg_gains) '] dB']);
disp([' Maximum Doppler shift: ' num2str(doppler) ' Hz']);
disp(['*********Transmission Result*********']);
disp([' Packet length: ' num2str(size(RxFrame, 1) / BW) ' us']);
if BER == 0
disp([' Frame reception successful!']);
else
disp([' Frame reception failed!']);
disp([' BER: ' num2str(BER)]);
end
disp(['*************************************']);
% Channel
figure;
for itx = 1: Ntxs
for irx = 1: Nrxs
subplot(Ntxs , Nrxs, (itx -1) * Nrxs + irx);
plot(abs(CSI(:, itx, irx)));
title(['Amp (TX: ' num2str(itx) '; RX: ' num2str(irx) ')']);
end
end
figure;
for itx = 1: Ntxs
for irx = 1: Nrxs
subplot(Ntxs , Nrxs, (itx -1) * Nrxs + irx);
plot(angle(CSI(:, irx, itx)));
title(['Phase (TX: ' num2str(itx) '; RX: ' num2str(irx) ')']);
end
end
for itx = 1: Ntxs
figure;
scatter(real(RxModData(:, itx)), imag(RxModData(:, itx)));
xlim([-2, 2]); ylim([-2, 2]);
title(['RX payload constellation (TX antenna ' num2str(itx) ')']);
end
figure;
stem(error_bits);
title('Error bits');
else
clc;
disp(['*************************************']);
disp(['Time synchronization error: Index = ' num2str(LTF_index)]);
disp(['*************************************']);
figure;
plot(abs(sum(sync_results, 2)));
title(["Time synchronization results"]);
end