-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTibiaAngleForImagingFrame.m
120 lines (90 loc) · 3.91 KB
/
TibiaAngleForImagingFrame.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
%Calculates the average tibia angle for each imaging frame and plot both
%the DFF and tibia angle. Use scale factor to plot DFF and tibia angle at a
%similar scale. (Scale factor multiply the DFF value)
%
%ConvertedTibiaAngleFile: output from the ConvertTibiaAngle_FindMoveOnset.m
%DetectCameraAndImagingFramesFile: output from the
%DetectCameraAndImagingFrames.m
%DFFfile: ouput file from the SelectROICalculateDFF.m or other script that
%shows DFF
%ScaleFactor: Factor to multiply the DFF trace. Should be around 30 to 100.
function []=TibiaAngleForImagingFrame(ConvertedTibiaAngleFile,DetectCameraAndImagingFramesFile,DFFfile,ScaleFactor)
load(ConvertedTibiaAngleFile);
load(DetectCameraAndImagingFramesFile);
%use the CameraMinusImageIndex and get the last camera frame before each
%end of the imaging frame.
PositiveIndex=CameraMinusImageIndex>0;
ImageInCameraIndex(PositiveIndex)=ImageInCameraIndex(PositiveIndex)-1;
%Now ImageInCameraIndex shows the camera frame that was right before the
%end of the image acquisition.
%Go through each StartVF and convert it to the imaging frame where the move
%occured and the offset measured from the end of the frame.
%Initialize matrix.
StartIF=zeros(size(StartVF,1),2);
for n=1:size(StartVF,1)
TimeDifference=ImageInCameraIndex-StartVF(n);
%Find the point closest to zero.
[Y,I]=min(abs(TimeDifference));
StartIF(n,1)=I;
StartIF(n,2)=TimeDifference(I);
%The above positive number shows how many video frames have past since
%the beginning of the move and the end of the image.
end
%Calculate the mean leg angle for each image.
%If there is no camera image that corresponds to the calcium imaging frame,
%the tibia angle will be set to -100 and we will have an index to show the
%invalid point.
ImageLegAngle=ones(size(ImageInCameraIndex,1),1);
ImageLegAngle=ImageLegAngle*-100;%Set to -100 so we know the missing values.
%If ImageInCameraIndex is 0 for few frames (as when the imaging starts
%earlier than the camera), skip those frames.
ExtraFrames=ImageInCameraIndex==0;
[Y,StartFrame]=min(ExtraFrames);
%We also have times when the video stops before the imaging.
if ImageInCameraIndex(end)>size(RealAngleWithOffset,1)
ExtraFramesBack=ImageInCameraIndex>size(RealAngleWithOffset,1);
[Y,EndFrame]=max(ExtraFramesBack);
EndFrame=EndFrame-1;
else
EndFrame=size(ImageInCameraIndex,1);
end
%Put the angles in radian for the use of circ_mean.
RealAngleWithOffsetR=deg2rad(RealAngleWithOffset);
%First frame will be calculated differently.
if StartFrame==1
if ImageInCameraIndex(1)>=22
ImageLegAngle(1)=circ_mean(RealAngleWithOffsetR(ImageInCameraIndex(1)-21:ImageInCameraIndex(1)));
else
ImageLegAngle(1)=circ_mean(RealAngleWithOffsetR(1:ImageInCameraIndex(1)));
end
StartFrame=StartFrame+1;
end
for n=StartFrame:EndFrame%This can only go as long as we have ImageInCameraIndex.
ImageLegAngle(n)=circ_mean(RealAngleWithOffsetR(ImageInCameraIndex(n-1)+1:ImageInCameraIndex(n)));
end
%Put it back to the degrees.
ImageLegAngle=rad2deg(ImageLegAngle);
%In case it wraps around (for angles larger than 180), we need to unwrap
%it. May need to change this value in some prep but for now we keep it at
%0.
NIndexAll=ImageLegAngle<0;
ImageLegAngle(NIndexAll)=ImageLegAngle(NIndexAll)+360;
load(DFFfile);
figure('position',[400 400 800 400])
plot(ImageLegAngle)
pbaspect([2.5 1 1])
hold on
plot(DFF1*ScaleFactor','k')
for n=1:size(StartIF,1)
plot([StartIF(n,1) StartIF(n,1)],[0 180],'m--','LineWidth',1)
end
ylim([-10 190]);
set(gca,'box','off');
set(gcf,'Color','w');
hold off
position=strfind(ConvertedTibiaAngleFile,'.');
NewName=ConvertedTibiaAngleFile(1:position-1);
Outfile = strcat(NewName,'AngleForImagingFrame');
%print(Outfile,'-djpeg','-r300')
save(Outfile,'ImageLegAngle','RealAngle*','Start*','Abs*')
clear