【问题标题】:Multiple arrays return types in CC中的多个数组返回类型
【发布时间】:2022-01-14 22:07:45
【问题描述】:

我从三个函数 calculate_twiddle_factors_1、calculate_twiddle_factors_2、calculate_twiddle_factors_3 返回三个(beta_1_1、beta_1_2、beta_2_1、beta_2_2、beta_3_1、beta_3_2)中的两个数组值,使用 int 指针并在函数内打印正确的值。但是,在主函数中调用该函数时,阶段 2 中的第二个返回参数与函数定义中直接打印的值不匹配。代码如下:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <complex.h>

unsigned int p = 7, N, Mp, *alpha_1, *alpha_2, N_max, t, r;
int alpha1, alpha2;
int complex C;
int * beta1, * beta2;

void calculate_twiddle_factors_1(int N1, int t1, int t2, int p1, int * beta_1_1, int * beta_1_2);
void calculate_twiddle_factors_2(int N1, int t1, int t2, int p1, int * beta_2_1, int * beta_2_2);
void calculate_twiddle_factors_3(int N1, int t1, int t2, int p1, int * beta_3_1, int * beta_3_2);
int mod(int input, int Mp);
void bitrevorder(int *vec, unsigned char l2N);

int main() {

    int * beta_11 = (int *)calloc(N, sizeof(int));
    int * beta_12 = (int *)calloc(N, sizeof(int));
    int * beta_21 = (int *)calloc(N, sizeof(int));
    int * beta_22 = (int *)calloc(N, sizeof(int));
    int * beta_31 = (int *)calloc(N, sizeof(int));
    int * beta_32 = (int *)calloc(N, sizeof(int));

    int *X0 = (int *)calloc(N, sizeof(int));
    int *X0r = (int *)calloc(N, sizeof(int));
    int *X1 = (int *)calloc(N, sizeof(int));
    int *X1r = (int *)calloc(N, sizeof(int));
    int *X2 = (int *)calloc(N, sizeof(int));
    int *X2r = (int *)calloc(N, sizeof(int));
    int *X3 = (int *)calloc(N, sizeof(int));
    int *X3r = (int *)calloc(N, sizeof(int));
    int *X = (int *)calloc(N, sizeof(int));

    N_max = (p-1)>>1;
    Mp = (1 << p)-1;
    printf("Mp = %d\n", Mp);

    int r = 2;
    N = pow(4,r);


    int x[]= {92,78, 121, 40, 17, 36, 67, 69, 72, 6, 60, 78, 30, 74, 51, 23};

    unsigned char l2N = log2(N);
    bitrevorder(x, l2N);
    printf("Input signal after bit reversing\n");

    alpha1 = 2; //2^q
    alpha2 = 3; //3^q

    for(int i = 0; i<p-2; i++){
        alpha1 = mod((alpha1 * alpha1),Mp);
        alpha2 = mod((alpha2 * alpha2),Mp);
    }

    //STAGES
    for(int m = 1; m <=r; m++) { //m = 0, 1
        printf("\nStage = %d\n", m);
        unsigned int L = pow(4, m); // L = 4

        //Calling twiddle factor functions
        calculate_twiddle_factors_1(L, alpha1, alpha2, p, beta_11, beta_12);

        for(int index = 0; index < L; index++){
            printf(" beta_1_1[%d]_in_main  = %d\n", index, beta_11[index]);
            printf(" beta_1_2[%d]_in_main  = %d\n", index, beta_12[index]);
        }

        calculate_twiddle_factors_2(L, alpha1, alpha2, p, beta_21, beta_22);
        for(int index = 0; index < L/2; index++){
            printf(" beta_2_1[%d]_in_main  = %d\n", index, beta_21[index]);
            printf(" beta_2_2[%d]_in_main  = %d\n", index, beta_22[index]);

        }

        calculate_twiddle_factors_3(L, alpha1, alpha2, p, beta_31, beta_32);
        for(int index = 0; index <= L/4; index++){
            printf(" beta_3_1[%d]_in_main = %d\n", index, beta_31[index]);
            printf(" beta_3_2[%d]_in_main = %d\n", index, beta_32[index]);
        }

        int index = 0;
        for (int k = 1; k <= N - L + 1; k = k + L) {
            for (int n = 0; n <= L/8; n++) { //Number of next points, BNEXT = BSEP/4



                X0[index] = x[k + n - 1]; // x[top_index + BNEXT] k = 1, n = 0 x[
                X0r[index] = x[k - n - 1 + L/4 - 1]; //1 - 0 - 1
                X1[index] = (x[k + n + L / 4 - 1] * beta_21[n]) + (x[k + L / 2 - n - 1 - 1] * beta_22[n]);
                X1r[index] = (x[k + n + L / 4 - 1] * beta_22[n]) - (x[k * L / 2 - n - 1 - 1] * beta_21[n]);
                X2[index] = (x[k + n + L / 2 - 1] * beta_11[n]) + (x[k + 3 * L / 4 - n - 1 - 1] * beta_12[n]);
                X2r[index] = (x[k + n + L / 2 -1] * beta_12[n]) - (x[k + 3 * L / 4 - n - 1 -1] * beta_11[n]);
                X3[index] = (x[k + n + 3 * L / 4 -1] * beta_31[n]) + (x[k + L - n - 1 -1] * beta_32[n]);
                X3r[index] = (x[k + n + 3 * L / 4 -1] * beta_32[n]) - (x[k + L - n - 1 -1] * beta_31[n]);



                x[k + n - 1 ] = X0[index] + X1[index] + X2[index] + X3[index];
                x[k - n - 1 + L / 4 -1] = X0r[index] + X1r[index] + X2[index] - X3[index];
                x[k + n + L / 4 -1] = X0[index] - X1[index] - X2r[index] + X3r[index];
                x[k - n - 1 + L / 2 -1] = X0[index] - X1r[index] + X2r[index] + X3r[index];
                x[k + n + L / 2 -1] = X0[index] + X1[index] - X2[index] - X3[index];
                x[k + n + 3 * L / 4 -1] = X0[index] - X1[index] + X2r[index] - X3r[index];
                x[k - n - 1 + 3 * L / 4 -1] = X0r[index] + X1r[index] - X2[index] + X3[index];
                x[k + L - n - 1-1] = X0r[index] - X1r[index] - X2r[index] - X3r[index];

                index++;
            }
        }
    }

    free(X0);
    free(X0r);
    free(X1);
    free(X1r);
    free(X2);
    free(X2r);
    free(X3);
    free(X3r);
    free(X);
    free(beta_11);
    free(beta_12);
    free(beta_21);
    free(beta_22);
    free(beta_31);
    free(beta_32);


    return 0;
}

// Perform Bit Reverse Order to a vector
void bitrevorder(int *vec, unsigned char l2N)
{
    unsigned long long newpos, temp;
    for(int loop = 0; loop < N; loop ++)
    {
        newpos = loop;
        // Derived from: http://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel
        newpos = ((newpos >> 1) & 0x55555555) | ((newpos & 0x55555555) << 1);
        newpos = ((newpos >> 2) & 0x33333333) | ((newpos & 0x33333333) << 2);
        newpos = ((newpos >> 4) & 0x0F0F0F0F) | ((newpos & 0x0F0F0F0F) << 4);
        newpos = ((newpos >> 8) & 0x00FF00FF) | ((newpos & 0x00FF00FF) << 8);
        newpos >>= (16 - l2N);
        if(loop < newpos)
        {
            temp = vec[loop];
            vec[loop] = vec[newpos];
            vec[newpos] = temp;
        }
    }
}

int mod(int input, int Mp){
    int result =  (input % Mp);
    return  (result < 0) ? result + Mp: result;
}

void calculate_twiddle_factors_1(int N1, int t1, int t2, int p1, int * beta_1_1, int * beta_1_2){

    int s = log2(N1);
    int Mp = (1 << p1)-1;
    int *T1 = (int *) malloc(sizeof(int) * 2*N);
    int *T2 = (int *) malloc(sizeof(int) * 2*N);

    for(int i = 0;  i < (p1 - s); i++){ // p = 7, s = 2, (p1-s) = 5
        C = (t1 + 1*I* t2);//t1 = alpha1; t2 = alpha2
        int temp1 = mod((t1 * t1) - (t2*t2),Mp);
        int temp2 = mod((2*t1*t2),Mp);
        t1 = temp1;
        t2 = temp2;
    }
    T1[0] = 1;
    T2[0] = 0;

    for(int n = 0; n <(2*N1)-1; n++){
        T1[n + 1] = mod((t1 * T1[n] - t2 * T2[n]),Mp);
        T2[n + 1] = mod((t1 * T2[n] + t2 * T1[n]), Mp);
    }

    int index_1 = 0;
    for(int index = 1; index < 2*N1; index = index + 2){
        beta_1_1[index_1] = T1[index];
        beta_1_2[index_1] = T2[index];
        printf("beta_1_1[%d]_from_function = %d\n", index_1, beta_1_1[index_1]);
        printf("beta_1_2[%d]_from_function = %d\n", index_1, beta_1_2[index_1]);
        index_1++;
    }
}

void calculate_twiddle_factors_2(int N1, int t1, int t2, int p1,  int * beta_2_1, int * beta_2_2){

    int s = log2(N1);
    int Mp = (1 << p1)-1;
    int *T1 = (int *) malloc(sizeof(int) * 2*N);
    int *T2 = (int *) malloc(sizeof(int) * 2*N);

    for(int i = 0;  i < (p1 - s); i++){ // p = 7, s = 2, (p1-s) = 5
        C = (t1 + 1*I* t2);//t1 = alpha1; t2 = alpha
        int temp1 = mod((t1 * t1) - (t2*t2),Mp);
        int temp2 = mod((2*t1*t2),Mp);
        t1 = temp1;
        t2 = temp2;
    }
    T1[0] = 1;
    T2[0] = 0;

    for(int n = 0; n <(2*N1)-1; n++){
        T1[n + 1] = mod((t1 * T1[n] - t2 * T2[n]),Mp);
        T2[n + 1] = mod((t1 * T2[n] + t2 * T1[n]), Mp);
    }

    int index_2 = 0;
    for(int index = 2; index < 2*N1; index = index + 4){
        beta_2_1[index_2] = T1[index];
        beta_2_2[index_2] = T2[index];
        printf("beta_2_1[%d]_from_function = %d\n", index_2, beta_2_1[index_2]);
        printf("beta_2_2[%d]_from_function = %d\n", index_2, beta_2_2[index_2]);
        index_2++;
    }
}

void calculate_twiddle_factors_3(int N1, int t1, int t2, int p1, int * beta_3_1, int * beta_3_2){
    int s = log2(N1);
    int Mp = (1 << p1)-1;
    int *T1 = (int *) malloc(sizeof(int) * 2*N);
    int *T2 = (int *) malloc(sizeof(int) * 2*N);

    for(int i = 0;  i < (p1 - s); i++){ // p = 7, s = 2, (p1-s) = 5
        C = (t1 + 1*I* t2);//t1 = alpha1; t2 = alpha2
        int temp1 = mod((t1 * t1) - (t2*t2),Mp);
        int temp2 = mod((2*t1*t2),Mp);
        t1 = temp1;
        t2 = temp2;
    }
    T1[0] = 1;
    T2[0] = 0;

    for(int n = 0; n <(2*N1)-1; n++){
        T1[n + 1] = mod((t1 * T1[n] - t2 * T2[n]),Mp);
        T2[n + 1] = mod((t1 * T2[n] + t2 * T1[n]), Mp);
    }

    int index_3 = 0;
    for(int index = 3; index < 2*N1; index = index + 6){
        beta_3_1[index_3] = T1[index];
        beta_3_2[index_3] = T2[index];
        printf("beta_3_1[%d]_from_function = %d\n", index_3, beta_3_1[index_3]);
        printf("beta_3_2[%d]_from_function = %d\n", index_3, beta_3_2[index_3]);
        index_3++;
    }
}


我期望 beta_11[n]、beta_12[n]、beta_21[n]、beta_22[n]、beta_31[n] 和 beta_32[n] 在阶段调用 main() 函数时的值应该相同2. 阶段 1 中的所有值都匹配,但阶段 2 中的某些值不匹配。我在函数定义中和在 main() 函数中调用时添加了 printf() 语句。理想情况下,beta_x_y[i]_from_function 和 beta_x_y[i]

这意味着 beta_xy[n] 属性在计算它们的函数内部和在 main() 内部调用时相同。

我分两个阶段计算 beta_xy(n) 的值(即旋转因子)。在第 1 阶段,N = 4,在第 2 阶段,N = 16。基于 N,旋转因子的值 beta_xy 将更新。在阶段 1 (N = 4) 和阶段 2 (N = 16) 计算了两组 beta_xy[n]。我检查了这些值。它们对于函数内部的阶段 1 和 2 是正确的。

旋转因子类似于系数,例如 X[k] = x[n] * beta_xy[n]。

【问题讨论】:

  • 如果您能提供更多关注minimal reproducible example,您获得帮助的机会会更大。 IE。一个只演示从多个数组中检索的问题。
  • “我期望值......应该是相同的......在第 2 阶段。”为什么一样?为什么在第 2 阶段?为什么不在第一阶段?各个阶段的意义是什么?玩弄的背景是什么?
  • 这意味着 beta_xy[n] 属性在计算它们的函数内部和在 main() 内部调用时相同。
  • 我分两个阶段计算 beta_xy(n) 的值(即旋转因子)。在第 1 阶段,N = 4,在第 2 阶段,N = 16。基于 N,旋转因子的值 beta_xy 将更新。在阶段 1 (N = 4) 和阶段 2 (N = 16) 计算了两组 beta_xy[n]。我检查了这些值。它们对于函数内部的第 1 阶段和第 2 阶段是正确的。
  • 请通过editing将该信息添加到问题中。

标签: arrays c pointers


【解决方案1】:

根据@printf 的评论,我解决了这些问题。在给它一个特定值之前,我使用了使用 N 的 calloc 。编译器没有给出错误 N 已定义为全局变量,但未按预期打印值。在分配 N 中的值后,我移动了 calloc()。它起作用了。

【讨论】:

  • 我感到很荣幸.....但不值得。我认为值得称赞的是@printf。我只是在唠叨我在问题中没有得到的东西......但 printf 实际上在评论中回答了。这就是我编辑远离我的原因......
猜你喜欢
  • 1970-01-01
  • 2014-05-16
  • 1970-01-01
  • 2012-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-19
  • 2015-12-11
相关资源
最近更新 更多