-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulationlsci.asv
301 lines (242 loc) · 11.3 KB
/
simulationlsci.asv
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
clear all, clc, close all,
% Modelling of particles in a volume
%% setup parameters:
% generating randomly distributed particles in a volume
nParticles = single(100);
particlesVolumeSizeXyz = single([10; 10; 0.01]);
% defining sensor
sensorDistanceToZ = single(1000);
sensorSizeXy = single([0.5; 0.5]);
sensorResolutionXy = single([100; 100]);
% particle moving
nMoves = single(1000);
maxSpeedParticle = single(0.001);
directionParticleDegreesConstant = single(0);
isOrderedMotion = uint8(1);
% %%%%%%%% PLOTS %%%%%%%%%%
% plot the figures
isPlot = uint8(0);
isSavePlot = uint8(0);
% create gif
isGif = uint8(0);
isSaveGif = uint8(0);
% Calculating Time Intensity Autocorrelation Function (tiaf)
isCalculateTiaf = uint8(1);
isSaveCalculatedTiaf = uint8(0);
maxTau = single(50);
% To calibrate the camera position and or particles volume, etc use the
% following visualizer.
% visualizing particles with respect to sensors:
isVisualizeParticlesSensors = uint8(0);
%% generating randomly distributed particles in a volume
% nParticles = 100;
% particlesVolumeSizeXyz = [0.5; 0.5; 0.1];
particlesPositionXyz = [rand(nParticles, 1)*particlesVolumeSizeXyz(1),...
rand(nParticles, 1)*particlesVolumeSizeXyz(2),...
-rand(nParticles, 1)*particlesVolumeSizeXyz(3)];
%% defining sensor
% sensorDistanceToZ = 10;
% sensorSizeXy = [10;10];
% sensorResolutionXy = [20;20];
sensorCentreXyz = [particlesVolumeSizeXyz(1)/2;...
particlesVolumeSizeXyz(2)/2;...
sensorDistanceToZ];
sensorPixelsCoordinatesX = linspace(sensorCentreXyz(1)-sensorSizeXy(1),sensorCentreXyz(1)+sensorSizeXy(1),sensorResolutionXy(1));
sensorPixelsCoordinatesY = linspace(sensorCentreXyz(2)-sensorSizeXy(2),sensorCentreXyz(2)+sensorSizeXy(2),sensorResolutionXy(2));
sensorPixelsCoordinates_X_RepeatedYTimes = repmat(sensorPixelsCoordinatesX,[sensorResolutionXy(2) 1]);
sensorPixelsCoordinates_X_RepeatedYTimesReshaped = reshape(sensorPixelsCoordinates_X_RepeatedYTimes,[1 sensorResolutionXy(1)*sensorResolutionXy(2)]);
clear sensorPixelsCoordinates_X_RepeatedYTimes
sensorPixelsCoordinates_Y_RepeatedXTimes = repmat(sensorPixelsCoordinatesY,[1 sensorResolutionXy(1)]);
clear sensorPixelsCoordinatesX sensorPixelsCoordinatesY
sensorPixelsCoordinatesXyz = [sensorPixelsCoordinates_X_RepeatedYTimesReshaped;...
sensorPixelsCoordinates_Y_RepeatedXTimes;...
repmat(sensorCentreXyz(3),[1,sensorResolutionXy(1)*sensorResolutionXy(2)])]';
clear sensorPixelsCoordinates_X_RepeatedYTimesReshaped sensorPixelsCoordinates_Y_RepeatedXTimes
%% visualizing particles with respect to sensors
if isVisualizeParticlesSensors
scatter3(sensorPixelsCoordinatesXyz(:,1),...
sensorPixelsCoordinatesXyz(:,2),...
sensorPixelsCoordinatesXyz(:,3));
hold on;
scatter3(particlesPositionXyz(:,1),...
particlesPositionXyz(:,2),...
particlesPositionXyz(:,3),'r');
hold off;
pause;
end
%% particle moving
% nMoves = 10000;
%
% maxSpeedParticle = 0.01;
% directionParticleDegreesConstant = 0;
% isOrderedMotion = 1;
% the corners are (X,Y):
% bottom left = 0, 0
% bottom right = particlesVolumeSizeXyz(1), 0
% upper left = 0, particlesVolumeSizeXyz(2)
% upper right = particlesVolumeSizeXyz(1), particlesVolumeSizeXyz(2)
particlesFrameCorners = [0,0;...
particlesVolumeSizeXyz(1), 0;...
0, particlesVolumeSizeXyz(2);...
particlesVolumeSizeXyz(1), particlesVolumeSizeXyz(2)];
timeIntensityAutocorrelationFunction = nan(sensorResolutionXy(1),sensorResolutionXy(2),nMoves);
for iter = 1:nMoves
if isOrderedMotion
speedParticle = maxSpeedParticle;
directionParticleDegrees = directionParticleDegreesConstant;
else
speedParticle = rand(1,nParticles)*maxSpeedParticle;
directionParticleDegrees = randi([0 360],1,nParticles);
end
particlesPositionXyz(:,1) = particlesPositionXyz(:,1)' + ...
speedParticle .* cosd(directionParticleDegrees);
particlesPositionXyz(:,2) = particlesPositionXyz(:,2)' + ...
speedParticle .* sind(directionParticleDegrees);
%% if particle is out of scope bring it to the begining of the frame
% in the same trajectory and direction
% when particles are out of the X axis:
% - from the side of the max volume side in the X axis
if sum(particlesPositionXyz(:,1) > particlesVolumeSizeXyz(1)) > 0
particlesPositionXyz(particlesPositionXyz(:,1) > particlesVolumeSizeXyz(1), 2) = ...
rand(1, sum(particlesPositionXyz(:,1) > particlesVolumeSizeXyz(1))) * particlesVolumeSizeXyz(1);
particlesPositionXyz(particlesPositionXyz(:,1) > particlesVolumeSizeXyz(1), 1) = 0;
% particlesPositionXyz(particlesPositionXyz(:,1) > particlesVolumeSizeXyz(1), 1) - ...
% cosd(directionParticleDegrees) * particlesVolumeSizeXyz(1);
end
% - below 0
if sum(particlesPositionXyz(:,1) < 0) > 0
particlesPositionXyz(particlesPositionXyz(:,1) < 0, 2) = ...
rand(1, sum(particlesPositionXyz(:,1) < 0)) * particlesVolumeSizeXyz(1);
particlesPositionXyz(particlesPositionXyz(:,1) < 0, 1) = particlesVolumeSizeXyz(2);
% particlesPositionXyz(particlesPositionXyz(:,1) < 0, 1) - ...
% cosd(directionParticleDegrees) * particlesVolumeSizeXyz(1);
end
% - from the side of the max volume side in the Y axis
if sum(particlesPositionXyz(:,2) > particlesVolumeSizeXyz(2)) > 0
particlesPositionXyz(particlesPositionXyz(:,2) > particlesVolumeSizeXyz(2), 2) = ...
rand(1, sum(particlesPositionXyz(:,2) > particlesVolumeSizeXyz(2))) * particlesVolumeSizeXyz(2);
particlesPositionXyz(particlesPositionXyz(:,2) > particlesVolumeSizeXyz(2), 1) = 0;
% particlesPositionXyz(particlesPositionXyz(:,1) > particlesVolumeSizeXyz(2), 2) - ...
% cosd(directionParticleDegrees) * particlesVolumeSizeXyz(2);
end
% - below 0
if sum(particlesPositionXyz(:,2) < 0) > 0
particlesPositionXyz(particlesPositionXyz(:,2) < 0, 2) = ...
rand(1, sum(particlesPositionXyz(:,2) < 0)) * particlesVolumeSizeXyz(2);
particlesPositionXyz(particlesPositionXyz(:,2) < 0, 1) = particlesVolumeSizeXyz(1);
% particlesPositionXyz(particlesPositionXyz(:,2) < 0, 1) - ...
% cosd(directionParticleDegrees) * particlesVolumeSizeXyz(2);
end
%% distance from particle to pixel
particlesAugmented = repmat(particlesPositionXyz,[1 1 sensorResolutionXy(1)*sensorResolutionXy(2)]);
% clear particlesPositionXyz
sensorPixelsCoordinatesXyzAugmented = repmat(sensorPixelsCoordinatesXyz',[1 1 nParticles]);
% clear sensorPixelsCoordinatesXyz
euclideanDistanceParticlesToPixelsDifference = bsxfun(@minus,...
particlesAugmented,...
permute(sensorPixelsCoordinatesXyzAugmented,[3 1 2]));
clear particlesAugmented sensorPixelsCoordinatesXyzAugmented
euclideanDistanceParticlesToPixelsSumOfSquares = sum(euclideanDistanceParticlesToPixelsDifference.^2,2);
clear euclideanDistanceParticlesToPixelsDifference
%% light scattered from particles for every pixel
% E=exp(i*k*r -1i*k*c*t)./r
%
% i - imaginary unit, r - distance from particle to pixel,
% c - speed of light, k - wavenumber (2pi/lambda), wavelength lambda=0.785 um, time t=0
c = single(299792458000000); % um/s
lambda = single(0.785); % microns
k = 2*pi/lambda;
t = 0; %single(1);
r = euclideanDistanceParticlesToPixelsSumOfSquares;
clear euclideanDistanceParticlesToPixelsSumOfSquares
E=exp(1i*k*r -1i*k*c*t)./r;
clear r
superpositionOfScatteredLight = sum(E,1);
clear E
pixelsIntensity = superpositionOfScatteredLight .* conj(superpositionOfScatteredLight);
clear superpositionOfScatteredLight
pixelsIntensity = pixelsIntensity/ mean(pixelsIntensity);
%% Reshaping to make sensor image
sensorImage = reshape(pixelsIntensity,[sensorResolutionXy(1) sensorResolutionXy(2)]);
clear pixelsIntensity
%% Save sensorImage to calculate Time Intensity Autocorrelation Fuction at the end
%
timeIntensityAutocorrelationFunction(:,:,iter) = sensorImage;
%% plot the figures
% isPlot = 0;
if isPlot
h = figure;
screenSize = get(0, 'ScreenSize');
set(gcf, 'Position', [floor(screenSize(3)/9), floor(screenSize(4)/5), ...
floor(screenSize(3)/9)*7, floor(screenSize(4)/5)*3])
subplot(1,2,1),
scatter(particlesPositionXyz(:,1),particlesPositionXyz(:,2)),
axisXmin = 0;
axisXmax = particlesVolumeSizeXyz(1);
axisYmin = 0;
axisYmax = particlesVolumeSizeXyz(2);
axis([axisXmin axisXmax axisYmin axisYmax]),
subplot(1,2,2),
imagesc(sensorImage)
% caxis([prctile(sensorImage(:),0.1),prctile(sensorImage(:),99.9)])
pause;
close all;
end
%% create gif
if isGif
% Capture the plot as an image
if isOrderedMotion
filename = 'Ordered_lscigif.gif';
else
filename = 'Brownian_lscigif.gif';
end
h = figure('Visible','off');
imagesc(sensorImage)
frame = getframe(h);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
if iter == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
clear sensorImage
if isCalculateTiaf ||
display(['iteration: ',num2str(iter),' of ',num2str(nMoves)])
end
end
close all
%% Calculating Time Intensity Autocorrelation Function (tiaf)
% isCalculateTiaf = 1;
if isCalculateTiaf
clear sensorPixelsCoordinatesXyz
% maxTau = 100;
tiafIntensityTimesIntensityDelayed = nan(sensorResolutionXy(1),...
sensorResolutionXy(2),...
nMoves-maxTau);
tiaf = nan(1,maxTau);
tiafMeanSquaredTime = mean(timeIntensityAutocorrelationFunction,3).^2;
for iTau = 1:maxTau
tiafIntensityTimesIntensityDelayed = mean(...
timeIntensityAutocorrelationFunction(:,:,1:end-maxTau) .* ...
timeIntensityAutocorrelationFunction(:,:,iTau:end-maxTau+iTau-1)...
,3);
tiafDivision = tiafIntensityTimesIntensityDelayed ./ tiafMeanSquaredTime;
tiaf(iTau) = mean(mean(tiafDivision,1),2);
end
decorrelationLag = linspace(1,maxTau,maxTau);
figure,plot(decorrelationLag, tiaf)
if isOrderedMotion
nameOrder = 'Ordered';
else
nameOrder = 'Brownian';
end
nameTitle = [nameOrder,' motion'];
title(nameTitle)
xlabel('decorrelation time \tau_{c}')
ylabel('Time Intensity Autocorrelation')
if isSaveCalculatedTiaf
end
end