title: Extract binary function from stochastic data in R
body: <p>I have power data from a cycling power meter (this is only required for context, not for the mechanics of the problem) which looks somewhat stochastic. See attached image. <a href="https://i.stack.imgur.com/v914B.jpg" rel="nofollow noreferrer">power_image</a></p>
  <p>This is a typical session which is based on a set of target 'levels' which are straight line targets of the power output required (so would look like a series of blocks with linear lines between them). The actual realised/achieved power output is naturally messy around this.</p>
  <p>What I am after is extracting the target session from the messy power data (as I don't have access to this). It will essentially look like binary waves (straight level lines), though as you can see from the attached image, sometimes the power target is upward sloping.</p>
  <p>A further complication is that for some of the other sessions the target effort blocks may be of short duration.</p>
  <p>I have looked at various things to achieve this (wavelets etc...) but can't seem to find anything obvious...I am using R.</p>
  <p>Any thoughts and help is greatly appreciated.</p>