Fixing data set with if/else, how do I index/indicate this properly?
1 回表示 (過去 30 日間)
古いコメントを表示
I have a data set of seconds (plotted image below).
Rather than consistently increasing, they are dropping down and resetting. I am trying to write something that fixes this so the end result will be them spaced as they are now but in a consistently increasing line. I think I want to indicate that each time a value v is lower than the previous (v-1), add the difference between the two to v and keep going. I am new to Matlab and started to write something like this:
for v=1:length(Seconds)
if (Seconds(v+1) <= Seconds(v))
Seconds(v+1) = Sec
onds(v+1)+(Seconds(v)-Seconds(v+1));
else
(Seconds(v+1)) = Seconds(v+1);
end
end
How do I properly assign the new values? Thank you
4 件のコメント
dpb
2018 年 10 月 31 日
Yeah, shorten the file to the first one (or at most two) cycles and show us what you think the answer would be to go with it...afaict, what you've written about is what cumsum does.
採用された回答
dpb
2018 年 10 月 31 日
編集済み: dpb
2018 年 11 月 1 日
OK, to increment every element from the previous by its absolute matnitude change is I guess what you're asking to do...
yy=cumsum([y(1);abs(diff(y(:)))]);
In this, then,
sum(diff(yy)<0) --> 0;
every point is the previous plus the change from the previous as if it were still growing rather than being cyclic; or, iow, as if you had rectified the signal.
NB: IF the data are a column you don't need the (:); I used it to ensure knew the orientation of the result from diff to do the concatenation with the first element.
ADDENDUM
W/ the revision of what the "reset" really is (I had presumed a sampled waveform, not the ramp)...
dy=diff(y); % get the differences
ibrk=find(dy<0); % find the breakpoints
y(ibrk+1)=y(ibrk+1)+abs(dy(ibrk)); % fix up those points to match previous
dy(ibrk)=0; % now eliminate the reset offset
yy=cumsum([y(1); dy]); % build the ramp
This ignores the ramp between the end of one cycle and beginning of next--those two points are same. If you don't want that plateau, you can estimate a slope and introduce an estimated dy at each break instead of setting to zero to cancel the reset.
8 件のコメント
dpb
2018 年 11 月 1 日
Ah! That's a completely different form than I had presumed--that's a periodic reset ramp, not a sampled waveform...the problem w/ too many points on the plot not showing the actual point locations, drew wrong conclusion.
See ADDENDUM above...
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Waveform Generation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!