|
1 | 1 | """Utilities for working with data and models.""" |
2 | 2 |
|
3 | 3 | from itertools import repeat |
| 4 | +from functools import partial |
4 | 5 |
|
5 | 6 | import numpy as np |
6 | 7 |
|
| 8 | +from fooof.core.modutils import docs_get_section, replace_docstring_sections |
| 9 | + |
7 | 10 | ################################################################################################### |
8 | 11 | ################################################################################################### |
9 | 12 |
|
@@ -138,6 +141,55 @@ def interpolate_spectrum(freqs, powers, interp_range, buffer=3): |
138 | 141 | return freqs, powers |
139 | 142 |
|
140 | 143 |
|
| 144 | +def wrap_interpolate_spectrum(powers, freqs, interp_range, buffer): |
| 145 | + """Wraps interpolate function, organizing inputs & outputs to use `partial`.""" |
| 146 | + return interpolate_spectrum(freqs, powers, interp_range, buffer)[1] |
| 147 | + |
| 148 | + |
| 149 | +@replace_docstring_sections(docs_get_section(interpolate_spectrum.__doc__, 'Notes', end='Examples')) |
| 150 | +def interpolate_spectra(freqs, powers, interp_range, buffer=3): |
| 151 | + """Interpolate a frequency region across a group of power spectra. |
| 152 | +
|
| 153 | + Parameters |
| 154 | + ---------- |
| 155 | + freqs : 1d array |
| 156 | + Frequency values for the power spectrum. |
| 157 | + powers : 2d array |
| 158 | + Power values for the power spectra. |
| 159 | + interp_range : list of float or list of list of float |
| 160 | + Frequency range to interpolate, as [lowest_freq, highest_freq]. |
| 161 | + If a list of lists, applies each as it's own interpolation range. |
| 162 | + buffer : int or list of int |
| 163 | + The number of samples to use on either side of the interpolation |
| 164 | + range, that are then averaged and used to calculate the interpolation. |
| 165 | +
|
| 166 | + Returns |
| 167 | + ------- |
| 168 | + freqs : 1d array |
| 169 | + Frequency values for the power spectrum. |
| 170 | + powers : 2d array |
| 171 | + Power values, with interpolation, for the power spectra. |
| 172 | +
|
| 173 | + Notes |
| 174 | + ----- |
| 175 | + % copied in from interpolate_spectrum |
| 176 | +
|
| 177 | + Examples |
| 178 | + -------- |
| 179 | + Using simulated spectra, interpolate away line noise peaks: |
| 180 | +
|
| 181 | + >>> from fooof.sim import gen_group_power_spectra |
| 182 | + >>> freqs, powers = gen_group_power_spectra(5, [1, 75], [1, 1], [[10, 0.5, 1.0], [60, 2, 0.1]]) |
| 183 | + >>> freqs, powers = interpolate_spectra(freqs, powers, [58, 62]) |
| 184 | + """ |
| 185 | + |
| 186 | + tfunc = partial(wrap_interpolate_spectrum, freqs=freqs, |
| 187 | + interp_range=interp_range, buffer=buffer) |
| 188 | + powers = np.apply_along_axis(tfunc, 1, powers) |
| 189 | + |
| 190 | + return freqs,powers |
| 191 | + |
| 192 | + |
141 | 193 | def subsample_spectra(spectra, selection, return_inds=False): |
142 | 194 | """Subsample a group of power spectra. |
143 | 195 |
|
|
0 commit comments