-
Notifications
You must be signed in to change notification settings - Fork 0
/
testWACOWC.m
392 lines (283 loc) · 11.1 KB
/
testWACOWC.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
function testWACOWC(experiment_name)
% TESTWACOWC Run simulation with a single emitter to get reading accuracy data
%% Prepare the workspace
clearvars -except experiment_name;
close all;
paramsfile = [experiment_name '.m'];
run(paramsfile);
% add the path to the projective geometry functions
addpath('../../ProjGeom');
addpath('../');
%% Files and folders
% Folter for storing results
resultsdir = 'results/';
% Base filename
resultsBaseFn = experiment_name;
% Experimente results filename
expFilename = [ resultsdir resultsBaseFn '_data' ];
%% Should not change below this line
% Position in x and y
xloc = 0:Wstep:W;
yloc = 0:Lstep:L;
% Save experiment parameters
params.W = W;
params.L = L;
params.H = H;
params.Wstep= Wstep;
params.Lstep = Lstep;
params.Nrep = Nrep;
params.HPA_v = HPA_v;
params.m_v = m_v;
params.NmNp = NmNp;
params.Psi_v = Psi_v;
params.rectifyIndication = rectifyIndication;
params.validReadingThreshold = validReadingThreshold;
params.Psi_mode = Psi_mode;
params.resultsBaseFn = resultsBaseFn;
% Create the results dir if necessary
if exist(resultsdir) ~= 7
% Results dir does not exist...
mkdir(resultsdir);
end
%% Iterate over experiment conditions
leaving = 0;
%Initialize array to store full experiment results
testresults = [];
% Save the experiment results
save(expFilename,'testresults','params');
if( (usegraphics == 0) )
barra = waitbar(0,'Progress...');
end
for m = m_v
for iConfig = 1:size(NmNp,1)
Nm = NmNp(iConfig,1);
Np = NmNp(iConfig,2);
Psi_min = 0.5*acos(cos(pi/(2*Np))^2);
Psi_max = pi/(2*Np);
switch Psi_mode
case 1
Psi_v = [ Psi_min (Psi_min + Psi_max)/2 Psi_max ];
case 2
Psi_v =Psi_min *params.Psi_v;
otherwise
error('Invalid value or undefined Psi_mode!');
end
params.Psi_v = Psi_v;
for Psi=Psi_v
if(leaving==1)
break;
end
disp([ '{m, N_p, N_m, Psi }={' num2str(m) ',' ...
num2str(Np) ',' num2str(Nm) ',' ...
num2str(round(180/pi*Psi)) '}']);
%% Create the light emitters
Emitters = newEmitters(n_Emitters,Pb,Ps,m);
% Emitter location in xy plane
locEm = [ W/2 L/2 H ];
Em_Base_HTM = Trans3( locEm' )*RotX3(pi); % Base HTM at the center of the ceiling.
% Light emitters placed at ceiling, in a circle of radius R
Rs = 0;
for i=1:n_Emitters
Emitters(i).HTM = Em_Base_HTM*RotZ3(i*(2*pi)/n_Emitters)*...
Trans3(Rs,0,0)*RotY3(0*pi/8);
end
if usegraphics
% Plot the emitters position
if(isgraphics(1))
clf(1)
else
figure(1)
end
PlotHTMArray(Emitters);
axis([-0.5 W+0.5 -0.5 L+0.5 0 H+0.5]);
view(3);
grid on
end
%% Create the receivers
n_Receivers = Np*Nm; % Number of receivers
% Create the receiver structure:
Receivers = newReceivers(n_Receivers,Ar, Ts, n, Psi, R);
% Receivers are organized in Parallel and Meridians arragement of photo
% detectors, with Nm Meridians and 3 Parallels, in a sphere with
% radius SR
SR = 0.05;
PDSensor = vlpCreateSensorParMer(Receivers, Np, Nm, SR, pi/8);
%% setup the values for computing received indication
% Quantities required for computation
% Bw - Bandwidth of receiver circuit
% Z - Vector with the transimpedance feedback resistors
% s_i - Vector with the operational amplifiers current PSD
% s_v - Vector with the operational amplifiers voltage PSD
% Z_p - Vector with the photo-diode equivalent impedances
% Theta - Thermodynamic temperature of feedback resistor, in Kelvin
Bw = 10e4; % Bandwidth= 10kHz
Theta = 273+30; % Feedback resistor at 30 degrees C
% Vector of ones for the receivers
nRec_v = ones(n_Receivers,1);
s_i = 1.3e-15*nRec_v; % Current noise "plateau" at 1.3 fA/sqrt(Hz)
s_v = 4.8e-9*nRec_v; % Voltage noise "plateau" at 4.8 nV/sqrt(Hz)
Z = 1e6*nRec_v; % Feedback resistors = 1M
Z_p = 100e6*nRec_v; % PD equivalent impedace = 100 MOhm
%% Experiment cycle for a set of parameters
% tempSensor will be modified when travelling the room floor
tempSensor = PDSensor;
% Save experiment parameters
% Emitter data
params.m = m;
params.Pb = Pb;
params.Ps = Ps;
% Receiver data
params.Np = Np;
params.Nm = Nm;
params.Psi = Psi;
for ix = 1:numel(xloc)
for iy = 1:numel(yloc)
if(leaving==1)
break;
end
f = ((ix-1)*numel(yloc) + iy)/(numel(xloc)*numel(yloc));
if( (usegraphics == 0) )
waitbar(f,barra);
end
% Move the sensor
% Apply the transformation to every HTM in the sensors
for i = 1:numel(tempSensor)
tempSensor(i).HTM = Trans3(xloc(ix),yloc(iy),0)*PDSensor(i).HTM;
end
% Compute received indication (mean and noise / variance)
[ Y, nu ] = vlpRecIndication( Emitters, tempSensor, Bw, Z, s_i, s_v, Z_p, Theta );
Nu = repmat(nu,1,n_Emitters);
% Initialize arrays for storing the experiment error values
locerrorv = [];
raderrorv = [];
% Iterate
for counter = 1:Nrep
s = sqrt(Nu).*randn(size(Y));
Ynoise = Y + s;
% If variable nonoise exists and if it is set, shut down noise
if exist('nonoise')
if nonoise
Ynoise = Y;
end
end
% If rectifyIndication is active, negative values are clipped
% at zero:
if rectifyIndication
Ynoise = max(Y+s,zeros(size(Y)));
end
% Check for valid reading
validReading = (max(Ynoise)/std(Ynoise) > validReadingThreshold);
% Get a matrix with all HTMs, side-by-side
x = [tempSensor.HTM];
E = x(1:3,3:4:end);
% Mvec is a matrix with the vectors pointing to the light sources
Mvec = E*Ynoise;
% Normalize Mvec
Mvec = Mvec./repmat(sqrt(sum(Mvec.^2)),3,1);
% The angle with the vertical is given by acos(kz*Mvec),
% where kz = [0 0 1] (a vector pointing up). The internal
% product is simply the third line of Mvec, the norm of both
% vectors being 1 (Mvec has been normalized), so the
% expression can be simplified.
vangles = acos(Mvec(3,:));
% Compute the distances to light sources in the xy plane
if validReading
radii = H*tan(vangles);
else
radii = NaN;
end
% Get the emitters position
temp = [Emitters.HTM];
posEm = temp(1:2,4:4:end);
% Compute the true value for radii
delta = posEm - repmat([xloc(ix);yloc(iy)],1,n_Emitters);
trueradii = sqrt(sum(delta.^2));
% PLOT SECTION
% Sensor coordinates:
Sx = xloc(ix);
Sy = yloc(iy);
Sz = 0;
% Pointing vector
Px = Mvec(1,1);
Py = Mvec(2,1);
Pz = Mvec(3,1);
% Compute estimated location of emitter
if validReading
locEmEstim = [ Sx Sy Sz] + [ Px Py Pz]*H/Pz;
else
locEmEstim = [NaN NaN NaN];
end
if usegraphics
% Plot the estimated point vector
hpv = quiver3(Sx,Sy,Sz,2*Px,2*Py,2*Pz,'m','LineWidth',2);
% Plot the circle
hcirc = drawCircleAtH(Sx,Sy,H,radii(1));
% Plot the estimated location of emitter in the ceiling
hlem = plot3(locEmEstim(1),locEmEstim(2),locEmEstim(3),'om');
if interactive == 1
resp = input('[ENTER] to continue, any value to stop...','s');
if numel(resp) ~= 0
display('Leaving...');
leaving = 1;
break;
end
else
pause(.1);
end
delete(hpv);
delete(hcirc);
delete(hlem);
end
% Compute errors
% Error on estimation of emitter localization in xy plane
locerror = norm( locEm(1:2) - locEmEstim(1:2) );
locerrorv = [ locerrorv locerror ];
% Error in radius
actualRad = norm( [Sx Sy] - locEm(1:2) );
raderror = abs(actualRad - radii(1));
raderrorv = [ raderrorv raderror ];
end
% Compute and save experiment data
%results(ix,iy).validReading =
results.locerroravg(ix,iy) = mean(locerrorv);
results.locerrorstd(ix,iy) = std(locerrorv);
results.locerrormax(ix,iy) = max(locerrorv);
results.locerrorrms(ix,iy) = rms(locerrorv);
results.raderroravg(ix,iy) = mean(raderrorv);
results.raderrorstd(ix,iy) = std(raderrorv);
results.raderrormax(ix,iy) = max(raderrorv);
results.raderrorrms(ix,iy) = rms(raderrorv);
end
end % end of room traveling
resultsfilename = createResultFilename( resultsdir, ...
resultsBaseFn, m, Np, Nm, Psi, Nrep);
save(resultsfilename,'params','results');
%% Compute and save full area aggregate results
%
roomstats.Nrep = Nrep;
roomstats.m = m;
roomstats.Psi = Psi;
roomstats.Np = Np;
roomstats.Nm = Nm;
roomstats.locerrmax = max([results.locerrormax(:)]);
roomstats.locerravg = mean([results.locerroravg(:)]);
roomstats.locerrstd = std([results.locerrorstd(:)]);
roomstats.locerrrms = rms([results.locerrorstd(:)]);
roomstats.raderrmax = max([results.raderrormax(:)]);
roomstats.raderravg = mean([results.raderroravg(:)]);
roomstats.raderrstd = std([results.raderrorstd(:)]);
roomstats.raderrrms = rms([results.raderrorstd(:)]);
load(expFilename);
% Add to array with all test results
testresults = [ testresults; roomstats ];
testresultstable = struct2table(testresults);
% Save the experiment results
save(expFilename,'testresults','testresultstable','params');
%% Close the iteration over the experiment conditions
end
end
end
if( (usegraphics == 0) )
delete(barra);
end
end