-
Notifications
You must be signed in to change notification settings - Fork 0
/
brownianPointing.m
1130 lines (940 loc) · 43.3 KB
/
brownianPointing.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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% BrownianPointing -
% ___________________________________________________________________
%
% Try to follow a dot with the mouse.
% _______________________________________________________________________
%
% HISTORY
% 4/23/13 lkc Wrote it based on MouseTraceDemo
% 4/25/13 lkc made ver 3, which does real rotation, and needs a new
% naming convention
% 4/26/13 lkc Going to implement some radness
% - sinusoidal varying of parameters
% - treating each frame as a trial + using lag in a revco way
% the goal being to get a quantitative measure of learning
% and/or sensorimotor adaptation.
% 4/28/13 lkc Implemented sinusoidal variation
% 4/29/13 lkc Origin of transform can now mooove wif cursor, such that
% each frame's movement is like a little mini trial starting
% at the origin
% 7/29 Tested handling actual cursor goes off-screen, place back in
% center and handle drawn cursor smoothly
% NOTES
% 4/28 - need to implement sinusoidal variation
% & *really* need to code a file saving & analysis pipeline
% 4/29 - I think a cool condition would be to have the target jump a
% relatively long way, and sit there until until the subject's
% responds "stops" by come criteria.
% 4/29 - Yummy!.
% 7/29 - Cursor path flipped for y but not x when looking at raw
% traces because x,y = (0,0) starts in top left by default. Logic
% of analyses ought to be the same, though.
%% set up initial parameters
trialTime = 32*8; %20; % trial duration in s
stepSize = 8; % ave or max stepsize in pixels - determines target speed
frameRate = 60; % will determine this "live" in future versions
totFrames = trialTime*frameRate; % yup
tfreq = 0.25; %0.2; % temp. freq. of parameter variation
updateRate = 3;
% these "maximums" are the extreme vals of the parameters in the dynamic
% case, and are simply the parameters in the static case
aMax = 0.6;
bMax = 0.6;
%thMax = 5*pi/12;
thMax = pi/4;
% flags for stuff that will become arguments or gui checkboxes at some
% point...
mouseVisibleFlag = 1; % visual feedback of whey yo finger at
dynoUpdateFlag = 1; % dynamic updating of transform
scalerFlag = 0; % scaler or rotation transformation?
origAtCurs = 1; % do transform around cursor or screen center?
% dot colorz & sizes
dotSizes = [30, 10]; % first one is target
%dotColors = [rTarg, rMaus; gTarg, gMaus; bTarg, bMaus];
if mouseVisibleFlag
dotColors = [20, 200; 200, 200; 20, 200];
else
dotColors = [20, 0; 200, 0; 20, 0];
end
% D-fine the tform
if scalerFlag % Kat's scaler rotation
a = 1;
b = aMax;
c = bMax;
d = 1;
if dynoUpdateFlag
b = aMax.*sin(2.*pi.*tfreq.*(1/frameRate).*(1:totFrames));
c = bMax.*(pi./6).*sin(2.*pi.*tfreq.*(1/frameRate).*(1:totFrames));
else
b = repmat(b, 1, totFrames);
c = repmat(c, 1, totFrames);
% m = [a, b; c, d];
end
else % a rotation transformation
th = thMax; %pi./2;
if dynoUpdateFlag
% th = linspace(0, pi./2, totFrames);
th = thMax.*sin(2.*pi.*tfreq.*(1/frameRate).*(1:totFrames));
else
th = repmat(th, 1, totFrames);
end
m = [cos(th(1)), -sin(th(1)); sin(th(1)), cos(th(1))]; % le rotation
end % end rotation type selection
% some analysis params
maxLag = .7; % specify max xcorr lag in secs
frontPorch = 1; % # of initial secs to drop from the xcorr
%% generate target sequence
% generate position jitter directly
% targCoords = [0;0];
% for i = 1:totFrames-1
% % need to work in step size here
% temp = stepSize*randn(2,1);
% targCoords(:,i+1) = targCoords(:,i) + temp;
% end
% or, to drop out some of the high freq. jitter, let's generate random
% velocities, smooove them, and then integrate them to get positions.
%{
for i = 1:totFrames
temp(i,:) = randn(2,1);
latestCoords = stepSize*sum(temp, 2);
if temp(1,end) > 0.95*theRect(RectRight)
temp(1,end) = randn(1,1)-0.5;
elseif temp(1,end) < 0.05*theRect(RectRight)
temp(1,end) = randn(1,1)+0.5;
end
if temp(2,end) > 0.95*theRect(RectBottom)
temp(2,end) = randn(1,1)-0.5;
elseif temp(2,end) < 0.05*theRect(RectBottom)
temp(2,end) = randn(1,1)+0.5;
end
end
%temp = randn(2,totFrames);
temp(1,:) = conv(temp(1,:), [.25,.25,.25,.25], 'same');
temp(2,:) = conv(temp(2,:), [.25,.25,.25,.25], 'same');
temp(:,1) = [0; 0];
targCoords = stepSize*cumsum(temp, 2);
respCoords = zeros(size(targCoords)); % response coordinate array
tFormRespCoords = respCoords;
%}
%% stimulus presentation
try
% Open up a window on the screen and clear it.
whichScreen = max(Screen('Screens'));
[theWindow,theRect] = Screen(whichScreen,'OpenWindow',0,[],[],2);
% Move the cursor to the center of the screen
centerX = theRect(RectRight)/2;
centerY = theRect(RectBottom)/2;
for i = 1:totFrames
temp(:,i) = randn(2,1);
latestCoords = stepSize*sum(temp, 2);
if latestCoords(1,1) + centerX > 0.95*theRect(RectRight)
temp(1,end) = randn(1,1)-0.5;
elseif latestCoords(1,1) + centerX < 0.05*theRect(RectRight)
temp(1,end) = randn(1,1)+0.5;
end
if latestCoords(2,1) + centerY > 0.95*theRect(RectBottom)
temp(2,end) = randn(1,1)-0.5;
elseif latestCoords(2,1) + centerY < 0.05*theRect(RectBottom)
temp(2,end) = randn(1,1)+0.5;
end
end
numConv = 15;
%temp = randn(2,totFrames);
temp(1,:) = conv(temp(1,:), repmat(1/numConv,1,numConv), 'same');
temp(2,:) = conv(temp(2,:), repmat(1/numConv,1,numConv), 'same');
temp(:,1) = [0; 0];
targCoords = stepSize*cumsum(temp, 2);
respCoords = zeros(size(targCoords)); % response coordinate array
tFormRespCoords = respCoords;
targCoords(1,:) = targCoords(1,:) + centerX;
targCoords(2,:) = targCoords(2,:) + centerY;
SetMouse(centerX,centerX);
HideCursor();
% Wait for a click
Screen(theWindow,'FillRect',0);
Screen(theWindow,'DrawText','Klick to start trial',50,50,255);
Screen('Flip', theWindow);
while(1)
[x,y,buttons] = GetMouse(whichScreen);
if buttons(1)
break
end
end
% short delay before the stimulus begins (make random)
pause(0.5);
% Loop and track the mouse, drawing the contour
SetMouse(centerX,centerY);
x = centerX;
y = centerY;
% DRIFT CONCEPT ADDED 07/29
drift_x = 0;
drift_y = 0;
tFormedCoords = [centerX; centerY];
txOld = x;
tyOld = y;
Screen('DrawDots', theWindow, [centerX, centerX; centerY, centerY], dotSizes, dotColors);
Screen('Flip', theWindow);
sampleTime = 0.01;
startTime = GetSecs;
nextTime = startTime+sampleTime;
pause(0.5); % ready... set...
repeatCoords = [targCoords(1,1), tFormedCoords(1,1) ; targCoords(2,1), tFormedCoords(2,1)];
for i = 1:totFrames
xOld = x;
yOld = y;
[x,y,buttons] = GetMouse(whichScreen);
% NEW PART ADDED TO DEAL WITH LEAVING SCREEN 07/29
if x > 0.95*theRect(RectRight)
drift_x = drift_x + (0.95-0.5)*theRect(RectRight);
xOld = x - xOld + centerX;
x = centerX;
SetMouse(centerX,round(y),whichScreen);
elseif x < 0.05*theRect(RectRight)
drift_x = drift_x - (0.95-0.5)*theRect(RectRight);
xOld = x - xOld + centerX;
x = centerX;
SetMouse(centerX,round(y),whichScreen);
end
if y > 0.95*theRect(RectBottom)
drift_y = drift_y + (0.95-0.5)*theRect(RectBottom);
yOld = y - yOld + centerY;
y = centerY;
SetMouse(round(x),centerY,whichScreen);
elseif y < 0.05*theRect(RectBottom)
drift_y = drift_y - (0.95-0.5)*theRect(RectBottom);
yOld = y - yOld + centerY;
y = centerY;
SetMouse(round(x),centerY,whichScreen);
end
% END NEW PART 07/29
if scalerFlag % scaler tranformation
m = [a, b(i); c(i), d];
else % rotation
sinPercent = 1;
m = [cos(pi/4*round(th(i)/thMax)), -sin(pi/4*round(th(i)/thMax)); sin(pi/4*round(th(i)/thMax)), cos(pi/4*round(th(i)/thMax))]; % le rotation
m = [cos(th(i)), -sin(th(i)); sin(th(i)), cos(th(i))]; % le rotation
%m = [cos(thMax*(th(i)>0) - thMax*(th(i)<=0)), -sin(thMax*(th(i)>0) - thMax*(th(i)<=0));...
% sin(thMax*(th(i)>0) - thMax*(th(i)<=0)), cos(thMax*(th(i)>0) - thMax*(th(i)<=0))];
m = [cos(sinPercent*th(i)+ (1-sinPercent)*thMax*(th(i)>0) - (1-sinPercent)*thMax*(th(i)<=0)), -sin(sinPercent*th(i)+ (1-sinPercent)*thMax*thMax*(th(i)>0) - (1-sinPercent)*thMax*(th(i)<=0));...
sin(sinPercent*th(i)+ (1-sinPercent)*thMax*thMax*(th(i)>0) - (1-sinPercent)*thMax*(th(i)<=0)), cos(sinPercent*th(i)+ (1-sinPercent)*thMax*thMax*(th(i)>0) - (1-sinPercent)*thMax*(th(i)<=0))];
%m = [cos(-thMax*(th(i)>0)), -sin(-thMax*(th(i)>0)); sin(-thMax*(th(i)>0)), cos(-thMax*(th(i)>0))]; % le rotation
%thMax*(th(1:totFrames)>0) - (thMax*th(1:totFrames)<=0)
%m = [cos(0) -sin(0); sin(0) cos(0)];
%m = [1+thMax*sin(th(i)) 0; 0 1+thMax*sin(th(i))];
end
% this is a bit subtle - get mo vector from mouse movement, but
% apply transformed version of it to cursor
if origAtCurs
% tFormedCoords = m*[x-xOld; y-yOld]; % transform coordinates!
% tFormedCoords = tFormedCoords + [xOld; yOld];
deltaCoords = m*[x-xOld; y-yOld]; % transform coordinates!
tFormedCoords = tFormedCoords + deltaCoords;
else
tFormedCoords = m*[x-centerX; y-centerY]; % transform coordinates!
tFormedCoords = tFormedCoords + [centerX; centerY];
end
% target is drawn first, so cursor will be visible on top of it
tempCoords = [targCoords(1,i), tFormedCoords(1,1) ; targCoords(2,i), tFormedCoords(2,1)];
%line below makes this the 'continuous cursor' condition,
%commenting it out will make it refresh with target instead
%Additionally, using 2 as column number makes the cursor continuous
%but using 1 as a column number makes the target continuous instead
repeatCoords(:,1) = tempCoords(:,1);
if mod(i,round(frameRate/updateRate)) == 0
repeatCoords = tempCoords;
end
Screen('DrawDots', theWindow, repeatCoords, dotSizes, dotColors);
%Screen('DrawDots', theWindow, tempCoords, dotSizes, dotColors);
Screen('Flip', theWindow, GetSecs()+sampleTime);
% DRIFT_X and Y ADDED TO X AND Y 07/29
respCoords(:,i) = [drift_x + x; drift_y + y]; % where the mouse or finger actually is
tFormRespCoords(:,i) = tFormedCoords; % where the cursor is on screen
txOld = tFormedCoords(1);
tyOld = tFormedCoords(2);
end % end trial for loop
% Close up
ShowCursor(0);
Screen(theWindow,'Close');
catch
Screen('CloseAll');
ShowCursor;
psychrethrow(psychlasterror);
end %try..catch..
%keyboard;
%% data analysis!
% let's look at x and y correlation peaks and latencies as our index of
% adaptation
% convert to samples for lag and front bumper
maxLag = round(maxLag*frameRate);
frontPorch = round(frontPorch*frameRate);
% compute transformed target coordinates just for the hell of it
tFormedTargCoords = m*[targCoords(1,:)-centerX; targCoords(2,:)-centerY]; % transform coordinates!
tFormedTargCoords = tFormedTargCoords + repmat([centerX; centerY], 1, totFrames);
% center and scale coords to screen width
% respCoords = where the mouse is on the desk (or the finger on the pad)
normRespCoords(1,:) = (respCoords(1,:) - centerX)./ centerX;
normRespCoords(2,:) = (respCoords(2,:) - centerY)./ centerY;
normTargCoords(1,:) = (targCoords(1,:) - centerX)./ centerX;
normTargCoords(2,:) = (targCoords(2,:) - centerY)./ centerY;
% tFormRespCoords = where the cursor is on the screen
normTformRespCoords(1,:) = (tFormRespCoords(1,:) - centerX)./ centerX;
normTformRespCoords(2,:) = (tFormRespCoords(2,:) - centerY)./ centerY;
normTformTargCoords(1,:) = (tFormedTargCoords(1,:) - centerX)./ centerX;
normTformTargCoords(2,:) = (tFormedTargCoords(2,:) - centerY)./ centerY;
% compute velocities (from raw data, tho it shouldn't make a diff -ha!
respVel = diff(respCoords, 1, 2);
targVel = diff(targCoords, 1, 2);
% compute positional and velocity cross correlations
% velocity
[xCorX, xLagsVel] = xcorr(respVel(1,frontPorch:end), targVel(1,frontPorch:end), maxLag, 'unbiased');
[xCorY, yLagsVel] = xcorr(respVel(2,frontPorch:end), targVel(2,frontPorch:end), maxLag, 'unbiased');
% find peak lag and height
[xMax, xLagAtMax] = max(xCorX);
[yMax, yLagAtMax] = max(xCorY);
xRelLag = xLagAtMax-maxLag-1; % lag relative to zero
yRelLag = yLagAtMax-maxLag-1; % lag relative to zero
xMaxLagSecs = xRelLag./frameRate;
yMaxLagSecs = yRelLag./frameRate;
% cursor error
mausErr = normRespCoords(:, (xRelLag+1):end) - normTargCoords(:, 1:(end-xRelLag));
% error re. maus on screen
cursErr = normTformRespCoords(:, (xRelLag+1):end) - normTargCoords(:, 1:(end-xRelLag));
pythagCursErr = sqrt(cursErr(1,:).^2+cursErr(2,:).^2);
cursErrNoLag = normTformRespCoords - normTargCoords;
pythagCursErrNoLag = sqrt(cursErrNoLag(1,:).^2+cursErrNoLag(2,:).^2);
theLen = length(pythagCursErr);
[fAx, myInds] = fftaxes(theLen, frameRate);
theMid = round(theLen./2);
% determine how much of the spectrum to plot in a non-magic way
stimFreqInd = find(fAx>3*tfreq, 1);
stimFreqInd = find(fAx>1.5, 1);
notMagicNumber = stimFreqInd - theMid;
absCursFT = fftshift(abs(fft(killDC(pythagCursErr))));
pythagRespVel = sqrt(respVel(1,:).^2+respVel(2,:).^2);
theLen = length(pythagRespVel);
[fAx2, myInds2] = fftaxes(theLen, frameRate);
theMid2 = round(theLen./2);
% determine how much of the spectrum to plot in a non-magic way
stimFreqInd = find(fAx2>3*tfreq, 1);
stimFreqInd = find(fAx>1.5, 1);
notMagicNumber2 = stimFreqInd - theMid2;
absRespVelFT = fftshift(abs(fft(killDC(pythagRespVel))));
%{
% compute velocities (from raw data, tho it shouldn't make a diff -ha!
respVel = diff(respCoords, 1, 2);
targVel = diff(targCoords, 1, 2);
% compute positional and velocity cross correlations
% velocity
[xCorX, xLagsVel] = xcorr(respVel(1,frontPorch:end), targVel(1,frontPorch:end), maxLag, 'unbiased');
[xCorY, yLagsVel] = xcorr(respVel(2,frontPorch:end), targVel(2,frontPorch:end), maxLag, 'unbiased');
%THIS CRASHES IT
% (normalized) position
%[xCorXPos, xLagsPos] = xcorr(normTformRespCoords(1,frontPorch:end), normTargCoords(1,frontPorch:end), maxLag, 'unbiased');
%[xCorYPos, yLagsPos] = xcorr(normTformRespCoords(2,frontPorch:end), normTargCoords(2,frontPorch:end), maxLag, 'unbiased');
% find peak lag and height
[xMax, xLagAtMax] = max(xCorX);
[yMax, yLagAtMax] = max(xCorY);
xRelLag = xLagAtMax-maxLag-1; % lag relative to zero
yRelLag = yLagAtMax-maxLag-1; % lag relative to zero
xMaxLagSecs = xRelLag./frameRate;
yMaxLagSecs = yRelLag./frameRate;
if xRelLag < 0
xRelLag = 0;
fprintf('Warning: x lag less than zero; S has ESP.');
end
if yRelLag < 0
yRelLag = 0;
fprintf('Warning: y lag less than zero; S has ESP.');
end
% compute running diffs between mouse/cursor and target position after
% subtracting the response lag. The idea is to treat each target movement
% as a "trial", and look at the associated response error on each of these
% trials. rad.
% cursor error
mausErr = normRespCoords(:, (xRelLag+1):end) - normTargCoords(:, 1:(end-xRelLag));
% error re. maus on screen
cursErr = normTformRespCoords(:, (xRelLag+1):end) - normTargCoords(:, 1:(end-xRelLag));
%% spatial cross correlations
% okay, now lets see if we can do some sort of correlation on the spatial
% distributions of the target vs. cursor and target vs. mouse data...
% The general plan is to make spatial arrays that are all zeros except for
% where the target, etc. went.
% make matrices of the right size (based on max excursion during that
% trial?) We'll want to be working in pixel space.
% tFormRespCoords, respCoords, and targCoords are the relevant data
% find overall max and min of the relevant data
maxCoords = ceil(max([tFormRespCoords, respCoords, targCoords], [], 2));
minCoords = floor(min([tFormRespCoords, respCoords, targCoords], [], 2));
xArrSize = maxCoords(1)-minCoords(1)+1;
yArrSize = maxCoords(2)-minCoords(2)+1;
% init arrays
targMat = zeros(yArrSize, xArrSize);
respMat = zeros(yArrSize, xArrSize);
cursMat = zeros(yArrSize, xArrSize);
% set pixels on a path to 1
ttemp = ceil(targCoords) - repmat(minCoords, 1, totFrames)+1;
tinds = sub2ind(size(targMat), ttemp(2,:), ttemp(1,:));
targMat(round(tinds)) = 1;
rtemp = ceil(respCoords) - repmat(minCoords, 1, totFrames)+1;
rinds = sub2ind(size(respMat), rtemp(2,:), rtemp(1,:));
respMat(round(rinds)) = 1;
ctemp = ceil(tFormRespCoords) - repmat(minCoords, 1, totFrames)+1;
cinds = sub2ind(size(cursMat), ctemp(2,:), ctemp(1,:));
cursMat(round(cinds)) = 1;
% blur a bit
mykern = ggaus(10, 1);
targMat = conv2(targMat, mykern, 'same');
respMat = conv2(respMat, mykern, 'same');
cursMat = conv2(cursMat, mykern, 'same');
% compute cross correlations
targAndResp = xcorr2(targMat, respMat);
targAndCurs = xcorr2(targMat, cursMat);
%}
%% plotting!!
% Change default axes fonts.
set(0,'DefaultAxesFontName', 'Times New Roman')
set(0,'DefaultAxesFontSize', 16)
% Change default text fonts.
set(0,'DefaultTextFontname', 'Times New Roman')
set(0,'DefaultTextFontSize', 16)
% raw data traces
rawFig = figure;
plot(normTargCoords(1,:),normTargCoords(2,:), 'go:');
hold on;
plot(normTformRespCoords(1,:),normTformRespCoords(2,:), 'co:');
plot(normRespCoords(1,:),normRespCoords(2,:), 'ko-');
legend('target on screen', 'cursor on screen', 'finger/mouse location');
% mark the beginning and end points
plot(normTargCoords(1,1),normTargCoords(2,1), 'b*', 'MarkerSize', 20, ...
'MarkerFaceColor', 'b');
plot(normTargCoords(1,end),normTargCoords(2,end), 'rh', 'MarkerSize', 20, ...
'MarkerFaceColor', 'r');
hold off;
axis equal;
%{
% plots of the positions and velocities as a function of time
% The instances of 1:end below can be replace by frontPorch:end to omit
% plotting the first n=frontPorch samples
tracesFig = figure;
subplot(2,2,1); % H pos
plot(normRespCoords(1,1:end), 'k')
hold on
plot(normTformRespCoords(1,1:end), 'c')
plot(normTargCoords(1,1:end), 'g')
legend('mouse', 'cursor', 'target');
title('Horizontal Position');
hold off;
subplot(2,2,2); % V pos
plot(normRespCoords(2,1:end), 'k')
hold on
plot(normTformRespCoords(2,1:end), 'c')
plot(normTargCoords(2,1:end), 'g')
legend('mouse', 'cursor', 'target');
title('Vertical Position');
hold off;
subplot(2,2,3); % H vel
plot(targVel(1,1:end), 'g')
hold on
plot(respVel(1,1:end), 'k')
legend('response', 'target');
title('Horizontal Velocity');
hold off;
subplot(2,2,4); % V vel
plot(targVel(2,1:end), 'g')
hold on
plot(respVel(2,1:end), 'k')
legend('response', 'target');
title('Vertical Velocity');
hold off;
% plots of the cross-correlations
xcorFig = figure;
subplot(1, 2, 1); % position
plot(xLagsPos./frameRate, xCorXPos);
hold on;
plot(yLagsPos./frameRate, xCorYPos, 'k');
legend('horizontal position', 'vertical position');
xlabel('lag (s)');
hold off;
subplot(1, 2, 2); % velocity
plot(xLagsVel./frameRate, xCorX);
hold on;
plot(yLagsVel./frameRate, xCorY, 'k');
myylim = ylim;
line([xMaxLagSecs, xMaxLagSecs], [myylim(1), myylim(2)], ...
'Color', 'b', ...
'LineStyle', ':');
line([yMaxLagSecs, yMaxLagSecs], [myylim(1), myylim(2)], ...
'Color', 'k', ...
'LineStyle', ':');
legend('horizontal velocity', 'vertical velocity', 'Location', 'NorthWest');
text(xMaxLagSecs, xMax, [num2str(xMaxLagSecs, '%1.3f'), ' ', num2str(xMax, '%1.2f')]);
text(yMaxLagSecs, yMax, [num2str(yMaxLagSecs, '%1.3f'), ' ', num2str(yMax, '%1.2f')]);
xlabel('lag (s)');
hold off;
% plot RT (lag) shifted errors
lagErrFig = figure;
subplot(2,2,1); % H pos, lag shifted
plot(normRespCoords(1,(xRelLag+1):end), 'k')
hold on
plot(normTformRespCoords(1,(xRelLag+1):end), 'c')
plot(normTargCoords(1,1:(end-xRelLag)), 'g')
legend('mouse', 'cursor', 'target');
title('Horizontal Position');
hold off;
subplot(2,2,2); % V pos, lag shifted
plot(normRespCoords(2,(xRelLag+1):end), 'k')
hold on
plot(normTformRespCoords(2,(xRelLag+1):end), 'c')
plot(normTargCoords(2,1:(end-xRelLag)), 'g')
legend('mouse', 'cursor', 'target');
title('Vertical Position');
hold off;
subplot(2,2,3); % H pos error
plot(mausErr(1,1:end), 'k')
hold on
plot(cursErr(1,1:end), 'c')
mylim = xlim;
line([mylim(1), mylim(2)], [0, 0], ...
'Color', 'g', 'LineStyle', ':');
legend('mouse error', 'cursor error');
title('Horizontal Position');
hold off;
subplot(2,2,4); % V pos error
plot(mausErr(2,1:end), 'k')
hold on
plot(cursErr(2,1:end), 'c')
mylim = xlim;
line([mylim(1), mylim(2)], [0, 0], ...
'Color', 'g', 'LineStyle', ':');
legend('mouse error', 'cursor error');
title('Vertical Position');
hold off;
% NEED TO FIX FOR SCALER TRANSFORMS
% plot error against mag. of tranform
errWithTformFig = figure;
subplot(1,2,1) % H err
plot(mausErr(1,1:end), 'k')
hold on
plot(cursErr(1,1:end), 'c')
mylim = xlim;
line([mylim(1), mylim(2)], [0, 0], ...
'Color', 'g', 'LineStyle', ':');
mylim = ylim;
scFac = 0.5*(mylim(2)./thMax);
plot(scFac.*th, 'r:');
legend('mouse error', 'cursor error', 'tForm');
title('Horizontal Position');
hold off;
subplot(1,2,2); % V pos error
plot(mausErr(2,1:end), 'k')
hold on
plot(cursErr(2,1:end), 'c')
mylim = xlim;
line([mylim(1), mylim(2)], [0, 0], ...
'Color', 'g', 'LineStyle', ':');
mylim = ylim;
scFac = 0.5*(mylim(2)./thMax);
plot(scFac.*th, 'r:');
legend('mouse error', 'cursor error', 'tForm');
title('Vertical Position');
hold off;
% DITTO
% Fourier plots of above - see if the error is frequency matched (or
% related) to the magnitude of the transform
baronDuFourierH = figure;
%subplot(1,2,1) % H err
magicNumber = 40;
theLen = length(cursErr(1,:));
[fAx, myInds] = fftaxes(theLen, frameRate);
theMid = round(theLen./2);
hCursFT = normalize(fftshift(abs(fft(killDC(cursErr(1,:))))));
plot(fAx(theMid-magicNumber:theMid+magicNumber), hCursFT(theMid-magicNumber:theMid+magicNumber));
[peaks,locs] = findpeaks(hCursFT(theMid-magicNumber:theMid+magicNumber),'sortstr','descend');
hold on
line([fAx(theMid-magicNumber+locs(1)-1), fAx(theMid-magicNumber+locs(1)-1)], [myylim(1), myylim(2)], ...
'Color', 'b', ...
'LineStyle', ':');
text(fAx(theMid-magicNumber+locs(1)-1), peaks(1), num2str(fAx(theMid-magicNumber+locs(1)-1), '%1.3f'));
line([fAx(theMid-magicNumber+locs(3)-1), fAx(theMid-magicNumber+locs(3)-1)], [myylim(1), myylim(2)], ...
'Color', 'b', ...
'LineStyle', ':');
text(fAx(theMid-magicNumber+locs(3)-1), peaks(3), num2str(fAx(theMid-magicNumber+locs(3)-1), '%1.3f'));
line([fAx(theMid-magicNumber+locs(5)-1), fAx(theMid-magicNumber+locs(5)-1)], [myylim(1), myylim(2)], ...
'Color', 'b', ...
'LineStyle', ':');
text(fAx(theMid-magicNumber+locs(5)-1), peaks(5), num2str(fAx(theMid-magicNumber+locs(5)-1), '%1.3f'));
line([fAx(theMid-magicNumber+locs(7)-1), fAx(theMid-magicNumber+locs(7)-1)], [myylim(1), myylim(2)], ...
'Color', 'b', ...
'LineStyle', ':');
text(fAx(theMid-magicNumber+locs(7)-1), peaks(7), num2str(fAx(theMid-magicNumber+locs(7)-1), '%1.3f'));
line([fAx(theMid-magicNumber+locs(9)-1), fAx(theMid-magicNumber+locs(9)-1)], [myylim(1), myylim(2)], ...
'Color', 'b', ...
'LineStyle', ':');
text(fAx(theMid-magicNumber+locs(9)-1), peaks(9), num2str(fAx(theMid-magicNumber+locs(9)-1), '%1.3f'));
transFT = normalize(fftshift(abs(fft(killDC(th(1:length(cursErr(1,:))))))));
plot(fAx(theMid-magicNumber:theMid+magicNumber), transFT(theMid-magicNumber:theMid+magicNumber), 'r');
legend('cursor error T', 'transform FT');
title('Horizontal freq');
hold off;
%subplot(1,2,2); % V pos error
baronDuFourierH = figure;
theLen = length(cursErr(1,:));
[fAx, myInds] = fftaxes(theLen, frameRate);
theMid = round(theLen./2);
vCursFT = normalize(fftshift(abs(fft(killDC(cursErr(2,:))))));
plot(fAx(theMid-magicNumber:theMid+magicNumber), vCursFT(theMid-magicNumber:theMid+magicNumber));
[peaks,locs] = findpeaks(vCursFT(theMid-magicNumber:theMid+magicNumber),'sortstr','descend');
hold on
line([fAx(theMid-magicNumber+locs(1)-1), fAx(theMid-magicNumber+locs(1)-1)], [myylim(1), myylim(2)], ...
'Color', 'b', ...
'LineStyle', ':');
text(fAx(theMid-magicNumber+locs(1)-1), peaks(1), num2str(fAx(theMid-magicNumber+locs(1)-1), '%1.3f'));
line([fAx(theMid-magicNumber+locs(3)-1), fAx(theMid-magicNumber+locs(3)-1)], [myylim(1), myylim(2)], ...
'Color', 'b', ...
'LineStyle', ':');
text(fAx(theMid-magicNumber+locs(3)-1), peaks(3), num2str(fAx(theMid-magicNumber+locs(3)-1), '%1.3f'));
line([fAx(theMid-magicNumber+locs(5)-1), fAx(theMid-magicNumber+locs(5)-1)], [myylim(1), myylim(2)], ...
'Color', 'b', ...
'LineStyle', ':');
text(fAx(theMid-magicNumber+locs(5)-1), peaks(5), num2str(fAx(theMid-magicNumber+locs(5)-1), '%1.3f'));
line([fAx(theMid-magicNumber+locs(7)-1), fAx(theMid-magicNumber+locs(7)-1)], [myylim(1), myylim(2)], ...
'Color', 'b', ...
'LineStyle', ':');
text(fAx(theMid-magicNumber+locs(7)-1), peaks(7), num2str(fAx(theMid-magicNumber+locs(7)-1), '%1.3f'));
line([fAx(theMid-magicNumber+locs(9)-1), fAx(theMid-magicNumber+locs(9)-1)], [myylim(1), myylim(2)], ...
'Color', 'b', ...
'LineStyle', ':');
text(fAx(theMid-magicNumber+locs(9)-1), peaks(9), num2str(fAx(theMid-magicNumber+locs(9)-1), '%1.3f'));
transFT = normalize(fftshift(abs(fft(killDC(th(1:length(cursErr(2,:))))))));
plot(fAx(theMid-magicNumber:theMid+magicNumber), transFT(theMid-magicNumber:theMid+magicNumber), 'r');
legend('cursor error T', 'transform FT');
title('Vertical freq');
hold off;
% show image version for 2D cross-correlations
imFig = figure;
subplot(1, 3, 1);
imagesc(targMat);
title('target');
subplot(1, 3, 2);
imagesc(respMat);
title('mouse/finger')
subplot(1, 3, 3);
imagesc(cursMat);
title('cursor');
colormap jet
TwoDXcorrFig = figure;
subplot(1,2,1);
imagesc(targAndResp);
title('target and mouse');
subplot(1,2,2);
imagesc(targAndCurs);
title('target and Cursor');
figure(TwoDXcorrFig);
figure(imFig);
figure(errWithTformFig);
figure(lagErrFig);
figure(tracesFig);
figure(rawFig);
figure(xcorFig);
%}
figure;
subplot(4,1,1);
sinFit(pythagCursErr, 2*tfreq*trialTime);
title('Absolute Cartesian Error by Time','FontWeight','bold');
xlabel('Time (ms)');
ylabel('Normalized Pixel Error');
legend('Cursor Error', 'Sinusoid Fit');
subplot(4,1,2)
plot(fAx(theMid-notMagicNumber:theMid+notMagicNumber), absCursFT(theMid-notMagicNumber:theMid+notMagicNumber));
hold on
transFT = fftshift(abs(fft(killDC(th(1:length(absCursFT))))));
transFT = (transFT./max(transFT)).*max(absCursFT);
plot(fAx(theMid-notMagicNumber:theMid+notMagicNumber), transFT(theMid-notMagicNumber:theMid+notMagicNumber), 'r');
legend('Cursor Error', 'Transform');
title('Absolute Cartesian Error Frequency Spectrum','FontWeight','bold');
xlabel('Frequency');
ylabel('Amplitude');
hold off;
subplot(4,1,3)
sinFit(pythagRespVel, 2*tfreq*trialTime);
title('Absolute Velocity by Time','FontWeight','bold');
xlabel('Time (ms)');
ylabel('Normalized Pixel Absolute Velocity');
legend('Cursor Error', 'Sinusoid Fit');
subplot(4,1,4)
plot(fAx2(theMid2-notMagicNumber2:theMid2+notMagicNumber2), absRespVelFT(theMid2-notMagicNumber2:theMid2+notMagicNumber2));
hold on
transFT = fftshift(abs(fft(killDC(th(1:length(absRespVelFT))))));
transFT = (transFT./max(transFT)).*max(absRespVelFT);
plot(fAx2(theMid2-notMagicNumber2:theMid2+notMagicNumber2), transFT(theMid2-notMagicNumber2:theMid2+notMagicNumber2), 'r');
legend('Cursor Error', 'Transform');
title('Absolute Velocity Frequency Spectrum','FontWeight','bold');
xlabel('Frequency');
ylabel('Amplitude');
hold off;
%SAVED JUST IN CASE YOU WANT TO SAVE TWO TRIALS TO PLOT
%{
pythagCursErr0_4 = pythagCursErr;
fAx0_4 = fAx;
absCursFT0_4 = absCursFT;
lowerMagic = theMid-notMagicNumber;
upperMagic = theMid+notMagicNumber;
pythagRespVel0_4 = pythagRespVel;
fAx20_4 = fAx2;
absRespVelFT0_4 = absRespVelFT;
lowerMagic2 = theMid2-notMagicNumber2;
upperMagic2 = theMid2+notMagicNumber2;
th0_4 = th;
save('0_4trial.mat','pythagCursErr0_4','fAx0_4','absCursFT0_4','lowerMagic','upperMagic','pythagRespVel0_4','fAx20_4','absRespVelFT0_4','lowerMagic2','upperMagic2','th0_4');
%}
%ONCE YOU LOAD 0_4trial.mat
%{
figure;
subplot(4,1,1);
sinFit(pythagCursErr, 2*tfreq*trialTime);
title('Absolute Cartesian Error by Time','FontWeight','bold');
xlabel('Time (ms)');
ylabel('Normalized Pixel Error');
legend('Cursor Error', 'Sinusoid Fit');
subplot(4,1,2)
plot(fAx(theMid-notMagicNumber:theMid+notMagicNumber), absCursFT(theMid-notMagicNumber:theMid+notMagicNumber));
hold on
transFT = fftshift(abs(fft(killDC(th(1:length(absCursFT))))));
transFT = (transFT./max(transFT)).*max(absCursFT);
plot(fAx(theMid-notMagicNumber:theMid+notMagicNumber), transFT(theMid-notMagicNumber:theMid+notMagicNumber), 'r');
legend('Cursor Error', 'Transform');
title('Absolute Cartesian Error Frequency Spectrum','FontWeight','bold');
xlabel('Frequency');
ylabel('Amplitude');
hold off;
subplot(4,1,3);
sinFit(pythagCursErr0_4, 2*0.4*trialTime);
title('Absolute Cartesian Error by Time','FontWeight','bold');
xlabel('Time (ms)');
ylabel('Normalized Pixel Error');
legend('Cursor Error', 'Sinusoid Fit');
subplot(4,1,4)
plot(fAx0_4(lowerMagic:upperMagic), absCursFT0_4(lowerMagic:upperMagic));
hold on
transFT = fftshift(abs(fft(killDC(th0_4(1:length(absCursFT0_4))))));
transFT = (transFT./max(transFT)).*max(absCursFT0_4);
plot(fAx0_4(lowerMagic:upperMagic), transFT(lowerMagic:upperMagic), 'r');
legend('Cursor Error', 'Transform');
title('Absolute Cartesian Error Frequency Spectrum','FontWeight','bold');
xlabel('Frequency');
ylabel('Amplitude');
hold off;
figure;
subplot(4,1,1)
sinFit(pythagRespVel, 2*tfreq*trialTime);
title('Absolute Velocity by Time','FontWeight','bold');
xlabel('Time (ms)');
ylabel('Normalized Pixel Absolute Velocity');
legend('Cursor Velocity', 'Sinusoid Fit');
subplot(4,1,2)
plot(fAx2(theMid2-notMagicNumber2:theMid2+notMagicNumber2), absRespVelFT(theMid2-notMagicNumber2:theMid2+notMagicNumber2));
hold on
transFT = fftshift(abs(fft(killDC(th(1:length(absRespVelFT))))));
transFT = (transFT./max(transFT)).*max(absRespVelFT);
plot(fAx2(theMid2-notMagicNumber2:theMid2+notMagicNumber2), transFT(theMid2-notMagicNumber2:theMid2+notMagicNumber2), 'r');
legend('Cursor Velocity', 'Transform');
title('Absolute Velocity Frequency Spectrum','FontWeight','bold');
xlabel('Frequency');
ylabel('Amplitude');
hold off;
subplot(4,1,3)
sinFit(pythagRespVel0_4, 2*0.4*trialTime);
title('Absolute Velocity by Time','FontWeight','bold');
xlabel('Time (ms)');
ylabel('Normalized Pixel Absolute Velocity');
legend('Cursor Velocity', 'Sinusoid Fit');
subplot(4,1,4)
plot(fAx20_4(lowerMagic2:upperMagic2), absRespVelFT0_4(lowerMagic2:upperMagic2));
hold on
transFT = fftshift(abs(fft(killDC(th0_4(1:length(absRespVelFT0_4))))));
transFT = (transFT./max(transFT)).*max(absRespVelFT0_4);
plot(fAx20_4(lowerMagic2:upperMagic2), transFT(lowerMagic2:upperMagic2), 'r');
legend('Cursor Velocity', 'Transform');
title('Absolute Velocity Frequency Spectrum','FontWeight','bold');
xlabel('Frequency');
ylabel('Amplitude');
hold off;
%}
window = 1/tfreq*frameRate/2;
angles_to_test = [0.1 0.2 0.28 0.35 0.4 0.5];
num_angles = numel(angles_to_test);
cursErr_snippetAve4_angles_cond = zeros(num_angles,16,4,window);
cursErr_snippet_freqAve4_angles_cond = zeros(num_angles,16,4,window);
VelXErr_snippet_freqAve4_angles_cond = zeros(num_angles,16,4,window);
VelYErr_snippet_freqAve4_angles_cond = zeros(num_angles,16,4,window);
[cursErr_snippetAve_angles_cond] = zeros(num_angles,16,10,window);
[cursErr_snippet_freqAve_angles_cond] = zeros(num_angles,16,10,window);
[cursErr_init_angles_cond] = zeros(num_angles,16,10);
[cursErr_init_freq_angles_cond] = zeros(num_angles,16,10);
for i = 1:numel(angles_to_test)
[all_inds,sum_inds] = IndsForAngles(0.0625,angles_to_test(i),60,window,trialTime);
[cursErrMaxAve, CursErrLagAtMaxAve, cursErr_snippetAve, cursErr_freqMaxAve, CursErr_freqLagAtMaxAve, cursErr_snippet_freqAve,...
cursErr_init,cursErr_freq_init,cursErr_snippetAve4,cursErr_snippet_freqAve4,VelXErr_snippet_freqAve4,VelYErr_snippet_freqAve4] = windowedErrsTrialAve(respVel,targVel,normRespCoords,normTargCoords,normTformRespCoords,all_inds,60,window,tfreq);
[cursErr_snippetAve_angles_cond] = shortened_snippet_placement(1,i,cursErr_snippetAve,cursErr_snippetAve_angles_cond,1,window);
[cursErr_snippet_freqAve_angles_cond] = shortened_snippet_placement(1,i,cursErr_snippet_freqAve,cursErr_snippet_freqAve_angles_cond,1,window);
[cursErr_init_angles_cond] = shortened_snippet_placement(1,i,cursErr_init,cursErr_init_angles_cond,1,window);
[cursErr_init_freq_angles_cond] = shortened_snippet_placement(1,i,cursErr_freq_init,cursErr_init_freq_angles_cond,1,window);
[cursErr_snippetAve4_angles_cond] = shortened_snippet_placement(1,i,cursErr_snippetAve4,cursErr_snippetAve4_angles_cond,4,window);
[cursErr_snippet_freqAve4_angles_cond] = shortened_snippet_placement(1,i,cursErr_snippet_freqAve4,cursErr_snippet_freqAve4_angles_cond,4,window);
[VelXErr_snippet_freqAve4_angles_cond] = shortened_snippet_placement(1,i,VelXErr_snippet_freqAve4,VelXErr_snippet_freqAve4_angles_cond,4,window);
[VelYErr_snippet_freqAve4_angles_cond] = shortened_snippet_placement(1,i,VelYErr_snippet_freqAve4,VelYErr_snippet_freqAve4_angles_cond,4,window);
end
cursErr_snippetAve4_angles_cond = squeeze(cursErr_snippetAve4_angles_cond(:,1,:,:));
for j = 1:num_angles
for k = 1:4
[cursErr_freq4MaxCond(j,k), cursErr_freq4LagAtMaxCond(j,k)] = max(squeeze(cursErr_snippetAve4_angles_cond(j,k,:)));
end
end
figure;
plot(cursErr_freq4LagAtMaxCond');
legend(num2str(angles_to_test'));
for i = 1:numel(angles_to_test)
[angle_inds,sum_inds] = IndsForAngles(tfreq,angles_to_test(i),frameRate,window,trialTime);
all_first_inds(i,:) = angle_inds(1:4);
end
WhereMaxLag = cursErr_freq4LagAtMaxCond(:,1:4) + all_first_inds;
figure;
plot(WhereMaxLag');
legend(num2str(angles_to_test'));
angle_at_Max = th(WhereMaxLag);
figure;
plot(angle_at_Max');
legend(num2str(angles_to_test'));
figure;
subplot(4,1,1);
plot(squeeze(cursErr_snippetAve4_angles_cond(1,1,:)));
subplot(4,1,2);
plot(squeeze(cursErr_snippetAve4_angles_cond(1,2,:)));
subplot(4,1,3);
plot(squeeze(cursErr_snippetAve4_angles_cond(1,3,:)));
subplot(4,1,4);
plot(squeeze(cursErr_snippetAve4_angles_cond(1,4,:)));
%other_th = pi/6*round(th(1:totFrames)/thMax);
other_th = thMax*(th(1:totFrames)>0) - thMax*(th(1:totFrames)<=0);
numFramesToPlot = 6000;
figure;
plot(other_th(1:numFramesToPlot)*max(pythagCursErr));
hold on;
plot(pythagCursErr(1:numFramesToPlot),'red');
positive_inds = find(other_th > 0);
negative_inds = find(other_th <= 0);
verid_inds = find(other_th == 0);
positive_inds(positive_inds>numel(pythagCursErr)) = [];
negative_inds(negative_inds>numel(pythagCursErr)) = [];
verid_inds(verid_inds>numel(pythagCursErr)) = [];
figure;