Using datenum to calculate acceleration
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (2)
Star Strider
il 25 Mag 2021
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 Commenti
Star Strider
il 25 Mag 2021
Modificato: Star Strider
il 25 Mag 2021
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
il 25 Mag 2021
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 Commenti
Steven Lord
il 25 Mag 2021
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)
Vedere anche
Categorie
Scopri di più su Dates and Time in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!