Last active
February 28, 2018 08:29
-
-
Save TPeterW/d32e0fac2a41d3b8ded52ca719bac853 to your computer and use it in GitHub Desktop.
C++ Kurtosis with correction for bias (using openCV Mat)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cmath> | |
#include <opencv2/core.hpp> | |
using namespace cv; | |
using namespace std; | |
Mat_<float> | |
ft_kurtosis(cv::Mat_<float> timeseries, int numWindows, int windowLen) | |
{ | |
Mat_<float> kurtosis = Mat(numWindows, 1, CV_32F); | |
/* calculate mean */ | |
Mat row_mean; | |
reduce(timeseries, row_mean, 1, CV_REDUCE_AVG); | |
float m4, m2, avg; | |
for (int i = 0; i < numWindows; i++) { | |
m4 = m2 = 0; | |
avg = row_mean.at<float>(i, 0); | |
for (int j = 0; j < windowLen; j++) { | |
m4 += pow(timeseries.at<float>(i, j) - avg, 4); | |
m2 += pow(timeseries.at<float>(i, j) - avg, 2); | |
} | |
float n = windowLen; | |
if (n > 3 && m2 > 0) { | |
kurtosis.at<float>(i, 0) = 1.0 / (n - 2) / (n - 3) | |
* ((pow(n, 2) - 1.0) * ((m4 / n) / pow(m2 / n, 2)) - 3 * pow(n - 1, 2)); | |
} else { | |
kurtosis.at<float>(i, 0) = (m4 / n) | |
/ pow(m2 / n, 2) - 3; | |
} | |
} | |
return kurtosis; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment