Пример #1
0
    def testFilterWavelet(self):
        numExamples = 100
        numFeatures = 16
        X = numpy.random.rand(numExamples, numFeatures)

        level = 10
        mode = "cpd"
        waveletStr = "db4"
        C = pywt.wavedec(X[0, :], waveletStr, mode, level=10)

        Xw = MetabolomicsUtils.getWaveletFeatures(X, waveletStr, level, mode)
        
        N = 10
        Xw2, inds = MetabolomicsUtils.filterWavelet(Xw, N)

        tol = 10**-6 
        self.assertEquals(inds.shape[0], N)
        self.assertTrue(numpy.linalg.norm( Xw[:, inds] - Xw2[:, inds] ) < tol)

        zeroInds = numpy.setdiff1d(numpy.arange(Xw.shape[1]), inds)
        self.assertTrue(numpy.linalg.norm(Xw2[:, zeroInds]) < tol)
Пример #2
0
mode = "cpd"

standardiser = Standardiser()
#X = standardiser.centreArray(X)

plotStyles = ['k-', 'k--', 'k-.', 'k:', 'k.']

for i in range(len(waveletStrs)):
    print(i)
    waveletStr = waveletStrs[i]
    Xw = MetabolomicsUtils.getWaveletFeatures(X, waveletStr, level, mode)
    C = pywt.wavedec(X[0, :], waveletStr, level=level, mode=mode)

    for j in range(len(Ns)):
        N = Ns[j]
        Xw2, inds = MetabolomicsUtils.filterWavelet(Xw, N)
        X2 = MetabolomicsUtils.reconstructSignal(X, Xw2, waveletStr, mode, C)

        errors[i, j] = numpy.linalg.norm(X - X2)

#Plot example wavelet after filtering 
waveletStr = "haar"
N = 100
Xw = MetabolomicsUtils.getWaveletFeatures(X, waveletStr, level, mode)
C = pywt.wavedec(X[0, :], waveletStr, level=level, mode=mode)
Xw2, inds = MetabolomicsUtils.filterWavelet(Xw, N)
X2 = MetabolomicsUtils.reconstructSignal(X, Xw2, waveletStr, mode, C)

plt.figure(3)
plt.plot(range(X.shape[1]), X[0, :], "k", label="raw")
plt.plot(range(X.shape[1]), X2[0, :], "r", label="filtered")