-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsignal.py
1241 lines (1109 loc) · 41 KB
/
signal.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
# -*- coding: utf-8 -*-
"""Signal analysis.
This is part of the kLib Python library for scientific data analysis.
The purpouse of this module is to give a set of functions relevant to the
oceanographic community.
AUTHOR
Sebastian Krieger
email: [email protected]
REVISION
1 (2013-12-02 15:37 DST)
"""
from __future__ import division
__version__ = '$Revision: 1 $'
# $Source$
__all__ = ['fftnegativeshift', 'next_power_of_two', 'power_spectral_density',
'errors', 'climatology']
from copy import copy
from datetime import datetime
from matplotlib.dates import drange, num2date
from numpy import (arange, arctan2, array, asarray, ceil, concatenate, cos,
deg2rad, digitize, empty, exp, flatnonzero, fliplr, flipud, floor,
iscomplex, isnan, linalg, log2, ma, median, nan, ndarray, ones, rad2deg,
sin, take, unique, zeros, append, hanning, convolve, pi)
from scipy import interpolate
from scipy.fftpack import fft2, fftfreq, fftshift
from scipy.misc import factorial
from scipy.signal import fftconvolve, detrend
from scipy.stats import binned_statistic
from sys import stdout
from time import time
import common
def speeddir_to_vector(m, a, dtype='from', masked=True):
"""
Converts polar vector notation to complex vector notation, assuming
geographical angle origin convention at north.
Parameters
----------
m : array like
Magnitude of the speed
a : array like
Angle in degrees.
dtype : string, optional
If `from`, uses convention that vector indicates direction from
which it is comming (e.g. wind from direction). If `to`
indicates direction to which it is pointing (e.g. currents).
Returns
-------
uv : array like
Vector in complex notation (u + 1j*v).
"""
# Converts angles from degrees to radians, assuming geographical angle
# origin convention at north rotating clockwise. Check
# <http://wx.gmu.edu/dev/clim301/lectures/wind/wind-uv.html> for further
# explanations.
if dtype == 'from':
a = deg2rad(270 - a)
elif dtype == 'to':
a = deg2rad(90 - a)
else:
raise ValueError('Invalid data type `{}`.'.format(dtype))
# Calcultes vector coordinates.
if masked:
uv = ma.masked_invalid(m * cos(a) + 1j * m * sin(a))
uv.data[uv.mask] = 0
return uv
else:
return m * cos(a) + 1j * m * sin(a)
def vector_to_speeddir(uv, dtype='from'):
"""
Converts complex vector to polar vector, assuming geographical angle
origin convention at north.
Parameters
----------
uv : array like
Array of cartesian vector components in complex notation.
dtype : string, optional
If `from`, uses convention that vector indicates direction from
which it is comming (e.g. wind from direction). If `to`
indicates direction to which it is pointing (e.g. currents).
Returns
-------
m, a : array like
Magnitude and angle.
"""
m = (uv.real**2 + uv.imag**2)**0.5
a = rad2deg(arctan2(uv.imag, uv.real))
# Converts angle from mathematical direction to meteorological direction.
# Check <http://wx.gmu.edu/dev/clim301/lectures/wind/wind-uv.html> for
# further explanations.
if dtype == 'from':
a = common.lon360(270 - a)
elif dtype == 'to':
a = common.lon360(90 - a)
else:
raise ValueError('Invalid data type `{}`.'.format(dtype))
#
return m, a
def derivative(A, axis, p=1, q=3, mask=None):
"""Higher order differential.
Calculates the p-th derivative of `A` using stencils of width n.
The gradient is computed using central differences in the interior
and first differences at the boundaries. The returned gradient hence
has the same shape as the input array.
Parameters
----------
A : array like
Data to calculate the derivate.
axis : array like
Axis onto which the derivative will be calculated. Must have
same size as `A`.
p : integer, optional
Order of the derivative to be calculated. Default is to
calculate the first derivative (p=1). 2*n-p+1 gives the relative
order of the approximation.
q (integer, optional) :
Length of the stencil used for centered differentials. The
length has to be odd numbered. Default is q=3.
mask : array like, optional :
Returns
-------
dA : array like
The calculated derivate with same dimensions as A
References
----------
Cushman-Roisin, B. & Beckers, J.-M. Introduction to geophysical
fluid dynamics: Physical and numerical aspects. Academic Press,
2011, 101, 828.
Arbic, Brian B. Scott, R. B.; Chelton, D. B.; Richman, J. G. and
Shriver, J. F. Effects of stencil width on surface ocean geostrophic
velocity and vorticity estimation from gridded satellite altimeter
data. Journal of Geophysical Research, 2012, 117, C03029.
"""
# Checks size of A and axis.
if A.shape != axis.shape:
raise ValueError('Data and axis array do not have same shape.')
# Makes shure the length of the stencil is odd numbered.
q += (q % 2) - 1
# Calculate left and right stencils.
q_left = (q - 1) / 2
q_right = (q - 1) / 2
# Calculate stencil coefficients
coeffs = _derivative_stencil_coefficients(axis, p=p, q=q)
# Calculate the p-th derivative
I = A.size
dA = zeros(I)
for i in arange(q) - q_left:
if i < 0:
u, v = -i, I
else:
u, v = 0, I - i
dA[u:v] += (coeffs[u:v, i+q_left] * A[u+i:v+i])
return dA
def _derivative_stencil_coefficients(axis, p=1, q=3):
"""Calculates the coefficients needed for the derivative.
Parameters
----------
axis : array like
Axis onto which the derivative will be calculated.
p : integer, optional
Order of the derivative to be calculated. Default is to
calculate the first derivative (p=1). 2*n-p+1 gives the relative
order of the approximation.
q : integer, optional
Length of the stencil used for centered differentials. The
length has to be odd numbered. Default is q=3.
Returns
-------
Coefficients (a_q) needed for the linear combination of `q` points
to get the first derivative according to Arbic et al. (2012)
equations (20) and (22). At the boundaries forward and backward
differences approximations are calculated.
References
----------
Cushman-Roisin, B. & Beckers, J.-M. Introduction to geophysical
fluid dynamics: Physical and numerical aspects Academic Press, 2011,
101, 828.
Arbic, Brian B. Scott, R. B.; Chelton, D. B.; Richman, J. G. &
Shriver, J. F. Effects of stencil width on surface ocean geostrophic
velocity and vorticity estimation from gridded satellite altimeter
data. Journal of Geophysical Research, 2012, 117, C03029.
"""
# Calculate left and right stencils.
q_left = (q - 1) / 2
q_right = (q - 1) / 2 + 1
#
I = axis.size
# Constructs matrices according to Cushman-Roisin & Beckers (2011)
# equations (1.25) and adapted for variable grids as in Arbic et
# al. (2012), equations (20), (22). The linear system of equations
# is solved afterwards.
coeffs = zeros((I, q))
smart_coeffs = dict()
for i in range(I):
A = zeros((q, q))
#
if i < q_left:
start = q_left - i
else:
start = 0
if i > I - q_right:
stop = i - (I - q_right)
else:
stop = 0
#
A[0, start:q+stop] = 1
da = axis[i-q_left+start:i+q_right-stop] - axis[i]
da_key = str(da)
#
if da_key not in smart_coeffs.keys():
for h in range(1, q):
A[h, start:q-stop] = da ** h
B = zeros((q, 1))
# This tells where the p-th derivative is calculated
B[p] = factorial(p)
C = linalg.solve(A[:q-start-stop, start:q-stop],
B[:q-(start+stop), :])
#
smart_coeffs[da_key] = C.flatten()
#
coeffs[i, start:q-stop] = smart_coeffs[da_key]
#
return coeffs
def continuous_timeseries(dat, dt, tlim=None, fill=None, max_gap=12, mask=True,
skip=[], skip_fill=[]):
"""Creates continuous data array.
Parameters
----------
dat : structured array
Input data.
dt : datetime.timedelta
Time step.
tlim : list, optional
Indicates the upper and lower limits for time.
fill : string, optional
If set to `climatology`, fills data gaps with climatological
data. If set to `interpolate` fills data gaps with cubic spline
interpolated data.
max_gap : integer, optional
Maximum gap size to fill with either climatology or interpolated
values, according to `fill` parameter.
mask : boolean, optional
If both `climatology` and `mask` are true, masks filled gaps.
skip : list, optional
List of fields to skip. Default is empty.
skip_fill : list, optional
List of fields to skip gap filling. Default is empty.
Returns
-------
out : structured array
Reordered data.
[clima] : dictionary of arrays, optional
If `climatology` is true, returns also the climatologies for
each input fields.
"""
# Some parameters and variable initialization. Determines the sampling
# interval, the size of the new array. Then creates a bin array in which
# original data will be fit and finally initializes new data array.
# Creates an array of indices of the new data.
if tlim == None:
T0 = num2date(dat['time'].min())
T1 = num2date(dat['time'].max())
elif (isinstance(tlim[0], datetime) &
isinstance(tlim[1], datetime)):
T0, T1 = tlim
elif isinstance(tlim[0], float) & isinstance(tlim[1], float):
T0 = num2date(tlim[0])
T1 = num2date(tlim[1])
else:
raise ValueError('Invalid temporal limits.')
t = drange(T0, T1, dt)
dt = (t[1] - t[0])
dt2 = dt * 0.5
# Appends a new time entry to the end.
t = append(t, t[-1] + dt)
#
N = t.size
bins = arange(t[0] - dt2, t[-1] + 2 * dt2, dt)
out = ma.empty(N, dtype=dat.dtype)
out_idx = arange(N)
if fill == 'climatology':
clima = dict()
# Determines to which bin each data point belongs to.
bin_sel = digitize(dat['time'], bins)
# Walks through each record and determines each bin mean.
for i, field in enumerate(dat.dtype.names):
if field in skip:
continue
if field == 'time':
out['time'] = t
else:
try:
sel = ~(dat[field].mask | isnan(dat[field]))
except:
sel = ~(isnan(dat[field]))
is_complex = iscomplex(dat[field][sel]).any()
if is_complex:
out_real, _, bin_nr = binned_statistic(dat['time'][sel],
dat[field][sel].real, statistic='mean', bins=bins)
out_imag, _, bin_nr = binned_statistic(dat['time'][sel],
dat[field][sel].imag, statistic='mean', bins=bins)
out[field] = out_real + 1j * out_imag
#bin_nr = list(set(bin_nr_real) | set(bin_nr_imag))
else:
out[field], _, bin_nr = binned_statistic(dat['time'][sel],
dat[field][sel], statistic='mean', bins=bins)
# Finds out where the gaps are and fills them either with the
# climatological means, interpolates them depending `on max_gap` or
# simply masks them. It is important to note that the bin number
# starts counting at one.
sel = list(set(out_idx) - set(unique(bin_nr) - 1))
sel.sort()
if ((fill in ['climatology', 'interpolate']) &
(field not in skip_fill)):
# Separates gaps in order to determine gap size
_spl = [0] + [i for i in range(1, len(sel)) \
if (sel[i] - sel[i-1]) > 1] + [None]
gaps = []
for i in range(1, len(_spl)):
if len(sel[_spl[i-1]:_spl[i]]) <= max_gap:
gaps += sel[_spl[i-1]:_spl[i]]
#
if fill == 'climatology':
# Calculates dayly climatological mean.
clima[field] = climatology(dat['time'], dat[field],
major='month', minor='hour', result='timeseries',
t_out=t)
# Fills invalid data with climatology and masks filled gaps
# according to `mask` input parameter.
out[field].data[gaps] = clima[field][gaps]
elif fill == 'interpolate':
if is_complex:
out_real = _interpolate(t[gaps], dat['time'],
dat[field].real)
out_imag = _interpolate(t[gaps], dat['time'],
dat[field].imag)
out[field].data[gaps] = out_real + 1j * out_imag
else:
out[field].data[gaps] = _interpolate(t[gaps],
dat['time'], dat[field])
# Masks all data not in continuous timeseries and sets mask in
# gaps according to `mask` parameter.
out[field].mask[sel] = True
out[field].mask[gaps] = mask
else:
# Mask gaps in new dataset.
out[field].data[sel] = 0
out[field].mask[sel] = True
#
if fill == 'climatology':
return out, clima
else:
return out
def circular_mean(a, A=2**0.5, dtype='mean'):
"""Calculates the average of an angle.
Parameters
----------
a : array_like
List of angles.
A : array_like, float, optional
In case of vector fields, gives the modulus of each vector.
dtype : string, optional
Accepted values are `mean` (default), `median` to either
calculate mean or median respectively.
Returns
-------
<a> : array_like
Average angle
<A> : array_like
If `A` is an array, then retunrs the average modulus.
"""
# Converts array of angles in array of radians.
a = deg2rad(a)
# Creates array of vectors using complex notation, and calculates mean.
z = A * cos(a) + 1j*A * sin(a)
if dtype == 'mean':
Z = z.mean()
elif dtype == 'median':
Z = median(z)
else:
raise ValueError('Invalid mean type `{}`.'.format(dtype))
#
if isinstance(A, float) | isinstance(A, int):
return rad2deg(arctan2(Z.imag, Z.real))
else:
return rad2deg(arctan2(Z.imag, Z.real)), abs(Z)
def climatology(t, f, major='month', minor='hour', result='climatology',
kind='data', t_out=None, averaging='mean', **kwargs):
"""Calculates climatology from data.
Parameters
----------
t, f : array_like
Time and value arrays for which climatology will be
calculated. Time has to be in matplotlib's number format.
major, minor : string, optional
Sets the major and minor scales for calculation. Scales can
be either `year`, `month`, `day`, `hour`, `julian`, `week`
or `season`.
result : string, optional
If `climatology` (default) returns climatology array
according to major and minor scales. If `all`, returns in
addition the major and minor indices for each time in
climatology array. If `timeseries`, returns only climatology
for every time t.
kind : string, optional
If set to `data`, adjusts minor and major axis to data. If
set to `valid`, adjusts minor and major axis to valid
values, irrespective if they occur in the dataset.
averaging: string, optional
If set to `mean` (default), calculates mean values. If set
to `sum`, sums all values.
t_out : array_like, optional
If result is `timeseries`, then returns climatology for time
give in `t_out` instead of `t`.
Returns
-------
clima : array_like
Climatology of input value.
stdev : array_like
Standard deviation in climatology.
count : array_like
Number of occurences.
ind_j, ind_i : array_like
Major and minor indices for each time.
"""
# Converts time array to year-month-day array.
YMD = common.num2ymd(t, **kwargs)
try:
YMD_out = common.num2ymd(t_out, **kwargs)
except:
pass
#
def _set_column(name):
if name == 'year':
return 0
elif name == 'month':
return 1
elif name == 'day':
return 2
elif name == 'hour':
return 3
elif name == 'julian':
return 6
elif name == 'week':
return 7
elif name == 'season':
return 8
else:
raise ValueError('Invalid column name `{}`.'.format(name))
#
def _set_axis(name, kind, data):
if kind == 'data':
return unique(data)
elif kind == 'valid':
if name == 'year':
return arange(data.min(), data.max()+1)
if name == 'month':
return arange(0, 12) + 1
elif name == 'day':
return arange(0, 31) + 1
elif name == 'hour':
return arange(0, 24)
elif name == 'julian':
return arange(0, 366) + 1
elif name == 'week':
return arange(53) + 1
elif name == 'season':
return arange(4) + 1
else:
raise ValueError('Invalid column name `{}`.'.format(name))
else:
raise ValueError('Invalid kind `{}`.'.format(kind))
#
major_col = _set_column(major)
minor_col = _set_column(minor)
#
if major_col == minor_col:
major_col = -1
Major = array([nan])
else:
Major = _set_axis(major, kind, YMD[:, major_col])
Minor = _set_axis(minor, kind, YMD[:, minor_col])
M, N = Major.size, Minor.size
#
clima = zeros((M, N), dtype=f.dtype)
count = zeros((M, N))
stdev = zeros((M, N))
ind_i = -ones(t.size, dtype=int)
ind_j = -ones(t.size, dtype=int)
try:
ind_i_out = -ones(t_out.size, dtype=int)
ind_j_out = -ones(t_out.size, dtype=int)
except:
pass
#
for m, major in enumerate(Major):
for n, minor in enumerate(Minor):
if major_col == -1:
sel = (YMD[:, minor_col] == minor)
else:
sel = (YMD[:, major_col] == major) & \
(YMD[:, minor_col] == minor)
if averaging == 'mean':
clima[m, n] = f[sel].mean()
elif averaging == 'sum':
clima[m, n] = f[sel].sum()
else:
raise ValueError('Invalid averaging `{}`.'.format(averaging))
count[m, n] = sel.sum()
stdev[m, n] = f[sel].std()
ind_j[sel] = m
ind_i[sel] = n
try:
if major_col == -1:
sel = (YMD_out[:, minor_col] == minor)
else:
sel = (YMD_out[:, major_col] == major) & \
(YMD_out[:, minor_col] == minor)
ind_j_out[sel] = m
ind_i_out[sel] = n
except:
pass
#
if result == 'climatology':
return Major, Minor, clima, stdev, count
elif result == 'all':
return Major, Minor, clima, stdev, count, ind_j, ind_i
elif result == 'timeseries':
try:
return clima[ind_j_out, ind_i_out]
except:
if (ind_i<0).any() | (ind_j<0).any():
raise ValuError('Some data in input array are undetermined.')
return clima[ind_j, ind_i]
def errors(a, b, result='dict'):
"""
Returns the mean error, mean average error, root-mean-square
error and goodness of fit between `a` and `b`.
The mean error (ME), mean average error (MAE), root-mean-square
error (RMSE) and goodness of fit (G) are defined as:
ME = \frac{1}{N} \sum\limits_{n=1}^{N} a(n) - b(n)
MAE = \frac{1}{N} \sum\limits_{n=1}^{N} \left| a(n) -
b(n) \right|
RMSE = \sqrt{\frac{1}{N} \sum\limits_{n=1}^{N} \left[ a -
b \right]^2}
G = \frac{\sum\limits_{n=1}^{N} \left[ <a> - b \right]^2}
{\sum\limits_{n=1}^{N} \left[ <a> - a \right]^2}
Parameters
----------
a, b : array_like
Samples to compare. Note that both arrays should have the same
shape.
result : string, optional
If set to `dict` (default) returns a dictionary containing
calculated parameters.
Returns
"""
# Makes sure input parameters are arrays and checks if they have same
# shape.
a = ma.masked_invalid(a.copy())
b = ma.masked_invalid(b.copy())
if a.shape != b.shape:
raise ValueError('Shape of arrays do not match.')
# Masks
# Calculates errors.
ME = (a - b).sum() / a.size
MAE = abs(a - b).sum() / a.size
RMSE = (((a - b)**2.).sum() / a.size)**0.5
G = ((a.mean() - b)**2).sum() / ((b.mean() - b)**2).sum()
if result == 'dict':
return dict(ME=ME, MAE=MAE, RMSE=RMSE, G=G)
else:
return ME, MAE, RMSE, G
def fftnegativeshift(x):
"""
Shift the zero-frequency component to the center of the spectrum,
inverting positive with negative frequencies.
This function swaps half-spaces for all axes listed (defaults to
all). Note that ``y[0]`` is the Nyquist component only if ``len(x)``
is even.
Note that this function works only with two-dimensional input
arrays.
PARAMETERS
x (array like) :
Input array.
RETURNS
y (array like) :
The shifted array.
"""
tmp = asarray(x)
if tmp.ndim != 2:
raise ValueError('Input array is not two-dimensional.')
else:
axes = range(tmp.ndim)
#
y = tmp.copy()
#
for k in axes:
n = tmp.shape[k]
p2 = (n + 1) // 2
print k, n, p2
mylist = concatenate((arange(p2, n), arange(p2)))
y = take(y, mylist, k)
if k == 1:
y[:p2, :] = fliplr(y[:p2, :])
#
print y[:p2, :]
return y
def next_power_of_two(A):
"""Takes every element in A and calculates the next integer power of
two.
PARAMETERS
A (integer, float, array like) :
Values to calculate the next power of two.
RETURNS
B (integer, array like) :
Next integer power of two.
"""
t = type(A)
a = asarray(A)
b = array([int(2**ceil(log2(i))) for i in a.flatten()])
b.shape = a.shape
return t(b)
def power_spectral_density(h, shape=None, delta=(1., 1.), mirror='',
negativeshift=False, method='ET01', window=None, detrend=False,
result='full'):
"""Calculates the two dimensional power spectral density (2D-PSD).
The 2D-PSD is defined as the squared amplitude per unit area of the
spectrum of a surface height map
PARAMETERS
h (array like) :
shape (list or tuple, optional) :
delta (list or tuple, optional) :
List containing the y-axis and x-axis sampling interval.
positivex, positivey (boolean, optional) :
negative (string, optional) :
method (string, optional) :
windows (array like, optional) :
detrend (boolean, optional) :
result (string, optional) :
RETURNS
freqx, freqy (array like) :
PSD (array like) :
phase (array like) :
REFERENCES
Emery, W. J. & Thomson, R. E. Data analysis methods in physical
oceanography Elsevier, 2001, 638, section 5.6.3.2
Sidick, E. Power spectral density specification and analysis
of large optical surfaces SPIE Europe Optical Metrology, 2009,
73900L-73900L.
"""
if h.ndim != 2:
raise ValueError('This function works only with two-dimensional '
'arrays.')
if window != None:
h = fftconvolve(window/window.sum(), h, mode='same')
if shape == None:
J, I = next_power_of_two(h.shape)
else:
J, I = shape
dy, dx = delta
#
try:
H = h.data * ~h.mask
H[isnan(H)] = 0
FFT = fft2(H, (J, I))
except:
FFT = fft2(h, (J, I))
freqx = fftfreq(I, dx)
freqy = fftfreq(J, dy)
if negativeshift:
j = (J + 1) // 2
FFT[1:j, :] = fliplr(FFT[1:j, :])
if mirror.find('x') >= 0:
fft = zeros((FFT.shape[0], FFT.shape[1]/2), dtype=complex)
fft[:, 0] = FFT[:, 0]
fft[:, 1:] = FFT[:, 1:I/2] + fliplr(FFT[:, I/2+1:])
freqx = freqx[:I/2]
FFT = fft.copy()
else:
FFT = fftshift(FFT, axes=1)
freqx = fftshift(freqx)
if mirror.find('y') >= 0:
fft = zeros((FFT.shape[0]/2, FFT.shape[1]), dtype=complex)
fft[0, :] = FFT[0, :]
fft[1:, :] = FFT[1:J/2, :] + flipud(FFT[J/2+1:, :])
freqy = freqy[:J/2]
FFT = fft.copy()
else:
FFT = fftshift(FFT, axes=0)
freqy = fftshift(freqy)
if method == 'ET01':
PSD = (FFT * FFT.conj()).real / (dx * dy * J * I)
elif method == 'S09':
PSD = (FFT * FFT.conj()).real * (dx * dy) / (J * I)
else:
raise ValueError ('Unrecognized method \'%s\'.' % (method))
phase = arctan2(FFT.imag, FFT.real)
if result == 'full':
return freqx, freqy, PSD, phase
elif result == 'psd':
return PSD
def bin_average(x, y, dx=1., bins=None, nstd=2., interpolate='bins', k=3,
s=None, extrapolate='repeat', mode='mean', profile=False, usemask=True):
"""Calculates bin average from input data.
Inside each bin, calculates the average and standard deviation, and
selects only those values inside the confidence interval given in
`nstd`. Finally calculates the bin average using spline
interpolation at the middle points in each bin. Linearly
extrapolates values outside of the data boundaries.
Parameters
----------
x : array like
Input coordinate to be binned. It has to be 1-dimensional.
y : array like
The data input array.
dx: float, optional
bins : array like, optional
Array of bins. It has to be 1-dimensional and strictly
increasing.
nstd : float, optional
Confidence interval given as number of standard deviations.
interpolate : string or boolean, optional
Valid options are `bins` (default), `full` or `False` and
defines whether to interpolate data to central bin points only
in filled bins, over full time-series, or skip interpolation
respectively.
k : int, optional
Specifies the order of the interpolation spline. Default is 3,
`cubic`.
s : float, optional
Positive smoothing factor used to choose the number of knots.
extrapolate : string, bool, optional
Sets if averaging outside data boundaries should be
extrapolated. If `True` or `linear`, extrapolates data linearly,
if `repeat` (default) repeats values from nearest bin.
mode : string, optional
Sets averaging mode: `mean` (default), `median`.
Returns
-------
bin_x : array like
Coordinate at the center of the bins.
bin_y : array like
Interpolated array of bin averages.
avg_x : array like
Average coordinate in each bin.
avg_y : array like
Average values inside each bin.
std_x : array like
Coordinate standard deviation in each bin.
std_y : array like
Standard deviation in each bin.
min_y : array like
Minimum values in each bin.
max_y : array like
Maximum values in each bin.
"""
t0 = time()
# If no bins are given, calculate them from input data.
if bins is None:
x_min = floor(x.min() / dx) * dx
x_max = 0. # numpy.ceil(x.max() / dx) * dx
bins = arange(x_min-dx, x_max+dx, dx) + dx/2
# Checks if bin array is strictly increasing.
if not all(x < y for x, y in zip(bins, bins[1:])):
raise ValueError('Bin array must be strictly increasing.')
# Ensures that input coordinate `x` is monotonically increasing.
_i = x.argsort()
x = x[_i]
y = y[_i]
# Data types
dtype_x = x.dtype
dtype_y = y.dtype
# Some variable initializations
nbins = len(bins) - 1
ndata = len(y)
Sel = zeros(ndata, dtype=bool)
# Initializes ouput arrays, masked or not.
if usemask:
bin_y = ma.empty(nbins, dtype=dtype_y) * nan
avg_x = ma.empty(nbins, dtype=dtype_x) * nan
avg_y = ma.empty(nbins, dtype=dtype_y) * nan
std_x = ma.empty(nbins, dtype=dtype_x) * nan
std_y = ma.empty(nbins, dtype=dtype_y) * nan
min_y = ma.empty(nbins, dtype=dtype_y) * nan
max_y = ma.empty(nbins, dtype=dtype_y) * nan
else:
bin_y = empty(nbins, dtype=dtype_y) * nan
avg_x = empty(nbins, dtype=dtype_x) * nan
avg_y = empty(nbins, dtype=dtype_y) * nan
std_x = empty(nbins, dtype=dtype_x) * nan
std_y = empty(nbins, dtype=dtype_y) * nan
min_y = empty(nbins, dtype=dtype_y) * nan
max_y = empty(nbins, dtype=dtype_y) * nan
# Determines indices of the bins to which each data points belongs.
bin_sel = digitize(x, bins) - 1
bin_sel_unique = unique(bin_sel)
_nbins = bin_sel_unique.size
#
t1 = time()
for i, bin_i in enumerate(bin_sel_unique):
if profile:
# Erase line ANSI terminal string when using return feed
# character.
# (source:http://www.termsys.demon.co.uk/vtansi.htm#cursor)
_s = '\x1b[2K\rBin-averaging... %s' % (common.profiler(_nbins, i, 0, t0,
t1))
stdout.write(_s)
stdout.flush()
# Ignores when data is not in valid range:
if (bin_i < 0) | (bin_i > nbins):
print 'Uhuuu (signal.py line 908)!!'
continue
# Calculate averages inside each bin in two steps: (i) calculate
# average and standard deviation; (ii) consider only those values
# within selected standard deviation range.
sel = flatnonzero(bin_sel == bin_i)
# Selects data within selected standard deviation or single
# data in current bin.
if sel.size > 1:
_avg_y = y[sel].mean()
_std_y = y[sel].std()
if _std_y > 1e-10:
_sel = ((y[sel] >= (_avg_y - nstd * _std_y)) &
(y[sel] <= (_avg_y + nstd * _std_y)))
#print bin_i, sel.size, _avg_y, _std_y
sel = sel[_sel]
#print sel.size
# Calculates final values
if mode == 'mean':
_avg_x = x[sel].mean()
_avg_y = y[sel].mean()
elif mode == 'median':
_avg_x = median(x[sel])
_avg_y = median(y[sel])
else:
raise ValueError('Invalid mode `{}`.'.format(mode))
_std_x = x[sel].std()
_std_y = y[sel].std()
_min_y = y[sel].min()
_max_y = y[sel].max()
else:
_avg_x, _avg_y = x[sel][0], y[sel][0]
_std_x, _std_y = 0, 0
_min_y, _max_y = nan, nan
#
#print i, sel_sum, _avg_x, _avg_y
#
avg_x[bin_i] = _avg_x
avg_y[bin_i] = _avg_y
std_x[bin_i] = _std_x
std_y[bin_i] = _std_y
min_y[bin_i] = _min_y
max_y[bin_i] = _max_y
#
if profile:
_s = '\rBin-averaging... %s' % (common.profiler(_nbins, i+1, 0, t0,
t1))
stdout.write(_s)
stdout.flush()
# Interpolates selected data to central data point in bin using spline.
# Only interpolates data in filled bins.
if interpolate in ['bins', 'full']:
sel = ~isnan(avg_y)
bin_x = (bins[1:] + bins[:-1]) * 0.5
if interpolate == 'bins':
bin_y[sel] = _interpolate(bin_x[sel], avg_x[sel], avg_y[sel], k=k,
s=s, outside=extrapolate)
elif interpolate == 'full':
bin_y = _interpolate(bin_x, avg_x[sel], avg_y[sel], k=k, s=s,
outside=extrapolate)
elif interpolate != False:
raise ValueError('Invalid interpolation mode `{}`.'.format(
interpolate))
# Masks invalid data.
if usemask:
bin_y = ma.masked_invalid(bin_y)
avg_x = ma.masked_invalid(avg_x)
avg_y = ma.masked_invalid(avg_y)
std_x = ma.masked_invalid(std_x)
std_y = ma.masked_invalid(std_y)
min_y = ma.masked_invalid(min_y)
max_y = ma.masked_invalid(max_y)
if interpolate:
return bin_x, bin_y, avg_x, avg_y, std_x, std_y, min_y, max_y
else:
return avg_x, avg_y, std_x, std_y, min_y, max_y
def _interpolate(xi, xd, yd, k=3, s=None, outside='repeat'):
"""."""
if s == None:
s = xi.size
yi = empty(xi.shape) * nan
# Checks for NaN's in data.
sel = ~(isnan(xd) | isnan(yd))
# Checks if number of points is greater then the order of the spline.
if sel.sum() <= 1:
print 'Array must have at least two data points.', xd, yd
return yi
elif sel.sum() <= k: