Skip to content

Commit e0865f5

Browse files
committed
Fix error computing spectral S/N when no noise window is available
1 parent 150e0d5 commit e0865f5

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

sourcespec/ssp_build_spectra.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ def _build_spectrum(config, trace):
399399

400400
def _build_uniform_weight(spec):
401401
weight = spec.copy()
402+
weight.snratio = None
402403
weight.data = np.ones_like(weight.data)
403404
weight.data_log = np.ones_like(weight.data_log)
404405
return weight
@@ -420,8 +421,8 @@ def _build_weight_from_frequency(config, spec):
420421
def _build_weight_from_ratio(spec, specnoise, smooth_width_decades):
421422
weight = spec.copy()
422423
weight.data /= specnoise.data
423-
# save data to raw_data
424-
weight.data_raw = weight.data.copy()
424+
# save signal-to-noise ratio before log10, smoothing, and normalization
425+
weight.snratio = weight.data.copy()
425426
# The inversion is done in magnitude units,
426427
# so let's take log10 of weight
427428
weight.data = np.log10(weight.data)
@@ -513,14 +514,17 @@ def _build_H(spec_st, specnoise_st=None, vertical_channel_codes=None,
513514

514515
def _check_spectral_sn_ratio(config, spec, specnoise):
515516
weight = _build_weight_from_noise(config, spec, specnoise)
517+
# if no noise window is available, snratio is not computed
518+
if weight.snratio is None:
519+
return
516520
if config.spectral_sn_freq_range is not None:
517521
sn_fmin, sn_fmax = config.spectral_sn_freq_range
518522
freqs = weight.get_freq()
519523
idx = np.where((sn_fmin <= freqs)*(freqs <= sn_fmax))
520524
else:
521-
idx = range(len(weight.data_raw))
525+
idx = range(len(weight.snratio))
522526
spectral_snratio =\
523-
weight.data_raw[idx].sum()/len(weight.data_raw[idx])
527+
weight.snratio[idx].sum()/len(weight.snratio[idx])
524528
spec.stats.spectral_snratio = spectral_snratio
525529
spec_id = spec.get_id()
526530
logger.info(

0 commit comments

Comments
 (0)