Skip to content

Commit 7c3c19c

Browse files
committed
Tools: removing NaN times for spectrum
1 parent b720fdc commit 7c3c19c

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

pydatview/tools/spectral.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
# --------------------------------------------------------------------------------}
3232
# --- FFT wrap
3333
# --------------------------------------------------------------------------------{
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):
3535
"""
3636
Wrapper to compute FFT amplitude or power spectra, with averaging.
3737
INPUTS:
@@ -51,20 +51,25 @@ def fft_wrap(t,y,dt=None, output_type='amplitude',averaging='None',averaging_win
5151
t = np.asarray(t)
5252
y = np.asarray(y)
5353
n0 = len(y)
54-
nt = len(t)
5554
if len(t)!=len(y):
5655
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
5762
y = y[~np.isnan(y)]
5863
n = len(y)
5964

6065
if dt is None:
6166
dtDelta0 = t[1]-t[0]
6267
# Hack to use a constant dt
6368
#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)
6570
relDiff = abs(dtDelta0-dt)/dt*100
66-
#if dtDelta0 !=dt:
6771
if relDiff>0.01:
72+
#if verbose:
6873
print('[WARN] dt from tmax-tmin different from dt from t2-t1 {} {}'.format(dt, dtDelta0) )
6974
Fs = 1/dt
7075
if averaging =='none':
@@ -80,7 +85,8 @@ def fft_wrap(t,y,dt=None, output_type='amplitude',averaging='None',averaging_win
8085
nExp=int(np.log(nFFTAll)/np.log(2))-1
8186
nPerSeg=2**nExp
8287
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.');
8490
nExp=int(np.log(nFFTAll)/np.log(2))-1
8591
nPerSeg=2**nExp
8692
if averaging_window=='hamming':
@@ -129,6 +135,8 @@ def psd_binned(y, fs=1.0, nPerDecade=10, detrend ='constant', return_onesided=Tr
129135
"""
130136
Return PSD binned with nPoints per decade
131137
"""
138+
if np.isnan(fs):
139+
raise Exception('Sampling frequecny is NaN')
132140
# --- First return regular PSD
133141
frq, PSD, Info = psd(y, fs=fs, detrend=detrend, return_onesided=return_onesided)
134142

0 commit comments

Comments
 (0)