存储到另一个阵列中的所有要素,以处理 [英] Store all elements of an array to another array in order to process

查看:97
本文介绍了存储到另一个阵列中的所有要素,以处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题等待处理数组中的所有元素。我有两个不同的阵列( X [] Y [] ),每一个都有40000元。我已经使用了滑动窗口来扫从每一个5个元素,以便计算块之间互相关( X [] Y [] )。计算后,它会不断地扫到第二个窗口..和一个数组的结尾。我怎样才能得到相关的结果,并将其存储在新的数组?输入是 gc_us_dist_data gc_us_prox_data ,我想计算的相关性时收到结果的数组。希望得到来自各位的帮助。

 常量int16_t gc_us_dist_data [样品] // 4000个样
    常量int16_t gc_us_prox_data [样品] // 4000个样
    / *这个功能是真正的实现滑动窗口算法* /
        INT slide_window_01(INT缓冲器1 [],INT N,INT buffer_len){        INT I = 0,J = 0,S;
        int16_t DIST [样品]        为(J = 0; J + N'LT; buffer_len; J ++){
          / *窗口开始于索引0是大小N *的/
          //的printf(\\ n当前窗口:);
           为(S =焦耳; S<Ĵ+ N; S ++){
                DIST [样品] =缓冲器1 [S]。
           }
        }
    }        INT slide_window_02(INT缓冲器2 [],INT N,INT buffer_len){            INT I = 0,J = 0,S;
            int16_t PROX [样品]            为(J = 0; J + N'LT; buffer_len; J ++){
                / *窗口开始于索引0是大小N *的/
                //的printf(\\ n当前窗口:);
                为(S =焦耳; S<Ĵ+ N; S ++){
                    PROX [样品] =缓冲器2 [S]。
                }
            }
        }
     / * -------------两个信号之间互相关-----------------------
      -------------------------------------------------- ------------------------- * /
    // X = gc_us_dist_data,Y = gc_us_prox_data    INT相关(INT DIST [],INT PROX []){        INT XY [样品],xsquare [样品],ysquare [样品]
        INT I,xsum,ysum,xysum,xsqr_sum,ysqr_sum;
        浮COEFF [样品],NUM,杰诺;        xsum = ysum = xysum = xsqr_sum = ysqr_sum = 0;        / *找到所需要的数据来操纵相关COEFF * /
        对于(i = 0; 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 *((样品* xysum) - (xsum * ysum));
        杰诺= 1.0 *((样品* xsqr_sum - xsum * xsum)*(样品* ysqr_sum - ysum * ysum));        / *计算相关系数* /
        的for(int i = 0; I<样本;我++)
        {
            COEFF [I] = NUM​​ /开方(杰诺);
        }    }INT主要(无效)
{
int16_t缓冲器1 [样品],DIST [样品]
    int16_t缓冲器2 [样品],PROX [样品]
    的memcpy(安培;缓冲器1,和放大器; gc_us_dist_data,sizeof的缓冲器1); //复制所有元素,以缓冲
    的memcpy(安培;缓冲器2,和放大器; gc_us_prox_data,sizeof的缓冲器2); //复制所有元素,以缓冲
    const int的N = 5;
    INT尺寸1 = sizeof的(缓冲器1)/的sizeof(缓冲器1 [0]);
    INT size2个= sizeof的(缓冲器2)/的sizeof(缓冲器2 [0]);
    slide_window_01(缓冲器1,N,尺寸1);
    slide_window_02(缓冲器2,N,尺寸2);    相关(DIST,PROX);
}


解决方案

slide_window_01 slide_window_02 并没有什么是函数调用后可见。

您只需填写值到本地阵列(例如 int16_t DIST [样品]; )。这是因为一旦丢失该函数返回

本地阵列 int16_t DIST [样品]; 在功能无关的 DIST [样品] 主。所以 - 换句话说 - 主变量确实的不可以的变化,当你调用函数

这也意味着,你叫相关未初始化数组,即你不会得到任何有意义的结果(事实上你已经未定义行为)。

在功能,您有

  DIST [样品] =缓冲器1 [S]。

这是非法的有效指标均 0 ...样品-1

功能相关不返回任何东西。所有的结果都存储在本地这样的成绩时丢失函数返回。

如果你想有一个函数修改数组中的所有你需要的是通过它作为指针完成的阵列。

一个简单的例子:

 的#include<&stdio.h中GT;无效incrementArray为(int * DST,为int * SRC,诠释大小)
{
    INT I;
    对于(i = 0; I<大小; ++ I)
    {
        DST由[i] = SRC [I] + 1;
    }
}INT主要(无效)
{
    INT I;
    INT X [5] = {1,2,3,4,5};
    INT结果[5];    对于x //打印开始值
    对于(I = 0; I&小于5 ++ⅰ)
    {
        的printf(%d个X [I]);
    }
    的printf(\\ n);    //调用函数
    incrementArray(结果中,x,5);    //打印结果
    对于(I = 0; I&小于5 ++ⅰ)
    {
        的printf(%d个,结果[I]);
    }
    的printf(\\ n);    返回0;
}

输出:

  1 2 3 4 5
2 3 4 5 6

I have got a problem with proccessing all elements of an array. I have two different arrays ( x[] and y[] ), each one has 40000 elements. I have used the Sliding Window to sweep 5 elements from each one in order to compute Cross Correlation between blocks (5 elements of x[] and 5 elements of y[]). After computing, it will continuously sweep to second Window .. and to the end of an array. How can I get the results of Correlation and store it in new array?. The inputs are gc_us_dist_data and gc_us_prox_data and I would like to receive an array of results when computing Correlation. Hope to receive the help from everybody.

    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);
}

解决方案

slide_window_01 and slide_window_02 does nothing that are visible after the function calls.

You just fill values into a local array (e.g. int16_t dist[SAMPLES];) which is lost as soon as the function returns.

The local array int16_t dist[SAMPLES]; in the function has nothing to do with the dist[SAMPLES] in main. So - in other words - the variable in main does not change when you call the function.

This also means that you call correlation with an uninitialized array, i.e. you won't get any meaningful result (in fact you have undefined behavior).

In the function you have

dist[SAMPLES] = buffer1[s];

this is illegal as the valid indexes are 0 ... SAMPLES-1

The function correlation doesn't return anything either. All results are stored locally so the results is lost when the function returns.

If you want a function to modify an array all you need is to pass the array which is done as a pointer.

A simple example:

#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;
}

Output:

1 2 3 4 5 
2 3 4 5 6 

这篇关于存储到另一个阵列中的所有要素,以处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆