Calculation of kurtosis realized by C language

Recently, I want to use C to calculate some statistical features, and read some posts on the Internet, but the goal has not been achieved. In the past, these features were calculated using Matlab, so I wrote it in C according to the help of functions about kurtosis and skewness in matlab.

kurtosis


%在matlab中,使用计算峰度的函数kurtosis默认的flag是等于1,
%也就是上面两个式子中的K1。
kurtosis_a_1 = kurtosis(a,1);
kurtosis_a_0 = kurtosis(a,0);

The code for kurtosis calculation using C language is given below

#include<stdio.h>
#include<math.h>

double mean(double a[128], double len)
{
    
    
	double sum = 0;
	double mean = 0;
	//求和
	for (int i = 0; i < len; i++)
	{
    
    
		sum += a[i];
	}
	//计算均值
	mean = sum / len;
	return mean;
}

double kurtosis(double a[128], double len)
{
    
    
	//数组的长度要根据自己需求更改
	double kurtosis_mean = 0;
	double k1 = 0;
	double m2 = 0;
	double m4 = 0;
	double peakdnees = 0;
	double section_1 = 0;
	double section_2 = 0;

	kurtosis_mean = mean(a, len);
	//128是数组的长度
	for (int i = 0; i < 128; i++)
	{
    
    
		m4 = m4 + pow((a[i] - kurtosis_mean),4);
		m2 = m2 + pow((a[i] - kurtosis_mean),2);
	}
	k1 = len * (m4 / (pow(m2,2)));

	section_1 = (len - 1)/((len - 2) * (len - 3));
	section_2 = (len + 1) * k1 - 3*(len - 1);

	peakdnees = section_1*section_2 + 3;
	//根据要计算的峰度不同,选择返回K1或者peakdness
	return peakdnees;
}

Guess you like

Origin blog.csdn.net/weixin_49216787/article/details/125428828