Menu

#2884 smooth unwrap starts in the negative if initial value is > pi

open
nobody
None
2026-08-17
2026-08-04
Anonymous
No

The smoothing should probably subtract or add 2pi if the first value is outside of [0:2pi].

Current behaviour is apparently that the previous point, used to evaluate if an unwrapping is in order, gets initialised to zero (interpol.c:887 : lasty = 0), maybe it's enough to initialise it to pi. Or just skip the smoothing operation for the first datapoint.

Example:

set xr [0:30]
set table $dat                                                       
plot '+' us 1:(fmod(4+(-sin(x)-x),(2*pi))) with table  
unset table                                                          
plot $dat smooth unwrap

While on the subject: could smooth unwrap be extended to have a "period" specifier like kdensity? Default to 2pi, but could be 246060 seconds, or whatever? That'd also solve the issue here https://sourceforge.net/p/gnuplot/patches/799/ without the user being forced to use the global "set angle degrees".
Whenever the value changes by period/2, period gets added or subtracted.

Discussion

  • Karl Ratzsch

    Karl Ratzsch - 2026-08-06

    The first datapoint should just be kept as-is, because there is no knowing in advance what the data range actually is, e.g. +-pi or 0 to 2pi. And if somebody has data that goes from 5pi to 7pi, they probably have some extra knowledge the plot should preserve. ;)

    Attached the two-liner fix.

    I was slightly confused about the while loop below the change, I think it can be removed and always run exactly once. If there is a jump larger than 3pi in some data, then smooth unwrap is obviously not the right treatment.

    Test:

    set xr [0:10]
    set samp 400
    mod(a,b) = a - b * floor(a / b)
    testf(x) = mod(1.9*pi*cos(x),2*pi)-pi
    plot testf(x),  '+' us 1:(testf(x)) smooth unwrap
    

    The test shows another bug: if no yrange is set, the unwrapped plot leaks out of the graph borders.

     
  • Karl Ratzsch

    Karl Ratzsch - 2026-08-17

    On a second look, the behaviour is exactly what is advertised in the help

    The smooth unwrap option modifies the input data so that any two successive
    points will not differ by more than pi; a point whose y value is outside this
    range will be incremented or decremented by multiples of 2pi until it falls
    within pi of the previous point. This operation is useful for making wrapped
    phase measurements continuous over time.

    Perhaps I'm missing something here. What kind of raw angular data eligible for unwrapping could have two points that differ by more than 2pi?

     

Log in to post a comment.