-
Notifications
You must be signed in to change notification settings - Fork 78
/
allantools.py
2189 lines (1786 loc) · 68.3 KB
/
allantools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Allan deviation tools
=====================
**Author:** Anders Wallin (anders.e.e.wallin "at" gmail.com)
Version history
---------------
**unreleased**
- ITU PRC, PRTC, ePRTC masks for TDEV and MTIE in new file mask.py
- psd2allan() - convert PSD to ADEV/MDEV
- GCODEV, Grosslambert deviation, an improved three-cornered-hat analysis
- PDEV, Parabolic deviation
**2019.09** 2019 September
- packaging changes, for conda package
(see https://anaconda.org/conda-forge/allantools)
**2019.07** 2019 August 3
- move edf-functions and noise-ID functions to ci.py
- mtotdev() htotdev() speed improvements
- save dataset results to text file
- real-time adev/mdev/hdev, in new file realtime.py
- travis testing on Linux, OSX, and Windows
**2018.03** 2018 March 27
- change license to LGPL v3 or later
- lag-1 autocorrelation noise identification function
- B1 noise identification
- R(n) noise identification
- Noise() class using Kasdin & Walter algorithm
- work on Greenhall's EDF and confidence intervals
- tests for confidence intervals
**2016.11** 2016 November 18
- Dataset class
- plotting with a Plot class
- confidence intervals based on Greenhall's EDF algorithm
- testing on multiple python versions with tox
- continuous integration with https://travis-ci.org/aewallin/allantools
- test coverage report on
https://coveralls.io/github/aewallin/allantools?branch=master
**2016.4** 2016 April 8
- convert tests to use pytest
- split tests into individual pytests, make them all pass
- accept a numpy.array as taus parameter.
- Switch to new signature (https://github.com/aewallin/allantools/issues/29)
- remove old and slow pure-python implementation
**2016.3** 2016 March
- improve documentation and add __version__
- added Theo1 deviation theo1()
- added Hadamard Total Deviatio htotdev()
- added Modified Total Deviation mtotdev(), and Time Total Deviation ttotdev()
http://www.anderswallin.net/2016/03/modified-total-deviation-in-allantools/
- automatic tau-lists: taus=[ "all" | "octave" | "decade" ]
- merge adev() and adev_phase() into one, requiring phase= or
frequency= argument
- add GPS dataset as example and test
**2016.2** 2016 February
- update release on PyPi https://pypi.python.org/pypi/AllanTools
- pytest and coverage
- setuptools
- change version number to year.month
**v1.2.1** 2015 July
- Python 3 compatibility using 2to3 tool, by kuzavas
- IPython notebook examples
- sphinx documentation, auto-built on readthedocs
**v1.2** 2014 November, Cantwell G. Carson conrtibuted:
- A gap-robust version of ADEV based on the paper by Sesia et al.
gradev_phase() and gradev()
- Improved uncertainty estimates: uncertainty_estimate()
This introduces a new dependency: scipy.stats.chi2()
**v1.1** 2014 August
- Danny Price converted the library to use numpy.
- many functions in allantools are now 100x faster than before.
- see http://www.anderswallin.net/2014/08/faster-allantools-with-numpy/
**v1.01** 2014 August
- PEP8 compliance improvements by Danny Price.
**v1.00** 2014 January, first version of allantools.
- see http://www.anderswallin.net/2014/01/allantools/
License
-------
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import json
import numpy as np
from scipy import interpolate # used in psd2allan()
from scipy.integrate import simps # used in psd2allan()
from . import ci # edf, confidence intervals
# Get version number from json metadata
pkginfo_path = os.path.join(os.path.dirname(__file__),
'allantools_info.json')
with open(pkginfo_path) as fp:
pkginfo = json.load(fp)
__version__ = pkginfo["version"]
def tdev(data, rate=1.0, data_type="phase", taus=None):
""" Time deviation.
Based on modified Allan variance.
.. math::
\\sigma^2_{TDEV}( \\tau ) = { \\tau^2 \\over 3 }
\\sigma^2_{MDEV}( \\tau )
Note that TDEV has a unit of seconds.
Parameters
----------
data: np.array
Input data. Provide either phase or frequency (fractional,
adimensional).
rate: float
The sampling rate for data, in Hz. Defaults to 1.0
data_type: {'phase', 'freq'}
Data type, i.e. phase or frequency. Defaults to "phase".
taus: np.array
Array of tau values, in seconds, for which to compute statistic.
Optionally set taus=["all"|"octave"|"decade"] for automatic
tau-list generation.
Returns
-------
(taus, tdev, tdev_error, ns): tuple
Tuple of values
taus: np.array
Tau values for which td computed
tdev: np.array
Computed time deviations (in seconds) for each tau value
tdev_errors: np.array
Time deviation errors
ns: np.array
Values of N used in mdev_phase()
References
----------
* http://en.wikipedia.org/wiki/Time_deviation
* NIST [SP1065]_ eqn (15), page 18.
"""
phase = input_to_phase(data, rate, data_type)
(taus, md, mde, ns) = mdev(phase, rate=rate, taus=taus)
td = taus * md / np.sqrt(3.0)
tde = td / np.sqrt(ns)
return taus, td, tde, ns
def mdev(data, rate=1.0, data_type="phase", taus=None):
""" Modified Allan deviation.
Used to distinguish between White and Flicker Phase Modulation.
.. math::
\\sigma^2_{MDEV}(m\\tau_0) = { 1 \\over 2 (m \\tau_0 )^2 (N-3m+1) }
\\sum_{j=1}^{N-3m+1} \\left[
\\sum_{i=j}^{j+m-1} {x}_{i+2m} - 2x_{i+m} + x_{i} \\right]^2
Parameters
----------
data: np.array
Input data. Provide either phase or frequency (fractional,
adimensional).
rate: float
The sampling rate for data, in Hz. Defaults to 1.0
data_type: {'phase', 'freq'}
Data type, i.e. phase or frequency. Defaults to "phase".
taus: np.array
Array of tau values, in seconds, for which to compute statistic.
Optionally set taus=["all"|"octave"|"decade"] for automatic
tau-list generation.
Returns
-------
(taus2, md, mde, ns): tuple
Tuple of values
taus2: np.array
Tau values for which td computed
md: np.array
Computed mdev for each tau value
mde: np.array
mdev errors
ns: np.array
Values of N used in each mdev calculation
References
----------
* NIST [SP1065]_ eqn (14), page 17.
* http://www.leapsecond.com/tools/adev_lib.c
"""
phase = input_to_phase(data, rate, data_type)
(phase, ms, taus_used) = tau_generator(phase, rate, taus=taus)
data, taus = np.array(phase), np.array(taus)
md = np.zeros_like(ms)
mderr = np.zeros_like(ms)
ns = np.zeros_like(ms)
# this is a 'loop-unrolled' algorithm following
# http://www.leapsecond.com/tools/adev_lib.c
for idx, m in enumerate(ms):
m = int(m) # without this we get: VisibleDeprecationWarning:
# using a non-integer number instead of an integer
# will result in an error in the future
tau = taus_used[idx]
# First loop sum
d0 = phase[0:m]
d1 = phase[m:2*m]
d2 = phase[2*m:3*m]
e = min(len(d0), len(d1), len(d2))
v = np.sum(d2[:e] - 2*d1[:e] + d0[:e])
s = v * v
# Second part of sum
d3 = phase[3*m:]
d2 = phase[2*m:]
d1 = phase[1*m:]
d0 = phase[0:]
e = min(len(d0), len(d1), len(d2), len(d3))
n = e + 1
v_arr = v + np.cumsum(d3[:e] - 3 * d2[:e] + 3 * d1[:e] - d0[:e])
s = s + np.sum(v_arr * v_arr)
s /= 2.0 * m * m * tau * tau * n
s = np.sqrt(s)
md[idx] = s
mderr[idx] = (s / np.sqrt(n))
ns[idx] = n
return remove_small_ns(taus_used, md, mderr, ns)
def adev(data, rate=1.0, data_type="phase", taus=None):
""" Allan deviation.
Classic - use only if required - relatively poor confidence.
.. math::
\\sigma^2_{ADEV}(\\tau) = { 1 \\over 2 \\tau^2 }
\\langle ( {x}_{n+2} - 2x_{n+1} + x_{n} )^2 \\rangle
= { 1 \\over 2 (N-2) \\tau^2 }
\\sum_{n=1}^{N-2} ( {x}_{n+2} - 2x_{n+1} + x_{n} )^2
where :math:`x_n` is the time-series of phase observations, spaced
by the measurement interval :math:`\\tau`, and with length :math:`N`.
Or alternatively calculated from a time-series of fractional frequency:
.. math::
\\sigma^{2}_{ADEV}(\\tau) = { 1 \\over 2 }
\\langle ( \\bar{y}_{n+1} - \\bar{y}_n )^2 \\rangle
where :math:`\\bar{y}_n` is the time-series of fractional frequency
at averaging time :math:`\\tau`
Parameters
----------
data: np.array
Input data. Provide either phase or frequency (fractional,
adimensional).
rate: float
The sampling rate for data, in Hz. Defaults to 1.0
data_type: {'phase', 'freq'}
Data type, i.e. phase or frequency. Defaults to "phase".
taus: np.array
Array of tau values, in seconds, for which to compute statistic.
Optionally set taus=["all"|"octave"|"decade"] for automatic
tau-list generation.
Returns
-------
(taus2, ad, ade, ns): tuple
Tuple of values
taus2: np.array
Tau values for which td computed
ad: np.array
Computed adev for each tau value
ade: np.array
adev errors
ns: np.array
Values of N used in each adev calculation
References
----------
* NIST [SP1065]_ eqn (6) and (7), pages 14 and 15.
* [wikipedia_adev]_
"""
phase = input_to_phase(data, rate, data_type)
(phase, m, taus_used) = tau_generator(phase, rate, taus)
ad = np.zeros_like(taus_used)
ade = np.zeros_like(taus_used)
adn = np.zeros_like(taus_used)
for idx, mj in enumerate(m): # loop through each tau value m(j)
(ad[idx], ade[idx], adn[idx]) = calc_adev_phase(phase, rate, mj, mj)
return remove_small_ns(taus_used, ad, ade, adn)
def calc_adev_phase(phase, rate, mj, stride):
""" Main algorithm for adev() (stride=mj) and oadev() (stride=1)
Parameters
----------
phase: np.array
Phase data in seconds.
rate: float
The sampling rate for phase or frequency, in Hz
mj: int
averaging factor, we evaluate at tau = m*tau0
stride: int
Size of stride
Returns
-------
(dev, deverr, n): tuple
Array of computed values.
Notes
-----
stride = mj for nonoverlapping Allan deviation
stride = 1 for overlapping Allan deviation
References
----------
* http://en.wikipedia.org/wiki/Allan_variance
* http://www.leapsecond.com/tools/adev_lib.c
* NIST [SP1065]_ eqn (7) and (11) page 16
"""
mj = int(mj)
stride = int(stride)
d2 = phase[2 * mj::stride]
d1 = phase[1 * mj::stride]
d0 = phase[::stride]
n = min(len(d0), len(d1), len(d2))
if n == 0:
RuntimeWarning("Data array length is too small: %i" % len(phase))
n = 1
v_arr = d2[:n] - 2 * d1[:n] + d0[:n]
s = np.sum(v_arr * v_arr)
dev = np.sqrt(s / (2.0*n)) / mj*rate
deverr = dev / np.sqrt(n)
return dev, deverr, n
def pdev(data, rate=1.0, data_type="phase", taus=None):
""" Parabolic deviation.
Use for evaluating uncertainty of omega-average of frequency.
.. math::
\\sigma^2_{PDEV}(m\\tau_0) = { 72 \\over (N-2m) (m \\tau_0 )^2 }
\\sum_{i=0}^{N-2m-1} \\left[
\\sum_{k=0}^{m-1} \\left( { m-1 \\over 2} - k \\right) {x}_{i+k} - x_{i+k+m} \\right]^2
for :math:`m>1` and for an averaging-factor of :math:`m=1` PDEV equals ADEV/MDEV: :math:`\\sigma_{PDEV}(\\tau_0)=\\sigma_{ADEV}(\\tau_0)`.
Parameters
----------
data: np.array
Input data. Provide either phase or frequency (fractional,
adimensional).
rate: float
The sampling rate for data, in Hz. Defaults to 1.0
data_type: {'phase', 'freq'}
Data type, i.e. phase or frequency. Defaults to "phase".
taus: np.array
Array of tau values, in seconds, for which to compute statistic.
Optionally set taus=["all"|"octave"|"decade"] for automatic
tau-list generation.
Returns
-------
(taus2, ad, ade, ns): tuple
Tuple of values
taus2: np.array
Tau values for which td computed
ad: np.array
Computed adev for each tau value
ade: np.array
adev errors
ns: np.array
Values of N used in each adev calculation
References
----------
* [Vernotte2020]_
* [Vernotte2015]_
"""
phase = input_to_phase(data, rate, data_type)
(phase, m, taus_used) = tau_generator(phase, rate, taus)
ad = np.zeros_like(taus_used)
ade = np.zeros_like(taus_used)
adn = np.zeros_like(taus_used)
for idx, mj in enumerate(m): # loop through each tau value m(j)
(ad[idx], ade[idx], adn[idx]) = calc_pdev_phase(phase, rate, mj)
return remove_small_ns(taus_used, ad, ade, adn)
def calc_pdev_phase(phase, rate, mj):
""" Parabolic deviation
Parameters
----------
phase: np.array
Phase data in seconds.
rate: float
The sampling rate for phase or frequency, in Hz
mj: int
M index value for stride
Returns
-------
(dev, deverr, n): tuple
Array of computed values.
References
----------
* [Vernotte2020]_
* [Vernotte2015]_
"""
mj = int(mj)
stride = int(1)
tau0 = 1.0/rate
if mj == 1: # same as OADEV
d2 = phase[2 * mj::stride]
d1 = phase[1 * mj::stride]
d0 = phase[::stride]
n = min(len(d0), len(d1), len(d2))
if n == 0:
RuntimeWarning("Data array length is too small: %i" % len(phase))
n = 1
v_arr = d2[:n] - 2 * d1[:n] + d0[:n]
s = np.sum(v_arr * v_arr)
dev = np.sqrt(s / (2.0*n)) / mj*rate
deverr = dev / np.sqrt(n)
else:
N = len(phase) # number of frequency samples
M = N-2*mj # Vernotte2020 has the correct(?) M = N - 2m
# Vernotte2015 has M = N-2m+2 which seems wrong, we get index out-of-bounds in the sum
if M<1:
return 0, 0, 0
Msum=0
Mi=0
for i in range(0,M): # 0..M-1
asum=0
krange = np.linspace(0,mj-1,mj)
asum = sum( ((mj-1.0)/2.0 - krange ) * (phase[i:i+mj] - phase[i+mj:i+2*mj]) )
Msum=Msum + pow(asum, 2)
Mi=Mi+1
dev = np.sqrt( 72*Msum / ((M)*pow(mj,4)*pow(mj*tau0,2 )) )
deverr = dev / np.sqrt(M)
n=M
return dev, deverr, n
def calc_gcodev_phase(phase_1, phase_2, rate, mj, stride):
"""
Main algorithm for the Groslambert codeviation (see arXiv:1904.05849)
Parameters
----------
phase_1 : np.array
Phase data of oscillator 1.
phase_2 : np.array
Phase data of oscillator 2.
mj: int
M index value for stride.
stride: int
Size of stride.
Returns
-------
(dev, deverr, n): tuple
Array of computed values.
stride = mj for nonoverlapping Allan deviation
stride = 1 for overlapping Allan deviation (used for GCODEV by default)
"""
mj = int(mj)
stride = int(stride)
d2_1 = phase_1[2 * mj::stride]
d1_1 = phase_1[1 * mj::stride]
d0_1 = phase_1[::stride]
d2_2 = phase_2[2 * mj::stride]
d1_2 = phase_2[1 * mj::stride]
d0_2 = phase_2[::stride]
n_1 = min(len(d0_1), len(d1_1), len(d2_1))
n_2 = min(len(d0_2), len(d1_2), len(d2_2))
if n_1 == 0:
RuntimeWarning("Data array length is too small: %i" % len(phase_1))
n_1 = 1
n_2 = 1
v_arr_1 = d2_1[:n_1] - 2 * d1_1[:n_1] + d0_1[:n_1]
v_arr_2 = d2_2[:n_2] - 2 * d1_2[:n_2] + d0_2[:n_2]
s = np.sum(v_arr_1 * v_arr_2)
# Result can be negative
if(s >= 0):
dev = np.sqrt(s / (2.0*n_1)) / mj*rate
else:
dev = np.sqrt(np.abs(s) / (2.0*n_1)) / mj*rate
deverr = dev / np.sqrt(n_1)
return dev, deverr, n_1
def gcodev(data_1, data_2, rate=1.0, data_type="phase", taus=None):
""" Groslambert codeviation a.k.a. Allan Covariance
Similarly to the three-cornered hat method, we consider three uncorrelated
oscillators A, B, C. The Groslambert codeviation estimates the noise of
one oscillator (e.g. B), given two synchronous measurements AB and BC.
Unlike three-cordenred hat, Gcodev is not affected by the (uncorrelated) noise of
the measurement devices (time-interval or frequency counter) used for
the measurements AB and BC.
Parameters
----------
data_1: np.array
Oscillator 1 input data. Provide either phase or frequency
data_2: np.array
Oscillator 2 input data. Provide either phase or frequency
rate: float
The sampling rate for data, in Hz. Defaults to 1.0
data_type: {'phase', 'freq'}
Data type, i.e. phase or frequency. Defaults to "phase".
taus: np.array
Array of tau values, in seconds, for which to compute statistic.
Optionally set taus=["all"|"octave"|"decade"] for automatic
tau-list generation.
Returns
-------
(taus, gd): tuple
Tuple of values
taus: np.array
Tau values for which gcodev computed
gd: np.array
Computed gcodev for each tau value
References
----------
* [Vernotte2016]_
* [Lantz2019]_
"""
phase_1 = input_to_phase(data_1, rate, data_type)
phase_2 = input_to_phase(data_2, rate, data_type)
(phase_1, m, taus_used) = tau_generator(phase_1, rate, taus)
(phase_2, m, taus_used) = tau_generator(phase_2, rate, taus)
gd = np.zeros_like(taus_used)
gde = np.zeros_like(taus_used)
gdn = np.zeros_like(taus_used)
for idx, mj in enumerate(m): # stride=1 for overlapping ADEV
(gd[idx], gde[idx], gdn[idx]) = calc_gcodev_phase(phase_1,
phase_2,
rate,
mj,
stride=1)
return remove_small_ns(taus_used, gd, gde, gdn)
def oadev(data, rate=1.0, data_type="phase", taus=None):
""" Overlapping Allan deviation.
General purpose - most widely used - first choice.
.. math::
\\sigma^2_{OADEV}(m\\tau_0) = { 1 \\over 2 (m \\tau_0 )^2 (N-2m) }
\\sum_{n=1}^{N-2m} ( {x}_{n+2m} - 2x_{n+1m} + x_{n} )^2
where :math:`\\sigma_{OADEV}(m\\tau_0)` is the overlapping Allan
deviation at an averaging time of :math:`\\tau=m\\tau_0`, and
:math:`x_n` is the time-series of phase observations, spaced by the
measurement interval :math:`\\tau_0`, with length :math:`N`.
Parameters
----------
data: np.array
Input data. Provide either phase or frequency (fractional,
adimensional).
rate: float
The sampling rate for data, in Hz. Defaults to 1.0
data_type: {'phase', 'freq'}
Data type, i.e. phase or frequency. Defaults to "phase".
taus: np.array
Array of tau values, in seconds, for which to compute statistic.
Optionally set taus=["all"|"octave"|"decade"] for automatic
tau-list generation.
Returns
-------
(taus2, ad, ade, ns): tuple
Tuple of values
taus2: np.array
Tau values for which td computed
ad: np.array
Computed oadev for each tau value
ade: np.array
oadev errors
ns: np.array
Values of N used in each oadev calculation
References
----------
* NIST [SP1065]_ eqn (11), page 16.
"""
phase = input_to_phase(data, rate, data_type)
(phase, m, taus_used) = tau_generator(phase, rate, taus)
ad = np.zeros_like(taus_used)
ade = np.zeros_like(taus_used)
adn = np.zeros_like(taus_used)
for idx, mj in enumerate(m): # stride=1 for overlapping ADEV
(ad[idx], ade[idx], adn[idx]) = calc_adev_phase(phase, rate, mj, 1)
return remove_small_ns(taus_used, ad, ade, adn)
def ohdev(data, rate=1.0, data_type="phase", taus=None):
""" Overlapping Hadamard deviation.
Better confidence than normal Hadamard.
.. math::
\\sigma^2_{OHDEV}(m\\tau_0) = { 1 \\over 6 (m \\tau_0 )^2 (N-3m) }
\\sum_{i=1}^{N-3m} ( {x}_{i+3m} - 3x_{i+2m} + 3x_{i+m} - x_{i} )^2
where :math:`x_i` is the time-series of phase observations, spaced
by the measurement interval :math:`\\tau_0`, and with length :math:`N`.
Parameters
----------
data: np.array
Input data. Provide either phase or frequency (fractional,
adimensional).
rate: float
The sampling rate for data, in Hz. Defaults to 1.0
data_type: {'phase', 'freq'}
Data type, i.e. phase or frequency. Defaults to "phase".
taus: np.array
Array of tau values, in seconds, for which to compute statistic.
Optionally set taus=["all"|"octave"|"decade"] for automatic
tau-list generation.
Returns
-------
(taus2, hd, hde, ns): tuple
Tuple of values
taus2: np.array
Tau values for which td computed
hd: np.array
Computed hdev for each tau value
hde: np.array
hdev errors
ns: np.array
Values of N used in each hdev calculation
References
----------
* NIST [SP1065]_ eqn (20), page 21
"""
phase = input_to_phase(data, rate, data_type)
(phase, m, taus_used) = tau_generator(phase, rate, taus)
hdevs = np.zeros_like(taus_used)
hdeverrs = np.zeros_like(taus_used)
ns = np.zeros_like(taus_used)
for idx, mj in enumerate(m):
(hdevs[idx],
hdeverrs[idx],
ns[idx]) = calc_hdev_phase(phase, rate, mj, 1)
return remove_small_ns(taus_used, hdevs, hdeverrs, ns)
def hdev(data, rate=1.0, data_type="phase", taus=None):
""" Hadamard deviation.
Rejects frequency drift, and handles divergent noise.
.. math::
\\sigma^2_{HDEV}( \\tau ) = { 1 \\over 6 \\tau^2 (N-3) }
\\sum_{i=1}^{N-3} ( {x}_{i+3} - 3x_{i+2} + 3x_{i+1} - x_{i} )^2
where :math:`x_i` is the time-series of phase observations, spaced
by the measurement interval :math:`\\tau`, and with length :math:`N`.
Parameters
----------
data : np.array
Input data. Provide either phase or frequency (fractional,
adimensional).
rate : float
The sampling rate for data, in Hz. Defaults to 1.0
data_type : string, {'phase', 'freq'}
Data type, i.e. phase or frequency. Defaults to "phase".
taus : np.array
Array of tau values, in seconds, for which to compute statistic.
Optionally set taus=["all"|"octave"|"decade"] for automatic
tau-list generation.
References
----------
* NIST [SP1065]_ eqn (17) and (18), page 20
"""
phase = input_to_phase(data, rate, data_type)
(phase, m, taus_used) = tau_generator(phase, rate, taus)
hdevs = np.zeros_like(taus_used)
hdeverrs = np.zeros_like(taus_used)
ns = np.zeros_like(taus_used)
for idx, mj in enumerate(m):
(hdevs[idx],
hdeverrs[idx],
ns[idx]) = calc_hdev_phase(phase, rate, mj, mj) # stride = mj
return remove_small_ns(taus_used, hdevs, hdeverrs, ns)
def calc_hdev_phase(phase, rate, mj, stride):
""" main calculation function for HDEV and OHDEV
1 N-3
s2y(t) = --------------- sum [x(i+3) - 3x(i+2) + 3x(i+1) - x(i) ]^2
6*tau^2 (N-3m) i=1
N=M+1 phase measurements
m is averaging factor
Parameters
----------
phase: np.array
Phase data in seconds.
rate: float
The sampling rate for phase or frequency, in Hz
mj: int
M index value for stride
stride: int
Size of stride
Returns
-------
(dev, deverr, n): tuple
Array of computed values.
References
----------
* http://www.leapsecond.com/tools/adev_lib.c
* NIST [SP1065]_ eqn (18) and (20) pages 20 and 21
"""
tau0 = 1.0 / float(rate)
mj = int(mj)
stride = int(stride)
d3 = phase[3 * mj::stride]
d2 = phase[2 * mj::stride]
d1 = phase[1 * mj::stride]
d0 = phase[::stride]
n = min(len(d0), len(d1), len(d2), len(d3))
v_arr = d3[:n] - 3 * d2[:n] + 3 * d1[:n] - d0[:n]
s = np.sum(v_arr * v_arr)
if n == 0:
n = 1
h = np.sqrt(s / 6.0 / float(n)) / float(tau0 * mj)
e = h / np.sqrt(n)
return h, e, n
def totdev(data, rate=1.0, data_type="phase", taus=None):
""" Total deviation.
Better confidence at long averages for Allan deviation.
.. math::
\\sigma^2_{TOTDEV}( m\\tau_0 ) = { 1 \\over 2 (m\\tau_0)^2 (N-2) }
\\sum_{i=2}^{N-1} ( {x}^*_{i-m} - 2x^*_{i} + x^*_{i+m} )^2
Where :math:`x^*_i` is a new time-series of length :math:`3N-4`
derived from the original phase time-series :math:`x_n` of
length :math:`N` by reflection at both ends.
The original data :math:`x_n` is in the center of :math:`x^*`:
.. math::
x^*_{1-j} = 2x_1 - x_{1+j} \quad \textrm{for} j=1..N-2
x^*_i = x_i \quad \textrm{for} i=1..N
x^*_{N+j} = 2x_N - x_{N-j} \quad \textrm{for} j=1..N-2
FIXME: bias correction http://www.wriley.com/CI2.pdf page 5
Parameters
----------
phase: np.array
Phase data in seconds. Provide either phase or frequency.
frequency: np.array
Fractional frequency data (nondimensional). Provide either
frequency or phase.
rate: float
The sampling rate for phase or frequency, in Hz
taus: np.array
Array of tau values for which to compute measurement
References
----------
* NIST [SP1065]_ eqn (25) page 23
"""
phase = input_to_phase(data, rate, data_type)
(phase, m, taus_used) = tau_generator(phase, rate, taus)
N = len(phase)
# totdev requires a new dataset
# Begin by adding reflected data before dataset
x1 = 2.0 * phase[0] * np.ones((N - 2,))
x1 = x1 - phase[1:-1]
x1 = x1[::-1]
# Reflected data at end of dataset
x2 = 2.0 * phase[-1] * np.ones((N - 2,))
x2 = x2 - phase[1:-1][::-1]
# check length of new dataset
assert len(x1)+len(phase)+len(x2) == 3*N - 4
# Combine into a single array
x = np.zeros((3*N - 4))
x[0:N-2] = x1
x[N-2:2*(N-2)+2] = phase # original data in the middle
x[2*(N-2)+2:] = x2
devs = np.zeros_like(taus_used)
deverrs = np.zeros_like(taus_used)
ns = np.zeros_like(taus_used)
mid = len(x1)
for idx, mj in enumerate(m):
mj = int(mj)
d0 = x[mid + 1:]
d1 = x[mid + mj + 1:]
d1n = x[mid - mj + 1:]
e = min(len(d0), len(d1), len(d1n))
v_arr = d1n[:e] - 2.0 * d0[:e] + d1[:e]
dev = np.sum(v_arr[:mid] * v_arr[:mid])
dev /= float(2 * pow(mj / rate, 2) * (N - 2))
dev = np.sqrt(dev)
devs[idx] = dev
deverrs[idx] = dev / np.sqrt(mid)
ns[idx] = mid
return remove_small_ns(taus_used, devs, deverrs, ns)
def ttotdev(data, rate=1.0, data_type="phase", taus=None):
""" Time Total Deviation
Modified total variance scaled by :math:`\\tau^2 / 3`
.. math::
\\sigma^2_{TTOTDEV}( \\tau ) = { \\tau^2 \\over 3 }
\\sigma^2_{TOTDEV}( \\tau )
Note that [SP1065]_ erroneously has tau-cubed here (!).
References
----------
* NIST [SP1065]_ eqn (28) page 26.
"""
(taus, mtotdevs, mde, ns) = mtotdev(data, data_type=data_type,
rate=rate, taus=taus)
td = taus*mtotdevs / np.sqrt(3.0)
tde = td / np.sqrt(ns)
return taus, td, tde, ns
def mtotdev(data, rate=1.0, data_type="phase", taus=None):
""" Modified Total deviation.
Better confidence at long averages for modified Allan
FIXME: bias-correction http://www.wriley.com/CI2.pdf page 6
The variance is scaled up (divided by this number) based on the
noise-type identified.
+------------+------------------+
| noise type | bias correction |
+============+==================+
| WPM | 0.94 |
+------------+------------------+
| FPM | 0.83 |
+------------+------------------+
| WFM | 0.73 |
+------------+------------------+
| FFM | 0.70 |
+------------+------------------+
| RWFM | 0.69 |
+------------+------------------+
Parameters
----------
data: np.array
Input data. Provide either phase or frequency (fractional,
adimensional).
rate: float
The sampling rate for data, in Hz. Defaults to 1.0
data_type: {'phase', 'freq'}
Data type, i.e. phase or frequency. Defaults to "phase".
taus: np.array
Array of tau values, in seconds, for which to compute statistic.
Optionally set taus=["all"|"octave"|"decade"] for automatic
tau-list generation.
References
----------
* NIST [SP1065]_ eqn (27) page 25
"""
phase = input_to_phase(data, rate, data_type)
(phase, ms, taus_used) = tau_generator(phase, rate, taus,
maximum_m=float(len(phase))/3.0)
devs = np.zeros_like(taus_used)
deverrs = np.zeros_like(taus_used)
ns = np.zeros_like(taus_used)
for idx, mj in enumerate(ms):
devs[idx], deverrs[idx], ns[idx] = calc_mtotdev_phase(phase, rate, mj)
return remove_small_ns(taus_used, devs, deverrs, ns)
def calc_mtotdev_phase(phase, rate, m):