我遇到了处理数组所有元素的问题。我有两个不同的数组(x[]
和y[]
),每个数组有40000个元素。我使用滑动窗口扫描每个元素中的5个元素,以计算块之间的交叉关联(x[]
的5个元素和y[]
的5个元素)。在计算之后,它将连续扫描到第二个窗口......并到达数组的末尾。如何获得Correlation的结果并将其存储在新数组中?输入为gc_us_dist_data
和gc_us_prox_data
,我希望在计算Correlation时收到一系列结果。希望得到每个人的帮助。
const int16_t gc_us_dist_data[SAMPLES] //4000 SAMPLES
const int16_t gc_us_prox_data[SAMPLES] //4000 SAMPLES
/* This function is real implementation of the sliding window algorithm */
int slide_window_01(int buffer1[], int N, int buffer_len){
int i = 0, j = 0, s;
int16_t dist[SAMPLES];
for(j=0 ; j + N < buffer_len; j++){
/* Window starts at index 0 and is of size N */
// printf("\nCurrent window :");
for(s =j; s<j+N; s++){
dist[SAMPLES] = buffer1[s];
}
}
}
int slide_window_02(int buffer2[], int N, int buffer_len){
int i = 0, j = 0, s;
int16_t prox[SAMPLES];
for(j=0 ; j + N < buffer_len; j++){
/* Window starts at index 0 and is of size N */
// printf("\nCurrent window :");
for(s =j; s<j+N; s++){
prox[SAMPLES] = buffer2[s];
}
}
}
/*------------- Cross Correlation between two signals -----------------------
---------------------------------------------------------------------------*/
// x = gc_us_dist_data, y = gc_us_prox_data
int correlation(int dist[], int prox[]){
int xy[SAMPLES], xsquare[SAMPLES], ysquare[SAMPLES];
int i, xsum, ysum, xysum, xsqr_sum, ysqr_sum;
float coeff[SAMPLES], num, deno;
xsum = ysum = xysum = xsqr_sum = ysqr_sum = 0;
/* find the needed data to manipulate correlation coeff */
for (i = 0; i < SAMPLES; i++)
{
xy[i] = dist[i] * prox[i];
xsquare[i] = dist[i] * dist[i];
ysquare[i] = prox[i] * prox[i];
xsum = xsum + dist[i];
ysum = ysum + prox[i];
xysum = xysum + xy[i];
xsqr_sum = xsqr_sum + xsquare[i];
ysqr_sum = ysqr_sum + ysquare[i];
}
num = 1.0 * ((SAMPLES * xysum) - (xsum * ysum));
deno = 1.0 * ((SAMPLES * xsqr_sum - xsum * xsum)* (SAMPLES * ysqr_sum - ysum * ysum));
/* calculate correlation coefficient */
for(int i=0; i < SAMPLES; i++)
{
coeff[i] = num / sqrt(deno);
}
}
int main(void)
{
int16_t buffer1[SAMPLES], dist[SAMPLES];
int16_t buffer2[SAMPLES], prox[SAMPLES];
memcpy(&buffer1, &gc_us_dist_data, sizeof buffer1); //copy all elements to buffer
memcpy(&buffer2, &gc_us_prox_data, sizeof buffer2 ); //copy all elements to buffer
const int N = 5;
int size1 = sizeof(buffer1)/ sizeof(buffer1[0]);
int size2 = sizeof(buffer2)/ sizeof(buffer2[0]);
slide_window_01(buffer1,N,size1);
slide_window_02(buffer2,N,size2);
correlation(dist, prox);
}
答案 0 :(得分:0)
slide_window_01
和slide_window_02
在函数调用后不会显示任何内容。
您只需将值填充到本地数组(例如int16_t dist[SAMPLES];
)中,该数组会在函数返回后立即丢失。
函数中的本地数组int16_t dist[SAMPLES];
与main中的dist[SAMPLES]
无关。所以 - 换句话说 - 当你调用函数时,main中的变量会改变而不是。
这也意味着您使用未初始化的数组调用correlation
,即您无法获得任何有意义的结果(实际上您有未定义的行为)。
在你的功能中
dist[SAMPLES] = buffer1[s];
这是非法的,因为有效索引是0 ... SAMPLES-1
函数correlation
也没有返回任何内容。所有结果都存储在本地,因此当函数返回时结果会丢失。
如果你想要一个函数来修改数组,你需要的只是传递作为指针完成的数组。
一个简单的例子:
#include <stdio.h>
void incrementArray(int* dst, int* src, int size)
{
int i;
for (i=0; i < size; ++i)
{
dst[i] = src[i] + 1;
}
}
int main(void)
{
int i;
int x[5] = {1, 2, 3, 4, 5};
int result[5];
// Print start values for x
for (i=0; i<5; ++i)
{
printf("%d ", x[i]);
}
printf("\n");
// Call function
incrementArray(result, x, 5);
// Print result
for (i=0; i<5; ++i)
{
printf("%d ", result[i]);
}
printf("\n");
return 0;
}
输出:
1 2 3 4 5
2 3 4 5 6