-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLALatticeNoiseReduction.m
225 lines (189 loc) · 6.82 KB
/
DLALatticeNoiseReduction.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
function [matrix,particleNumber,diameter] = DLALatticeNoiseReduction(numberOfParticles,noiseParameter);
%% DLA SIMULATION USING SQUARE MATRIX FOR ON LATTICE
tic
%% Initial Setup:
% We create a matrix of zeros and then plant a seed at the centre of the
% matrix. Change entry of matrix to 1 if it is aggregated.
%
% This is the matrix set up, we start from inner square which grows as the cluster grows and consider
% particle escaped if it reaches the outer square. We apply noise reduction
% in the following manner: each growth site has a counter which increases by a
% unit every time the site is visited. An empty site is occupied only after
% its selection M times.
% -----------
% | ---- |
% | | | |
% | | | |
% | ---- |
% -----------
matrix = zeros(round(numberOfParticles/4));
siteCounter = zeros(round(numberOfParticles/4));
middle = round(numberOfParticles/8);
matrix(middle,middle) = 1;
% Initialise our variables
particleNumber = 1; %integer
endScript = 0; %bool
maximumDistance = 0;
% We record the stuck particles
% in a matrix (stuck_particles) which records the x position, y position,
% distance from origin, angle from origin
stuck_particles = zeros(1,4);
stuck_particles(1,1) = middle;
stuck_particles(1,2) = middle;
%% Random Walk Script
while endScript == 0
%first decide where the particle starts from
R = maximumDistance + 5;
randAngle = 2*pi*rand;
if randAngle < pi/2
x = R*sin(randAngle);
x = round(x);
y = R*cos(randAngle);
y = round(y);
x = middle + x;
y = middle - y;
elseif randAngle < pi
theta = randAngle - pi/2;
x = R*cos(theta);
x = round(x);
y = R*sin(theta);
y = round(y);
x = middle + x;
y = middle + y;
elseif randAngle < 3*pi/2
theta = randAngle - pi;
x = R*sin(theta);
x = round(x);
y = R*cos(theta);
y = round(y);
x = middle - x;
y = middle + y;
else
theta = randAngle - 3*pi/2;
x = R*cos(theta);
x = round(x);
y = R*sin(theta);
y = round(y);
x = middle - x;
y = middle - y;
end
% Now we have a starting point on the edge of the inner square of the matrix. Now we want
% to perform random walk in the matrix until we are next to an entry
% which is 1. Set aggregate & escape to false.
distanceFromCenter = R;
aggregate = 0; %bool to exit below while loop
escape = 0; %bool to exit below while loop
numberOfSteps = 0; %record number of steps taken until aggregated
while (aggregate == 0) && (escape == 0)
randWalk = rand;
if distanceFromCenter > 4/3*R
x0 = x - middle;
y0 = middle - y;
r0 = sqrt(x0^2 + y0^2);
V = ((r0 - R)/(r0 + R))*tan(pi*randWalk);
x = (R/r0)*(((1-V^2)*x0 - 2*V*y0)/(1+V^2));
y = (R/r0)*(((1-V^2)*y0 + 2*V*x0)/(1+V^2));
x = round(x + middle);
y = round(middle - y);
else
if randWalk < 1/4
% go right
x = x+1;
elseif randWalk < 2/4
% go left
x = x-1;
elseif randWalk < 3/4
% go up
y = y-1;
else
% go down
y = y+1;
end
end
xdistanceFromCenter = abs(x - middle);
ydistanceFromCenter = abs(y - middle);
distanceFromCenter = sqrt(xdistanceFromCenter^2 + ydistanceFromCenter^2);
% aggregate test
if (matrix(y,x+1) + matrix(y,x-1) + matrix(y+1,x) + matrix(y-1,x)) ~= 0
aggregate = 1;
end
%We keep repeating this until the particle is finally stuck or
%escapes
end
%if escape
%disp('particle escaped')
%end
if aggregate
siteCounter(y,x) = siteCounter(y,x) + 1;
if siteCounter(y,x) == noiseParameter;
particleNumber = particleNumber + 1;
stuck_particles(particleNumber,1) = x;
stuck_particles(particleNumber,2) = y;
stuck_particles(particleNumber,3) = distanceFromCenter;
if distanceFromCenter > maximumDistance
maximumDistance = distanceFromCenter;
end
xquadrant = sign(x - middle);
yquadrant = sign(y - middle);
if xquadrant == 1
if yquadrant == 1
particleAngle = atan(ydistanceFromCenter/xdistanceFromCenter) + pi/2;
elseif yquadrant == -1
particleAngle = atan(xdistanceFromCenter/ydistanceFromCenter);
else
particleAngle = pi/2;
end
elseif xquadrant == -1
if yquadrant == 1
particleAngle = atan(xdistanceFromCenter/ydistanceFromCenter) + pi;
elseif yquadrant == -1
particleAngle = atan(ydistanceFromCenter/xdistanceFromCenter) + 3*pi/2;
else
particleAngle = 3*pi/2;
end
else
if yquadrant == 1
particleAngle = pi;
else
particleAngle = 0;
end
end
stuck_particles(particleNumber,4) = particleAngle;
disp(['particle aggregated: ' num2str(particleNumber)])
matrix(y,x) = 1;
% end of script check
if particleNumber == numberOfParticles
endScript = 1;
% calculate fractal dimension
diameter = 2*distanceFromCenter;
end
end
end
end
%% Plot graph
%matrix = matrix/particleNumber;
imagesc(matrix)
colormap(gray)
title(['Noise Reduction DLA with ' num2str(noiseParameter) ' as noise parameter and ' num2str(numberOfParticles) ' particles'])
%text(width/3,3*R/2 + R/6,['Fractal Dimension: ' num2str(fractalDimension)]);
%text(R/3,3*R/2,['Radius: ' num2str(maximumDistance)]);
timeElapsed = toc;
%text(R/3,3*R/2 - R/6, ['Time Elapsed: ' num2str(timeElapsed)]);
%xlabel(num2str(2*width))
%ylabel(num2str(2*width))
%axis equal
miny = min(stuck_particles(:,1));
maxy = max(stuck_particles(:,1));
minx = min(stuck_particles(:,2));
maxx = max(stuck_particles(:,2));
xdif = maxx-minx;
ydif = maxy-miny;
maxaxis = max(xdif,ydif)/2;
xlim([middle - maxaxis - 10,middle + maxaxis + 10])
ylim([middle - maxaxis - 10,middle + maxaxis + 10])
axis off
%% Display Outputs
disp(['Number of particles: ' num2str(particleNumber)]);
%disp(['Fractal Dimension: ' num2str(fractalDimension)]);
disp(['Diameter: ' num2str(diameter)]);
disp(['Time Elapsed: ' num2str(timeElapsed)]);