Forward, backward and central differences
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Bruno Luong
2019-8-28
编辑:Bruno Luong
2019-8-28
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 个评论
Bruno Luong
2019-8-28
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.
更多回答(1 个)
Adam Danz
2019-8-27
编辑:Adam Danz
2019-8-27
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!