31
31
# --------------------------------------------------------------------------------}
32
32
# --- FFT wrap
33
33
# --------------------------------------------------------------------------------{
34
- def fft_wrap (t ,y ,dt = None , output_type = 'amplitude' ,averaging = 'None' ,averaging_window = 'hamming' ,detrend = False ,nExp = None , nPerDecade = None ):
34
+ def fft_wrap (t ,y ,dt = None , output_type = 'amplitude' ,averaging = 'None' ,averaging_window = 'hamming' ,detrend = False ,nExp = None , nPerDecade = None , verbose = False ):
35
35
"""
36
36
Wrapper to compute FFT amplitude or power spectra, with averaging.
37
37
INPUTS:
@@ -51,20 +51,25 @@ def fft_wrap(t,y,dt=None, output_type='amplitude',averaging='None',averaging_win
51
51
t = np .asarray (t )
52
52
y = np .asarray (y )
53
53
n0 = len (y )
54
- nt = len (t )
55
54
if len (t )!= len (y ):
56
55
raise Exception ('t and y should have the same length' )
56
+ # Removing times that are NaN
57
+ t_NaN = np .isnan (t )
58
+ t = t [~ t_NaN ]
59
+ y = y [~ t_NaN ]
60
+ nt = len (t )
61
+ # Removing signal that is NaN
57
62
y = y [~ np .isnan (y )]
58
63
n = len (y )
59
64
60
65
if dt is None :
61
66
dtDelta0 = t [1 ]- t [0 ]
62
67
# Hack to use a constant dt
63
68
#dt = (np.max(t)-np.min(t))/(n0-1)
64
- dt = (t [- 1 ]- t [0 ])/ (n0 - 1 )
69
+ dt = (t [- 1 ]- t [0 ])/ (nt - 1 )
65
70
relDiff = abs (dtDelta0 - dt )/ dt * 100
66
- #if dtDelta0 !=dt:
67
71
if relDiff > 0.01 :
72
+ #if verbose:
68
73
print ('[WARN] dt from tmax-tmin different from dt from t2-t1 {} {}' .format (dt , dtDelta0 ) )
69
74
Fs = 1 / dt
70
75
if averaging == 'none' :
@@ -80,7 +85,8 @@ def fft_wrap(t,y,dt=None, output_type='amplitude',averaging='None',averaging_win
80
85
nExp = int (np .log (nFFTAll )/ np .log (2 ))- 1
81
86
nPerSeg = 2 ** nExp
82
87
if nPerSeg > n :
83
- print ('[WARN] Power of 2 value was too high and was reduced. Disable averaging to use the full spectrum.' );
88
+ if verbose :
89
+ print ('[WARN] Power of 2 value was too high and was reduced. Disable averaging to use the full spectrum.' );
84
90
nExp = int (np .log (nFFTAll )/ np .log (2 ))- 1
85
91
nPerSeg = 2 ** nExp
86
92
if averaging_window == 'hamming' :
@@ -129,6 +135,8 @@ def psd_binned(y, fs=1.0, nPerDecade=10, detrend ='constant', return_onesided=Tr
129
135
"""
130
136
Return PSD binned with nPoints per decade
131
137
"""
138
+ if np .isnan (fs ):
139
+ raise Exception ('Sampling frequecny is NaN' )
132
140
# --- First return regular PSD
133
141
frq , PSD , Info = psd (y , fs = fs , detrend = detrend , return_onesided = return_onesided )
134
142
0 commit comments