예제 #1
0
def task_mad(data):
    """
    http://statsmodels.sourceforge.net/devel/generated/statsmodels.robust.scale.stand_mad.html
    """

    mad = stand_mad(data)

    return mad
예제 #2
0
def wavelet_transform(noisySignal):
    '''
    Method that applies wavelt tarnsform (Daubechies, decomposition level=12, filter size=12) to a given signal.
        Inputs: 
            - noisySignal: noisy signal to be transformed (array [m])            
        Output: (signal) wavelet transform of noisySignal (array [m])
    '''
    ########################################
    from statsmodels.robust import stand_mad
    ########################################
    levels = 12
    denoised = pywt.wavedec(noisySignal, 'db12', level=levels, mode='per')
    sigma = stand_mad(denoised[-1])
    threshold = sigma * np.sqrt(2 * np.log(len(noisySignal)))
    denoised = map(lambda x: pywt.thresholding.soft(x, value=threshold),
                   denoised)
    signal = pywt.waverec(denoised, 'db12', mode='per')
    return signal
예제 #3
0
파일: Denoise.py 프로젝트: devmax/RAIG
def visualize(data, wavelet, sigma=None):
    coefs = pywt.wavedec(data, wavelet)

    if sigma is None:
        sigma = stand_mad(coefs[-1])

    thresh = sigma*np.sqrt(2*np.log(len(data)))

    denoised = coefs[:]
    denoised[1:] = (pywt.thresholding.soft(i, value=thresh) for i in
                    denoised[1:])

    rec = pywt.waverec(denoised, wavelet)

    plt.plot(data, 'r')
    plt.plot(rec, 'g')
    plt.show()

    return rec
예제 #4
0
파일: wldenoise.py 프로젝트: vpaeder/terapy
 def apply_filter(self, array):
     if len(array.shape)!=1:
         return False
     mlv = pywt.dwt_max_level(array.shape[0],pywt.Wavelet(self.type))
     coeffs = pywt.wavedec(array.data, self.type, level=mlv, mode='per')
     if self.auto_threshold:
         sigma = stand_mad(coeffs[-1])
         uthresh = sigma*np.sqrt(2*np.log(len(coeffs)))
         self.threshold = uthresh
     else:
         uthresh = self.threshold
     denoised = coeffs[:]
     if self.thresholding==0: # Hard thresholding
         denoised[1:] = (pywt.thresholding.hard(i, value=uthresh) for i in denoised[1:])
     elif self.thresholding==1: # Soft thresholding
         denoised[1:] = (pywt.thresholding.soft(i, value=uthresh) for i in denoised[1:])
     signal = pywt.waverec(denoised, self.type, mode='per')
     array.data = signal[0:array.shape[0]]
     return True
예제 #5
0
            parnh[xi][yi] = minimize(
                res1,
                par2hned.params,
                args=(velx, spec, []),
            )
            resc[:, xi, yi] = parnh[xi][yi].residual

import pywt
from statsmodels.robust import stand_mad

wings = zeros(cube.shape)
for (x, y), val in ndenumerate(cube[0]):
    spectra2 = cube[:, x, y]
    noisy_coefs = pywt.wavedec(spectra2, 'db4', level=11, mode='per')
    sigma = stand_mad(noisy_coefs[-1])
    uthresh = sigma * np.sqrt(2 * np.log(len(spectra2)))
    denoised = noisy_coefs[:]

    denoised[:] = (pywt.thresholding.soft(i, value=uthresh)
                   for i in denoised[:])

    signal = pywt.waverec(denoised, 'db4', mode='per')
    wings[:, x, y] = signal[:-1]


def getImages(par):
    dv1 = ma.zeros((nh3bg.shape[0], nh3bg.shape[1]))
    sigma1 = ma.zeros((nh3bg.shape[0], nh3bg.shape[1]))
    I1 = ma.zeros((nh3bg.shape[0], nh3bg.shape[1]))
예제 #6
0
def removeBaselinesWave(cube):
    from statsmodels.robust import stand_mad
    from scipy import stats
    import numpy as np
    import pywt

    def plfunc(w0, I, sigma, k, b, x):
        d = -0.5 * (((x - w0) / sigma)**2)
        aa = np.exp(d) * I + k * (x - w0) + b
        return aa

    constants = np.array([0])
    for (x, y), val in np.ndenumerate(cube[1, :, :]):
        print(x, y)
        spectra = cube[:, x, y]

        frist = spectra - baseline(spectra, fitfunc, 600)
        sigma = np.std(frist)
        arrs = []
        ca = []
        pos = np.where(abs(frist) > 1.5 * sigma)[0]
        for i in range(len(pos) - 1):
            ca.append(pos[i])
            if pos[i + 1] - pos[i] != 1:
                if len(ca) > 1:
                    arrs.append(ca)
                    ca = []
        arrs = np.array(arrs)
        ids = []
        for i in range(len(arrs)):
            if len(arrs[i]) < 3:
                ids.append(i)
        arrs = np.delete(arrs, ids)

        w = []
        s = []
        I = []
        b = []
        k = []
        for ran in arrs:
            f = round(np.array(ran).mean())
            if (abs(frist[f]) >
                    1.5 * sigma) and f > 200 and f < len(spectra) - 200:
                params = Parameters()
                params.add('w0', f, min=f - 10, max=f + 10)
                params.add('I', spectra.max(), min=-10, max=10)
                params.add('sigma', 1, min=0.01, max=10)
                params.add('k', 0)
                params.add('b', 1)
                xr = np.arange(f - 100, f + 100, 1, dtype=int)
                minn = minimize(resgaus,
                                params,
                                args=(xr, spectra[f - 100:f + 100], constants))
                #                print('({0},{1}):{2:.0f}'.format(x,y,x))
                #                report_fit(minn.params)
                if minn.params['w0'].value not in w:
                    w.append(minn.params['w0'].value)
                    s.append(minn.params['sigma'].value)
                    I.append(minn.params['I'].value)
                    k.append(minn.params['k'].value)
                    b.append(minn.params['b'].value)
        spectra2 = spectra.copy()
        for i in range(len(w)):
            xr = np.arange(w[i] - s[i] * 3, w[i] + s[i] * 3, 1, dtype=int)
            model = plfunc(w[i], I[i], s[i], 0, 0, xr)
            if (spectra2[xr] - model).std() < spectra2[xr].std():
                #                plot(xr,plfunc(w[i],I[i],s[i],k[i],b[i],xr),'-g',linewidth=2)
                spectra2[xr] -= model

        noisy_coefs = pywt.wavedec(spectra2, 'db8', level=11, mode='per')
        sigma = stand_mad(noisy_coefs[-1])
        uthresh = 2 * sigma * np.sqrt(2 * np.log(len(spectra2)))
        denoised = noisy_coefs[:]

        denoised[1:] = (pywt.threshold(i, uthresh) for i in denoised[1:])

        signal = pywt.waverec(denoised, 'db8', mode='per')
        cube[:, x, y] = spectra - signal
예제 #7
0
    plt.plot(xx, data)
    plt.title('Original Signal')
    plt.show()

    # 调用wavedec()多级分解函数对数据进行小波变换
    # mode指定了数据补齐的方式
    #‘per’指周期延拓数据
    # 分解层数为9
    wavelet_coefs = pywt.wavedec(data, wavtag, level=3, mode='per')

    # 进行阈值value构造
    # 进行软阈值去噪
    # cA9为细节系数
    # cD9, cD8, cD7, cD6, cD5, cD4, cD3, cD2, cD1 为模糊系数
    # denoised_coefs格式为[array([...]),array([...]),...,array([...])]
    sigma = stand_mad(wavelet_coefs[-1])
    uthresh = sigma * np.sqrt(2 * np.log(len(data)))
    denoised_coefs = wavelet_coefs[:]
    denoised_coefs[1:] = (pywt.threshold(coefs, value=uthresh, mode='soft') for coefs in denoised_coefs[1:])
    #cA9, cD9, cD8, cD7, cD6, cD5, cD4, cD3, cD2, cD1 = denoised_coefs

    # 将去噪得到的系数进行处理,以方便存入txt
    # 首先将denoised_coefs中每个array转换tolist为列表,在append入noisy_coefs_list
    # 再将noisy_coefs_list中每个列表的float数据转换为str并以“ ”分隔
    # 将i_str进行LZW压缩
    # 返回的aIn为list,将其中每个0~256整数做参数,返回一个对应字符并拼接
    wavelet_coefs_list=[]
    for i in denoised_coefs:# i为array
        wavelet_coefs_list.append(i.tolist())
    storeData=''
    i_strAll=''