Calculate the sum CSD in the given frequency range(s). If the exact given frequencies are not available, the nearest frequencies will be chosen. Parameters ---------- fmin : float | list of float | None Lower bound of the frequency range in Hertz
(self, fmin=None, fmax=None)
| 199 | ) |
| 200 | |
| 201 | def sum(self, fmin=None, fmax=None): |
| 202 | """Calculate the sum CSD in the given frequency range(s). |
| 203 | |
| 204 | If the exact given frequencies are not available, the nearest |
| 205 | frequencies will be chosen. |
| 206 | |
| 207 | Parameters |
| 208 | ---------- |
| 209 | fmin : float | list of float | None |
| 210 | Lower bound of the frequency range in Hertz. Defaults to the lowest |
| 211 | frequency available. When a list of frequencies is given, these are |
| 212 | used as the lower bounds (inclusive) of frequency bins and the sum |
| 213 | is taken for each bin. |
| 214 | fmax : float | list of float | None |
| 215 | Upper bound of the frequency range in Hertz. Defaults to the |
| 216 | highest frequency available. When a list of frequencies is given, |
| 217 | these are used as the upper bounds (inclusive) of frequency bins |
| 218 | and the sum is taken for each bin. |
| 219 | |
| 220 | Returns |
| 221 | ------- |
| 222 | csd : instance of CrossSpectralDensity |
| 223 | The CSD matrix, summed across the given frequency range(s). |
| 224 | """ |
| 225 | if self._is_sum: |
| 226 | raise RuntimeError( |
| 227 | "This CSD matrix already represents a mean or sum across frequencies." |
| 228 | ) |
| 229 | |
| 230 | # Deal with the various ways in which fmin and fmax can be specified |
| 231 | if fmin is None and fmax is None: |
| 232 | fmin = [self.frequencies[0]] |
| 233 | fmax = [self.frequencies[-1]] |
| 234 | else: |
| 235 | if isinstance(fmin, numbers.Number): |
| 236 | fmin = [fmin] |
| 237 | if isinstance(fmax, numbers.Number): |
| 238 | fmax = [fmax] |
| 239 | if fmin is None: |
| 240 | fmin = [self.frequencies[0]] * len(fmax) |
| 241 | if fmax is None: |
| 242 | fmax = [self.frequencies[-1]] * len(fmin) |
| 243 | |
| 244 | if any(fmin_ > fmax_ for fmin_, fmax_ in zip(fmin, fmax)): |
| 245 | raise ValueError( |
| 246 | "Some lower bounds are higher than the corresponding upper bounds." |
| 247 | ) |
| 248 | |
| 249 | # Find the index of the lower bound of each frequency bin |
| 250 | fmin_inds = [self._get_frequency_index(f) for f in fmin] |
| 251 | fmax_inds = [self._get_frequency_index(f) + 1 for f in fmax] |
| 252 | |
| 253 | if len(fmin_inds) != len(fmax_inds): |
| 254 | raise ValueError("The length of fmin does not match the length of fmax.") |
| 255 | |
| 256 | # Sum across each frequency bin |
| 257 | n_bins = len(fmin_inds) |
| 258 | new_data = np.zeros((self._data.shape[0], n_bins), dtype=self._data.dtype) |