用小波基估计背景信号
安装
不能直接安装:
pip install pywt
这是另外一个不同的库,如果不幸安装了,需要
pip uninstall pywt
pip uninstall PyWavelets
然后:
conda install PyWavelets
pip install PyWavelets
输出
添加背景信号
from pywt import wavedec,waverec
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(-100,100,50)
bg = 5
y = 50*np.exp(-t**2/(2*2^2)) +bg
plt.plot(t,y)
小波基估计背景
需要截去高频信息,然后用小波基估计出背景信号
iter = 10
x_filt = y
th = 1
for i in range(iter):
#wavelet transform
coeffs = wavedec(x_filt,'db6',level=5)
cA = coeffs[0]
cD = []
# 令高频信息为0,也就是令cDn全部设置为0
for j in range(1,len(coeffs)):
a = np.squeeze( np.array([np.zeros([np.size(coeffs[j]),1 ])]) )
cD.append(a)
cD.insert(0,cA)
# inverse wavelet transform by only using low-freq components
x_new = waverec(cD, 'db6')
if th > 0:
# cut off values over current estimated background level.
eps = np.sqrt(abs(x_filt))/2
ind = y > (x_new + eps)
x_filt[ind] = x_new[ind]+eps[ind]
# re-estimate background
coeffs = wavedec(x_filt,'db6',level=5)
cA = coeffs[0]
cD = []
for j in range(1,len(coeffs)): #排除cA5,cDn全部设置为0
a = np.squeeze( np.array([np.zeros([np.size(coeffs[j]),1 ])]) )
cD.append(a)
cD.insert(0,cA)
x_new = waverec(cD, 'db6')
黄色的点表示估计出来的背景