MATLAB Answers

Tomer
0

Forward, backward and central differences

Asked by Tomer
on 27 Aug 2019
Latest activity Edited by Adam Danz
on 2 Sep 2019
Hi, I am a biology student and new to MATLAB. I am working on a project to study the behavior of small aquatic fishes. I am have captured a video of 2000 frames and the time difference between frames is 1/500 sec. I have a software which gives me the positions of the fishes. I have a MAT file which contains the data of the centroid positions of the fishes. The structure array looks like below.
Fish(1).frames=[10, 11,12,13,14];
Fish(1).position1=[20.5, 20.9, 21.3, 21.7,22.1];
Fish(1).position2=[4.1, 4.27, 4.53, 4.79,4.88];
Fish(2).frames=[1,2,3,4,5,6,7,8,9];
Fish(2).position1=[10.4,10.6,10.7,10.8,11.1, 11.47,11.82,12.31,12.44];
Fish(2).position2=[4.2, 4.7, 4.8, 4.9,5.2,5.28,5.63,5.89,6.01];
The size of the structure is 30,000 X 1. I want to calculate the velocity of the fishes based on position1 (velocity1) and position2(velocity2). I want to apply forward and backward differences to the end data points and central difference to the inside data. I don't know how to do this. Please, help me.

  0 Comments

Sign in to comment.

2 Answers

Answer by Bruno Luong
on 28 Aug 2019
Edited by Bruno Luong
on 28 Aug 2019
 Accepted Answer

dt=1/500;
for k=1:length(Fish)
Fish(k).velocity1 = gradient(Fish(k).position1,dt);
Fish(k).velocity2 = gradient(Fish(k).position2,dt);
end

  3 Comments

Thank you.
In the gradient doc it states:
Algorithms
gradient calculates the central difference for interior data points. For example, consider a matrix with unit-spaced data, A, that has horizontal gradient G = gradient(A). The interior gradient values, G(:,j), are
G(:,j) = 0.5*(A(:,j+1) - A(:,j-1));
The subscript j varies between 2 and N-1, with N = size(A,2).
gradient calculates values along the edges of the matrix with single-sided differences:
G(:,1) = A(:,2) - A(:,1);
G(:,N) = A(:,N) - A(:,N-1);
If you specify the point spacing, then gradient scales the differences appropriately. If you specify two or more outputs, then the function also calculates differences along other dimensions in a similar manner. Unlike the diff function, gradient returns an array with the same number of elements as the input.
Thanks.

Sign in to comment.


Answer by Adam Danz
on 27 Aug 2019
Edited by Adam Danz
on 27 Aug 2019

Assuming the camera is looking down on the fish, since you do not have position 3 (vertical position within the water) you can only compute the x and y velocity components. This could result in an underestimation of velocity. Imagine a fish is swiming quickly in an upward and forward direction. As the angle from the ground increases toward 90 degrees, the forward component becomes smaller relative to the vertical component even though the fish is accelerating. Some systems include a 2nd camera to capture the vertical movement as well in order to get around that problem.
Nevertheless, the solution to your task can be broken down into a few manageable parts.
First, given the (x,y) coordinates and assuming no change in z, you'll need to compute the distance moved between coordinates. So if you have n coordinates, you'll have n-1 distances. Hint: distance formula.
Second, you'll need to compute the time between each position datapoint. That should be easy because you have a fixed frame rate and you have frame counts. Again, if you have n timestamps, you'll end up with n-1 durations.
Now you have a vector of distances and a vector of times. Lastly, velocity is just distance divided by time.
That should get you started. Feel free to followup if you get stuck.

  8 Comments

No, the above gives total velocity. If you want the x and y components of velocity, you'll need to adapt the code above. The distance calculation is simplified to
distance_x = Fish(i).position1(2:end) - Fish(i).position1(1:end-1) ;
distance_y = Fish(i).position2(2:end) - Fish(i).position2(1:end-1) ;
% time = same thing as above
Fish(i).velocity_x = distance_x ./ time;
% same for velocity_y....
Again, velocity is just distance divided by time. That's why velocity is reported in units such as miles per hour (miles is distance, divided by time, hours). If you want x and y velocities, you need x and y distances which are just the different between each x and y position value. Taking some time to understand that will be beneficial to the rest of your analysis.
Thanks.
No problem.
Feel free to come back and check in if you get stuck.
When you get a chance, you might want to accept the answers that were helpful to you. Here are a list of your questions:

Sign in to comment.