def Entropymethod2(self): maxl = DWTP(self.data).maxdeclevel(self.wtname) R = [] SN = [] for j in range(maxl): coeffs = DWTP(self.data).dwtdec(self.wtname, delevel=j + 1) er = np.median([abs(x) for x in coeffs[-(j + 1)]]) / 0.6745 sher = 0.5 * mt.log(2 * mt.pi * mt.e * er**2) ln = len(coeffs) EG = 0 egl = [] for i in range(ln): lce = coeffs[i] eg = 0 for ce in lce: eg = eg + ce**2 egl.append(eg) EG = EG + eg P = [] p = egl[0] / EG P.append(p) shfs = [] for i in range(1, ln): p = egl[i] / EG P.append(p) shf = 0 for p in P: shf = shf - p * mt.log(p) shfs.append(shf) r = shf / sher R.append(r) SN.append(sher) return SN
def EntropySelect(self): maxl = DWTP(self.data).maxdeclevel(self.wtname) R = [] dR = [] for j in range(maxl): coeffs = DWTP(self.data).dwtdec(self.wtname, delevel=j + 1) er = np.median([abs(x) for x in coeffs[-(j + 1)]]) / 0.6745 sher = 0.5 * mt.log(2 * mt.pi * mt.e * er**2) ln = len(coeffs) EG = 0 egl = [] for i in range(ln): lce = coeffs[i] eg = 0 for ce in lce: eg = eg + ce**2 egl.append(eg) EG = EG + eg P = [] p = egl[0] / EG P.append(p) shfs = [] for i in range(1, ln): p = egl[i] / EG P.append(p) shf = 0 for p in P: shf = shf - p * mt.log(p) shfs.append(shf) r = shf / sher R.append(r) if len(R) < 3: dl = len(R) else: for i in range(len(R) - 2): dr = (R[i + 2] - R[i + 1]) / (R[i + 1] - R[i]) dR.append(dr) maxdr = np.max(dR) dl = dR.index(maxdr) return dl
def WNrecgmethod(self): coeffs = DWTP(self.data).dwtdec(wtname=self.wtname) ln = len(coeffs) mps = [] for i in range(1, ln): ld = coeffs[i] if len(ld) < 6: break elif len(ld) < 200: sn = [x for x in range(1, len(ld) // 2, 1)] else: sn = [x for x in range(1, 200, 1)] wnr = acorr_ljungbox(ld, sn, return_df=False) # ld至少有六个元素 mp = np.average(wnr[1]) mps.append(mp) return mps
def Entropymethod1(self, delevel): coeffs = DWTP(self.data).dwtdec(wtname=self.wtname, delevel=delevel) ln = len(coeffs) shes = [] for i in range(1, ln): ces = coeffs[i] eg = 0 for j in range(len(ces)): eg = eg + ces[j]**2 she = 0 for j in range(len(ces)): p = ces[j]**2 / eg if p == 0: p = mt.e**(-10) she = she - p * mt.log(p) shes.append(she) return shes
def WNrecgSelect(self): coeffs = DWTP(self.data).dwtdec(wtname=self.wtname) ln = len(coeffs) sl = 0 stop = False for i in range(ln - 1, 0, -1): ld = coeffs[i] if len(ld) < 6: break elif len(ld) < 80: sn = [x for x in range(1, len(ld) // 2, 1)] else: sn = [x for x in range(1, 40, 1)] wnr = acorr_ljungbox(ld, sn, return_df=False) # ld至少有六个元素 for p in wnr[1]: if p < 0.05: stop = True # 任一延迟阶数的p值小于0.05,认为该层不是白噪声序列 else: pass if stop: break else: sl += 1 return sl
od2 = s.createPulse(50, 60, 4) od3 = s.createSpikes(4) ods = [od1, od2, od3] spds = [] hpds = [] thsemispds = [] thf1pds = [] thf2pds = [] thf3pds = [] dnames = ['Sine', 'Pulse', 'Spike'] thfname = ['soft', 'hard', 'thsemisf', 'thf1', 'thf2', 'thf3'] pdsmatrix = [spds, hpds, thsemispds, thf1pds, thf2pds, thf3pds] thps = [None, None, None, 0.5, 2, 2] for od in ods: dwd = DWTP(od) coeffs = dwd.dwtdec(wtname='db4', delevel=2) for i in range(len(pdsmatrix)): ncoeffs = dwd.thprocess(coeffs, thf=thfname[i], thp=thps[i]) pd = dwd.dwtrec(ncoeffs, wtname='db4') pdsmatrix[i].append(pd) ssnrs = [] hsnrs = [] thsemissnrs = [] thf1snrs = [] thf2snrs = [] thf3snrs = [] smses = [] hmses = []
from DenoiseResult import DenoiseRsult if __name__ == '__main__': s = Signal(1000) od1 = s.createSin(50, 60, 4) od2 = s.createPulse(50, 60, 4) od3 = s.createSpikes(4) ods = [od1, od2, od3] dnames = ['Sine', 'Pulse', 'Spike'] gthss = [] bsthss = [] gthpds = [] bsthpds = [] dl = 4 for od in ods: dwd = DWTP(od) coeffs = dwd.dwtdec(wtname='db4', delevel=dl) gths = [] bsths = [] for i in range(1, len(coeffs)): bthe = BestThEstimate(coeffs[i], thfunction='thf3', thp=2 + 2 * i) thsa = ThSearchAlgorithm() gth = ThSelect(coeffs[i]).DonohoThEx() bsth = thsa.FibonacciSearch(bthe.msesurefuction, [0, 1.5 * gth], 0.001) gths.append(gth) bsths.append(bsth) gthss.append(gths) bsthss.append(bsths) for i in range(len(ods)):
from GetActualSignal import GetActualSignal from LayerSelect import LayerSelect from WtProcess import DWTP from NoiseDetection import NoiseDetection if __name__ == '__main__': ad = GetActualSignal() ad.getwhvalue() od1 = ad.selectvalue(start=4000, end=5024) dwt = DWTP(od1) coeffs = dwt.dwtdec(wtname='db2') ps1 = NoiseDetection(od1, 'db2').WNrecgmethod() ps2 = NoiseDetection(od1, 'db2').Entropymethod1(4) print(ps1) print(ps2)
from SignalModel import Signal from LayerSelect import LayerSelect import matplotlib.pyplot as plt from WtProcess import DWTP if __name__ == '__main__': s = Signal(1000) od1 = s.createSin(50, 60, 4) od2 = s.createPulse(50, 60, 4) od3 = s.createSpikes(4) ods = [od1, od2, od3] lssm1 = [] lssm2 = [] for od in ods: coeffs = DWTP(od).dwtdec('db4') lsm1 = LayerSelect(od, 'db4').WNrecgSelect() lssm1.append(lsm1) lsm2 = LayerSelect(od, 'db4').EntropySelect() lssm2.append(lsm2) print(lssm1, lssm2)
from SignalModel import Signal import matplotlib.pyplot as plt from WtProcess import DWTP if __name__ == '__main__': s = Signal(1000) od1 = s.createSin(50, 60, 4) od2 = s.createPulse(50, 60, 4) od3 = s.createSpikes(4) ods = [od1, od2, od3] cfss = [] for od in ods: coeffs = DWTP(od).dwtdec('db4', delevel=3) cfss.append(coeffs) for i in range(1, 4): x = [x for x in range(len(cfss[0][-i]))] plt.figure() plt.subplot(3, 1, 1) plt.title('Sine signal d{0} coefficient'.format(i)) plt.plot(x, cfss[0][-i]) plt.subplot(3, 1, 2) plt.title('Pulse signal d{0} coefficient'.format(i)) plt.plot(x, cfss[1][-i]) plt.subplot(3, 1, 3) plt.title('Spike signal d{0} coefficient'.format(i)) plt.plot(x, cfss[2][-i]) plt.tight_layout() plt.show()
dnames = ['Sine', 'Pulse', 'Spike'] gthss = [] cvthss = [] gcvthss = [] srmthss = [] thsmatrix = [gthss, cvthss, gcvthss, srmthss] thmname = ['gth', 'cv', 'gcv', 'srm'] thfname = ['soft', 'hard', 'thsemisf', 'thf1', 'thf2', 'thf3'] gpds = [] cvpds = [] gcvpds = [] srmpds = [] pdsmatrix = [gpds, cvpds, gcvpds, srmpds] for od in ods: dwd = DWTP(od) coeffs = dwd.dwtdec(wtname='db4', delevel=4) gths = [] cvths = [] gcvths = [] srmths = [] for i in range(1, len(coeffs)): bthe = BestThEstimate(coeffs[i], thfunction=thfname[4], thp=4) thsa = ThSearchAlgorithm() gth = ThSelect(coeffs[i]).DonohoThEx() cvthhf = thsa.FibonacciSearch(bthe.cvalfunction, [0, 1.5 * gth], 0.001) cvth = (1 + mt.log(2) / mt.log(len(coeffs[i])))**0.5 * cvthhf gcvth = thsa.FibonacciSearch(bthe.gcvfunction, [0, 1.5 * gth], 0.001) srmth = thsa.FibonacciSearch(bthe.msesurefuction, [0, 1.5 * gth],
from GetActualSignal import GetActualSignal from ThresholdSelect import ThSelect from ThresholdFunction import ThFuction import numpy as np import pywt import matplotlib.pyplot as plt from DenoiseResult import DenoiseRsult from WtProcess import DWTP from AcSNR import AcSNR if __name__ == '__main__': ad = GetActualSignal() ad.getwhvalue() od1 = ad.selectvalue(start=0, end=23552) dwt = DWTP(od1) coeffs = dwt.dwtdec(wtname='db4', delevel=4) nscoeffs = dwt.thprocess(coeffs, thf='soft') nhcoeffs = dwt.thprocess(coeffs, thf='hard') spd = dwt.dwtrec(nscoeffs, wtname='db4') hpd = dwt.dwtrec(nhcoeffs, wtname='db4') ad.outputdata(startidex=0, ogdata=od1, pddata=spd) ssm = DenoiseRsult([], spd).smooth(od1) hsm = DenoiseRsult([], hpd).smooth(od1) slrepv = DenoiseRsult([], spd).lrepv(od1, 128) hlrepv = DenoiseRsult([], hpd).lrepv(od1, 128) print('ssm = {0}'.format(ssm)) print('hsm = {0}'.format(hsm)) print('slrepv = {0}'.format(slrepv)) print('hlrepv = {0}'.format(hlrepv))
dnames = ['Sine', 'Pulse', 'Spike'] gthss = [] srmthss = [] srmthss2 = [] thsmatrix = [gthss, gthss, srmthss, srmthss2] thmname = ['sth', 'hth', 'srm', 'srm'] thfname = ['soft', 'hard', 'thf3', 'thf3wp'] spds = [] hpds = [] thf3pds = [] thf3wppds = [] pdsmatrix = [spds, hpds, thf3pds, thf3wppds] dl = 5 for od in ods: dwd = DWTP(od) coeffs = dwd.dwtdec(wtname='sym6', delevel=dl) gths = [] gcvths = [] srmths = [] srmths2 = [] for i in range(1, len(coeffs)): bthe = BestThEstimate(coeffs[i], thfunction='thf3', thp=4) bthe2 = BestThEstimate(coeffs[i], thfunction='thf3', thp=2 + 2 * i) thsa = ThSearchAlgorithm() gth = ThSelect(coeffs[i]).DonohoThEx() srmth = thsa.FibonacciSearch(bthe.msesurefuction, [0, 1 * gth], 0.01) srmth2 = thsa.FibonacciSearch(bthe.msesurefuction, [0, 1 * gth], 0.01) gths.append(gth)
from DenoiseResult import DenoiseRsult from NoiseDetection import NoiseDetection if __name__ == '__main__': ad = GetActualSignal() ad.getwhvalue() od = ad.selectvalue(start=0) ln = len(od) // 1024 spd = [] hpd = [] npd = [] dl = 3 for k in range(ln): od1 = od[k * 1024:k * 1024 + 1024] swt = SWTP(od1) dwt = DWTP(od1) coeffs = swt.swtdec(wtname='db2', delevel=dl) dcoeffs = dwt.dwtdec(wtname='db2', delevel=dl) n1coeffs = swt.thprocess(coeffs, thf='soft') n2coeffs = swt.thprocess(coeffs, thf='hard') spd1 = swt.swtrec(n1coeffs, wtname='db2') hpd1 = swt.swtrec(n2coeffs, wtname='db2') shes = NoiseDetection(od1, 'db2').Entropymethod1(4) gth2s = [] gth3s = [] for i in range(1, len(coeffs)): gth = ThSelect(coeffs[i]).DonohoThEx() gth3 = ThSelect(dcoeffs[i]).EntropyTh2() if shes[i - 1] > 1.5: shes[i - 1] = 1.5
import matplotlib.pyplot as plt import numpy as np from BlockThMethod import BlockThMethod from WtProcess import DWTP from DenoiseResult import DenoiseRsult if __name__ == '__main__': s = Signal(1000) od1 = s.createSin(50, 60, 4) od2 = s.createPulse(50, 60, 4) od3 = s.createSpikes(4) ods = [od1, od2, od3] spds = [] blpds = [] for od in ods: coeffs = DWTP(od).dwtdec(wtname='db4', delevel=3) nscoeffs = DWTP(od).thprocess(coeffs, thf='soft') spd = DWTP(od).dwtrec(nscoeffs, 'db4') spds.append(spd) nbcoeffs = [coeffs[0]] for j in range(1, len(coeffs)): bces = BlockThMethod(coeffs[j]).dividedblock() bceths = BlockThMethod(coeffs[j]).blockths() bces.insert(0, [0]) # 使系数列表与阈值列表相匹配 a = len(bces) b = len(bceths) nbces = DWTP(od).thprocess(bces, ths=bceths, thf='soft') nbcoeff = BlockThMethod(coeffs[j]).recoverdata( nbces[1:]) # 除去添加的[0]系数 nbcoeffs.append(np.asarray(nbcoeff))
from ThSearchAlgorithm import ThSearchAlgorithm from DenoiseResult import DenoiseRsult from WtProcess import DWTP if __name__ == '__main__': s = Signal(1000) od1 = s.createSin(50, 60, 4) od2 = s.createPulse(50, 60, 4) od3 = s.createSpikes(4) ods = [od1, od2, od3] td1 = s.createSin(50, 60, 0) td2 = s.createPulse(50, 60, 0) td3 = s.createSpikes(0) tds = [td1, td2, td3] for i in range(len(ods)): dwd = DWTP(ods[i]) dwtd = DWTP(tds[i]) coeffs = dwd.dwtdec(wtname='db4', delevel=4) tcoeffs = dwtd.dwtdec(wtname='db4', delevel=4) gcvths = [] srmths = [] mses = [] bthe = BestThEstimate(coeffs[2], thfunction='thf3', thp=4) for k in range(1, 100): gcvth = bthe.gcvfunction(k / 10) srmth = bthe.msesurefuction(k / 10) thc = ThFuction(coeffs[2]).thf3(k / 10, 4) mse = DenoiseRsult(tcoeffs[2], thc).mse() gcvths.append(gcvth) srmths.append(srmth) mses.append(mse)