I would like to calculate the Teager Energy Kurtosis in a function in Python 3.8. I think this should also work with list comprehension.
I tried it with the following code, but I get an error message that the numpy object is not iterable. The variable data contains a list with measured values from an accelerometer.
def EO(data):
numerator = pow(len(data),2)*sum((pow(((pow(data[i+1],2) - pow(data[i],2))-(sum(pow(data[i+1],2) - pow(data[i],2))/len(data))),4)) for i in range(len(data)-1))
denominator = pow(sum(pow(((pow(data[i+1],2) - pow(data[i],2))-(sum(pow(data[i+1],2) - pow(data[i],2))/len(data))),2) for i in range(len(data)-1)),2)
energy_operator = numerator/denominator
return energy_operator
What is the general approach for implementing such formulas where you have to iterate multiple times, also of course with regard to efficiency. The dataset from which the values are to be calculated contains 133329 entries.
I guess the main problem is that the sum of the denominator contains another sum which has to be formed first. How to do that ?. Without list comprehension I would iterate through the whole dataset twice with a for loop to first get the average value and with that calculate the rest in the second iteration. The readability of this is then of course gone.
Any suggestions are welcome !
Cheers, Gerrit
EDIT: This is the working code without using list comprehension:
def EO_5(data):
summe = 0
num_sum = 0
den_sum = 0
for i in range(1,len(data)-1):
summe += pow(data[i],2)-((data[i-1])*(data[i+1]))
ave = summe/len(data)
for i in range(1,len(data)-1):
num_sum += pow((pow(data[i],2)-((data[i-1])*(data[i+1])))-ave,4)
den_sum += pow((pow(data[i],2)-((data[i-1])*(data[i+1])))-ave,2)
numerator = (len(data)-1)*num_sum
denominator = pow(den_sum,2)
return numerator/denominator