Regular time intervals of time series without interpolation
4 ビュー (過去 30 日間)
古いコメントを表示
Hello, I have the data of a following time series :
Time(s) Value
0 12 15
0.14 8
0.15 10
0.19 11
1.2 12
1.3 13
2.4 14
2.8 17
3.1 15
3.5 25
4.1 14
4.4 20
5.8 11
5.9 6
I want to get these data on regular time intervals (each 1 s) without interpolation. How I can achieve this task under matlab?
0 件のコメント
採用された回答
Walter Roberson
2017 年 3 月 19 日
You do not have a regular csv file. You need to read the data in first:
filecontent = fileread('Book1.csv');
data = str2double(regexp(regexprep(filecontent,{',', ';'},{'.', ','}), '[,\r\n]+', 'split'));
if mod(length(data),2) ~= 0; data(end) = []; end %a trailing empty line will have become a NaN, get rid of it
data = reshape(data,2,[]).';
Now, if you have R2016b or later:
tt = timetable(data(:,2), 'RowTimes', seconds(data(:,1)));
After that you can
retime(tt,'secondly', 'mean')
or
retime(tt,'secondly', 'sum')
as appropriate for your needs.
3 件のコメント
Walter Roberson
2017 年 3 月 20 日
編集済み: Walter Roberson
2017 年 3 月 20 日
timetables do not exist in R2015a, and retime() does not exist either.
Do you want the sum for every second, or do you want the mean for every second, or something else?
In the attached .mat, the variable tmean is the average of each 1 second interval for the Book2 data, and the variable tsum is the total of each 1 second's values.
その他の回答 (1 件)
John D'Errico
2017 年 3 月 19 日
編集済み: John D'Errico
2017 年 3 月 19 日
You cannot do so.
You want to interpolate a set of points. But you refuse to use interpolation? Sorry, but that makes no sense at all. I suppose we can make up some other name for the process, even though it still does the same thing as interpolation. I think you need to use magic here. Sadly, magic is already used to generate magic squares of order n. :)
Seriously, however you want to name the problem, you still need to interpolate the data.
Perhaps you were told you cannot use the interpolation tools in MATLAB like spline or interp1. You still need to interpolate. A rose by any other name would smell as sweet.
Just do linear interpolation. This is very clearly homework. I will not do your homework, as that is there for you to do, and you will learn by so doing.
You can easily find the formula for linear interpolation online. Google is your friend. Personally, I'd just search for the term "interpolation wiki" that will pop up this site on top of the list. In there you will find linear interpolation described. Or, you can be more demanding and search for a "linear interpolation algorithm". There are lots of hits for that online too, but the algorithm is pretty basic.
A simple loop will suffice, although vectorized solutions are not that difficult. For homework, I'd suggest that you don't bother with the vectorization unless your MATLAB skills are up to the task. For each point in question, you locate the pair of points on the real line (time) that bracket the point to be interpolated. (Oh dear, I used the bad word!) Then apply the linear interpolation formula that you have found.
3 件のコメント
per isakson
2017 年 3 月 19 日
編集済み: per isakson
2017 年 3 月 19 日
"are larger, so I think an interpolation is not the right solution"   To me that sounds strange. Please, elaborate.
John D'Errico
2017 年 3 月 20 日
But we cannot know why you have decided that interpolation is not meaningful here. Why do you think that the numbers being "larger" should imply that interpolation is right or not?
As well, if you know something about the data that you have not bothered to tell us, then how can we know how to solve the problem?
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!