-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwhereami.m
198 lines (155 loc) · 5.75 KB
/
whereami.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
function total_error = whereami(row_count, column_count, max_ap_number, max_ble_number, max_tp_number )
% definition of global variables:
% row_count = 20; %matrix row count
% column_count = 20; %matrix column count
% max_ap_number = 6; %access point count
% max_tp_number = 50; %test point count
% max_ble_number = 6; %bluetooth beacon count
sigma = 5/3;
wifi_n = 3;
wifi_Pd0 = -30;
ble_n = 1.35;
ble_Pd0 = -72.3;
% definition of Matrix to store all area.
M = rand(row_count, column_count);
[r c] = size(M);
%# text location and labels
[xloc yloc] = meshgrid(1:c,1:r);
xloc = xloc(:); yloc = yloc(:);
str = strtrim(cellstr( num2str(M(:),'%.3g') ));
xticklabels = cellstr( num2str((1:c)','X%d') );
yticklabels = cellstr( num2str((1:r)','Y%d') );
% TODO: Extract a function -> Define Points
%# plot access point cells
msize = numel(M);
aps = M(randperm(msize, max_ap_number)); %choose random # access points
apmask = ismember( M, aps );
msize1 = numel(M);
bps = M(randperm(msize1, max_ble_number)); %choose random # ble beacon points
bpmask = ismember( M, bps );
msize2 = numel(M);
tps = M(randperm(msize2, max_tp_number)); %choose random # test points
tpmask2 = ismember( M, tps );
%assign 'a' or 't' or '' for each cell
%TODO: Update label for overlapping cells
T = zeros(size(M));
for ii = 1:numel(M)
if ismember(M(ii), aps)
T(ii) = 'a';
elseif ismember(M(ii), tps)
T(ii) = 't';
else
T(ii) = 32;
end
end
str = cellstr( char((T(:))) );
%{
%plot access points
h = imagesc(1:c, 1:r, ones(size(M)));
set(h, 'AlphaData', apmask)
%plot test points
hold on;
h2 = imagesc(1:c, 1:r, ones(size(M)));
set(h2, 'AlphaData', tpmask2)
%}
%{
%calculate RSS and store in results array
results = strings(length(aps)*length(tps),3);
rowNo = 1;
textY = 0;
textX = 12;
%Set headers
results(rowNo, 1) = 'Access Point';
results(rowNo, 2) = 'Test Point';
results(rowNo, 3) = 'P(d)';
%display headers
text(textX, textY, results(rowNo, 1), 'FontSize',8, 'HorizontalAlignment','center');
textX = textX+2;
text(textX, textY, results(rowNo, 2), 'FontSize',8, 'HorizontalAlignment','center');
textX = textX+2;
text(textX, textY, results(rowNo, 3), 'FontSize',8, 'HorizontalAlignment','center');
textX = textX+2;
%}
% offline db should be created for KNN algorithm.
db = offline_training_rss_values(M, aps, bps, row_count, column_count, wifi_n, ble_n, wifi_Pd0, ble_Pd0);
% displaying offline database.
% TODO: Commented out or use a compiler switch for all trace lines.
%%disp("--Offline Database--")
%%disp(db)
% saving offline db in csv format.
csvwrite('offline_db', db);
% each row of FingerprintMatrix will be a fingerprint value of related test point.
FingerprintMatrix = zeros(max_tp_number, max_ap_number + max_ble_number);
FingerprintMatrix_wnoise = zeros(max_tp_number, max_ap_number + max_ble_number);
% total prediction error
total_error = 0;
for i=1:max_tp_number
randomTPValue = tps(i);
[TProw, TPcolumn]=find(M == randomTPValue);
[fp, fp_wn] = calculate_fingerprint(M, aps, bps, TProw, TPcolumn, wifi_n, ble_n, wifi_Pd0, ble_Pd0, sigma);
FingerprintMatrix(i,:) = fp;
FingerprintMatrix_wnoise(i,:) = fp_wn;
% location prediction algorithm should be run in here.
% calculate and sum error. (predX, predY; TProw, TPcolumn)
closest_location = predict_location(db, fp_wn);
%aaa disp([num2str(TProw) ,', ' , num2str(TPcolumn) , ' position predicts :: ' , num2str(closest_location(1)) , ', ' , num2str(closest_location(2))]);
total_error = total_error + get_error_margin([TProw, TPcolumn], closest_location);
end
total_error = total_error / max_tp_number;
disp(num2str(row_count));
disp(['Total error ', num2str(total_error)]);
%disp(FingerprintMatrix)
%disp("Calculated FingerPrinting Matrix With Noise: ")
%disp(FingerprintMatrix_wnoise)
%{
for i=1:length(aps)
randomAPValue=aps(i);
[AProw, APcolumn]=find(M == randomAPValue);
for j=1:length(tps)
rowNo = rowNo + 1;
textY = textY+0.3;
textX = 12;
randomTPValue=tps(j);
[TProw, TPcolumn]=find(M == randomTPValue);
%find euclidean distance
X = [AProw, APcolumn; TProw,TPcolumn];
d = pdist(X,'euclidean');
n = 3;
Pd0 = -30;
sigma = 5;
Pd = Pd0 - (10.*n.*log10(d)) + sigma; %%calculate RSS Value for wifi
results(rowNo, 1) = strcat('AP [X', num2str(APcolumn), ',Y',num2str(AProw),']');
results(rowNo, 2) = strcat('TP [X',num2str(TPcolumn), ',Y',num2str(TProw),']');
results(rowNo, 3) = num2str(Pd);
%display the access point, test point and P(d)
text(textX, textY, results(rowNo, 1), 'FontSize',8, 'HorizontalAlignment','center');
textX = textX+2;
text(textX, textY, results(rowNo, 2), 'FontSize',8, 'HorizontalAlignment','center');
textX = textX+2;
text(textX, textY, results(rowNo, 3), 'FontSize',8, 'HorizontalAlignment','center');
textX = textX+2;
end
end
%T = array2table(results);
%hold on;
%f = figure;
%uit = uitable(f);
%uit.Data = T;
%uit.Position = [20 20 258 78];
colormap(summer) %# colormap([0 1 0])
set(gca, 'Box','on', 'XAxisLocation','top', 'YDir','reverse', ...
'XLim',[0 c]+0.5, 'YLim',[0 r]+0.5, 'TickLength',[0 0], ...
'XTick',1:c, 'YTick',1:r, ...
'XTickLabel',xticklabels, 'YTickLabel',yticklabels, ...
'LineWidth',2, 'Color','none', ...
'FontWeight','bold', 'FontSize',8, 'DataAspectRatio',[1 1 1]);
%# plot grid
xv1 = repmat((2:c)-0.5, [2 1]); xv1(end+1,:) = NaN;
xv2 = repmat([0.5;c+0.5;NaN], [1 r-1]);
yv1 = repmat([0.5;r+0.5;NaN], [1 c-1]);
yv2 = repmat((2:r)-0.5, [2 1]); yv2(end+1,:) = NaN;
line([xv1(:);xv2(:)], [yv1(:);yv2(:)], 'Color','k', 'HandleVisibility','off')
%# plot text
text(xloc, yloc, str, 'FontSize',8, 'HorizontalAlignment','center');
%}
end