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.
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:
The test shows another bug: if no yrange is set, the unwrapped plot leaks out of the graph borders.
On a second look, the behaviour is exactly what is advertised in the help
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?