0

I a have a 2D double complex array of size 1001(rows)*144(columns) in C. I want to apply FFT on each of the rows and finally want the output in 4096*144 format. Here N point = 4096. Finally compare the result with matlab ouput. I am using renowned FFTW C library. I have read the tutorials but could not understand how to use properly. Which routine I should use, Like 1D routine or 2D routine and then how?

#Update

double complex reshaped_calib[1001][144]={{1.0 + 0.1 * I}};

double complex** input; //[4096][144];
 // have to take dynamic array as I was getting segmentation fault here   
input = malloc(4096 * sizeof(double complex*));
for (i = 0; i < 4096; i++) {
  input[i] = malloc(144* sizeof(double complex));
}
// input is array I am sending to fftw to apply fft

for (i= 0; i< 1001; i++)
{
  for (j= 0; j< 144; j++)
  {
    input[i][j]=reshaped_calib[i][j];
  }
}

// Pad the extra rows
for (i= 1001; i< 4096; i++)
{
  for (j= 0; j< 144; j++)
  {
    input[i][j] = 0.0;
  }
}

 int N=144, howmany=4096;
 fftw_complex* data = (fftw_complex*)  fftw_malloc(N*howmany*sizeof(fftw_complex));

i=0,j=0;
int dataCount=0;
for(i=0;i<4096;i++)
    {
        for(j=0;j<144;j++)
        {
            data[dataCount++]=CMPLX(creal(input[i][j]),cimag(input[i][j]));
        }
    }
int istride=1, idist=N;// as in C data as row major
// ... if data is column-major, set istride=howmany, idist=1
//    if data is row-major, set istride=1, idist=N
fftw_plan p = fftw_plan_many_dft(1,&N,howmany,data,NULL,howmany,1,data,NULL,howmany,1,FFTW_FORWARD,FFTW_MEASURE);
fftw_execute(p);
Makau
  • 71
  • 2
  • 10
  • 1
    You can just zero-pad the input array to the required size (4096x144) and then use a straightforward complex-to-complex 2D FFT. – Paul R Apr 23 '18 at 08:33
  • @PaulR I tried to implement your idea. Did something wrong obviously. Got segmentation error. Can you have a look please ? – Makau Apr 23 '18 at 09:13
  • You would need to post a [mcve] if we are going to help you debug this. – Paul R Apr 23 '18 at 09:22

1 Answers1

0

Your attempt at padding the array with

int pad = 4096;
memset(reshaped_calib, 0.0, pad * sizeof(double complex)); //zero padding

essentially overwrites the first 4096 values of the array reshaped_calib. For proper padding, you would instead need to extend the size of the 2D array to your required size of 4096 x 144, and set to zero only the entries outside the input's 1001 x 144 range.

Since you are only extending the number of rows, the following could be used for the padding:

double complex input[1001][144]={{1.0 + 0.1 * I}};

// Allocate storage for the larger 4096x144 2D size, and copy the input.
double complex reshaped_calib[4096][144];
for (int row = 0; row < 1001; row++)
{
  for (int col = 0; col < 144; col++)
  {
    reshaped_calib[row][col] = input[row][col];
  }
}
// Pad the extra rows
for (int row = 1001; row < 4996; row++)
{
  for (int col = 0; col < 144; col++)
  {
    reshaped_calib[row][col] = 0.0;
  }
}

That said, if you want 1D FFT of each of the rows separately, you should instead use fftw_plan_dft_1d and call fftw_execute multiple times, or otherwise use fftw_plan_many_dft. This is described in more details in this answer by @Dylan.

SleuthEye
  • 14,379
  • 2
  • 32
  • 61
  • thank you. I updated the code. But I had to use dynamic memory allocation for the input array otherwise I was getting segmentation fault on that part. But now when I am creating the fft routune "fftw_plan_many_dft" I am getting segmentation fault there. From the error it seems a incompatible variable type :( Can you help me again? – Makau Apr 23 '18 at 19:26
  • Solved the segmentation problem. I flattened the input array from 2d to 1D and all the warnings and segmentation fault is gone. thanks again :) – Makau Apr 23 '18 at 19:46
  • one more question. Now there is no execution error. But the ouput is empty. I mean all the values are zero. Any idea, why I am getting this? – Makau Apr 23 '18 at 19:55
  • Most of the rows (all but the first one) of your input are zeros, and the FFT of a zero-vector is a zero-vector. – SleuthEye Apr 24 '18 at 11:28