Reputation: 21
I'm returning two array values from three (beta_1_1, beta_1_2, beta_2_1, beta_2_2, beta_3_1, beta_3_2) from three functions calculate_twiddle_factors_1, calculate_twiddle_factors_2, calculate_twiddle_factors_3, using int pointer and printing the right values inside the function. However when calling the function in the main function, the second return parameter in Stage 2, does not match directly printed values from function definition. The code is below:
#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++;
}
}
I'm expecting values of beta_11[n], beta_12[n], beta_21[n], beta_22[n], beta_31[n] and beta_32[n] should be same when calling in main() function in stage 2. All values in stage 1 is matched but some of the values do not match for Stage 2. I added printf() statement inside the function definition and when calling in main() function. Ideally, beta_x_y[i]_from_function and beta_x_y[i]
The same means beta_xy[n] properties will be the same inside the function where they are calculated and when calling inside main().
I'm calculating the values of beta_xy(n) (i.e. twiddle factors) in two stages. In stage 1, N = 4 and in Stage 2, N = 16. Based on N, the values of twiddle factors, beta_xy will update. There are two set of beta_xy[n] one calculated in stage 1 (N = 4) and in stage 2 (N = 16). I checked that the values. They are correct for stage 1 and 2 inside the function.
Twiddle factors are like coefficients such as X[k] = x[n] * beta_xy[n].
Upvotes: 0
Views: 78
Reputation: 21
As per the comment of @printf, I solved the issues. I used the calloc using N before giving it a specific value. The compiler gave no error N was already defined as a global variable but the values were not printed as expected. I moved the calloc() after assigning the values in N. It worked.
Upvotes: 1