Using datenum to calculate acceleration
조회 수: 7 (최근 30일)
이전 댓글 표시
Good Morning all,
I have a CSV-based data set that is created at approximately 1Hz - but not always perfect, and I think I'd like to maintain that precision, as it is a highly accurate satellite clock. In any event, the CSV has many columns, but I only have a few of interest, namely velocity. I've used readtable to obtain the columns, and with an for-loop, would like to calculate the change in velocity (Vdiff) between each sensor output. Currently, I'm converting a datetime to datenum array, subtracting the two (D), and using the seconds(D) function to convert to the exact time elapsed between data points. Eventually I'd like to run this over hundreds of days of data, totalling multiple millions of data points - is there a better way to compute seconds for each calculation? Using datetime directly outputs a "duration" type variable, which I can't use to divide into Vdiff.
댓글 수: 0
답변 (2개)
Star Strider
2021년 5월 25일
I’m not certain that I understand in detail what you’re doing.
Using datenum is likely the best option here. I experimented with caldiff (using synthetic data) and was not able to get a satisfactory result of the sort you probably need, although with your data it may work.
The diff function has the disadvantage of shortening the output by one element, since it takes successive differences. One alternative is to compute the numeric derivative with the gradient function, and to get the acceleration from the velocity and time vectors, use them together as:
dVdt = gradient(velocity) ./ gradient(time);
The calculation you choose entirely depends on the result you want.
댓글 수: 2
Star Strider
2021년 5월 25일
편집: Star Strider
2021년 5월 25일
My pleasure!
The datenum function outputs in days and decimal fractions of days (out to whatever tthe precision is, for example milliseconds), so it is likely not necessary to perform the conversion to seconds (and decimal fractions of them) except at the end of the calculation. I believe that datetime variables can go to microseconds at least, although that obviously depends on the resolution of the time measurement itself.
I’m not certain that there is one ‘correct’ way to do this, however converting back to datetime to store summary data may offer some advantages, although for calculations of the sort you’re doing, it’s not going to be efficient, even it there turns out to be a way to do it (that I haven’t discovered yet).
EDIT — (25 May 2021 at 16:26)
In the interim, I figured out how to do that only using datetime variables and functions —
DT = datetime('now')+seconds(sort(rand(2000,1)*100000)) % Create datetime’ Array
dDT = caldiff(DT,'time') % Calculate Differences
tsDT = seconds(time(dDT)) % Get Seconds
It also calculates the second differences correctly so that it doesn’t start over with a new day (after midnight). The differences are correct.
.
Steven Lord
2021년 5월 25일
Using datetime directly outputs a "duration" type variable, which I can't use to divide into Vdiff.
That's true, but you don't need to convert to a datenum.
format longg
dt1 = datetime('now')
P = 5*rand
dt2 = dt1 + seconds(P)
difference = dt2 - dt1
differenceInSeconds = seconds(difference)
check = differenceInSeconds - P
differenceInSeconds matches P. Of course this example is "synthetic" in that I knew P ahead of time and used it to create dt2. But if you started with dt1 and dt2 you could find P aka differenceInSeconds and you can divide by differenceInSeconds.
댓글 수: 2
Steven Lord
2021년 5월 25일
Do you need a loop or can you operate on the vectors of data at once?
x = randi([-10 10], 10, 1)
dt = datetime('now') + seconds(x)
seconds(diff(dt))
diff(x)
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!