Python 中一维小波的能量

Energy for 1-D wavelet in Python



我想知道在 Python 中是否存在一维小波能量的实现,与 Matlab '[Ea,Ed] = wenergy(C,L) ' 相同。 我试过自己写一个,但我不确定: 公式为:

其中 Dj 是细节向量,j = 1,2,...,ld 和 N1 是分解级别的数据长度。

import json
import pywt
f=open('DataFile.txt','r')
D=json.load(f)
f.close()
#create the wavelet function
db1 = pywt.Wavelet('db13')
#calculate the number of necessary decompositions
NbrDecomp= pywt.dwt_max_level(len(D), db1)+1
#Initialize an empty list to receive the Detail and Approximation
Vector = [None] * NbrDecomp
#we use the Wavelet decomposition in the pywt module 
Vector = pywt.wavedec(D, db1)
#Now Vector = [Approxiamtion N, Details N, Details N-1,.....] 
#Where N would be the number of decompositions

根据定义,k级能量为:

Energy(k)=np.sqrt(sum([x**2 for x in Vecktor[len(Vektor)-N-1-k]])/len(Vektor))

实施是否正确?

您可以稍微简化一下代码:

coeffs[len(coeffs) - k - 1]

可以改写为

coeffs[-k]

并且您可以将平方和求和作为一个 NumPy 运算来执行(因为您已经在使用 NumPy)

def Energy(coeffs, k):
    return np.sqrt(np.sum(np.array(coeffs[-k]) ** 2)) / len(coeffs[-k])