1
votes

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.

enter image description here

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?

1
Where is data defined? // Please post a SSCCEKDecker
data is based on DEAP dataset.Haniyeh Asemi
That may be the case but in the code you've posted its undefined. There's not much help that can be given without the entire picture of what's happening. I see no issue with what you've posted so the issue must lie with the code not posted.KDecker
I've updated my question and now I'm using a simple data than you can download it and use.Haniyeh Asemi
It looks like you are squaring k, and not D_j(k).Karl Wilhelm

1 Answers

1
votes

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()