相关系数运算



//互相关函数_from word
*#include
#include
#include

#define NN 10

void xcorr(float *r, int *x, int *y, int N);
void xx_yy(float *x1, int *x, float *y1, int *y, int N);
void specific_r(float *a, float *x1, float *y1, float r, int N);

int main()
{
int x[10] = { 1,1,2,2,3,4,5,6,7,8 };
int y[10] = { 1,3,4,5,6,7,8,9,5,2 };
float r[19] = { 0 };
// float x1[19] = { 0 };
// float y1[19] = { 0 };
// float F[19] = { 0 };
xcorr(r, x, y, NN);
// xx_yy(x1, x, y1, y, NN);
// specific_r(F, x1, y1, r, NN);
//printf("a");
getchar();
system("pause");
return 0;
}
//互相关运算
void xcorr(float *r, unsigned short *x, unsigned short *y, int N)
{
float sxy;
int delay, i, j;

for (delay = -N + 1; delay < N; delay++)
{
//Calculate the numerator
sxy = 0;
for (i = 0; i{
j = i + delay;
if ((j < 0) || (j >= N)) //The series are no wrapped,so the value is ignored
continue;
else
sxy += (x[i] * y[j]);
}

//Calculate the correlation series at "delay"
r[delay + N - 1] = sxy;
}
}
//自相关运算
void xx_yy(float *x1, int *x, float *y1, int *y, int N)
{
float sxx,syy;
int delay, i, j;

for (delay = -N + 1; delay < N; delay++)
{
//Calculate the numerator
sxx = 0;
for (i = 0; i{
j = i + delay;
if ((j < 0) || (j >= N)) //The series are no wrapped,so the value is ignored
continue;
else
sxx += (x[i] * x[j]);
}

//Calculate the correlation series at "delay"
x1[delay + N - 1] = sxx;
}
for (delay = -N + 1; delay < N; delay++)
{
//Calculate the numerator
syy = 0;
for (i = 0; i{
j = i + delay;
if ((j < 0) || (j >= N)) //The series are no wrapped,so the value is ignored
continue;
else
syy += (y[i] * y[j]);
}

//Calculate the correlation series at "delay"
y1[delay + N - 1] = syy;
}
}
//相关系数计算
void specific_r(float *a, float *x1, float *y1, float *r,int N)
{
int i;
for (i = 0; i < 2 * N; i++)
{
a[i] = r[i] / sqrt(x1[10] * y1[10]);
}
}*/



//互相关函数_from word
//该函数可以生成out_xcorr.txt文件
#include
#include
#include

#define NN 10

void xcorr(float *r, float *x, float *y, int N);
void xx_yy(float *x1, float *x, float *y1, float *y, int N);
void ZZR(float *t, float *x1, float *y1, float *r, int N);

int main()
{
float x[10] = { 1,2,3,4,5,6,7,8,9,10 };
//float y[10] = { 1,2,3,4,5,6,7,8,9,10 };
//float y[10] = { 10,9,8,7,6,5,4,3,2,1 };
//float y[10] = { 1,1,2,2,3,8,6,4,5,1 };
float y[10] = { 1,1,1,1,3,8,6,4,5,1 };
float r[19] = { 0 };
float x1[19] = { 0 };
float y1[19] = { 0 };
float F[19] = { 0 };
xcorr(r, x, y, NN);
xx_yy(x1, x, y1, y, NN);
ZZR(F, x1, y1, r, NN);
getchar();
system("pause");
return 0;
}

void xcorr(float *r, float *x, float *y, int N)
{
float sxy;
int delay, i, j;


for (delay = -N + 1; delay < N; delay++)
{
//Calculate the numerator
sxy = 0;
for (i = 0; i{
j = i + delay;
if ((j < 0) || (j >= N)) //The series are no wrapped,so the value is ignored
continue;
else
sxy += (x[i] * y[j]);
}

//Calculate the correlation series at "delay"
r[delay + N - 1] = sxy;
}
}

//自相关运算
void xx_yy(float *x1, float *x, float *y1, float *y, int N)
{
float sxx, syy;
int delay, i, j;

for (delay = -N + 1; delay < N; delay++)
{
//Calculate the numerator
sxx = 0;
for (i = 0; i{
j = i + delay;
if ((j < 0) || (j >= N)) //The series are no wrapped,so the value is ignored
continue;
else
sxx += (x[i] * x[j]);
}

//Calculate the correlation series at "delay"
x1[delay + N - 1] = sxx;
}
for (delay = -N + 1; delay < N; delay++)
{
//Calculate the numerator
syy = 0;
for (i = 0; i{
j = i + delay;
if ((j < 0) || (j >= N)) //The series are no wrapped,so the value is ignored
continue;
else
syy += (y[i] * y[j]);
}

//Calculate the correlation series at "delay"
y1[delay + N - 1] = syy;
}
}
//相关系数计算
void ZZR(float *t, float *x1, float *y1, float *r, int N)
{
int i;
for (i = 0; i <19; i++)
{
t[i] = r[i] / sqrt(x1[9] * y1[9]);
}
}

相关文档
最新文档