function [v,t_run,t_tumb,theta]=getdata(coord,frame_rate,v_thres,um)

% Function to derive run velocity, tumbling angle, run and tumbling
% duration from x,y coordinates.

% clear all;
% load('/matlab/IGEM/Workbook2.dat');
% coordinates=transpose(Workbook2);

%Initialise Parameters
v_x=zeros(length(coord)-1,1);
v_y=zeros(length(coord)-1,1);
v=zeros(length(coord)-1,1);
tumb_dur=zeros(length(coord),1);
run_dur=zeros(length(coord),1);
theta=zeros(length(coord)-2,1);

run_count=0; 
tumb_count=0;
i=1;
j=1;

for n=1:length(coord)-1  % number of frames 
    v_x(n)=(coord(n+1,1)-coord(n,1))*frame_rate*um;  % instanteneous velocity in µm/s
    v_y(n)=(coord(n+1,2)-coord(n,2))*frame_rate*um; 
    v(n)=sqrt((v_x(n))^2+(v_y(n))^2);
    %Considering bacteria in a run state
    if v(n)>v_thres;
        run_count=run_count+1;
        tumb_dur(j)=tumb_count/frame_rate;
        tumb_count=0;
        j=j+1;
    else
        tumb_count=tumb_count+1;
        run_dur(i)=run_count/frame_rate;
        run_count=0;
        i=i+1;
    end;
    %theta(n)=atan((coord(n+1,2)-coord(n,2))/(coord(n+1,1)-coord(n,1)))- ...
    %    atan((coord(n,2)-coord(n-1,2))/(coord(n,1)-coord(n-1,1)));
end;

for n=2:length(coord)-1
    A=sqrt( (coord(n,1)-coord(n-1,1))^2 + (coord(n,2)-coord(n-1,2))^2 );
    B=sqrt( (coord(n+1,1)-coord(n,1))^2 + (coord(n+1,2)-coord(n,2))^2 );
    C=sqrt( (coord(n+1,1)-coord(n-1,1))^2 + (coord(n+1,2)-coord(n-1,2))^2 );
    theta(n-1)=acos((B^2+A^2-C^2)/(2*A*B));
    if theta(n-1)>(pi/2)
        theta(n-1)=pi-theta(n-1);
    end;
end;

temp1=find(run_dur);
temp2=find(tumb_dur);

t_run=zeros(length(temp1));
t_tumb=zeros(length(temp2));

for i=1:length(temp1)
    t_run(i)=run_dur(temp1(i));
end;

for i=1:length(temp2);
    t_tumb(i)=tumb_dur(temp2(i));
end;