简体   繁体   English

Python中一维小波的能量

[英]Energy for 1-D wavelet in Python



I was wondering if there is an implementation for the Energy for 1-D wavelet in Python, the same as the Matlab '[Ea,Ed] = wenergy(C,L) '. 我想知道是否有Python中的1-D小波能量实现,与Matlab'[Ea,Ed] = wenergy(C,L)'相同。 I have tried to write one on my own but i am not sure of it: The formula is: 我试图自己写一个,但是我不确定:公式是:

在此处输入图片说明

Where Dj is supposed the detail vector, and j = 1,2,...,ld and N1 is the data length at the decomposition level. 其中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

According to the definition the energy at the level k is : 根据定义,k级能量为:

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

Was the implementation correct ? 实施正确吗?

You can simplify your code a little bit: 您可以稍微简化一下代码:

coeffs[len(coeffs) - k - 1]

can be rewritten as 可以改写成

coeffs[-k]

and you can do the squaring and summation as one NumPy operation (since you're using NumPy already) 并且您可以将NumPy运算进行平方和求和(因为您已经在使用NumPy)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM