//聚类算法
//RANGE:离散阈值
double ShowMost(double Data[], int Count, double RANGE)
{
double cluster;
double nSum = 0;
int nCount = 0;
int nSection = 0;
double nAvg;
int nMaxSection = 0; //最密集的段的ID
int nMaxSectionCount = 0; //最密集的段的元素个数
double nResult = 0; //计算后得出的平均值
int nIndex = -1; //段结束的下标
int i;
//double RANGE = 0.02 ;
for (i=0; i<Count; i++)
{
nSum += Data[i];
nCount++;
nAvg = nSum/nCount;
if (!(nAvg+RANGE>=Data[i] && nAvg-RANGE<=Data[i]))
{
if (nCount-1>nMaxSectionCount) //如果当前段比上一个段还大,则当前段设置成最大段
//如果存在多个数量相同的最大段,则使用大于取最后一个最大段,使用大于等于取第一个最大段
{
nMaxSectionCount = nCount -1;
nMaxSection = nSection;
nResult = (nSum - Data[i])/nMaxSectionCount;
nIndex = i - 1;
}
nSum = Data[i];
nCount = 1;
nSection++;
}
}
//打印和验证结果
printf("nMaxSectionCount=%d ", nMaxSectionCount);
printf("nMaxSection=%d ", nMaxSection);
printf("nResult=%4.4f ", nResult);
printf("\r\n");
nSum = 0;
for (i=nIndex-nMaxSectionCount+1; i<=nIndex; i++)
{
printf("data[%d]=%4.4f ", i, Data[i]);
nSum += Data[i];
}
printf("\r\n");
printf("sum=%4.4f, avg=%4.4f ", nSum, nSum/nMaxSectionCount);
printf("\r\n");
cluster = nSum/nMaxSectionCount;
return cluster;
}
double almnLB[601];
double* almnarray(double AI_IN,int LB_CS)
{
int almnN = 0;
for(almnN = LB_CS - 1;almnN>=1;almnN--)
{
almnLB[almnN] = almnLB[almnN-1];
}
almnLB[0] = AI_IN;
return almnLB;
}
稳定 = 2 //卡涩
递减 = 1
无序 = 0 //震荡
递 增= -1
alarray = almnarray(OKQ1/OMQ1,inum);
printf("tr=%d ", tr);
printf("\r\n");
//for (i=0; i<inum; i++)
//{
// printf("alarray[%d]=%4.4f ", i, alarray[i]);
// printf("\r\n");
//}
//clustering algorithm
almn = ShowMost(alarray, inum, th);