Skip to content

Instantly share code, notes, and snippets.

@mgmarino
Forked from stefan789/allan.py
Last active September 10, 2015 12:30
Show Gist options
  • Save mgmarino/7b3c44ff4b94ccd97daa to your computer and use it in GitHub Desktop.
Save mgmarino/7b3c44ff4b94ccd97daa to your computer and use it in GitHub Desktop.
allan deviaton
import numpy as np
def allanVar(data, sf=1):
'''
calculates allan variance
according to eq 8.13a in
David W. Allan, John H. Shoaf and Donald Halford: Statistics of Time and Frequency Data Analysis,
NBS Monograph 140, pages 151–204, 1974
'''
nd = np.array(data)
# list of integration lengths
# Note, this avoids using the full length!
integration_lengths = np.unique(len(nd)/np.arange(1, len(nd)+1))[:-1]
av = []
for anint in integration_lengths:
# Shorten array if necessary
shorten_by = len(nd) % anint
thend = nd
if shorten_by != 0:
thend = nd[:-shorten_by]
# reshape and calculate mean
x = thend.reshape(-1, anint).mean(axis=1)
# Save the deviation
av.append([anint*sf, ((x[1:] - x[:-1])**2).mean()/2])
return np.array(av)
def allanDev(data, sf=1):
alv = allanVar(data, sf)
alv[:,1] = np.sqrt(alv[:,1])
return alv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment