Skip to content

Commit da7fdea

Browse files
author
Wenz
committed
Changed the way how the baseline rms is computed. Created new dtype raw_records which is identical to the previous records dtype.
Added a new field to the record dytpe called rms.
1 parent adc14b1 commit da7fdea

2 files changed

Lines changed: 43 additions & 10 deletions

File tree

strax/dtypes.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
"""
77
import numpy as np
88

9-
__all__ = ('interval_dtype record_dtype hit_dtype peak_dtype '
9+
__all__ = ('interval_dtype record_dtype raw_record_dtype hit_dtype peak_dtype '
1010
'DIGITAL_SUM_WAVEFORM_CHANNEL DEFAULT_RECORD_LENGTH').split()
1111

1212
DIGITAL_SUM_WAVEFORM_CHANNEL = -1
13-
DEFAULT_RECORD_LENGTH = 110
13+
DEFAULT_RECORD_LENGTH = 600
1414

1515

1616
# Base dtype for interval-like objects (pulse, peak, hit)
@@ -29,6 +29,7 @@
2929
# and int32 for per-channel waveforms (area in ADC x samples)
3030
]
3131

32+
3233
def raw_record_dtype(samples_per_record=DEFAULT_RECORD_LENGTH):
3334
"""Data type for a waveform raw_record.
3435
@@ -60,9 +61,9 @@ def record_dtype(samples_per_record=DEFAULT_RECORD_LENGTH):
6061
Length can be shorter than the number of samples in data,
6162
this indicates a record with zero-padding at the end.
6263
"""
63-
return interval_dtype + raw_record_dtype(samples_per_record) + [
64+
return raw_record_dtype(samples_per_record) + [
6465
(('Baseline RMS in ADC counts. data = baseline - data_orig',
65-
'baseline'), np.float32)
66+
'rms'), np.float32)
6667
]
6768

6869

strax/processing/pulse_processing.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import strax
99
export, __all__ = strax.exporter()
10-
__all__ += ['NO_RECORD_LINK']
10+
__all__ += ['NO_RECORD_LINK', 'baseline_rms']
1111

1212
# Constant for use in record_links, to indicate there is no prev/next record
1313
NO_RECORD_LINK = -1
@@ -31,26 +31,58 @@ def baseline(records, baseline_samples=40):
3131
# We only care about the channels in this set of records; a single .max()
3232
# is worth avoiding the hassle of passing n_channels around
3333
last_bl_in = np.zeros(records['channel'].max() + 1, dtype=np.int16)
34-
last_rms_in = np.zeros(records['channel'].max() + 1, dtype=np.int16)
3534

3635
for d_i, d in enumerate(records):
3736

3837
# Compute the baseline if we're the first record of the pulse,
3938
# otherwise take the last baseline we've seen in the channel
4039
if d.record_i == 0:
4140
bl = last_bl_in[d.channel] = d.data[:baseline_samples].mean()
42-
rms = _baseline_rms(d['data'], bl, baseline_samples)
4341
else:
4442
bl = last_bl_in[d.channel]
45-
rms = last_rms_in[d['channel']]
4643

4744
# Subtract baseline from all data samples in the record
4845
# (any additional zeros should be kept at zero)
4946
last = min(samples_per_record,
5047
d.pulse_length - d.record_i * samples_per_record)
5148
d.data[:last] = int(bl) - d.data[:last]
5249
d.baseline = bl
53-
d['rms'] = rms
50+
51+
52+
@export
53+
@numba.njit(cache=True, nogil=True)
54+
def baseline_rms(records, nsampels=40):
55+
"""
56+
Function which estimates the baseline rms within a certain number of samples.
57+
58+
The rms value is estimated for all samples with adc counts <= 0.
59+
60+
Args:
61+
records (np.array): Array of the data_kind raw_records or records.
62+
63+
Keyword Args:
64+
nsampels (int): First n samples on which the rms is estimated.
65+
66+
Note:
67+
68+
69+
Returns:
70+
np.array: array of the length of records containing the rms values.
71+
"""
72+
# Init result and temp_rms storage:
73+
res = np.zeros(len(records))
74+
last_rms_in = np.zeros(records['channel'].max() + 1, dtype=np.float32)
75+
76+
for ind, r in enumerate(records):
77+
if r['record_i'] == 0:
78+
rms = _baseline_rms(r['data'], r['baseline']%1, nsampels)
79+
last_rms_in[r['channel']] = rms
80+
res[ind] = rms
81+
else:
82+
# if higher fragment take previous rms value:
83+
res[ind] = last_rms_in[r['channel']]
84+
85+
return res
5486

5587

5688
@numba.njit(cache=True, nogil=True)
@@ -67,7 +99,7 @@ def _baseline_rms(d, b, n_samples=40):
6799
Keyword Args:
68100
n_samples (int): First n samples on which the rms is estimated.
69101
"""
70-
d = b - d
102+
d = b + d
71103
n = 0
72104
rms = 0
73105
for s in d[:n_samples]:

0 commit comments

Comments
 (0)