Creating a duration loses nanosecond precision

조회 수: 5 (최근 30일)
mura0087
mura0087 2021년 12월 8일
댓글: mura0087 2021년 12월 9일
Hi- I have the signal output of an electronic device, voltage, with respect to time, nanoseconds. I would like to get it into a timetable so that I can use signal processing toolbox functions. The problem is, I am losing precision in the time values when making it into a time table. When this happens, I get duplicate time values with unique voltage values. I have attached a sample csv with data and pasted my code below.
A=readmatrix('D60ALT_MLF.csv');
time=(A(:,1)); %nanoseconds
volts=(A(:,2)); %millivolts
%makes a duration, but has the wrong units
time_sec=seconds(time);
%this one has the right units, but I lost precision
time_sec2=seconds(time)/(10^9);
%loss of precision results in repeated times with unique voltage values
%when I assemble the timetable. Signal processing toolbox gets upset.
TT_demo=array2timetable(volts,"RowTimes",time_sec2);
%how do I get time into a "duration" datatype, without losing precision?
  댓글 수: 4
dpb
dpb 2021년 12월 8일
Your problem is that your data file loses precision, not with a duration...
....
99981442983,2797,0
99981449233,2836,0
1.00E+11,2788,0
1.00E+11,2773,0
1.00E+11,2812,0
1.00E+11,2788,0
...
Unless you saved the original data in another format, you've lost the time detail. The above is at record 19,228 out of 154,195 total records.
But, if you have the raw data still, or can regenerate it, the timestamp variable is sufficiently small to be able to be represented in a double which has ~15 decimal digits so you shouldn't have any trouble with duration and a timetable.
The other thread got into trouble because was trying to put an absolute time together that include the actual date and time and hold the nanosecond precision. That failed owing to internals in the datetime implementation that converted to double internally which, for the given case, did not have sufficient precision.
Your problem (at least in the attached file) is that the csv file was written without specifying the precision for all values and when passed E10, it reverted to a default short form to save disk space. That's a little rude, but is documented behavior; to save full precision a text file isn't that great a choice to begin with.
ttmura=readtable('D60ALT_MLF.csv','ReadVariableNames',1);
ttmura.TIMESTAMP=seconds(ttmura.TIMESTAMP/1E9);
ttmura=table2timetable(ttmura,"RowTimes","TIMESTAMP");
>> [head(ttmura); tail(ttmura)]
ans =
16×2 timetable
TIMESTAMP CH1 CH2
______________ ____ ___
0.00086047 sec 2827 0
0.0075338 sec 2836 0
0.0075461 sec 2768 0
0.039702 sec 2792 0
0.039716 sec 2753 0
0.039718 sec 2783 0
0.03972 sec 2846 0
0.040655 sec 2827 0
801 sec 2705 0
801 sec 2744 0
801 sec 2705 0
801 sec 2695 0
801 sec 2661 0
801 sec 2744 0
801 sec 2690 0
801 sec 2695 0
>>
The timestep doesn't seem to be at all uniform at the beginning of the file...
>> tmp=head(ttmura); diff(tmp.TIMESTAMP)
ans =
7×1 duration array
0.0066733 sec
1.2344e-05 sec
0.032156 sec
1.375e-05 sec
2.5e-06 sec
2.187e-06 sec
0.00093443 sec
>>
mura0087
mura0087 2021년 12월 9일
Thank you! This explains it, I found out the file was opened and saved as in Microsoft excel before I got my hands on it.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by