%Calculates all of the following things: %Prey Distance at time 0 %Time to prey capture %Maximum Jaw Protrusion %Maximum speed and acceleration of fish %Maximum speed and acceleration of prey item %Maximum speed and acceleration of prey in the frame of reference of the %fish %% Setting up for analysis. Create variables, load data. clear all; currentDirectory = ['']; dir1 = ['']; load('Hybrid_HSV_data.mat'); rooow = input('Input row number'); frfinish = (data(rooow,11)-data(rooow,9))+1; n = frfinish date = textdata{rooow,3}; matrixlabel1 = num2str(date); animal = textdata{rooow,4}; matrixlabel2 = num2str(animal); event = textdata{rooow,5}; matrixlabel3 = num2str(event); species = textdata{rooow,2}; %Load the data dataPrey = []; dataPreyfilename = [matrixlabel1 matrixlabel2 matrixlabel3 'dataPrey']; dataPrey = load(dataPreyfilename, '-ascii'); % Calibration filename_cal = textdata{rooow,7}; save_filename_cal = [filename_cal '.mat']; j = '000001'; doesIt = exist(save_filename_cal); if doesIt==0 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Make sure filename is right name = [dir1 filename_cal '\' filename_cal j '.tif']; [X,map] = imread(name); N = [X,map]; imshow(N) disp('draw line .5cm long') h = imline; position = wait(h); pos = h.getPosition(); cal_line = reshape(pos,1,4); length_in_pixels = sqrt(((cal_line(2)-cal_line(1))^2)+(cal_line(4)-cal_line(3))^2); cal = (length_in_pixels/.5)*100; save(save_filename_cal, 'cal') else load(save_filename_cal); end %Images are 1024x1024 pixels. This changes the y coordinates for plotting %with 0,0 in the lower left corner instead of the upper left corner. The %loop below fixes that by subrtracting each y value from 512 i = length(dataPrey(:,1)); for rig = 1:i dataPrey(rig,3) = dataPrey(rig,3)-1024; end dataPreySorted = sortrows(dataPrey,1); %this checks to see if there were any points picked, indicating that part %of the line could not be seen, and replaces those x and y coordinates with %blank cells % for r = 1:i % if dataEyeSorted(r,2) < 0 % dataEyeSorted(r,2) = NaN; % dataEyeSorted(r,3) = NaN; % end % if dataEyeSorted(r,3) < 0 % dataEyeSorted(r,2) = NaN; % dataEyeSorted(r,3) = NaN; % end % % % if dataUJSorted(r,2) < 0 % dataUJSorted(r,2) = NaN; % dataUJSorted(r,3) = NaN; % end % if dataUJSorted(r,3) < 0 % dataUJSorted(r,2) = NaN; % dataUJSorted(r,3) = NaN; % end % % % if dataLJSorted(r,2) < 0 % dataLJSorted(r,2) = NaN; % dataLJSorted(r,3) = NaN; % end % if dataLJSorted(r,3) < 0 % dataLJSorted(r,2) = NaN; % dataLJSorted(r,3) = NaN; % end % % % if dataPreySorted(r,2) < 0 % dataPreySorted(r,2) = NaN; % dataPreySorted(r,3) = NaN; % end % if dataPreySorted(r,3) < 0 % dataPreySorted(r,2) = NaN; % dataPreySorted(r,3) = NaN; % end % end %this allows you to only analyze the part of the strike with continuous %visibility %sframe = input('do you need to use only a subsection of the frames? No=0 Yes = start frame of frames you will use'); %if sframe > 0; % eframe = input('end frame'); %then turn this into a row % [rows1 cols1] = find(dataEyeSorted(:,1) == sframe); % [rowe1 cole1] = find(dataEyeSorted(:,1) == eframe); % dataEyeSorted = dataEyeSorted(rows1:rowe1,:); % [rows2 cols2] = find(dataUJSorted(:,1) == sframe); % [rowe2 cole2] = find(dataUJSorted(:,1) == eframe); % dataUJSorted = dataUJSorted(rows2:rowe2,:); % [rows3 cols3] = find(dataLJSorted(:,1) == sframe); % [rowe3 cole3] = find(dataLJSorted(:,1) == eframe); % dataLJSorted = dataLJSorted(rows3:rowe3,:); % [rows4 cols4] = find(dataPreySorted(:,1) == sframe); % [rowe4 cole4] = find(dataPreySorted(:,1) == eframe); % dataPreySorted = dataPreySorted(rows4:rowe4,:); %end %% Data Analysis u = length(dataPreySorted); %% Distance Prey Travelled preyx1 = dataPreySorted(1,2); preyy1 = dataPreySorted(1,3); preyx2 = dataPreySorted(u,2); preyy2 = dataPreySorted(u,3); rowf1 = u; preyDistTravelled = sqrt(((preyx1-preyx2)^2) + ((preyy1-preyy2)^2)); preyDistTravelled = preyDistTravelled/cal; %% Raw Velocity Analysis dispPrey = []; relDispPrey=[]; timeP = []; velTimeMat = []; velTimeMat2 = []; velPreyMat = []; for v = 2:u velTime1 = dataPreySorted(v,1) velTime2 = dataPreySorted(v-1,1) velTime = (velTime1-velTime2)/500 velTimeMat = [velTimeMat ; velTime] end for v = 2:u xPrey = dataPreySorted(v,2) yPrey = dataPreySorted(v,3) x2Prey = dataPreySorted(v-1,2) y2Prey = dataPreySorted(v-1,3) velTime1 = dataPreySorted(v,1) velTime2 = dataPreySorted(v-1,1) velTime = (velTime1-velTime2)/500 velTimeMat2 = [velTimeMat2 ; velTime] % prey dispPreyT = sqrt(((xPrey-x2Prey)^2) + ((yPrey-y2Prey)^2)) dispPreyT = dispPreyT/cal dispPrey = [dispPrey ; dispPreyT] velPrey = dispPreyT/velTime velPreyMat = [velPreyMat ; velPrey] end cumsumTime = cumsum(velTimeMat); dt=diff(cumsumTime) cumsumTime2 = cumsum(velTimeMat2); dt2 = diff(cumsumTime2) diffPreyVel = diff(velPreyMat); rawAccPrey = diffPreyVel./dt2; maxRawPreyVel = max(abs(velPreyMat)); maxRawPreyAcc = max(abs(rawAccPrey)); %% Fitted Velocity and Accelerations cumsumDispPrey = cumsum(dispPrey); % Prey [p,S,mu] = polyfit(cumsumTime2(:,1),cumsumDispPrey(:,1),10); fitVelPrey = polyval(p,cumsumTime2(:,1),[],mu); dFitVelPrey = diff(fitVelPrey); linVelPrey = dFitVelPrey./dt2; dvPrey = diff(linVelPrey); linAccPrey = dvPrey./dt2(2:end,1); % Find max fitted kinematic values maxFitVelPrey = max(linVelPrey); maxFitAccPrey = max(linAccPrey); % Graphs of Kinematics preyVelGraph=figure plot(cumsumTime2, velPreyMat, 'r') hold on plot(cumsumTime2(2:end), linVelPrey, 'b') title('Velocity of Prey, smoothed (blue) and raw (red)') namePV = [species matrixlabel2 matrixlabel3 'Velocity Prey' '.jpg']; saveas(preyVelGraph,namePV) preyAccGraph=figure plot(cumsumTime2(2:end), rawAccPrey, 'r') hold on plot(cumsumTime2(2:end-1), linAccPrey, 'b') title('Acceleration of Prey, smoothed (blue) and raw (red)') namePA = [species matrixlabel2 matrixlabel3 'Acceleration Prey' '.jpg']; saveas(preyAccGraph,namePA) %% Save it all name1 = [species matrixlabel2 matrixlabel3 'analyzed_prey_data' '.mat']; allData = [preyDistTravelled,maxRawPreyVel,maxRawPreyAcc,maxFitVelPrey,maxFitAccPrey] save(name1, 'preyDistTravelled', 'maxRawPreyVel', 'maxRawPreyAcc', 'maxFitVelPrey', 'maxFitAccPrey'); %% Directory of important Variables % rooow : The row # in the spreadsheet of the video currently being analyzed % data____ : the x and y coordinates of the specified body part over the % whole strike. Only change is to subtract 512 from the x and y values so % the origin is 0,0. Has all frames %data_____Sorted : data_____ sorted by the frame number. It is then % cut to include only the frames that you want. %time : Vector starting at zero, increasing by one up to the last time % point. %timeSec : Time in units of seconds, not frames. %timeCap : Time of capture corrected for time 0. %timeCapSec : Time of capture (corrected for time 0) in units of seconds. %preyDist : Distance from the eye to the prey at time 0. If the prey is not % visible, it is recorded with a value of -1 %preyVelTime : Matrix of time and velocity of prey %eyeVelTime : Matrix of time and velocity of fishes eye %relPreyVelTime : Matrix of time and velocity of prey in frame of reference % of fish %preyDispTime : Matrix of time and displacement of prey %eyeDispTime : Matrix of time and displacement of fishes eye %relPreyDispTime : Matrix of time and displacement of prey in frame of reference % of fish %velTime : matrix of time that pairs with velocity data. Is in units of % seconds. %maxRaw_____Vel : Maximum raw velocity of the given point. %protrusion : List of the protrusion at each frame %maxProt : The maximum protrusion during the strike in meters %diffProt : difference between maximum protrusion and protrusion at time % zero. Acts as a maximum protrusion corrected for initial protrusion. % NOT CORRECTED FOR FISH SIZE. %preyDistTravelled : Distance that the prey travelled from time zero until % the fish either captured it or until the end of the strike. (I should % standardize the end of the strike). %eyeDistTravelled : same but for the eye of the fish. %RSI : Ram suction index. (Xpred-Xprey)/(Xpred+Xprey).