def denoise(nblck,filename,mode='sym', wv='sym5' ):
    from statsmodels.robust import mad
    #noisy_coefs = pywt.wavedec(nblck, 'sym5', level=5, mode='per')
    noisy_coefs = pywt.wavedec(nblck, wavelet=wv,   mode=mode) #level=5,  #dwt is for single level decomposition; wavedecoding is for more levels
    sigma = mad(noisy_coefs[-1])
    #uthresh=np.std(ca)/2
    uthresh = sigma*np.sqrt(2*np.log(len(nblck)))
    denoised = noisy_coefs[:]
    denoised[1:] = [pywt.threshold(i, value=uthresh,mode='soft') for i in denoised[1:]]
    signal = pywt.waverec(denoised, wavelet=wv, mode=mode)
    from matplotlib import pyplot as plt
    fig, axes = plt.subplots(1, 2, sharey=True, sharex=True,figsize=(8,4))
    ax1, ax2 = axes
    
    ax1.plot(signal)
    #ax1.set_xlim(0,2**10)
    ax1.set_title("Recovered Signal")
    ax1.margins(.1)
    
    ax2.plot(nblck)
    ax2.set_title("Noisy Signal")
    
    for ax in fig.axes:
        ax.tick_params(labelbottom=False, top=False, bottom=False, left=False,       right=False)
    fig.tight_layout()
    fig.savefig(filename+'_'+wv+'.pdf')
    plt.clf()
    return signal
Beispiel #2
0
def wavelet_trasnform(sig):
    mode = pywt.Modes.smooth
    w = 'coif3'
    w = pywt.Wavelet(w)
    
#    hasil=[]
#    for n in range(data.shape[0]):
        #dd = data[n,1:24]
    ca = []
    cd = []
    for i in range(5):
            (a, d) = pywt.dwt(sig, w, mode)
            ca.append(a)
            cd.append(d)
    rec_a = []
    rec_d = []
    for i, coeff in enumerate(ca):
        coeff_list = [coeff, None] + [None] * i
        rec_a.append(pywt.waverec(coeff_list, w))
    for i, coeff in enumerate(cd):
        coeff_list = [None, coeff] + [None] * i
        rec_d.append(pywt.waverec(coeff_list, w))
#    hasil.append(rec_a[0])
        
    return rec_a[0];
Beispiel #3
0
def waverec_lin(x,wavelet,mode="per",sc_lengths=None):
    """Reconstructs the timeseries from the wavelet-coefficients as returned by wavedec_lin.""" 
    assert len(x.shape) in [1,2], "Only 1d and 2d-arrays supported up to now!"
    if sc_lengths != None:
        assert sum(sc_lengths) == x.shape[0], "The values given for sc_lengths don't make sense!"
    else:
        sc_lengths = []
        l = x.shape[0]
        while l>1:
            l/=2
            sc_lengths.insert(0,l)
        sc_lengths.insert(0,l)
        assert sum(sc_lengths) == x.shape[0], "The values automatically calculated for sc_lengths don't make sense!"
            
    if len(x.shape) == 1:
        wts = []
        for j in range(len(sc_lengths)):
            offset = sum(sc_lengths[:j])
            wts.append(x[offset:offset+sc_lengths[j]])
        rv = waverec(wts,wavelet,mode=mode)
    else: #len(x.shape)==2
        for i in range(x.shape[1]):
            wts = []
            for j in range(len(sc_lengths)):
                offset = sum(sc_lengths[:j])
                wts.append(x[offset:offset+sc_lengths[j],i])
            if i==0:
                ar = waverec(wts,wavelet,mode=mode)
                rv = n.zeros((ar.shape[0],x.shape[1]),"d")
                rv[:,i] = ar
            rv[:,i] = waverec(wts,wavelet,mode=mode)
    return rv
Beispiel #4
0
def ApproximationsV(data, family, levels):
    """
    get approximation reconstrutions at different levels
    for a DWT family.
    returns levels+1 arrays with A[0]=full reconstruction,
    and A[1]=first approx, A[levels] is smoothest
    """
    # subtract off mean data
    meandata=np.mean(data)
    #meandata=0.0
    # get DWT coefficients
    coeffs = pywt.wavedec(data-meandata, family, mode='sym',level=Nlevels)
    lcoeffs=len(coeffs)
    for i,l in enumerate(coeffs):
        vl=np.var(l)
        l[:]=vl
        coeffs[i]=l
    #print "len coeffs",lcoeffs
    #for i in coeffs: print len(i)
    # reconstruct approximations
    A=[]
    c=pywt.waverec(coeffs,family,mode='sym')
    A.append(np.array(c)+meandata)
    for j in range(Nlevels,0,-1):
        coeffs[j][0:]=0.0
        c=pywt.waverec(coeffs,family,mode='sym')
        A.append(np.array(c)+meandata)
    return A
Beispiel #5
0
    def _wm_reverse(self, wd):
        if __debug__:
            debug('MAP', "Performing iDWT")
        signal = None
        wd_offsets = [0] + list(np.cumsum(self.lengths))
        nlevels = len(self.lengths)
        Ntime_points = self._intimepoints #len(time_points)
        # unfortunately sometimes due to padding iDWT would return longer
        # sequences, thus we just limit to the right ones

        for indexes in _get_indexes(wd.shape, self._dim):
            if __debug__:
                debug('MAP_', " %s" % (indexes,), lf=False, cr=True)
            wd_sample = wd[indexes]
            wd_coeffs = [wd_sample[wd_offsets[i]:wd_offsets[i+1]] for i in xrange(nlevels)]
            # need to compose original list
            time_points = pywt.waverec(
                wd_coeffs, wavelet=self._wavelet, mode=self._mode)
            if signal is None:
                newdim = list(wd.shape)
                newdim[self._dim] = Ntime_points
                signal = np.empty(newdim)
            signal[indexes] = time_points[:Ntime_points]
        if __debug__:
            debug('MAP_', "")
            debug('MAP', "Done iDWT. Total size %s" % (signal.shape, ))
        return signal
	def filterData(self, icurr, Fs):
		"""
			Denoise an ionic current time-series and store it in self.eventData

			:Parameters:
				- `icurr` :	ionic current in pA
				- `Fs` :	original sampling frequency in Hz
		"""
		# self.eventData=icurr
		self.Fs=Fs

		# Set up the wavelet
		w=pywt.Wavelet(self.waveletType)

		# Calculate the maximum wavelet level for the data length
		self.maxWaveletLevel=pywt.dwt_max_level(len(icurr), filter_len=w.dec_len)

		# Perform a wavelet decomposition to the specified level
		wcoeff = pywt.wavedec(icurr, w, mode='sym', level=self.waveletLevel)

		# Perform a simple threshold by setting all the detailed coefficients
		# up to level n-1 to zero
		thresh=np.std(wcoeff[-1])*self._thselect(wcoeff, self.waveletThresholdSubType)
		thrfunc=self.thrtypedict[self.waveletThresholdType]

		# print thresh, np.std(wcoeff[-1])
		wcoeff[1:] = [ thrfunc(wc, thresh) for wc in wcoeff[1:] ]
		# for i in range(1, self.waveletLevel):
		# 	wcoeff[-i]=np.zeros(len(wcoeff[-i]))

		# Reconstruct the signal with the thresholded wavelet coefficients
		self.eventData = pywt.waverec(wcoeff, self.waveletType, mode='sym')
Beispiel #7
0
    def filter(self, mode='soft'):

        if self.level > self.max_dec_level():
            clevel = self.max_dec_level()
        else:
            clevel = self.level

        # decompose
        coeffs = pywt.wavedec(self.sig, pywt.Wavelet(self.wt), \
                              mode=self.mode, \
                              level=clevel)

        # threshold evaluation
        th = sqrt(2 * log(len(self.sig)) * power(self.sigma, 2))

        # thresholding
        for (i, cAD) in enumerate(coeffs):
            if mode == 'soft':
                coeffs[i] = pywt.thresholding.soft(cAD, th)
            elif mode == 'hard':
                coeffs[i] = pywt.thresholding.hard(cAD, th)

        # reconstruct
        rec_sig = pywt.waverec(coeffs, pywt.Wavelet(self.wt), mode=self.mode)
        if len(rec_sig) == (len(self.sig) + 1):
            self.sig = rec_sig[:-1]
Beispiel #8
0
def wave_stein_filter(y, sigma, tau, w):
    coeffs = pywt.wavedec(y, w)
    threshold = sigma * tau
    hcoeffs = []
    for scale, x in enumerate(coeffs):
            hcoeffs.append(stein_thresholding(x, threshold))
    return pywt.waverec(hcoeffs, w)
Beispiel #9
0
def wave_semisoft_filter(y, sigma, tau, w, mu):
    coeffs = pywt.wavedec(y, w)
    threshold = sigma * tau
    hcoeffs = []
    for scale, x in enumerate(coeffs):
            hcoeffs.append(thresholding_semisoft(x, threshold, mu))
    return pywt.waverec(hcoeffs, w)
Beispiel #10
0
def soft_threshold(bins, tau, level=1, plot=False):
        coeffs = pywt.wavedec(bins, 'haar', level=level)
        old_coeffs = copy.deepcopy(coeffs) if plot else None
        coeffs[1:] = [sign(a) * np.maximum(0, abs(a)-tau) for a in coeffs[1:] ]
        filtered = pywt.waverec(coeffs, 'haar')

        if plot:
                from matplotlib import pyplot as pl
                fig = pl.figure(figsize=(15,8))
                fig.suptitle("Level %d Wavelet Decomposition" % level)
                fig.subplots_adjust(hspace=0.3, wspace=0.3)

                ax = fig.add_subplot(2,1,1)
                ax.plot(bins, label='Raw')
                ax.plot(filtered, label='Filtered')
                ax.legend()

                ax = fig.add_subplot(2,level,1+level)
                ax.set_title("Approximation coefficients")
                ax.plot(coeffs[0])
                for l in range(1,level):
                        ax = fig.add_subplot(2, level, 1+l+level)
                        ax.plot(old_coeffs[l])
                        ax.axhline(-tau, color='g'); ax.axhline(+tau, color='g')
                        ax.set_title("Level %d detail coefficients" % l)

                fig.savefig('wavelet-analysis.pdf')

        return filtered
Beispiel #11
0
def haar(vals, p=80):
	hC = pywt.wavedec(vals,'haar')
	cutVal = np.percentile(np.abs(np.concatenate(hC)), p)
	for A in hC:
		A[np.abs(A) < cutVal] = 0
	tVals = pywt.waverec(hC,'haar')
	return tVals[:len(vals)]
Beispiel #12
0
    def _call(self, coeff):
        """Compute the discrete 1D, 2D or 3D inverse wavelet transform.

        Parameters
        ----------
        coeff : `DiscreteLpVector`

        Returns
        -------
        arr : `DiscreteLpVector`

        """
        if len(self.range.shape) == 1:
            coeff_list = array_to_pywt_coeff(coeff, self.size_list)
            x = pywt.waverec(coeff_list, self.wbasis, self.mode)
            return self.range.element(x)
        elif len(self.range.shape) == 2:
            coeff_list = array_to_pywt_coeff(coeff, self.size_list)
            x = pywt.waverec2(coeff_list, self.wbasis, self.mode)
            return self.range.element(x)
        elif len(self.range.shape) == 3:
            coeff_dict = array_to_pywt_coeff(coeff, self.size_list)
            x = wavelet_reconstruction3d(coeff_dict, self.wbasis, self.mode,
                                         self.nscales)
            return self.range.element(x)
def generateCompression(cumHist, wavelet, compression):
	# Do a full multilevel wavelet decomposition until some level/resolution
	coeffs = pywt.wavedec(cumHist, wavelet)

	# Reduce trailing equal values from vectors
	vectors, lengths = reduceVectors(coeffs, 0.01)

	# Overwrite least significant detail coefficients in coeffs with 0-arrays (simulating removal)
	if compression < len(vectors):
		for i in range(compression,len(vectors)):
			arraySize = len(vectors[i])
			vectors[i] = np.zeros(arraySize)
	
	# Generate statistics
	stored = len(lengths)
	traildel = 0
	for i in range(0,min(len(vectors), compression)):
		stored += len(vectors[i])
		traildel += lengths[i] - len(vectors[i])
	
	# Restore trailing equal values
	restoredCoeffs = restoreVectors(vectors, lengths)

	# Do a full multilevel wavelet reconstruction from coeffs (zero-padded)
	recCumHist = pywt.waverec(restoredCoeffs, wavelet)
	return restoredCoeffs, recCumHist, stored, traildel, coeffs
Beispiel #14
0
    def filter(self):

        if self.level > self.max_dec_level():
            clevel = self.max_dec_level()
        else:
            clevel = self.level

        # decompose
        coeffs = pywt.wavedec(self.sig, pywt.Wavelet(self.wt), \
                              mode=self.mode, \
                              level=clevel)

        # threshold evaluation
        th = sqrt(2 * log(len(self.sig)) * power(self.sigma, 2))

        # thresholding
        for (i, cAD) in enumerate(coeffs):
            if i == 0:
                continue
            coeffs[i] = sign(cAD) * pywt.thresholding.less(abs(cAD), th)

        # reconstruct
        rec_sig = pywt.waverec(coeffs, pywt.Wavelet(self.wt), mode=self.mode)
        if len(rec_sig) == (len(self.sig) + 1):
            self.sig = rec_sig[:-1]
Beispiel #15
0
 def denoise(self, data, wavelet):
     noiseSigma = median(absolute(data - median(data))) / 0.6745
     levels = int(floor(log(len(data))))
     WC = pywt.wavedec(data, wavelet, level=levels)
     threshold = noiseSigma * sqrt(2 * log(len(data)))
     NWC = map(lambda x: pywt.thresholding.hard(x, threshold), WC)
     return pywt.waverec(NWC, wavelet)
def plot(data, w, title):
    print title
    w = pywt.Wavelet(w)
    a = data
    ca = []
    cd = []
    for i in xrange(5):
        (a, d) = pywt.dwt(a, w, mode)
        ca.append(a)
        cd.append(d)

    rec_a = []
    rec_d = []

    for i, coeff in enumerate(ca):
        coeff_list = [coeff, None] + [None] * i
        rec_a.append(pywt.waverec(coeff_list, w))

    for i, coeff in enumerate(cd):
        coeff_list = [None, coeff] + [None] * i
        rec_d.append(pywt.waverec(coeff_list, w))

    pylab.figure()
    ax_main = pylab.subplot(len(rec_a) + 1, 1, 1)
    pylab.title(title)
    ax_main.plot(data)
    pylab.xlim(0, len(data) - 1)
    pylab.locator_params(axis = 'y', nbins = 5)
    pylab.subplots_adjust(hspace=.5)

    for i, y in enumerate(rec_a):
        #print len(data), len(x), len(data) / (2**(i+1))
        ax = pylab.subplot(len(rec_a) + 1, 2, 3 + i * 2)
        ax.plot(y, 'r')
        pylab.xlim(0, 65)
        pylab.ylabel("Coef%d" % (i + 1))
        pylab.locator_params(axis = 'y', nbins = 5)


    for i, y in enumerate(rec_d):
        ax = pylab.subplot(len(rec_d) + 1, 2, 4 + i * 2)
        ax.plot(y, 'g')
        pylab.xlim(0, 65)
        #pylab.ylim(min(0,1.4*min(x)), max(0,1.4*max(x)))
        pylab.ylabel("D%d" % (i + 1))
        pylab.locator_params(axis = 'y', nbins = 5)
def sure_shrink_denoise(f, wave, v):
    true_coefs = pywt.wavedec(f, wave, level=None)
    res = [sure_shrink_fast(coef) for coef in true_coefs[1:]]
    den_coefs, thresholds = zip(*res)
    den_coefs = list(den_coefs)
    den_coefs.insert(0, true_coefs[0])
    f_denoised = pywt.waverec(den_coefs, wave)
    return f_denoised
def visu_shrink_denoise(f, wave, v):
    true_coefs = pywt.wavedec(f, wave, level=2)
    res = [visu_shrink(coef) for coef in true_coefs]
    den_coefs, thresholds = zip(*res)
    den_coefs = list(den_coefs)
    #den_coefs.insert(0, true_coefs[0])
    f_denoised = pywt.waverec(den_coefs, wave)
    return f_denoised
Beispiel #19
0
def idwt(E):
	# print E
	E_rep=E.tolist()[0:2]
	for i in range(1,int(pow(len(E),0.5))):
		E_rep.append(E[pow(2,i):pow(2,i+1)])
	E_idwt=pywt.waverec(E_rep,'db1')
	# print E_idwt
	return matrix(E_idwt)
Beispiel #20
0
def waveletpreprocess(X,threshold=20,threshold2=40):
	wave_coefs = pywt.wavedec(X,'haar',mode='sym',level=10)
	for coefs in wave_coefs[1:]:
		coefs[np.abs(coefs)<=threshold]=0
		bcast = [coef>threshold and coef<=threshold2 for coef in np.abs(coefs)]
		coefs[bcast] = np.sign(coefs[bcast])*(threshold2*(np.abs(coefs[bcast])-threshold)/(threshold2-threshold))
	filtered_signal=pywt.waverec(wave_coefs,'haar')
	return filtered_signal
Beispiel #21
0
 def rmatvec(x):
     x_list = []
     count = 0
     for el in b:
         n_el = np.asarray(el).size
         x_list.append(np.array(x[count:count+n_el]))
         count += n_el
     return pywt.waverec(x_list, wavelet, mode=mode)
Beispiel #22
0
def test_waverec_none():
    x = [3, 7, 1, 1, -2, 5, 4, 6]
    coeffs = pywt.wavedec(x, 'db1')

    # set some coefficients to None
    coeffs[2] = None
    coeffs[0] = None
    assert_(pywt.waverec(coeffs, 'db1').size, len(x))
Beispiel #23
0
def test_waverec_axis_db2():
    # test for fix to issue gh-293
    rstate = np.random.RandomState(0)
    data = rstate.standard_normal((16, 16))
    for axis in [0, 1]:
        coefs = pywt.wavedec(data, 'db2', axis=axis)
        rec = pywt.waverec(coefs, 'db2', axis=axis)
        assert_allclose(rec, data, atol=1e-14)
def waveletDenoise(data, threshold=1, wavelet='haar'):
    """ Wavelet denoise test """
    # Decompose
    wl_dec = pywt.wavedec(data, pywt.Wavelet(wavelet))
    # Threshold
    wl_thr = map(lambda x: pywt.thresholding.soft(x, threshold), wl_dec)
    # Reconstruct
    return pywt.waverec(wl_thr, wavelet)
Beispiel #25
0
def test_waverec_axes_subsets():
    rstate = np.random.RandomState(0)
    data = rstate.standard_normal((8, 8, 8))
    # test all combinations of 1 out of 3 axes transformed
    for axis in [0, 1, 2]:
        coefs = pywt.wavedec(data, 'haar', axis=axis)
        rec = pywt.waverec(coefs, 'haar', axis=axis)
        assert_allclose(rec, data, atol=1e-14)
Beispiel #26
0
def wavelet_smoother(ts,long_cut=np.inf,short_cut=0,wavelet='db4',preserve_gamma=True):
	''' Smooth ts using the wavelet specified.

		@long_cut is the longest wavelength to keep.
		@short_cut is the shortest wavelength to keep.
		@normalize will normalize positive and negative
		contributions after smoothing.
	'''
	smooth_long=True
	smooth_short=True
	if long_cut >= len(ts):
		smooth_long=False
	if short_cut < 1:
		smooth_short=False
	
	# No smoothing needed
	if smooth_long == False and smooth_short == False:
		return ts
	
	# Oversmoothed; Output 0.
	if long_cut <= short_cut or short_cut >= len(ts):
		return ts*0
	if preserve_gamma==True:
		avg=np.average(ts)
		rawts=ts-avg
	coeffs=pywt.wavedec(rawts,wavelet)
	
	#indices of short and long wavelengths.
	if smooth_short==True:
		si=np.log2((1.0*len(ts))/short_cut)
		if si >= len(coeffs):
			smooth_short=False
	if smooth_long == True:
		li=np.log2((1.0*len(ts))/long_cut)
		if li >= len(coeffs):
			return ts*0
	if smooth_long==True:
		intpart=int(li)
		fracpart=li-intpart
		for i in range(intpart):
			coeffs[i]*=0
		coeffs[intpart]*=1-fracpart

	if smooth_short==True:
		intpart=int(si)
		fracpart=si-intpart
		for i in range(intpart+1,len(coeffs)):
			coeffs[i]*=0
		coeffs[intpart]*=fracpart
	
	smoothts=pywt.waverec(coeffs,wavelet)

	# Normalization step, ensures the positive and negative
	# parts of smoothed match those of ts. "Preserves gamma"
	if preserve_gamma==True:
		smoothts+=avg
	return np.array(smoothts)
Beispiel #27
0
def test_waverec_accuracies():
    rstate = np.random.RandomState(1234)
    x0 = rstate.randn(8)
    for dt, tol in dtypes_and_tolerances:
        x = x0.astype(dt)
        if np.iscomplexobj(x):
            x += 1j*rstate.randn(8).astype(x.real.dtype)
        coeffs = pywt.wavedec(x, 'db1')
        assert_allclose(pywt.waverec(coeffs, 'db1'), x, atol=tol, rtol=tol)
        def dot(self, m):
            d = []

            if m.shape[0] != m.size:
                for i in xrange(m.shape[1]):
                    sizes_col = self.sizes[i]
                    sizes = np.zeros(len(sizes_col) + 1)
                    sizes[1:] = np.cumsum(sizes_col)
                    c = [m[:, i][sizes[k]:sizes[k + 1]] for k in xrange(0, len(sizes) - 1)]
                    d.append(pywt.waverec(c, self.name))
                return np.asarray(d).T
            else:
                sizes_col = self.sizes[0]
                sizes = np.zeros(len(sizes_col) + 1)
                sizes[1:] = np.cumsum(sizes_col)

                m = [m[sizes[k]:sizes[k + 1]] for k in xrange(0, len(sizes) - 1)]
                return pywt.waverec(m, self.name)
Beispiel #29
0
def test_waverec_all_wavelets_modes():
    # test 2D case using all wavelets and modes
    rstate = np.random.RandomState(1234)
    r = rstate.randn(80)
    for wavelet in wavelist:
        for mode in pywt.Modes.modes:
            coeffs = pywt.wavedec(r, wavelet, mode=mode)
            assert_allclose(pywt.waverec(coeffs, wavelet, mode=mode),
                            r, rtol=tol_single, atol=tol_single)
Beispiel #30
0
 def Denoise(sig):
     """
     Wavelet denoise
     """
     coeffs = pywt.wavedec(sig, 'db6', level=9)
     coeffs[-1] = np.zeros(len(coeffs[-1]))
     coeffs[-2] = np.zeros(len(coeffs[-2]))
     coeffs[0] = np.zeros(len(coeffs[0]))
     sig_filt = pywt.waverec(coeffs, 'db6')
     return sig_filt
Beispiel #31
0
    def filterOperation(self):
        w = pywt.Wavelet('db11')
        a = self.sequence
        ca, cd = [], []
        for i in range(2):
            (a, d) = pywt.dwt(a, w)
            ca.append(a)
            cd.append(d)

        rec_a, rec_d = [], []
        for i, coeff in enumerate(ca):
            coeff_list = [coeff, None] + [None] * i
            rec_a.append(pywt.waverec(coeff_list, w))

        for i, coeff in enumerate(cd):
            coeff_list = [None, coeff] + [None] * i
            rec_d.append(pywt.waverec(coeff_list, w))

        return rec_a[-1]
def FreqWarp(img, v, wavelet='db4',level=2, mode='constant'):
    "Applies warp based on a wavelet decomposition"
    if v <= 0: return img
    seq_len = img.shape[-1]
    level = 1 if level is None else level
    new_x = random_cum_noise_generator(img[:img.shape[-1] // 2], magnitude=v)
    coeff = pywt.wavedec(img.cpu(), wavelet, mode=mode, level=level)
    coeff[1:] = [CubicSpline(np.arange(c.shape[-1]), c, axis=-1)(new_x[:c.shape[-1]]) for c in coeff[1:]]
    output = img.new_tensor(pywt.waverec(coeff, wavelet, mode=mode)[..., :seq_len])
    return output
Beispiel #33
0
 def de_noising(x):
     wavelet = 'haar'
     level = 1
     coeff = pywt.wavedec(x, wavelet, mode="per")
     sigma = (1 / 0.6745) * maddest(coeff[-level])
     uthresh = sigma * np.sqrt(2 * np.log(len(x)))
     coeff[1:] = (pywt.threshold(i, value=uthresh, mode='hard')
                  for i in coeff[1:])
     x_dn = pywt.waverec(coeff, wavelet, mode='per')
     return x_dn
Beispiel #34
0
def doSingleCWTDenoise(x_in, wavelet="db4", level=1):
    x = copy.copy(x_in)
    coeff = pywt.wavedec(x, wavelet, mode="per")
    sigma = mad(coeff[-level])
    uthresh = sigma * np.sqrt(2 * np.log(len(x)))
    coeff[1:] = (pywt.threshold(i, value=uthresh, mode="soft")
                 for i in coeff[1:])
    y = pywt.waverec(coeff, wavelet, mode="per")
    del (x)
    return y
 def emgdenoise(emgraw):
     w = pywt.Wavelet('db8')
     maxlev = pywt.dwt_max_level(len(emgraw), w.dec_len)
     threshold = 0.06
     coeffs = pywt.wavedec(emgraw, 'db8', level=maxlev)
     for i in range(1, len(coeffs)):
         coeffs[i] = pywt.threshold(coeffs[i], threshold * max(coeffs[i]))
     dataout = pywt.waverec(coeffs, 'db8').astype('float32')
     #        print("denoise emg",dataout)
     return dataout
Beispiel #36
0
def apply_signal_processing(df):

    med = signal.medfilt(df['linear_acceleration.x'], kernel_size=9)

    coeffs = pywt.wavedec(df['linear_acceleration.x'], wavelet='haar')
    A = pywt.waverec(coeffs[:-3] + [None] * 3, 'haar')

    lp = lowpass_filt(df, 75)

    return med, A, lp
Beispiel #37
0
    def dot(self, m):
        d = []

        if m.shape[0] != m.size:
            for i in xrange(m.shape[1]):
                sizes_col = self.sizes[i]
                sizes = np.zeros(len(sizes_col) + 1)
                sizes[1:] = np.cumsum(sizes_col)
                c = [m[:, i][sizes[k]:sizes[k + 1]] for k in xrange(0, len(sizes) - 1)]
                d.append(pywt.waverec(c, self.name))
            return np.asarray(d).T

        else:
            sizes_col = self.sizes[0]
            sizes = np.zeros(len(sizes_col) + 1)
            sizes[1:] = np.cumsum(sizes_col)

            m = [m[sizes[k]:sizes[k + 1]] for k in xrange(0, len(sizes) - 1)]
            return pywt.waverec(m, self.name)
Beispiel #38
0
def lowpassfilter(signal, threshold=0.63, wavelet="db4"):
    """ Deconstruct the series using a waveform. Apply a threshold filter
        then reconstruct
    """
    threshold = threshold * np.nanmax(signal)
    coeff = pywt.wavedec(signal, wavelet, mode="per")
    coeff[1:] = (pywt.threshold(i, value=threshold, mode="soft")
                 for i in coeff[1:])
    reconstructed_signal = pywt.waverec(coeff, wavelet, mode="per")
    return reconstructed_signal
Beispiel #39
0
def cD3_features(x):
    Bands_D3 = np.empty((x.shape[0], x.shape[1], 448))
    for i in range(x.shape[0]):
        for ii in range(x.shape[1]):
            cA3, cD3, cD2, cD1 = Dwt(x[i, ii, :])
            cA3 = np.zeros(62)
            cD2 = np.zeros(117)
            cD1 = np.zeros(227)
            Bands_D3[i, ii, :] = pywt.waverec([cA3, cD3, cD2, cD1], db4)
    return Bands_D3
Beispiel #40
0
    def wavelet_decomposition(self, arr, name="..", wvlt='sym9', write=True):
        wavelet = pywt.Wavelet(wvlt)
        scaler = MinMaxScaler(
            feature_range=(0.0,
                           1.0))  # to normalize the data between 0 and 1.0
        mode = pywt.Modes.smooth  # unused now
        approximations = []
        details = []
        data = a = arr
        for i in range(6):
            (a, d) = pywt.dwt(a, wavelet)  # removed mode, jde to nahoru pak
            approximations.append(a)
            details.append(d)

        rec_details = []
        rec_approx = []

        acka = ""
        for i, coeff in enumerate(approximations):
            coeff_list = [coeff, None] + [None] * i
            fitted = scaler.fit_transform(
                pywt.waverec(coeff_list, wavelet).reshape(-1, 1))  # scale data
            a = []
            for b in fitted[0:250]:  # bad format, let's adapt it
                a.extend(b)

            acka += str(len(a)) + ";"
            rec_approx.append(a)

        for i, coeff in enumerate(details):
            coeff_list = [None, coeff] + [None] * i
            fitted = scaler.fit_transform(
                pywt.waverec(coeff_list, wavelet).reshape(-1, 1))
            a = []
            for b in fitted[0:250]:
                a.extend(b)
            acka += str(len(a)) + ";"
            rec_details.append(a)
        if write:
            with open("stats_wavs2.csv", "a") as f:
                f.write(wvlt + ";" + name + ";" + acka + '\n')

        return (rec_approx, rec_details)  # return tuple
Beispiel #41
0
def principal_components(num_of_components, pca, shape,coeff_slices,wave):
    selfies_output = 'selfie/principal/'
    components = pca.components_

    for x in range(num_of_components):
        img = np.array([components[x, i] for i in range(len(components[0]))])
        # coeffs_from_arr = pywt.array_to_coeffs(img, coeff_slices[x])
        # cam_recon = pywt.waverecn(coeffs_from_arr, wavelet='db14')


        c2a3, c2d3, c2d2, c2d1, cd3, cd2, cd1 = divide(img, coeff_slices[1])

        cf = [c2a3, c2d3, c2d2, c2d1]
        cd = pywt.waverec(cf, wavelet=wave)
        cf2 = [cd, cd3, cd2, cd1]
        cam_recon = pywt.waverec(cf2, wavelet=wave)

        img = scale(cam_recon, 255)
        cv2.imwrite(selfies_output + wave +  'principal' + str(x) + '.jpg',img.reshape(shape))
Beispiel #42
0
def denoise(data, family="db4", sigma=None, detrend=False):
    coefs = pywt.wavedec(data, family, mode="per")
    threshold = sigma * np.sqrt(2 * np.log(len(data)))
    new_coefs = coefs.copy()
    if detrend:
        new_coefs[0] = np.zeros_like(new_coefs[0])
    new_coefs[1:] = (pywt.threshold(c, value=threshold, mode="soft")
                     for c in coefs[1:])
    y = pywt.waverec(new_coefs, family, mode="per")
    return y
Beispiel #43
0
def waveletSmooth(x, wavelet="db4", level=1, title=None):
    print("Running waveletSmooth :")
    coeff = pywt.wavedec(x, wavelet, mode="per")
    sigma = mad(coeff[-level])
    uthresh = sigma * np.sqrt(2 * np.log(len(x)))
    coeff[1:] = (pywt.threshold(i, value=uthresh, mode="soft")
                 for i in coeff[1:])

    y = pywt.waverec(coeff, wavelet, mode="per")
    return y
Beispiel #44
0
def wavelet_dec(data):
    db4 = pywt.Wavelet('db4')
    A5, D5, D4, D3, D2, D1 = pywt.wavedec(data, db4, mode='symmetric', level=5)
    D5 = np.zeros(D5.shape[0])
    D4 = np.zeros(D4.shape[0])
    D3 = np.zeros(D3.shape[0])
    D2 = np.zeros(D2.shape[0])
    D1 = np.zeros(D1.shape[0])
    data_rec = pywt.waverec([A5, D5, D4, D3, D2, D1], db4)
    return data_rec
Beispiel #45
0
def multiscale_recompose(coeffs_array, split_indices, N=None):
    """
    inverse of multiscale_decompose
    """
    coeffs_list = np.split(coeffs_array, split_indices)
    log_timeseries = pywt.waverec(coeffs_list, 'haar', pywt.MODES.cpd)
    timeseries = np.exp(log_timeseries)
    if N is not None:
        timeseries = timeseries[:N]
    return timeseries
Beispiel #46
0
def smooth_wavelet2D(indat,
                     wave='sym8',
                     nlevel=5,
                     ncycle=10,
                     threshtype='hard'):
    """
    Compute the wavelet-denoised version of a set of profiles/waveforms.
  
    Required argument:
        - 'indat' = (nbins x nchans) data array.
  
    Default arguments: 
        - 'wave' = mother wavelet (default 'sym8') 
        - 'nlevel' = number of decomposition levels (default '5') 
        - 'ncycle' = number of circulant averages to compute (default '10') 
        - 'threshtype' = type of wavelet thresholding (default 'hard')
    """

    nbins = len(indat[:, 0])
    nchan = len(indat[0, :])

    outdat = np.zeros((nbins, nchan))

    # smooth each channel.
    for i in range(nchan):
        prof = indat[:, i]
        data = 0.
        # carry out translation-invariant wavelet denoising.
        for j in range(ncycle):
            m = j - ncycle / 2 - 1
            coeffs = pywt.wavedec(np.roll(prof, m), wave, level=nlevel)
            # get threshold value.
            lopt = np.median(np.fabs(coeffs[1])) / 0.6745 * np.sqrt(
                2. * np.log(nbins))
            # now do wavelet thresholding.
            for k in range(1, nlevel + 1):
                # hard threshold.
                if (threshtype == 'hard'):
                    (coeffs[k])[np.where(np.fabs(coeffs[k]) < lopt)] = 0.
                # or soft threshold.
                else:
                    (coeffs[k])[np.where(np.fabs(coeffs[k]) < lopt)] = 0.
                    (coeffs[k])[np.where(
                        coeffs[k] > lopt)] = (coeffs[k])[np.where(
                            coeffs[k] > lopt)] + lopt
                    (coeffs[k])[np.where(
                        coeffs[k] < -lopt)] = (coeffs[k])[np.where(
                            coeffs[k] < -lopt)] - lopt
            # reconstruct data.
            data = data + np.roll(pywt.waverec(coeffs, wave), -m)
        # save averaged profile.
        outdat[:, i] = data / np.float(ncycle)

    # return smoothed portrait.
    return outdat
Beispiel #47
0
def autoRemoveMuscular(icas):
    waveletLevel = 0
    if len(icas) > 0:
        # getting the level of decomposition for the wavelet transform
        # muscular artifacts appear in the Beta band 16-31Hz
        frq = icas[0].frequency
        while frq >= 16:
            frq = frq / 2
            waveletLevel += 1
    for ica in icas:
        maxs = []
        s1s = []
        s2s = []
        wavelets = []
        for c in ica.components:
            # applying soft thresholding to denoise, removing everything above 50Hz
            c = pywt.threshold(c, 50, 'less')
            # We use the Haar wavelet since it's the most similar to the muscular artifacts
            waveletC = pywt.wavedec(c, 'Haar', level=waveletLevel)
            wavelets.append(waveletC)
            # wavelet[0] = Ca2 wavelet[waveletLevel-1] = Cd2 wavelet[waveletLevel] = Cd1
            # padding Cd2 to make it same length of Cd1
            new = []
            cd2 = waveletC[waveletLevel - 1]
            for i in range(len(waveletC[waveletLevel - 1])):
                new.append(cd2[i])
                new.append(0.0)
            cd2 = np.array(new)
            # getting the wavelet power spectral density
            # elevating to power 2 the elements of Cd1
            cd1 = np.power(waveletC[waveletLevel], 2)
            # elevating to power 2 the elements of Cd2
            cd2 = np.power(cd2, 2)
            s1 = np.sum(cd1)
            s1s.append(s1)
            s2 = np.sum(cd2)
            s2s.append(s2)
            if s1 > s2:
                maxs.append(s1)
            else:
                maxs.append(s2)
        # getting the mean of all the peaks in the IC
        mean = np.mean(maxs)
        newComponents = []
        for i in range(len(ica.components)):
            if s1s[i] > mean:
                # making all samples zero since it's an EMG artifact
                wavelets[i][waveletLevel][:] = 0.0
            if s2s[i] > mean:
                # making all samples zero since it's an EMG artifact
                wavelets[i][waveletLevel - 1][:] = 0.0
            # recreating the component with the inverse wavelet transform
            component = pywt.waverec(wavelets[i], 'Haar')
            newComponents.append(component)
        ica.components = newComponents
Beispiel #48
0
def WaveletSmooth(time, flux, threshold=1, wavelet='db6', all=False):
    '''
    WORK IN PROGRESS - DO NOT USE

    Generate a wavelet transform of the data, clip on some noise level,
    then do inverse wavelet to generate model.

    Requires uniformly sampled data!
    If your data has gaps, watch out....
    '''
    _, dl, dr = FindGaps(time)

    if all is True:
        dl = [0]
        dr = [len(time)]

    model = np.zeros_like(flux)
    # now loop over every chunk of data and fit wavelet
    for i in range(0, len(dl)):

        flux_i = flux[dl[i]:dr[i]]

        # Do basic wavelet decontruct
        WC = pywt.wavedec(flux_i, wavelet)

        # now do thresholding
        # got some tips here:
        # https://blancosilva.wordpress.com/teaching/mathematical-imaging/denoising-wavelet-thresholding/
        # and:
        # https://pywavelets.readthedocs.org/en/latest/ref/idwt-inverse-discrete-wavelet-transform.html

        NWC = map(
            lambda x: pywt.thresholding.hard(
                x,
                threshold * np.sqrt(2. * np.log(len(flux_i))) * np.std(flux_i)
            ), WC)

        model_i = pywt.waverec(NWC, wavelet)
        # print(len(flux_i), len(model_i), len(model[dl[i]:dr[i]]))
        #
        # print(model_i[0:10])
        # print(model_i[-10:])
        if len(model_i) != len(model[dl[i]:dr[i]]):
            print("length error on gap ", i)
            print(len(flux_i), len(model_i), len(model[dl[i]:dr[i]]))
            model_i = model_i[1:]

        model[dl[i]:dr[i]] = model_i

    # WC = pywt.wavedec(flux, wavelet)
    # NWC = map(lambda x: pywt.thresholding.soft(x,threshold * np.sqrt(2.*np.log(len(flux))) * np.std(flux)), WC)
    # model = pywt.waverec(NWC, wavelet)

    print(len(model), len(time))
    return model
Beispiel #49
0
def visualize_reconstructed_signal_bylevel(coef_bank,
                                           signal=None,
                                           wv=None,
                                           wavelet_type='coif4',
                                           mode='symmetric',
                                           plot_approximation_bkgd=True):
    """
    Given DWT coefficient bank (list of array of DWT coefficients for each level), plot 
    """
    lvl = len(coef_bank) - 1
    # initialize filtered coefficient bank
    coef_bkgd = []
    coef_filt = []
    for coefs_lvl in coef_bank:
        coef_bkgd.append(np.zeros(len(coefs_lvl), ))
    if plot_approximation_bkgd:
        coef_filt = coef_bkgd.copy()
        coef_filt[0] = coef_bank[0]

    fig, ax = plt.subplots(int(np.ceil((lvl + 1) / 3)),
                           3,
                           sharex='col',
                           sharey='row',
                           figsize=(16, 10))
    color = ['k', 'm', 'b', 'g', 'orange', 'r']

    # plot the various levels of coefficients
    for l in range(lvl + 1):
        # get plot indices
        i = int(np.floor(l / 3))
        j = l % 3
        if not plot_approximation_bkgd:
            coef_filt = coef_bkgd.copy()
        coef_filt[l] = coef_bank[l]
        if l == 0:
            plot_title = 'Signal reconstruction using cA' + str(
                lvl) + ' coefficients'
        else:
            if plot_approximation_bkgd:
                plot_title = 'Signal reconstruction using cD' + str(
                    lvl - l + 1) + ' + cA5 coefficients'
            else:
                plot_title = 'Signal reconstruction using cD' + str(
                    lvl - l + 1) + ' coefficients'
        sig_rec_partial = pywt.waverec(coef_filt,
                                       wavelet_type,
                                       mode=mode,
                                       axis=-1)
        if wv is None:
            wv = np.arange(len(sig_rec_partial))
        if signal is not None:
            ax[i, j].plot(wv, signal, linewidth=0.25, color='grey')
        ax[i, j].plot(wv, sig_rec_partial, linewidth=1, color=color[l - 1])
        ax[i, j].title.set_text(plot_title)
    plt.show()
    def cal_cycle(self, data):

        if len(data) % 2 == 1:
            data = data[1:]
        # We will use the Daubechies(4) wavelet
        wname = self.wname

        data = np.atleast_2d(data)
        numwires, datalength = data.shape
        # Initialize the container for the filtered data
        fdata = np.empty((numwires, datalength))

        for i in range(numwires):
            # Decompose the signal
            c = pywt.wavedec(data[i, :], wname, level=self.maxlevel)
            #print c
            # Destroy the approximation coefficients
            #for j in self.filter_level:
            #    c[j][:] = 0
            # Reconstruct the signal and save it

            c[0][:] = pywt.threshold(c[0],
                                     np.percentile(abs(c[0]), 100) + 1, 'soft',
                                     0)
            c[1][:] = pywt.threshold(c[1],
                                     np.percentile(abs(c[1]), 100) + 1, 'soft',
                                     0)
            #c[2][:] = pywt.threshold(c[2], np.percentile(abs(c[2]), 100) + 1, 'soft', 0)
            c[3][:] = pywt.threshold(c[3],
                                     np.percentile(abs(c[3]), 100) + 1, 'soft',
                                     0)
            c[4][:] = pywt.threshold(c[4],
                                     np.percentile(abs(c[4]), 100) + 1, 'soft',
                                     0)
            c[5][:] = pywt.threshold(c[5],
                                     np.percentile(abs(c[5]), 100) + 1, 'soft',
                                     0)
            c[6][:] = pywt.threshold(c[6],
                                     np.percentile(abs(c[6]), 100) + 1, 'soft',
                                     0)
            c[7][:] = pywt.threshold(c[7],
                                     np.percentile(abs(c[7]), 100) + 1, 'soft',
                                     0)
            fdata[i, :] = pywt.waverec(c, wname)

            fdata = fdata.ravel()  # If the signal is 1D, return a 1D array

        wave = fdata
        spectrum = fft.fft(wave)
        freq = fft.fftfreq(len(wave))
        order = np.argsort(abs(spectrum)[:spectrum.size / 2])[::-1]
        cycle = 1 / freq[order[:20]]
        max_cycle = cycle[0] / 5

        return max_cycle
def plot_signal_decomp(data, w, title):
    """Decompose and plot a signal S.

    S = An + Dn + Dn-1 + ... + D1
    """
    w = pywt.Wavelet(w)
    a = data
    ca = []
    cd = []
    for i in range(5):
        (a, d) = pywt.dwt(a, w, mode)
        ca.append(a)
        cd.append(d)

    rec_a = []
    rec_d = []

    for i, coeff in enumerate(ca):
        coeff_list = [coeff, None] + [None] * i
        rec_a.append(pywt.waverec(coeff_list, w))

    for i, coeff in enumerate(cd):
        coeff_list = [None, coeff] + [None] * i
        rec_d.append(pywt.waverec(coeff_list, w))

    fig = plt.figure()
    ax_main = fig.add_subplot(len(rec_a) + 1, 1, 1)
    ax_main.set_title(title)
    ax_main.plot(data)
    ax_main.set_xlim(0, len(data) - 1)

    for i, y in enumerate(rec_a):
        ax = fig.add_subplot(len(rec_a) + 1, 2, 3 + i * 2)
        ax.plot(y, 'r')
        ax.set_xlim(0, len(y) - 1)
        ax.set_ylabel("A%d" % (i + 1))

    for i, y in enumerate(rec_d):
        ax = fig.add_subplot(len(rec_d) + 1, 2, 4 + i * 2)
        ax.plot(y, 'g')
        ax.set_xlim(0, len(y) - 1)
        ax.set_ylabel("D%d" % (i + 1))
Beispiel #52
0
 def wavelet(y, name):
     noiseSigma = 0.16
     levels = int(np.floor(np.log2(y.shape[0]))) - 3
     coeffs = pywt.wavedec(data=y, wavelet=name, level=levels)
     threshold = noiseSigma * np.sqrt(2 * np.log2(y.size))
     NewWaveletCoeffs = list(
         map(lambda x: pywt.threshold(x, threshold, mode='soft'), coeffs))
     #print(NewWaveletCoeffs)
     #print(levels)
     New_y = pywt.waverec(NewWaveletCoeffs, wavelet=name)
     return New_y
Beispiel #53
0
def cleanEELS_wavelet(data, threshold):
    wave = pywt.Wavelet("sym4")
    max_level = pywt.dwt_max_level(len(data), wave.dec_len)
    coeffs = pywt.wavedec(data, "sym4", level=max_level)
    coeffs2 = coeffs
    threshold = 0.1
    for ii in numba.prange(1, len(coeffs)):
        coeffs2[ii] = pywt.threshold(coeffs[ii],
                                     threshold * np.amax(coeffs[ii]))
    data2 = pywt.waverec(coeffs2, "sym4")
    return data2
Beispiel #54
0
def detrend_data(RR_array):
    new_coeffs=[]; 
    coeffs=pywt.wavedec(RR_array,'db3',mode='sp1',level=6);
    cA6,cD6,cD5,cD4,cD3,cD2,cD1 = coeffs;
    #print("cA is: " + str(cA));
    #print("cD is: " + str(cD));
    for i in range(len(cA6)):
        cA6[i]=0;    
    new_coeffs=cA6,cD6,cD5,cD4,cD3,cD2,cD1;
    RR_detrended=pywt.waverec(new_coeffs,'db3',mode='sp1');
    return RR_detrended
Beispiel #55
0
def test_waverec_all_wavelets_modes():
    # test 2D case using all wavelets and modes
    rstate = np.random.RandomState(1234)
    r = rstate.randn(80)
    for wavelet in wavelist:
        for mode in pywt.Modes.modes:
            coeffs = pywt.wavedec(r, wavelet, mode=mode)
            assert_allclose(pywt.waverec(coeffs, wavelet, mode=mode),
                            r,
                            rtol=tol_single,
                            atol=tol_single)
Beispiel #56
0
def WT(index_list, wavefunc='db4', lv=4, m=1, n=4, plot=False):
    '''
    WT: Wavelet Transformation Function

    index_list: Input Sequence;
   
    lv: Decomposing Level;
 
    wavefunc: Function of Wavelet, 'db4' default;
    
    m, n: Level of Threshold Processing
   
    '''

    # Decomposing
    coeff = pywt.wavedec(
        index_list, wavefunc, mode='sym',
        level=lv)  #  Decomposing by levels,cD is the details coefficient
    sgn = lambda x: 1 if x > 0 else -1 if x < 0 else 0  # sgn function

    # Denoising
    # Soft Threshold Processing Method
    for i in range(
            m, n + 1
    ):  #  Select m~n Levels of the wavelet coefficients,and no need to dispose the cA coefficients(approximation coefficients)
        cD = coeff[i]
        Tr = np.sqrt(2 * np.log2(len(cD)))  # Compute Threshold
        for j in range(len(cD)):
            if cD[j] >= Tr:
                coeff[i][j] = sgn(cD[j]) * (np.abs(cD[j]) - Tr
                                            )  # Shrink to zero
            else:
                coeff[i][j] = 0  # Set to zero if smaller than threshold

    # Reconstructing
    coeffs = {}
    for i in range(len(coeff)):
        coeffs[i] = copy.deepcopy(coeff)
        for j in range(len(coeff)):
            if j != i:
                coeffs[i][j] = np.zeros_like(coeff[j])

    for i in range(len(coeff)):
        coeff[i] = pywt.waverec(coeffs[i], wavefunc)
        if len(coeff[i]) > len(index_list):
            coeff[i] = coeff[i][:-1]

    if plot:
        denoised_index = np.sum(coeff, axis=0)
        data = pd.DataFrame({'CLOSE': index_list, 'denoised': denoised_index})
        data.plot(figsize=(10, 10), subplots=(2, 1))
        data.plot(figsize=(10, 5))

    return coeff
Beispiel #57
0
def GetSignalList(filepath,pre_mode='no',lvbo_mode='no',lvbo1=8,low=0,high=0,wave1='sym5',wave2=3):
    # this function just for test 
    print(filepath)
    filepathaa1=os.path.abspath(filepath)
    print('lalaal')
    print(filepathaa1)
    volt_data2 = sio.loadmat(filepath)  # r'E:\huihuang_mat\4/4-11.mat'
    volt_data2 = volt_data2['signal'] 
    volt_data2 = volt_data2.reshape(volt_data2.shape[0],)

    if pre_mode=="Min-Max Normalization":

        volt_min=volt_data2.min()
        volt_max=volt_data2.max()

        volt_data2=(volt_data2-volt_min)/(volt_max-volt_min)

        volt_data2=volt_data2*2-1
    if pre_mode=="Mean Normaliztion":
        volt_data2=(volt_data2 - volt_data2.mean())/volt_data2.std()

    if lvbo_mode=="High filter":
        high=float((2*high)/5000)
        b, a = signal.butter(lvbo1, high, 'highpass')
        volt_data2 = signal.filtfilt(b, a, volt_data2)
    if lvbo_mode=="Low filter":
        low=float((2*low)/5000)
        b, a = signal.butter(lvbo1, 0.02, 'lowpass')  
        volt_data2 = signal.filtfilt(b, a, volt_data2)    
    if lvbo_mode=="Brand filter":
        low=float((2*low)/5000)
        high=float((2*high)/5000)
        b, a = signal.butter(lvbo1, [low,high], 'bandpass')
        volt_data2 = signal.filtfilt(b, a, volt_data2)  

    if lvbo_mode=="Wave filter":   # 此处需要修改,只返回一个值,因为数据量太大,不支持有多个
        w = pywt.Wavelet(wave1)#选取小波函数
        a = volt_data2
        ca = []#近似分量
        cd = []#细节分量
        mode = pywt.Modes.smooth
        for i in range(5):
            (a, d) = pywt.dwt(a, w, mode)#进行5阶离散小波变换
            ca.append(a)
            cd.append(d)
       #选择特定的层数进行小波变换     
        coeff_list = [None,cd[wave2]] + [None] * wave2
        volt_data2=pywt.waverec(coeff_list, w)
    
    model_parfile='E:\\huihuang_mat\\4'
    #sio.savemat(model_parfile+'\\signal_wyk2.mat', {'signal':volt_data2})
    ecg = pywt.data.ecg()
    data2=volt_data2.tolist()
    return data2
Beispiel #58
0
def denoise1D(data, noiseSigma, wavelet='db3'):
    """performed the 1D denoising
    data : a 1D numpy array
    wavelet : the wavelet basis used, 
    
    """
    levels = ilog2(data.shape[0])
    WC = pywt.wavedec(data, wavelet, level=levels)
    threshold = noiseSigma * np.sqrt(2 * ilog2(data.size))
    NWC = map(lambda x: pywt.thresholding.soft(x, threshold), WC)
    return pywt.waverec(NWC, wavelet)
Beispiel #59
0
def wavelet_denoising(data):

    db4 = pywt.Wavelet('db4')

    coeffs = pywt.wavedec(data, db4,level=3)

    for i in range(1,len(coeffs)):
        coeffs[i]=pywt.threshold(coeffs[i],0.05*max(coeffs[i]))

    meta = pywt.waverec(coeffs, db4)
    return meta
Beispiel #60
0
 def DWT_synthesize(self, coefs, slices, wavelet_name="db10"):
     """
     Returns the original array from a numpy array that contains the
     wavelet coefficients.
     """
     wavelet = pywt.Wavelet(wavelet_name)
     samples = np.empty(coefs.shape, dtype=np.int32)
     decomposition_0 = pywt.array_to_coeffs(coefs[:, 0],
                                            slices,
                                            output_format="wavedec")
     decomposition_1 = pywt.array_to_coeffs(coefs[:, 1],
                                            slices,
                                            output_format="wavedec")
     samples[:, 0] = np.rint(
         pywt.waverec(decomposition_0, wavelet=wavelet,
                      mode="per")).astype(np.int32)
     samples[:, 1] = np.rint(
         pywt.waverec(decomposition_1, wavelet=wavelet,
                      mode="per")).astype(np.int32)
     return samples