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

1 view (last 30 days)
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 Comments
dpb
dpb on 31 Oct 2018
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 on 31 Oct 2018
Edited: L on 31 Oct 2018
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)

Sign in to comment.

Accepted Answer

dpb
dpb on 31 Oct 2018
Edited: dpb on 1 Nov 2018
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 Comments
dpb
dpb on 1 Nov 2018
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...

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!