diff --git a/specparam/tests/utils/test_data.py b/specparam/tests/utils/test_data.py index 860d7f5a..c23d5651 100644 --- a/specparam/tests/utils/test_data.py +++ b/specparam/tests/utils/test_data.py @@ -9,6 +9,13 @@ ################################################################################################### ################################################################################################### +def test_get_freq_ind(): + + freqs = np.array([1, 2, 3, 4, 5]) + + assert get_freq_ind(freqs, 2.2) == 1 + assert get_freq_ind(freqs, 3.7) == 3 + def test_compute_average(): data = np.array([[0., 1., 2., 3., 4., 5.], diff --git a/specparam/utils/data.py b/specparam/utils/data.py index 097780ec..6661c473 100644 --- a/specparam/utils/data.py +++ b/specparam/utils/data.py @@ -30,6 +30,25 @@ ################################################################################################### ################################################################################################### +def get_freq_ind(freqs, freq): + """Get the index of the closest frequency value to a specified input frequency. + + Parameters + ---------- + freqs : 1d array + Frequency values. + freq : float + Frequency value to select closest index to. + + Returns + ------- + int + Index of closest value in `freqs` to `freq`. + """ + + return np.argmin(np.abs(freqs - freq)) + + def compute_average(data, average='mean'): """Compute the average across an array of data.