Based on this paragraph of a paper about brain-computer-interface, I wanna extract time-frequency domain features using discrete wavelet transform and then calculate energy and entropy with the giving equation.
So I've chosen pywt in python, and now I have the below code for getting wavelet and entropy from each frequency band ( for example I'm using D2 ), and here is the link of data:
import numpy as np
data = np.loadtxt('data.txt')
import pywt
cA5, cD5, cD4, cD3, cD2, cD1 = pywt.wavedec(data,'db4',mode='symmetric',level= 5)
Ent = 0
for d in data:
E = d**2
p = cD2 * E
Ent -= np.sum( np.abs( p * np.log(p) ) )
print(Ent)
But I'm getting nan
for entropy for every frequency band. How can I solve getting nan value for the entropy of the wavelet?
Your problem is with negative numbers in second frequency band's result from wavelet transform. The logarithm of a negative number results in nan
using numpy
and a ValueError: math domain error
raised exception using python's math
library.
BTW I think you've made a mistake in implementing the formula. I think this is the correct implementation:
ENT2 = -np.dot(np.log(np.square(cD2)), np.square(cD2))
ENG = np.square(cD2).sum()