-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTest_8192_v15.m
More file actions
477 lines (351 loc) · 11.9 KB
/
Copy pathTest_8192_v15.m
File metadata and controls
477 lines (351 loc) · 11.9 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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
% Specify the main folder
mainFolder = '/home/mentis/Github/MU-MIMO-Pi-Radio/new_radio';
% Generate the path string for the main folder and all its subfolders
allSubfolders = genpath(mainFolder);
% Add the paths to the MATLAB path
addpath(allSubfolders);
% Save the changes to the MATLAB path (optional)
savepath;
close all;
%% Configure the Switches
% sdr0.obsCtrl.configure(0);
% sdr1.obsCtrl.configure(0);
%% Clear all workspace variables except for sdr objects
% List of variables to keep
varsToKeep = {'sdr0', 'sdr1'};
% Get the list of all variables in the workspace
allVars = whos;
% Construct the command to clear all variables except those in varsToKeep
clearCommand = 'clear ';
for k = 1:length(allVars)
if ~ismember(allVars(k).name, varsToKeep)
clearCommand = [clearCommand allVars(k).name ' '];
end
end
% Execute the clear commandtxfd_ant
eval(clearCommand);
clear allVars clearCommand k
%% Generate the data
sdrtx = sdr0;
sdrrx = sdr1;
nSymbols = 3;
range = 1:4;
nFFT = 512; % number of FFT points
txPower = 8000;
% scMin = 335;
% scMax = 350;
uncoded_bits = 112*4; % integer multiple of 4 (from the coding)
aod = 0;
aod = deg2rad(aod);
coding_rate = 7/4;
mod_rate = 1/2;
coded_bits_max = 400;
uncoded_bits_max = floor(coded_bits_max/mod_rate/coding_rate);
if uncoded_bits > uncoded_bits_max
disp('ERROR, too many bits to transmit')
end
coded_bits = ceil(uncoded_bits*mod_rate*coding_rate);
scMin = -coded_bits/2;
scMax = coded_bits/2;
input_bits = randi([0, 1], 1, uncoded_bits)';
[tx_symbols_1, encoded_bits_1] = qpsk_modulation((randi([0, 1], 1, uncoded_bits)'));
[tx_symbols_2, encoded_bits_2] = qpsk_modulation((randi([0, 1], 1, uncoded_bits)'));
[tx_symbols_3, encoded_bits_3] = qpsk_modulation(input_bits);
constellation = [1+1j 1-1j -1+1j -1-1j];
txfd_1 = zeros(nFFT, 8);
txfd_2 = zeros(nFFT, 8);
txfd_3 = zeros(nFFT, 8);
tx_antennas = [1, 2, 3, 4, 5, 6, 7, 8];
for i = 1:length(tx_antennas)
antenna_index = tx_antennas(i);
txfd_1(nFFT/2 + 1 + scMin:nFFT/2 + scMax, antenna_index) = tx_symbols_1;
end
for i = 1:length(tx_antennas)
antenna_index = tx_antennas(i);
txfd_2(nFFT/2 + 1 + scMin:nFFT/2 + scMax, antenna_index) = tx_symbols_2;
end
for i = 1:length(tx_antennas)
antenna_index = tx_antennas(i);
txfd_3(nFFT/2 + 1 + scMin:nFFT/2 + scMax, antenna_index) = tx_symbols_3;
end
txfd = zeros(nFFT, nSymbols, 8);
txfd(:, 1, :) = txfd_1;
txfd(:, 2, :) = txfd_2;
txfd(:, 3, :) = txfd_3;
txfd = fftshift(txfd, 1);
txtd = ifft(txfd, [], 1);
txtd = reshape(txtd, nFFT * nSymbols, 8);
txtd = sdrtx.applyCalTxArray(txtd);
% Normalize the energy of the tx array and scale with txPower.
txtd = txPower * txtd ./ max(abs(txtd));
%% Send the data to the DACs for the first stage
sdrtx.send(txtd);
%% Receive data
% Configure the Switches
%sdr0.set_switches("normal");
nskip = nFFT * nSymbols * 3; % Skip ADC data
nbatch = 100; % Number of batches
for ind = 1:3
rxtd = sdrrx.recv(nFFT * nSymbols, nskip, nbatch);
end
%
rxtd = sdrrx.applyCalRxArray(rxtd);
rxtd = rxtd(:, 1, :);
rxtd = reshape(rxtd, nFFT, nSymbols, 8);
%% Select packet indices for Synchronization and Channel Estimation for First Stage
sync_idx = 1;
channel_idx = 2;
data_idxs = 3;
%% Find the synchronization instant
rxtd_sync = squeeze(rxtd(:, sync_idx, :));
rxfd_sync = fftshift(fft(rxtd_sync, [], 1));
txfd_sync = fftshift(squeeze(txfd(:, sync_idx, :)), 1);
% Find the starting of the symbol
% it should be the same across antennas
% correlate each rx symbol (sum of all tx symbols) with each tx symbol
locs = zeros(size(tx_antennas, 2), sdrrx.nch);
for tx_idx = tx_antennas
corrfd = txfd_sync(:, tx_idx) .* conj(rxfd_sync);
corrtd = ifft(corrfd, [], 1);
[val, loc] = max(abs(corrtd), [], 1);
loc = loc + 0; % add a timing offset to test
locs(tx_idx, :) = loc;
end
loc_selected = round(mean(locs, 'all'));
%% Estimate the channel through pilots
rxtd_pilot = squeeze(rxtd(:, channel_idx, :));
rxtd_pilot = [rxtd_pilot(nFFT - loc_selected + 2:nFFT, :); ...
rxtd_pilot(1:nFFT - loc_selected + 1, :)];
rxfd_pilot = fftshift(fft(rxtd_pilot, [], 1));
txfd_pilot = fftshift(squeeze(txfd(:, channel_idx, :)), 1);
CFR = zeros(nFFT, sdrrx.nch, sdrtx.nch);
for rx_idx = 1:sdrrx.nch
h_tmp = rxfd_pilot(:, rx_idx) ./ txfd_pilot;
CFR(nFFT/2 + 1 + scMin:nFFT/2 + scMax, rx_idx, :) = ...
h_tmp(nFFT/2 + 1 + scMin:nFFT/2 + scMax, :);
end
%% Equalize data through channel estimates
rxtd_data = squeeze(rxtd(:, data_idxs, :));
rxtd_data = [rxtd_data(nFFT-loc_selected+2:nFFT, :); ...
rxtd_data(1:nFFT-loc_selected+1, :)];
rxfd_data = fftshift(fft(rxtd_data, [], 1));
t_hat = zeros(nFFT, size(tx_antennas, 2), sdrrx.nch);
for tx_idx = 1:size(tx_antennas, 2)
h_idx = CFR(:, :, tx_idx);
t_hat(:, tx_idx, :) = rxfd_data(:, :) ./ h_idx;
end
%% Plot
stream_idx = 1;
rx_idx = 8;
figure();
plot(real(t_hat(:, stream_idx, rx_idx)), ...
imag(t_hat(:, stream_idx, rx_idx)), '.', 'MarkerSize', 20)
ylim([-2 2]);
xlim([-2 2]);
grid on;
%% BER
t_hat_no_bfm = mean(t_hat, 3);
t_hat_no_bfm = t_hat_no_bfm(:, range);
t_hat_a = mean(t_hat_no_bfm, 2);
rx_symbols = t_hat_a(nFFT/2 + 1 + scMin:nFFT/2 + scMax, 1);
[rx_bits, received_bits] = qpsk_demodulation(rx_symbols);
errors = sum(received_bits ~= input_bits.');
BER_Initial = errors / size(input_bits ,1)
%% Equalized SNR
n = 0;
sig = 0;
for sc = scMin:scMax-1
if sc == 0
continue;
end
% power domain calculations
sig = sig + 2; % since it is QPSK
x = abs(real(t_hat_a(nFFT/2 + 1 + sc))) - 1;
y = abs(imag(t_hat_a(nFFT/2 + 1 + sc))) - 1;
n = n + x^2 + y^2;
end
snr_equalized_no_Beamform = 10*log10(sig/n)
%% Precoding
% Multiplication for each of the subcarrier
CFR_permute = permute(CFR, [2, 3, 1]); % Nr-by-Nsts-by-Nst
[~,~,V] = pagesvd(CFR_permute, 'econ');
steeringMatrix = permute(V,[3 1 2]); % Permute to Nst-by-Ntx-by-Nsts
% Nst=K, Ntx=M, Nsts=Nss
% Zero-forcing precoding solution
for i = 1:nFFT
% Channel inversion precoding
h = squeeze(steeringMatrix(i,:,:));
steeringMatrix(i,:,:) = (((h)*h')) / (h'*h);
end
% steeringMatrix should be K x M x Nss (Nss=M)
input_bits = randi([0, 1], 1, uncoded_bits)';
[tx_symbols_1, encoded_bits_1] = qpsk_modulation((randi([0, 1], 1, uncoded_bits)'));
[tx_symbols_2, encoded_bits_2] = qpsk_modulation((randi([0, 1], 1, uncoded_bits)'));
[tx_symbols_3, encoded_bits_3] = qpsk_modulation(input_bits);
constellation = [1+1j 1-1j -1+1j -1-1j];
txfd_1 = zeros(nFFT, 8);
txfd_2 = zeros(nFFT, 8);
txfd_3 = zeros(nFFT, 8);
tx_antennas = [1, 2, 3, 4, 5, 6, 7, 8];
for i = 1:length(tx_antennas)
antenna_index = tx_antennas(i);
txfd_1(nFFT/2 + 1 + scMin:nFFT/2 + scMax, antenna_index) = tx_symbols_1;
end
for i = 1:length(tx_antennas)
antenna_index = tx_antennas(i);
txfd_2(nFFT/2 + 1 + scMin:nFFT/2 + scMax, antenna_index) = tx_symbols_2;
end
for i = 1:length(tx_antennas)
antenna_index = tx_antennas(i);
txfd_3(nFFT/2 + 1 + scMin:nFFT/2 + scMax, antenna_index) = tx_symbols_3;
end
txfd(:, 1, :) = txfd_1;
txfd(:, 2, :) = txfd_2;
txfd(:, 3, :) = txfd_3;
%% Apply precoding
txfd_precoded = zeros(nFFT, nSymbols, 8);
for symidx = 1:nSymbols
txfd_i = squeeze(txfd(:, symidx, :));
% for subcarrier = 1:nFFT
for subcarrier = (nFFT/2 + 1 + scMin):(nFFT/2 + scMax)
txfd_precoded(subcarrier, symidx, :) = squeeze(steeringMatrix(subcarrier, :, :)) * squeeze(txfd_i(subcarrier, :, :)).';
end
end
txfd = fftshift(txfd_precoded, 1);
% txfd = fftshift(txfd, 1);
txtd = ifft(txfd, [], 1);
txtd = reshape(txtd, nFFT * nSymbols, 8);
txtd = sdrtx.applyCalTxArray(txtd);
% Normalize the energy of the tx array and scale with txPower.
txtd = txPower * txtd ./ max(abs(txtd));
%% Send the data to the DACs for the first stage
sdrtx.send(txtd);
%% Receive data
% Configure the Switches
%sdr0.set_switches("normal");
nskip = nFFT * nSymbols * 3; % Skip ADC data
nbatch = 100; % Number of batches
for ind = 1:3
rxtd = sdrrx.recv(nFFT * nSymbols, nskip, nbatch);
end
%
rxtd = sdrrx.applyCalRxArray(rxtd);
rxtd = rxtd(:, 1, :);
rxtd = reshape(rxtd, nFFT, nSymbols, 8);
%% Select packet indices for Synchronization and Channel Estimation for First Stage
sync_idx = 1;
channel_idx = 2;
data_idxs = 3;
%% Find the synchronization instant
rxtd_sync = squeeze(rxtd(:, sync_idx, :));
rxfd_sync = fftshift(fft(rxtd_sync, [], 1));
txfd_sync = fftshift(squeeze(txfd(:, sync_idx, :)), 1);
% Find the starting of the symbol
% it should be the same across antennas
% correlate each rx symbol (sum of all tx symbols) with each tx symbol
locs = zeros(size(tx_antennas, 2), sdrrx.nch);
for tx_idx = tx_antennas
corrfd = txfd_sync(:, tx_idx) .* conj(rxfd_sync);
corrtd = ifft(corrfd, [], 1);
[val, loc] = max(abs(corrtd), [], 1);
loc = loc + 0; % add a timing offset to test
locs(tx_idx, :) = loc;
end
loc_selected = round(mean(locs, 'all'));
%% Estimate the channel through pilots
rxtd_pilot = squeeze(rxtd(:, channel_idx, :));
rxtd_pilot = [rxtd_pilot(nFFT - loc_selected + 2:nFFT, :); ...
rxtd_pilot(1:nFFT - loc_selected + 1, :)];
rxfd_pilot = fftshift(fft(rxtd_pilot, [], 1));
txfd_pilot = fftshift(squeeze(txfd(:, channel_idx, :)), 1);
CFR = zeros(nFFT, sdrrx.nch, sdrtx.nch);
for rx_idx = 1:sdrrx.nch
h_tmp = rxfd_pilot(:, rx_idx) ./ txfd_pilot;
CFR(nFFT/2 + 1 + scMin:nFFT/2 + scMax, rx_idx, :) = ...
h_tmp(nFFT/2 + 1 + scMin:nFFT/2 + scMax, :);
end
%% Equalize data through channel estimates
rxtd_data = squeeze(rxtd(:, data_idxs, :));
rxtd_data = [rxtd_data(nFFT-loc_selected+2:nFFT, :); ...
rxtd_data(1:nFFT-loc_selected+1, :)];
rxfd_data = fftshift(fft(rxtd_data, [], 1));
t_hat = zeros(nFFT, size(tx_antennas, 2), sdrrx.nch);
for tx_idx = 1:size(tx_antennas, 2)
h_idx = CFR(:, :, tx_idx);
t_hat(:, tx_idx, :) = rxfd_data(:, :) ./ h_idx;
end
%% Plot
stream_idx = 1;
rx_idx = 8;
figure();
plot(real(t_hat(:, stream_idx, rx_idx)), ...
imag(t_hat(:, stream_idx, rx_idx)), '.', 'MarkerSize', 20)
ylim([-2 2]);
xlim([-2 2]);
grid on;
%% BER
t_hat_no_bfm = mean(t_hat, 3);
t_hat_no_bfm = t_hat_no_bfm(:, range);
t_hat_a = mean(t_hat_no_bfm, 2);
rx_symbols = t_hat_a(nFFT/2 + 1 + scMin:nFFT/2 + scMax, 1);
[rx_bits, received_bits] = qpsk_demodulation(rx_symbols);
errors = sum(received_bits ~= input_bits.');
BER_Precoding = errors / size(input_bits ,1)
%% Equalize data through channel estimates with G
Nss_i = 8;
Nss = Nss_i;
I_Nss = eye(Nss_i, Nss);
% G matrix
G_k = zeros(nFFT, sdrtx.nch, sdrrx.nch);
% Final received symbol matrix
X_k = zeros(nFFT, sdrtx.nch);
% G_k for each subcarrier
for subcarrier = (nFFT/2 + 1 + scMin):(nFFT/2 + scMax)
HW = squeeze(CFR(subcarrier, :, :)); % Combined H*W matrix for subcarrier
Hw_C = HW';
G = I_Nss * Hw_C / (HW * Hw_C + I_Nss);
G_k(subcarrier,:,:) = G;
Y = rxfd_data(subcarrier, :).';
X_k(subcarrier, :) = Nss * G * Y;
end
%% Plot
stream_idx = 3;
figure();
plot(real(X_k(:, stream_idx)), ...
imag(X_k(:, stream_idx)), '.', 'MarkerSize', 20);
grid on;
ylim([-2 2]);
xlim([-2 2]);
%% BER
X_k = X_k(:, range);
X_k_mean = mean(X_k, 2);
rx_symbols = X_k_mean(nFFT/2 + 1 + scMin:nFFT/2 + scMax, 1);
[rx_bits, received_bits] = qpsk_demodulation(rx_symbols);
errors = sum(received_bits ~= input_bits.');
BER_Final = errors / size(input_bits,1)
%% Equalized SNR
n = 0;
sig = 0;
for sc = scMin:scMax-1
if sc == 0
continue;
end
% power domain calculations
sig = sig + 2; % since it is QPSK
x = abs(real(X_k_mean(nFFT/2 + 1 + sc))) - 1;
y = abs(imag(X_k_mean(nFFT/2 + 1 + sc))) - 1;
n = n + x^2 + y^2;
end
snr_equalized_Beamformed = 10*log10(sig/n)
%% Transmit processed data
sdrtx.send(zeros(nFFT, 8));
%% Receive data
sdrrx.recv(nFFT, nFFT * 3, 100);
%% Specify the main folder
mainFolder = '/home/mentis/Github/MU-MIMO-Pi-Radio/new_radio';
% Generate the path string for the main folder and all its subfolders
allSubfolders = genpath(mainFolder);
% Add the paths to the MATLAB path
rmpath(allSubfolders);
% Save the changes to the MATLAB path (optional)
savepath;