r""" The coherence between *x* and *y*. Coherence is the normalized cross spectral density: .. math:: C_{xy} = \frac{|P_{xy}|^2}{P_{xx}P_{yy}} Parameters ---------- x, y Array or sequence containing the data %(Spectral)s %(PSD)s noverlap : i
(x, y, NFFT=256, Fs=2, detrend=detrend_none, window=window_hanning,
noverlap=0, pad_to=None, sides='default', scale_by_freq=None)
| 737 | |
| 738 | @_docstring.interpd |
| 739 | def cohere(x, y, NFFT=256, Fs=2, detrend=detrend_none, window=window_hanning, |
| 740 | noverlap=0, pad_to=None, sides='default', scale_by_freq=None): |
| 741 | r""" |
| 742 | The coherence between *x* and *y*. Coherence is the normalized |
| 743 | cross spectral density: |
| 744 | |
| 745 | .. math:: |
| 746 | |
| 747 | C_{xy} = \frac{|P_{xy}|^2}{P_{xx}P_{yy}} |
| 748 | |
| 749 | Parameters |
| 750 | ---------- |
| 751 | x, y |
| 752 | Array or sequence containing the data |
| 753 | |
| 754 | %(Spectral)s |
| 755 | |
| 756 | %(PSD)s |
| 757 | |
| 758 | noverlap : int, default: 0 (no overlap) |
| 759 | The number of points of overlap between segments. |
| 760 | |
| 761 | Returns |
| 762 | ------- |
| 763 | Cxy : 1-D array |
| 764 | The coherence vector. |
| 765 | freqs : 1-D array |
| 766 | The frequencies for the elements in *Cxy*. |
| 767 | |
| 768 | See Also |
| 769 | -------- |
| 770 | :func:`psd`, :func:`csd` : |
| 771 | For information about the methods used to compute :math:`P_{xy}`, |
| 772 | :math:`P_{xx}` and :math:`P_{yy}`. |
| 773 | """ |
| 774 | if len(x) < 2 * NFFT: |
| 775 | raise ValueError( |
| 776 | "Coherence is calculated by averaging over *NFFT* length " |
| 777 | "segments. Your signal is too short for your choice of *NFFT*.") |
| 778 | Pxx, f = psd(x, NFFT, Fs, detrend, window, noverlap, pad_to, sides, |
| 779 | scale_by_freq) |
| 780 | Pyy, f = psd(y, NFFT, Fs, detrend, window, noverlap, pad_to, sides, |
| 781 | scale_by_freq) |
| 782 | Pxy, f = csd(x, y, NFFT, Fs, detrend, window, noverlap, pad_to, sides, |
| 783 | scale_by_freq) |
| 784 | Cxy = np.abs(Pxy) ** 2 / (Pxx * Pyy) |
| 785 | return Cxy, f |
| 786 | |
| 787 | |
| 788 | class GaussianKDE: |