-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGeneralMPIPhysics_Demo_v2.asv
More file actions
476 lines (372 loc) · 14.9 KB
/
Copy pathGeneralMPIPhysics_Demo_v2.asv
File metadata and controls
476 lines (372 loc) · 14.9 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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
function [] = GeneralMPIPhysics_Demo_v2(SaveVid,varargin)
%% Function Inputs/ help
% SaveVid is a binary option, if you want to save a video (1) or not (0)
% For Varargin:
% - FileName: is the file name that will be saved
% - VidFormat: either "gif" or "mp4"
% - MoveSample: Binary 1/0 if you want the sample to move
% - SoundOn: Binary 1/0 if you want it to make a beep when it finishes
%%
addpath([pwd,'\Called Functions'])
VidFormat='mp4'; %Default. WIll change if user enters something else
filename = [date(),'MPI_Physics_Vid.mp4']; %Default value. Will change if user enters something else when starting function
MoveSample = 0; %Default
yloc = 0;
yloc_FFP =0;
NumVarArgsIn = size(varargin,2);
if mod(NumVarArgsIn,2)>0
error('Number of arguments inputted must be even')
elseif NumVarArgsIn==0
filename = [date(),'MPI_Physics_Vid_v2.mp4'];
end
varargin = reshape(varargin,2,NumVarArgsIn/2);
for i = 1:NumVarArgsIn/2
if strcmpi(varargin(1,i),'FileName')
filename = cell2mat(varargin(2,i));
end
if strcmpi(varargin(1,i),'VidFormat')
VidFormat = cell2mat(varargin(2,i));
VidFormat(VidFormat=='.')=[]; %Making sure there is no period in the format. So ".GIF" goes to "GIF", Just for consistency
if not(strcmpi(VidFormat,'mp4')|strcmpi(VidFormat,'gif'))
error('Video format must be mp4 or gif')
end
filename = [date(),'MPI_Physics_Vid.',VidFormat];
end
if strcmpi(varargin(1,i),'MoveSample') %Selecting MoveSample as 1(Yes) or 0(No) moves the sample along the axis to show how the signal changes with biasing
MoveSample = cell2mat(varargin(2,i));
else
end
if strcmpi(varargin(1,i),'SoundOn')
SoundOn = cell2mat(varargin(2,i));
end
if strcmpi(varargin(1,i),'MorseIn')
MorseIn = cell2mat(varargin(2,i));
end
if strcmpi(varargin(1,i),'YLoc')
yloc = cell2mat(varargin(2,i));
end
if strcmpi(varargin(1,i),'YLoc_FFP')
yloc_FFP = cell2mat(varargin(2,i));
end
end
filename_HasExt = sum(filename=='.')>0;
if filename_HasExt==1
ExtensionStart = find(filename=='.');
VidFormat = filename(ExtensionStart+1:end);
else
filename = [filename,'.',VidFormat];
end
SavePath = uigetdir();
filename = [SavePath,'\',filename];
isGIF = strcmpi(VidFormat,'gif');
isMP4 = strcmpi(VidFormat,'mp4');
fDrive = 25e3;%Hz
Fs= 2e6; %Hz If this is too low, you start to get discretization artifacts in the figures. Keep above ~50x fDrive
x = -.025:.001:.025;%FOV meters
y = x;
y = y-yloc_FFP;
x_downSamp = x(1:5:end);
y_downSamp = x_downSamp;
[X,Y] = meshgrid(x_downSamp,y_downSamp);
XZero_Ind = find(x==0);
if exist('MoveSample','var')
if MoveSample==1
X_EndIndex=length(x);
else
X_EndIndex = XZero_Ind;
end
else %The default if you do not select motion or no is to not move
X_EndIndex = XZero_Ind;
end
Grad = 2; %Gradient in Tesla per meter
BfflFunc = @(x) Grad.*x;
Bffl = BfflFunc(x); %B field of the FFL
B_FFP_X= BfflFunc(X); %B field of the FFP
B_FFP_Y = BfflFunc(Y); %B field of the FFP
t = 0:1/Fs:3e-3-(1/Fs);
t_Up = 0:1/(Fs*4):3e-3-(1/Fs);
dt = 1/Fs;
Count = 0;
GIF_Count = 1;
% for XPos_Ind = XZero_Ind:1:length(x)
for XPos_Ind = XZero_Ind:X_EndIndex
Count = Count +1;
XPos = x(XPos_Ind);
B = Grad*x(end)/1.5*cos(2*pi*fDrive*t);
B_Big = 2*Grad*x(end)*cos(2*pi*fDrive*t_Up);
B_Bias = sqrt((B+ Grad*XPos).^2 + (Grad*(yloc-yloc_FFP)).^2);
if (yloc-yloc_FFP)==0
B_Bias = B_Bias.* sign(B);
end
M = Langevin(B,1);
M_Big = Langevin(B_Big,1);
M_Bias = Langevin(B_Bias,1);
Signal = diff(M);
FT_Sig = fft(Signal(1:end-1));
L_FT = length(FT_Sig);
P1_FT = abs(FT_Sig(1:L_FT/2+1));
f = Fs*(0:L_FT/2)/L_FT;
%%
if Count==1
h= figure('Position',[100 100 1300 850]);set(gcf, 'Color', 'White');
CitationBox = annotation('textbox',[.0,0,.1,.025],'String',{'Adapted from Gleich and Weizenecker, 2005 Fig. 1'},'FitBoxToText','on');
AnimationFromBox = annotation('textbox',[.0,0,.2,.025],'String','Animation from OS-MPI.GitHub.io','FitBoxToText','off');
drawnow
AnimationFromBox.Position(1) = 1-AnimationFromBox.Position(3);
AnimationFromBox.Position(2) = CitationBox.Position(2);
AnimationFromBox.Position(4) = CitationBox.Position(4);
P1 = subplot(2,3,1);
P1.Position(2) = P1.Position(2);
P1.Position(4) = P1.Position(4);
P2 = subplot(2,3,2);
P2.Position(2) = P2.Position(2);
P2.Position(4) = P2.Position(4);
P3 = subplot(2,3,3);
P3.Position(2) = P3.Position(2);
P3.Position(4) = P3.Position(4);
P4 = subplot(2,3,4);
P4.Position(2) = P4.Position(2);
P4.Position(4) = P4.Position(4);
P5 = subplot(2,3,5);
P5.Position(2) = P5.Position(2);
P5.Position(4) = P5.Position(4);
P6 = subplot(2,3,6);
P6.Position(2) = P6.Position(2);
P6.Position(4) = P6.Position(4);
% TMP_Ax = axes('Position',[0 0 1 .25]);
% rectangle('Position',[0 0 1 .125],'FaceColor',[.85 .85 .85])
% TMP_Ax.Color = 'None';
% set(gca,'XTick',[], 'YTick', [])
% TMP_Ax.YColor = 'None';
% TMP_Ax.XColor = 'None';
AxGap14 = axes('Position',[P1.Position(1) P4.Position(2)+P4.Position(4) P4.Position(3) P1.Position(2)-(P4.Position(2)+P4.Position(4))]);
Ax14Over = axes('Position',P1.Position);
AxGap12 = axes('Position',[P1.Position(1)+P1.Position(3) P1.Position(2) P2.Position(1)-(P1.Position(1)+P1.Position(3)) P1.Position(4)]);
Ax12Over = axes('Position',P1.Position);
% FFPAx = axes('Position',[P1.Position(1) .05 (P6.Position(1)+P6.Position(3)-P1.Position(1)) .125]);
end
BPick = B_Bias;
MPick = M_Bias;
Signal = diff(MPick);
FT_Sig = fft(Signal(1:end-1));
L_FT = length(FT_Sig);
P1_FT = abs(FT_Sig(1:L_FT/2+1));
if Count==1
P1_FT_Orig = P1_FT;
end
f = Fs*(0:L_FT/2)/L_FT;
if mod(Count,2)==0
StartI = round(Fs/fDrive/2)+1;
EndI = round(Fs/fDrive/2)*2;
else
StartI = 1;
EndI = round(Fs/fDrive/2);
if MoveSample==0
EndI = round(Fs/fDrive);
end
end
for ii = StartI:EndI
if ii==StartI && Count==1
axes(P1)
PlotMBLine = plot(B_Big(1:400),M_Big(1:400),'k','LineWidth',3);
hold on
Dot = plot(BPick(ii),MPick(ii),'bo','MarkerSize',10,'LineWidth',2);
hold off
xlim([min(B_Big) max(B_Big)])
xlabel('External Magnetic Field','FontSize',14,'FontWeight','bold')
title('Particle Magnetization Curve','FontSize',14,'FontWeight','bold')
ylabel({'SPION';'Magnetization'},'FontSize',14,'FontWeight','bold')
else
set(PlotMBLine,'XData',B_Big(1:400))
set(PlotMBLine,'YData',M_Big(1:400))
set(Dot,'XData',BPick(ii))
set(Dot,'YData',MPick(ii))
end
if ii==StartI && Count==1
axes(P4)
FieldOverTime = plot(BPick(1+ii:200+ii),t(1+ii:200+ii),'Color',[30,119,20]/255,'LineWidth',3);
ylim([ t(1+ii) t(200+ii)])
xlim([min(B_Big) max(B_Big)])
axis ij
ylabel('Time (seconds)','FontSize',14,'FontWeight','bold')
xlabel('External Magnetic Field','FontSize',14,'FontWeight','bold')
else
set(FieldOverTime,'XData',BPick(1+ii:200+ii))
set(FieldOverTime,'YData',t(1+ii:200+ii))
set(P4,'ylim',([ t(1+ii) t(200+ii)]))
set(P4,'xlim',[min(B_Big) max(B_Big)])
end
if ii==StartI&& Count==1
axes(P2)
MagOverTime = plot(t(1+ii:200+ii),MPick(1+ii:200+ii),'k','LineWidth',3);
xlim([t(1+ii) t(200+ii)])
ylim(P1.YLim)
xlabel('Time (seconds)','FontSize',14,'FontWeight','bold')
title('Magnetization over time','FontSize',14,'FontWeight','bold')
else
set(MagOverTime,'YData',MPick(1+ii:200+ii))
set(MagOverTime,'XData',t(1+ii:200+ii))
set(P2,'xlim',([ t(1+ii) t(200+ii)]))
end
if ii==StartI&& Count==1
axes(P3)
SigOverTime = plot(t(1+ii:200+ii),Signal(1+ii:200+ii),'Color',[33,204,192]/255,'LineWidth',3);
xlim([t(1+ii) t(200+ii)])
ylim([-0.5 0.5])
xlabel('Time (seconds)','FontSize',14,'FontWeight','bold')
ylabel('dM/dt','FontSize',14,'FontWeight','bold')
title('Signal over time','FontSize',14,'FontWeight','bold')
else
set(SigOverTime,'YData',Signal(1+ii:200+ii))
set(SigOverTime,'XData',t(1+ii:200+ii))
set(P3,'xlim',([ t(1+ii) t(200+ii)]))
end
if ii==StartI&& Count==1
axes(P5)
FieldQuiver = quiver(X,Y,-B_FFP_X+B(1+ii),-B_FFP_Y,1,'Color',[30,119,20]/255,'LineWidth',2);
ylim([min(x) max(x)])
xlim([min(x) max(x)])
axis image
title('External field','FontSize',14,'FontWeight','bold')
hold on
ParticlePlot0 = plot(x(XPos_Ind),yloc,'ko','LineWidth',4,'MarkerSize',1);
ParticlePlot1 = plot(x(XPos_Ind),yloc,'ko','LineWidth',4,'MarkerSize',10);
ParticlePlot2 = plot(x(XPos_Ind),yloc,'ro','LineWidth',2);
FFPPlot = plot(B(1+ii)/Grad,yloc_FFP,'mo','LineWidth',2,'MarkerSize',30);
SPIONField = quiver(x(XPos_Ind),yloc,B(1+ii),-BfflFunc(yloc),0.1,'r','LineWidth',3);
% set(gca,'XTick',[1:5:length(x)], 'YTick', [])
% FFPAx.XTickLabel = {x(1:5:length(x))};
% title('B-Field along 1D Line')
% xlabel('Position (meters)')
% Lgd = legend('','Particle location');
% Lgd.Location = 'northeastoutside';
else
set(FieldQuiver,'UData',-B_FFP_X+B(1+ii))
set(SPIONField,'UData',B(1+ii))
set(SPIONField,'XData',x(XPos_Ind))
% set(FieldQuiver,'YData',t(1+ii:200+ii))
set(ParticlePlot0,'XData',x(XPos_Ind))
set(ParticlePlot1,'XData',x(XPos_Ind))
set(ParticlePlot2,'XData',x(XPos_Ind))
set(FFPPlot,'XData',B(1+ii)/Grad)
set(P5,'ylim',[min(x) max(x)])
set(P5,'xlim',[min(y) max(y)])
end
if ii==StartI&& Count==1
axes(P6)
FTPlot = plot(f/fDrive,P1_FT,'Color',[33,204,192]/255,'LineWidth',3);
ylim([0 max(P1_FT_Orig)])
xlabel('Frequencies (f/f_{Drive})','FontSize',14,'FontWeight','bold')
ylabel('Magnitude','FontSize',14,'FontWeight','bold')
else
set(FTPlot,'YData',P1_FT)
end
% axes(FFPAx)
% imagesc(repmat(abs(Bffl+B(ii)),3,1))
% caxis([0 max(abs(B))*1.25])
% if ii==1 && Count==1
% CBR = colorbar;
% CBR.Label.String = {'External Field'; 'Magnitude'};
% CBR.Label.FontWeight = 'Bold';
% CBR.Location = 'westoutside';
% end
% hold on
% plot(XPos_Ind,2,'ko','LineWidth',4)
% % plot(XPos_Ind,2,'bo','LineWidth',1)
% set(gca,'XTick',[1:5:length(x)], 'YTick', [])
% FFPAx.XTickLabel = {x(1:5:length(x))};
% title('B-Field along 1D Line')
% xlabel('Position (meters)')
% Lgd = legend('Particle location');
% Lgd.Location = 'northeastoutside';
if ii==StartI&& Count==1
axes(AxGap14)
Axes14GapPlot = plot([BPick(ii) BPick(ii)],[0 1],'b--');
AxGap14.Color = 'None';
set(gca,'XTick',[], 'YTick', [])
AxGap14.YColor = 'None';
AxGap14.XColor = 'None';
xlim(P1.XLim)
ylim([0 1])
else
set(Axes14GapPlot,'XData',[BPick(ii) BPick(ii)])
end
if ii==StartI && Count==1
axes(Ax14Over)
Axes14OverFigPlot = plot([BPick(ii) BPick(ii)],[P1.YLim(1) MPick(ii) ],'b--');
Ax14Over.Color = 'None';
set(gca,'XTick',[], 'YTick', [])
Ax14Over.YColor = 'None';
Ax14Over.XColor = 'None';
xlim(P1.XLim)
ylim(P1.YLim)
else
set(Axes14OverFigPlot,'XData',[BPick(ii) BPick(ii)])
set(Axes14OverFigPlot,'YData',[P1.YLim(1) MPick(ii) ])
end
if ii==StartI && Count==1
axes(AxGap12)
Axes12GapPlot = plot([0 1],[MPick(ii) MPick(ii)],'b--');
AxGap12.Color = 'None';
set(gca,'XTick',[], 'YTick', [])
AxGap12.YColor = 'None';
AxGap12.XColor = 'None';
ylim(P1.YLim)
xlim([0 1])
else
set(Axes12GapPlot,'YData',[MPick(ii) MPick(ii)])
end
if ii==StartI && Count==1
axes(Ax12Over)
Axes12OverPlot = plot([BPick(ii) P1.XLim(2) ],[MPick(ii) MPick(ii) ],'b--');
Ax12Over.Color = 'None';
set(gca,'XTick',[], 'YTick', [])
Ax12Over.YColor = 'None';
Ax12Over.XColor = 'None';
xlim(P1.XLim)
ylim(P1.YLim)
else
set(Axes12OverPlot,'XData',[BPick(ii) P1.XLim(2) ])
set(Axes12OverPlot,'YData',[MPick(ii) MPick(ii) ])
end
drawnow
% pause(.001)
if SaveVid==1
frame = getframe(h);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
end
if isGIF && SaveVid==1
if GIF_Count == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',inf,'DelayTime',1/32);
else
imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',1/32);
end
end
disp([num2str(ii),',' num2str(GIF_Count)])
frame_1(GIF_Count) = getframe(gcf);
clear tmp
GIF_Count = GIF_Count+1;
end
% sound(sawtooth(2*pi*.3e3*(0:1/10e3:1)).*sin(2*pi*.3e3*(0:1/10e3:1)),10e3) %Obnoxious beeping to alert me it is done
end
if isMP4 && SaveVid==1
v = VideoWriter(filename,'MPEG-4');
open(v)
writeVideo(v,frame_1)
close(v)
end
if exist('SoundOn','var')
if SoundOn==1
if not(exist('MorseIn','var'))
MorseBeep('-- .-.- ..')
else
if MorseIn==1
MorseBeep(MorseIn)
else
MorseBeep('-- .-.- ..')
end
end
end
end
end