Fixing data set with if/else, how do I index/indicate this properly?

1 回表示 (過去 30 日間)
L
L 2018 年 10 月 31 日
コメント済み: L 2018 年 11 月 1 日
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
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.
L
L 2018 年 10 月 31 日
編集済み: L 2018 年 10 月 31 日
Thanks for the follow up. I've read more about cumsum and if I take, for example, this point marked in the image below, I can understand that I would need to add all the previous values to "stack" it. Because the first line of data is correct, though, I would need to exclude it from cumsum calculations until I need it for the start of the second line set of data, correct? (I want my data points to stay as they are until they drop back down to 0 for the first time)

サインインしてコメントする。

採用された回答

dpb
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
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...
L
L 2018 年 11 月 1 日
Thanks, this was great. I really appreciate the help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWaveform Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by