Skip to content

Commit ddb05ca

Browse files
committed
adjust nperseg for psd calculation for short signals
1 parent a7a7adc commit ddb05ca

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

qats/app/funcs.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def calculate_psd(container, twin, fargs, nperseg, normalize):
2121
fargs : tuple
2222
Filter arguments. Time series are filtered before estimating psd.
2323
nperseg : int
24-
Size of segments used for ustimating PSD using Welch's method.
24+
Size of segments used for estimating PSD using Welch's method.
25+
NOTE: the minimum of specified `nperseg` and signal length is used.
2526
normalize : bool
2627
Normalize power spectral density on maximum density.
2728
@@ -33,8 +34,14 @@ def calculate_psd(container, twin, fargs, nperseg, normalize):
3334
container_out = dict()
3435

3536
for name, ts in container.items():
36-
# resampling to average time step for robustness (necessary for series with varying time step)
37-
f, s = ts.psd(twin=twin, filterargs=fargs, resample=ts.dt, taperfrac=0.1, nperseg=nperseg, normalize=normalize)
37+
# NOTE: nperseg passed on as minimum of specified value and signal input length
38+
# this is done in scipy anyway, which also issues a UserWArnings stating:
39+
# "nperseg = {nperseg} is greater than input length = {input_length:d}, using nperseg = {input_length:d}"
40+
t, _ = ts.get(twin=twin, resample=ts.dt)
41+
nperseg_ = np.min((nperseg, t.size))
42+
# calculate power spectral density
43+
# (resampling to average time step for robustness, necessary for series with varying time step)
44+
f, s = ts.psd(twin=twin, filterargs=fargs, resample=ts.dt, taperfrac=0.1, nperseg=nperseg_, normalize=normalize)
3845
container_out[name] = tuple([f, s])
3946

4047
return container_out

qats/app/gui.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1462,12 +1462,12 @@ def __init__(self, psdnorm, nperseg, nbins, twindec, parent=None):
14621462
self.psdnpersegspinbox.setSingleStep(10)
14631463
self.psdnpersegspinbox.setEnabled(True)
14641464
self.psdnpersegspinbox.setValue(nperseg)
1465-
self.psdnpersegspinbox.setToolTip("When esimtating power spectral density using Welch's method the signal\n"
1466-
"is dived into overlapping segments and psd is estimated for each segment\n"
1465+
self.psdnpersegspinbox.setToolTip("When estimating power spectral density using Welch's method the signal\n"
1466+
"is divided into overlapping segments and psd is estimated for each segment\n"
14671467
"and then averaged. The overlap is half of the segment length. The \n"
14681468
"psd-estimate is smoother with shorter segments.")
14691469
psdlayout = QHBoxLayout()
1470-
psdlayout.addWidget(QLabel("Length of segment used when estimating power spectral density"))
1470+
psdlayout.addWidget(QLabel("Length of segment used when estimating power spectral density *"))
14711471
psdlayout.addStretch(1)
14721472
psdlayout.addWidget(self.psdnpersegspinbox)
14731473
layout.addLayout(psdlayout)
@@ -1494,14 +1494,15 @@ def __init__(self, psdnorm, nperseg, nbins, twindec, parent=None):
14941494
self.twindecspinbox.setValue(twindec)
14951495
self.twindecspinbox.setToolTip("Number of decimals in the data processing time window from/to boxes.")
14961496
twindeclayout = QHBoxLayout()
1497-
twindeclayout.addWidget(QLabel("Number of decimals in data processing time window *"))
1497+
twindeclayout.addWidget(QLabel("Number of decimals in data processing time window **"))
14981498
twindeclayout.addStretch(1)
14991499
twindeclayout.addWidget(self.twindecspinbox)
15001500
layout.addLayout(twindeclayout)
15011501

15021502
# help text
15031503
helptext = QHBoxLayout()
1504-
helptext.addWidget(QLabel("* Close and re-open application for this setting to have effect"))
1504+
helptext.addWidget(QLabel("* Parameter 'nperseg' in scipy.signal.welch \n (signal length is used if smaller than specified value)\n"
1505+
"** Close and re-open application for this setting to have effect"))
15051506
helptext.addStretch(1)
15061507
layout.addLayout(helptext)
15071508

0 commit comments

Comments
 (0)