Пример #1
0
    def winRemoteEqnCoherence(self):
        """Calulates coherences for the remote reference solver equations

        For example, this is the coherence of Ex-HxR, Ex-HyR, Ey-HxR, Ey-HyR, Hx-HxR, Hx-HyR, Hy-HxR, Hy-HyR

        todo:
        Write more information in these comments

        Returns
        -------
        Dict : 
            Dictionary with statistic values, indexed by [evaluation frequency][statistic component]
        """
        # now calculate out the relevant coherencies
        # here we calculate the coherency between <Ex,HyR> and <Hy,HyR> for example
        for iOut, outChan in enumerate(self.outChans):
            for iIn, inChan in enumerate(self.inChans):
                for iRemote, remoteChan in enumerate(self.remoteChans):
                    # calculate powers
                    c1c1 = smooth1d(
                        self.getReferenceCrossPower(outChan, remoteChan) *
                        np.conjugate(
                            self.getReferenceCrossPower(outChan, remoteChan)),
                        self.winLen,
                        self.winType,
                    )
                    c2c2 = smooth1d(
                        self.getReferenceCrossPower(inChan, remoteChan) *
                        np.conjugate(
                            self.getReferenceCrossPower(inChan, remoteChan)),
                        self.winLen,
                        self.winType,
                    )
                    c1c2 = smooth1d(
                        self.getReferenceCrossPower(outChan, remoteChan) *
                        np.conjugate(
                            self.getReferenceCrossPower(inChan, remoteChan)),
                        self.winLen,
                        self.winType,
                    )
                    # now interpolate
                    c1c1 = self.interpolateToEvalFreq(c1c1)
                    c2c2 = self.interpolateToEvalFreq(c2c2)
                    c1c2 = self.interpolateToEvalFreq(c1c2)
                    # now calculate the nominator and denominator
                    cohNom = np.power(np.absolute(c1c2), 2)
                    cohDenom = c1c1 * c2c2
                    coh = (
                        cohNom / cohDenom
                    )  # cast as float - discard complex part (complex part should be zero anyway)
                    # now need the coherencies for the evaluation frequencies
                    # this involves interpolation
                    key = "{}{}R-{}{}R".format(outChan, remoteChan, inChan,
                                               remoteChan)
                    for iFreq, eFreq in enumerate(self.evalFreq):
                        self.outData[eFreq][key] = coh[iFreq]
        return self.getOutData()
Пример #2
0
    def smooth(self,
               smoothLen: int,
               smoothFunc: str,
               inplace: bool = False) -> Union[None, ResisticsBase]:
        """Smooth the power data

        Parameters
        ----------
        smoothLen : int
            The window length to use
        smoothFunc : str
            The window function
        inplace : bool, optional
            Whether to do the smoothing in place or return a new PowerData instance

        Returns
        -------
        None, PowerData
            Returns None if inplace is True, otherwise returns a new PowerData instance
        """
        from resistics.common.smooth import smooth1d

        newdata = np.empty(shape=self.data.shape, dtype="complex")
        for iPri in range(len(self.primaryChans)):
            for iSec in range(len(self.secondaryChans)):
                newdata[iPri, iSec] = smooth1d(self.data[iPri, iSec],
                                               smoothLen, smoothFunc)
        if inplace:
            self.data = newdata
            return None
        return PowerData(self.primaryChans, self.secondaryChans, newdata,
                         self.sampleFreq)
Пример #3
0
    def calculateRemoteSpectralMatrix(self):
        """Calculate out the cross power spectral matrix for the remote reference data

        The method calculates out the cross powers for the remote reference channels which will then be used in the other statistic calculations.

        The remote reference cross powers spectral matrix is a 3-D matrix of size:
        numRemoteChans * numRemoteChans * numFrequencies
        The elements of this are calculated by multiplying the spectra of one channel by the complex conjugate of the spectra of another channel.
        """
        # create the 3d array
        numRemoteChans = len(self.remoteChans)
        self.remoteSpectralMatrix = np.empty(shape=(numRemoteChans,
                                                    numRemoteChans,
                                                    self.dataSize),
                                             dtype="complex")
        # now need to go through the chans
        for ii in range(0, numRemoteChans):
            for jj in range(ii, numRemoteChans):
                chan1 = self.remoteChans[ii]
                chan2 = self.remoteChans[jj]
                self.remoteSpectralMatrix[ii, jj] = smooth1d(
                    self.remoteSpec[chan1] *
                    np.conjugate(self.remoteSpec[chan2]),
                    self.winLen,
                    self.winType,
                )
                if ii == jj:
                    # conjugate symmtry
                    self.remoteSpectralMatrix[jj, ii] = np.conjugate(
                        self.remoteSpectralMatrix[ii, jj])
Пример #4
0
    def calculateReferenceSpectralMatrix(self):
        """Calculate out the cross power spectral matrix between the site spectral data and the remote reference spectral data

        The reference cross powers spectral matrix is a 3-D matrix of size:
        numChans * numRemoteChans * numFrequencies
        The elements of this are calculated by multiplying a channel of the site spectral data by the complex conjugate of a channel from the remote reference. 
        """
        # cannot use conjugate symmetry in this case
        self.referenceSpectralMatrix = np.empty(shape=(self.numChans,
                                                       len(self.remoteChans),
                                                       self.dataSize),
                                                dtype="complex")
        for ii, chan1 in enumerate(self.specChans):
            for jj, chan2 in enumerate(self.remoteChans):
                self.referenceSpectralMatrix[ii, jj] = smooth1d(
                    self.spec[chan1] * np.conjugate(self.remoteSpec[chan2]),
                    self.winLen,
                    self.winType,
                )
def test_smooth1d() -> None:
    """Test single dimension smoothing"""
    from resistics.common.smooth import smooth1d
    import numpy as np
    import pytest

    with pytest.raises(ValueError):
        x = np.array([10])
        smooth1d(x, 11)
    with pytest.raises(ValueError):
        x = np.array([10, 5, 7])
        smooth1d(x, 7)
    # window length < 3
    x = np.array([10, 5, 7, 9, 7, 8])
    assert np.array_equal(smooth1d(x, 2), x)
    # do a smooth
    x = np.array([10, 5, 7, 9, 7, 8, 6, 7, 4, 2, 4, 6])
    expected = np.array(
        [8.75, 6.75, 7, 8, 7.75, 7.25, 6.75, 6, 4.25, 3, 4, 5.5])
    assert len(x) == len(expected)
    assert np.array_equal(smooth1d(x, 5), expected)