【发布时间】:2017-02-19 01:44:11
【问题描述】:
我编写了一个程序,(希望)计算两个信号之间的互相关。尽管我已经很好地搜索了应该如何执行计算,但我无法弄清楚一些重要的细节。我特别关心平均计算。似乎有些算法利用整个数据集的平均值来执行每个班次(或延迟)的相关性计算。换句话说,他们使用恒定的平均值。我什至发现了一些只计算一次分母的算法,将其用作其余延迟的常数值。但是,我认为平均值和分母都应该迭代计算,只考虑叠加范围内的数据。因此,我为这个程序编写了两个版本。它们似乎在较小的延迟下产生了非常相似的结果。我想知道哪个是正确的。
迭代平均:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
FILE *input1, *input2, *output;
int m = 0, n = 0, t;
float *VarA, *VarB, *Results, *Results2;
void open_inputs_and_output();
void count_and_check_lines();
void allocate_memory();
void read_data();
void allocate_memory2();
void write_output();
int main()
{
float SumAverageA = 0, SumAverageB = 0, AverageA, AverageB, SubA, SubB, SumAB = 0, SumAs = 0, SumBs = 0, Correl;
int p = 0, i, delay;
open_inputs_and_output();
count_and_check_lines();
rewind(input1);
rewind(input2);
allocate_memory();
read_data();
fclose(input1);
fclose(input2);
printf("How many lag steps from the origin do you want to calculate?\nIf you want as many steps as the number of input points, type -1.\n");
scanf("%i", &p);
if(p < -1)
{
printf("Bad number!\n");
exit(1);
}
else if(p == -1)
t = n;
else
t = p;
allocate_memory2();
printf("\nWait...\n\n");
for(delay = 0; delay < t; delay++)
{
for(i = delay; i < n; i ++)
{
SumAverageA += VarA[i];
SumAverageB += VarB[(i - delay)];
}
AverageA = SumAverageA / (n - delay);
AverageB = SumAverageB / (n - delay);
for(i = delay; i < n; i++)
{
SubA = VarA[i] - AverageA;
SubB = VarB[(i - delay)] - AverageB;
SumAB += (SubA * SubB);
SumAs += (SubA * SubA);
SumBs += (SubB * SubB);
}
Correl = SumAB / (sqrt(SumAs * SumBs));
Results[delay] = Correl;
SumAverageA = 0;
SumAverageB = 0;
SumAB = 0;
SumAs = 0;
SumBs = 0;
for(i = delay; i < n; i++)
{
SubB = VarB[i] - AverageB;
SubA = VarA[(i - delay)] - AverageA;
SumAB += (SubA * SubB);
SumAs += (SubA * SubA);
SumBs += (SubB * SubB);
}
Correl = SumAB / (sqrt(SumAs * SumBs));
Results2[delay] = Correl;
SumAverageA = 0;
SumAverageB = 0;
SumAB = 0;
SumAs = 0;
SumBs = 0;
}
printf("Calculations performed.\n");
free(VarA);
free(VarB);
write_output();
free(Results);
free(Results2);
fclose(output);
return 0;
}
void open_inputs_and_output()
{
input1 = fopen("C:\\...\\test.txt","r");
if (input1 == NULL)
{
printf("Error! Could not open input 1.\n");
exit(1);
}
else
printf("Input1 opening: OK.\n");
input2 = fopen("C:\\...\\test2.txt","r");
if (input2 == NULL)
{
printf("Error! Could not open input 2.\n");
exit(1);
}
else
printf("Input2 opening: OK.\n");
output = fopen("C:\\...\\out.txt","w");
if (output == NULL)
{
printf("Error! Could not open output.\n");
exit(1);
}
else
printf("Output opening: OK.\n");
}
void count_and_check_lines()
{
float k;
while(fscanf(input1,"%f",&k) == 1)
n++;
printf("n = %i\n", n);
while(fscanf(input2,"%f",&k) == 1)
m++;
printf("m = %i\n", m);
if(m != n)
{
printf("Error: Number of rows does not match!\n");
exit(1);
}
else
printf("Number of rows matches.\n");
}
void allocate_memory()
{
VarA = calloc(n, sizeof(float));
if(VarA == NULL)
{
printf("Could not allocate memory for VarA.\n");
exit(1);
}
VarB = calloc(m, sizeof(float));
if(VarA == NULL)
{
printf("Could not allocate memory for VarB.\n");
exit(1);
}
}
void read_data()
{
int i;
for(i = 0; i < n; i++)
fscanf(input1,"%f",&VarA[i]);
printf("Data A successfully read.\n");
for(i = 0; i < m; i++)
fscanf(input2,"%f",&VarB[i]);
printf("Data B successfully read.\n");
}
void allocate_memory2()
{
Results = calloc(t, sizeof(float));
if(Results == NULL)
{
printf("Could not allocate memory for Results.\n");
exit(1);
}
Results2 = calloc(t, sizeof(float));
if(Results2 == NULL)
{
printf("Could not allocate memory for Results2.\n");
exit(1);
}
}
void write_output()
{
int i;
for(i = t - 1; i > 0; i--)
fprintf(output,"-%i %f\n", i , Results2[i]);
for(i = 0; i < t; i++)
fprintf(output,"%i %f\n", i , Results[i]);
printf("Results written.\n");
}
恒定平均值:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
FILE *input1, *input2, *output;
int m = 0, n = 0, t;
float *VarA, *VarB, *Results, *Results2;
void open_inputs_and_output();
void count_and_check_lines();
void allocate_memory();
void read_data();
void allocate_memory2();
void write_output();
int main()
{
float SumAverageA = 0, SumAverageB = 0, AverageA, AverageB, SubA, SubB, SumAB = 0, SumAs = 0, SumBs = 0, Correl;
int p = 0, i, delay;
open_inputs_and_output();
count_and_check_lines();
rewind(input1);
rewind(input2);
allocate_memory();
read_data();
fclose(input1);
fclose(input2);
printf("How many lag steps from the origin do you want to calculate?\nIf you want as many steps as the number of input points, type -1.\n");
scanf("%i", &p);
if(p < -1)
{
printf("Bad number!\n");
exit(1);
}
else if(p == -1)
t = n;
else
t = p;
allocate_memory2();
printf("\nWait...\n\n");
for(i = 0; i < n; i ++)
{
SumAverageA += VarA[i];
SumAverageB += VarB[i];
}
AverageA = SumAverageA / n;
AverageB = SumAverageB / n;
for(delay = 0; delay < t; delay++)
{
for(i = delay; i < n; i++)
{
SubA = VarA[i] - AverageA;
SubB = VarB[(i - delay)] - AverageB;
SumAB += (SubA * SubB);
SumAs += (SubA * SubA);
SumBs += (SubB * SubB);
}
Correl = SumAB / (sqrt(SumAs * SumBs));
Results[delay] = Correl;
SumAverageA = 0;
SumAverageB = 0;
SumAB = 0;
SumAs = 0;
SumBs = 0;
for(i = delay; i < n; i++)
{
SubB = VarB[i] - AverageB;
SubA = VarA[(i - delay)] - AverageA;
SumAB += (SubA * SubB);
SumAs += (SubA * SubA);
SumBs += (SubB * SubB);
}
Correl = SumAB / (sqrt(SumAs * SumBs));
Results2[delay] = Correl;
SumAverageA = 0;
SumAverageB = 0;
SumAB = 0;
SumAs = 0;
SumBs = 0;
}
printf("Calculations performed.\n");
free(VarA);
free(VarB);
write_output();
free(Results);
free(Results2);
fclose(output);
return 0;
}
void open_inputs_and_output()
{
input1 = fopen("C:\\...\\test.txt","r");
if (input1 == NULL)
{
printf("Error! Could not open input 1.\n");
exit(1);
}
else
printf("Input1 opening: OK.\n");
input2 = fopen("C:\\...\\test2.txt","r");
if (input2 == NULL)
{
printf("Error! Could not open input 2.\n");
exit(1);
}
else
printf("Input2 opening: OK.\n");
output = fopen("C:\\...\\out.txt","w");
if (output == NULL)
{
printf("Error! Could not open output.\n");
exit(1);
}
else
printf("Output opening: OK.\n");
}
void count_and_check_lines()
{
float k;
while(fscanf(input1,"%f",&k) == 1)
n++;
printf("n = %i\n", n);
while(fscanf(input2,"%f",&k) == 1)
m++;
printf("m = %i\n", m);
if(m != n)
{
printf("Error: Number of rows does not match!\n");
exit(1);
}
else
printf("Number of rows matches.\n");
}
void allocate_memory()
{
VarA = calloc(n, sizeof(float));
if(VarA == NULL)
{
printf("Could not allocate memory for VarA.\n");
exit(1);
}
VarB = calloc(m, sizeof(float));
if(VarA == NULL)
{
printf("Could not allocate memory for VarB.\n");
exit(1);
}
}
void read_data()
{
int i;
for(i = 0; i < n; i++)
fscanf(input1,"%f",&VarA[i]);
printf("Data A successfully read.\n");
for(i = 0; i < m; i++)
fscanf(input2,"%f",&VarB[i]);
printf("Data B successfully read.\n");
}
void allocate_memory2()
{
Results = calloc(t, sizeof(float));
if(Results == NULL)
{
printf("Could not allocate memory for Results.\n");
exit(1);
}
Results2 = calloc(t, sizeof(float));
if(Results2 == NULL)
{
printf("Could not allocate memory for Results2.\n");
exit(1);
}
}
void write_output()
{
int i;
for(i = t - 1; i > 0; i--)
fprintf(output,"-%i %f\n", i , Results2[i]);
for(i = 0; i < t; i++)
fprintf(output,"%i %f\n", i , Results[i]);
printf("Results written.\n");
}
参考资料:
【问题讨论】:
-
欢迎来到 Stack Overflow。很抱歉,您在 5 个多小时内没有收到任何回复;这是相对不寻常的。由于您似乎已经找到了一些以各种方式做事的算法,也许答案是“当算法是应该使用的算法时,每个都是正确的”,而您的问题是您不确定应该在您的算法中使用哪种算法情况。您阅读了哪些参考资料?他们在算法何时适用时说了些什么?他们有没有谈到替代品以及为什么你不应该使用另一个? (请在您的问题中添加额外信息,而不是作为评论。)
-
他们对替代方法没有多说。我的方法只是基于对相关方程和互相关概念的分析的逻辑推论。它可能是正确的,但也可能是不正确的。数据处理和统计并不完全是我的领域。
标签: c statistics signal-processing cross-correlation