diff --git a/pysp2/util/normalized_derivative_method.py b/pysp2/util/normalized_derivative_method.py index c36acb9..27aaa0b 100644 --- a/pysp2/util/normalized_derivative_method.py +++ b/pysp2/util/normalized_derivative_method.py @@ -1,7 +1,7 @@ import numpy as np import xarray as xr -def central_difference(S, num_records=None, normalize=True): +def central_difference(S, num_records=None, normalize=True, baseline_to_zero=True): """ Compute fourth order derivative S'(t) using the @@ -39,6 +39,11 @@ def central_difference(S, num_records=None, normalize=True): for ch in channels: y = S[ch].isel(event_index=slice(0, num_records)).values + + # Baseline shift: make each record's minimum be 0 + if baseline_to_zero: + y_min = np.nanmin(y, axis=1, keepdims=True) # shape (n_records, 1) + y = y - y_min d = np.full_like(y, np.nan, dtype=np.float64) # Interior points (vectorized) diff --git a/tests/baseline/test_plot_normalized_derivative.png b/tests/baseline/test_plot_normalized_derivative.png index 3b1b894..d794486 100644 Binary files a/tests/baseline/test_plot_normalized_derivative.png and b/tests/baseline/test_plot_normalized_derivative.png differ diff --git a/tests/test_ndm.py b/tests/test_ndm.py index af06f75..d670855 100644 --- a/tests/test_ndm.py +++ b/tests/test_ndm.py @@ -5,7 +5,7 @@ def test_central_difference(): my_sp2b = pysp2.io.read_sp2(pysp2.testing.EXAMPLE_SP2B) my_ini = pysp2.io.read_config(pysp2.testing.EXAMPLE_INI) my_binary = pysp2.util.gaussian_fit(my_sp2b, my_ini, parallel=False) - dSdt = pysp2.util.central_difference(my_binary, normalize=False) + dSdt = pysp2.util.central_difference(my_binary, normalize=False, baseline_to_zero=False) np.testing.assert_almost_equal(dSdt['Data_ch4'].isel(event_index=5876, time=0).item(), 8.3333333333e6, decimal=2) @@ -15,7 +15,7 @@ def test_central_difference(): 1.5e7, decimal=2) assert np.isfinite(dSdt).all() - dSdt_norm = pysp2.util.central_difference(my_binary, normalize=True) + dSdt_norm = pysp2.util.central_difference(my_binary, normalize=True, baseline_to_zero=False) np.testing.assert_almost_equal(dSdt_norm['Data_ch4'].isel(event_index=5876, time=0).item(), 8.3333333333e6/-30168, decimal=2) np.testing.assert_almost_equal(dSdt_norm['Data_ch4'].isel(event_index=5876, time=99).item(), diff --git a/tests/test_vis.py b/tests/test_vis.py index 92b75ca..d4f109f 100644 --- a/tests/test_vis.py +++ b/tests/test_vis.py @@ -14,10 +14,10 @@ def test_plot_normalized_derivative(): my_sp2b = pysp2.io.read_sp2(pysp2.testing.EXAMPLE_SP2B) my_ini = pysp2.io.read_config(pysp2.testing.EXAMPLE_INI) my_binary = pysp2.util.gaussian_fit(my_sp2b, my_ini, parallel=False) - dSdt_norm = pysp2.util.central_difference(my_binary, normalize=True) + dSdt_norm = pysp2.util.central_difference(my_binary, normalize=True, baseline_to_zero=True) # Test the plotting function for channel 0 and record number 2 - ax = plot_normalized_derivative(dSdt_norm, record_no=304, chn=0) + ax = plot_normalized_derivative(dSdt_norm, record_no=499, chn=0) fig = ax.figure return fig \ No newline at end of file