Godtone
Joined 17 December 2020
→My Python 3 code: added circular mean and step_err |
|||
| Line 887: | Line 887: | ||
x = iv(neji[i+1],neji[i]) | x = iv(neji[i+1],neji[i]) | ||
print(striv( x, map_iv(v,x) )) | print(striv( x, map_iv(v,x) )) | ||
def avg(vs,weights=None): | |||
if not weights: | |||
weights=[ 1.0 for v in vs ] | |||
return [ sum([ vs[vi][i]*weights[vi] for vi in range(len(vs)) ])/sum(weights) for i in range(len(vs[0])) ] | |||
def length(v): | |||
return sum([ c**2 for c in v ])**(1/2) | |||
def turns(v): # assumes v is 2D vector; turns expressed as unit up being 0 turns and unit left being -1/2 turns | |||
return math.atan2(v[0],v[1]) | |||
# [0,1] (unit up) is exactly in-tune with et2; note the below: | |||
# mean = turns(result)/math.tau, variance = 1 - length(result), badness = length(result) | |||
def circular_mean(et2,harmonics,weights=None): | |||
herr=[ (h,step_err(h,et2)) for h in harmonics ] | |||
return avg([ [math.sin(math.tau*e[1]),math.cos(math.tau*e[1])] for e in herr ],weights) | |||
def circular_normalise(e,m): # into range -1/2, +1/2 | |||
return (e-m)%1 if (e-m)%1 < 0.5 else (e-m)%1 - 1 | |||
def circular_step_err(x,et2,m): | |||
return circular_normalise(step_err(x,et2),m) | |||
</syntaxhighlight> | </syntaxhighlight> | ||