@@ -538,6 +538,9 @@ def adc(self, expanded=False, inplace=False):
538538 # the input array is modified!
539539 def adc_inplace (p_signal , adc_gain , baseline , d_nan ):
540540 nanlocs = np .isnan (p_signal )
541+ # Convert to float64 for precise arithmetic to avoid precision loss
542+ # that can occur with float32 input at digital value boundaries
543+ p_signal = p_signal .astype ("float64" , copy = False )
541544 np .multiply (p_signal , adc_gain , p_signal )
542545 np .add (p_signal , baseline , p_signal )
543546 np .round (p_signal , 0 , p_signal )
@@ -826,10 +829,14 @@ def calc_adc_params(self):
826829 if np .where (np .isinf (self .p_signal ))[0 ].size :
827830 raise ValueError ("Signal contains inf. Cannot perform adc." )
828831
832+ # Convert to float64 for precise arithmetic to avoid precision loss
833+ # that can occur with float32 input when computing gain and baseline
834+ p_signal = self .p_signal .astype ("float64" , copy = False )
835+
829836 # min and max ignoring nans, unless whole channel is NAN.
830837 # Should suppress warning message.
831- minvals = np .nanmin (self . p_signal , axis = 0 )
832- maxvals = np .nanmax (self . p_signal , axis = 0 )
838+ minvals = np .nanmin (p_signal , axis = 0 )
839+ maxvals = np .nanmax (p_signal , axis = 0 )
833840
834841 for ch in range (np .shape (self .p_signal )[1 ]):
835842 adc_gain , baseline = self .calc_adc_gain_baseline (
@@ -842,8 +849,10 @@ def calc_adc_params(self):
842849 minvals = []
843850 maxvals = []
844851 for ch in self .e_p_signal :
845- minvals .append (np .nanmin (ch ))
846- maxvals .append (np .nanmax (ch ))
852+ # Convert to float64 for precise arithmetic
853+ ch_f64 = ch .astype ("float64" , copy = False )
854+ minvals .append (np .nanmin (ch_f64 ))
855+ maxvals .append (np .nanmax (ch_f64 ))
847856
848857 if any (x == math .inf for x in minvals ) or any (
849858 x == math .inf for x in maxvals
0 commit comments