Python中一维小波的能量

时间:2016-06-06 13:58:03

标签: python matlab wavelet energy



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

enter image description here

其中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))

实施是否正确?

1 个答案:

答案 0 :(得分:1)

You can simplify your code a little bit:

    g.top <- ggplotGrob(top)
index <- subset(g.top$layout, name == "axis-b") 
names <- g.top$layout$name[g.top$layout$t<=index$t]
g.top <- gtable_filter(g.top, paste(names, sep="", collapse="|"))
# set height of remaining, empty rows to 0
for (i in (index$t+1):length(g.top$heights))
{
g.top$heights[[i]] <- unit(0, "cm")
}

# Table g1 will be the bottom table. We chop off everything above the panel
g.scatter <- ggplotGrob(scatter)
index <- subset(g.scatter$layout, name == "panel") 
# need to work with b here instead of t, to prevent deletion of background
names <- g.scatter$layout$name[g.scatter$layout$b>=index$b]
g.scatter <- gtable_filter(g.scatter, paste(names, sep="", collapse="|"))
# set height of remaining, empty rows to 0
for (i in 1:(index$b-1))
{
g.scatter$heights[[i]] <- unit(0, "cm")
}

# bind the two plots together
g.main <- rbind(g.top, g.scatter, size='first')

#grid.newpage()
#grid.draw(g.main)
# add the grob that holds the shadows
g.line <- gtable_filter(ggplotGrob(line), "panel") # extract the plot panel containing the shadows
index <- subset(g.main$layout, name == "panel") # locate where we want to insert the shadows
# find the extent of the two panels
t <- min(index$t)
b <- max(index$b)
l <- min(index$l)
r <- max(index$r)
# add grob
g.main <- gtable_add_grob(g.main, g.line, t, l, b, r)

# plot is completed, show
grid.newpage()
grid.draw(g.main)

can be rewritten as

coeffs[len(coeffs) - k - 1]

and you can do the squaring and summation as one NumPy operation (since you're using NumPy already)

coeffs[-k]