the C-code output is already 0....359, positive range.
but if we are then filtering multiple outputs, that cross the 359-0-1 degree wraparound point, then we need to account for the wraparound.
eg let's say that the variativeness is half what you're seeing, ie +/- 10% = +/- 36 degrees,
and we have 5 readings to filter being
342 +/- 36 degrees ie 306, 324, 342, 0, 18 degrees, then:
1/ a simple average of those would be (306 + 324 + 342 + 0 + 18) / 5 = 990 / 5 = "average" bearing of 198 degrees which is obviously nowhere near correct
2/ the median is 306 which is off by 36 degrees
3/ the minimum is 0 which is off by 18 degrees
whereas the filter in this post should work:
first draft of an EMA that handles this
lol, you're going to make me actually test it, aren't you? ? not that I'm complaining, because you've highlighted an unexpectedly interesting puzzle ?