77
88import strax
99export , __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
1313NO_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