-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBER_Analog.m
More file actions
245 lines (190 loc) · 6.16 KB
/
BER_Analog.m
File metadata and controls
245 lines (190 loc) · 6.16 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
% %% Add the folder containing +piradio to the MATLAB path.
addpath('../../');
close all
%% Configure the Switches
% sdr0.obsCtrl.configure(0);
% sdr1.obsCtrl.configure(0);
%% Generate the data
sdrtx = sdr0;
sdrrx = sdr1;
analog_beamforming = true; % all_channel_same_data
nFFT = 1024; % number of FFT points
txPower = 4000;
% scMin = 335;
% scMax = 350;
uncoded_bits = 228*4; % integer multiple of 4 (from the coding)
aod = 30;
aod = deg2rad(aod);
coding_rate = 7/4;
mod_rate = 1/2;
coded_bits_max = 800;
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, encoded_bits] = qpsk_modulation(input_bits);
constellation = [1+1j 1-1j -1+1j -1-1j];
txfd = zeros(nFFT, 8);
tx_antennas = [1, 2, 3, 4, 5, 6, 7, 8];
% tx_antennas = [1, 2, 3, 4];
for i = 1:length(tx_antennas)
antenna_index = tx_antennas(i);
txfd(nFFT/2 + 1 + scMin:nFFT/2 + scMax, antenna_index) = tx_symbols;
end
txfd = fftshift(txfd, 1);
txtd = ifft(txfd, [], 1);
txtdMod = zeros(nFFT, sdrrx.nch);
for txIndex=1:sdrrx.nch
txtdMod(:, txIndex) = txtd(:, txIndex) * exp(1j*txIndex*pi*sin(aod)); % Apply BF
end
txtd = sdrtx.applyCalTxArray(txtdMod);
% Normalize the energy of the tx array and scale with txPower.
txtd = txPower*txtd./max(abs(txtd));
%% Send the data to the DACs
sdrtx.send(txtd);
%% Receive data
% Configure the Switches
%sdr0.set_switches("normal");
nFFT = 1024;
nskip = 1024*3; % skip ADC data
nbatch = 100; % num of batches
for ind = 1:3
rxtd = sdrrx.recv(nFFT, nskip, nbatch);
end
rxtd = sdrrx.applyCalRxArray(rxtd);
% rxtd = rxtd(: , :, tx_antennas);
%% Select packet indices for syncronization, channel estimation and data reception
sync_idx = 1;
channel_idx = 2;
data_idxs = 3;
%% Find the syncronization instant
rxtd_sync = squeeze(rxtd(:, sync_idx, :));
rxfd_sync = fft(rxtd_sync, [], 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); % timing offset
for tx_idx = tx_antennas
corrfd = txfd(:, 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
% Remove outliers
median_loc = round(median(locs, 'all'));
locs(abs(locs-median_loc)>5) = median_loc;
loc_selected = round(mean(locs, 'all'));
%% Estimate the channel through pilots
rxtd_pilot = squeeze(rxtd(:, channel_idx, :));
% Compensate for the timing offset
rxtd_pilot = [rxtd_pilot(nFFT-loc_selected+2:nFFT, :); ...
rxtd_pilot(1:nFFT-loc_selected+1, :)];
rxfd_pilot = fftshift(fft(rxtd_pilot, [], 1));
txfd = fftshift(txfd, 1);
figure(5)
for rx_idx = 1:sdrrx.nch
subplot(4, 2, rx_idx);
plot(abs(rxfd_pilot(:, rx_idx)), 'LineWidth', 2);
title(strcat('FFT rx data ', num2str(tx_idx), '-', num2str(rx_idx)), 'FontSize', 15);
end
CFR = zeros(nFFT, sdrrx.nch, sdrtx.nch);
for rx_idx = 1:sdrrx.nch
h_rx_idx = rxfd_pilot(:, rx_idx) ./ txfd;
CFR(nFFT/2 + 1 + scMin:nFFT/2 + scMax, rx_idx, :) = ...
h_rx_idx(nFFT/2 + 1 + scMin:nFFT/2 + scMax, :);
end
figure()
tx_idx = 1;
for rx_idx = 1:sdrrx.nch
subplot(4, 2, rx_idx);
plot(mag2db(abs(CFR(:, rx_idx, tx_idx))), 'LineWidth', 2);
ylim([100 120]);
xlim([0 nFFT]);
title(strcat('CFR', num2str(tx_idx), '-', num2str(rx_idx)), 'FontSize', 20);
set(gca, 'FontSize', 20); % Change 12 to your desired font size
end
CIR = ifft(CFR, [], 1);
figure()
tx_idx = 1;
for rx_idx = 1:sdrrx.nch
subplot(4, 2, rx_idx);
stem(abs(CIR(:, rx_idx, tx_idx)), 'LineWidth', 2);
title(strcat('CIR ', num2str(tx_idx), '-', num2str(rx_idx)), 'FontSize', 20);
set(gca, 'FontSize', 20); % Change 12 to your desired font size
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_dataF = fftshift(fft(rxtd_data, [], 1));
t_hat = zeros(nFFT, size(tx_antennas, 2), sdrrx.nch);
for tx_idx = 1: sdrrx.nch
h_idx = CFR(:, :, tx_idx);
t_hat(:, tx_idx, :) = rxfd_dataF(:, :) ./ h_idx;
end
constellation = [1+1j 1-1j -1+1j -1-1j];
%% Plot
% stream_idx = 1;
% rx_idx = 7;
% figure();
% plot(real(t_hat(:, stream_idx, rx_idx)), ...
% imag(t_hat(:, stream_idx, rx_idx)), '.', 'MarkerSize', 20)
% grid on;
% ylim([-2 2]);
% xlim([-2 2]);
%% CFO compensation
%% Keeping only the data streams
t_hat_data = t_hat(:, tx_antennas, : );
%% Combining
t_hat_comb = mean(t_hat_data, 3);
t_hat_all = mean(t_hat_comb, 2);
%% SNR
figure()
rxtd_single_value = squeeze(rxtd(:, 1, 1:size(tx_antennas, 2)));
a = sum(rxtd_single_value, 2);
b = ((fftshift(abs(fft(a)))));
plot(mag2db(b), 'LineWidth', 4);
grid on; grid minor;
startIndex = nFFT/2 + 1 + scMin;
stopIndex = nFFT/2 + 1 + scMax;
totSig2 = sum(abs(b(startIndex:stopIndex)) .* abs(b(startIndex:stopIndex)));
S = totSig2 / (stopIndex-startIndex+1);
totNoiA2 = sum(abs(b(1:startIndex-1)) .* abs(b(1:startIndex-1)));
totNoiB2 = sum(abs(b(stopIndex+1:nFFT)) .* abs(b(stopIndex+1:nFFT)));
totNoise2 = totNoiA2 + totNoiB2;
N = totNoise2 / (2*startIndex - 3);
snr = 10*log10(S/N)
%% 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_all(nFFT/2 + 1 + sc))) - 1;
y = abs(imag(t_hat_all(nFFT/2 + 1 + sc))) - 1;
n = n + x^2 + y^2;
end
snr_equalized = 10*log10(sig/n)
%% BER
rx_symbols = t_hat_all(nFFT/2 + 1 + scMin:nFFT/2 + scMax, 1);
[rx_bits, received_bits] = qpsk_demodulation(rx_symbols);
errors = sum(received_bits ~= input_bits.');
BER = errors / size(input_bits,1)
figure();
plot(real(t_hat_all(:)), ...
imag(t_hat_all(:)), '.', 'MarkerSize', 20)
grid on;
ylim([-2 2]);
xlim([-2 2]);
s = sprintf('ZF Equalized Symbols. SNR = %2.2f dB', snr_equalized);
title(s, 'FontSize', 20);
set(gca, 'FontSize', 20); % Change 12 to your desired font size
%% Precoding