5

I need your help.

I defined array and plan in my main cpp. Then, I put the data in the array and do fftw execute in my sub cpp. sub cpp is in the thread, that is looped.

I defined array size enough because the number of data is dynamic. but fftw is doing fft to whole size of array, so result is not correct in my case. because the number of data is always smaller than array size.

Define array in sub cpp is easy way to solve the problem, but I don't want to define the array in sub cpp because sub cpp is looped.

I want to define array only once and change fft window size independent of array size.

How can I do?

/////// in my main cpp //////
fft_in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*nScanSize);
fft_out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*nScanSize);
p = fftw_plan_dft_1d(nScanSize, fft_in, fft_out, FFTW_FORWARD, FFTW_ESTIMATE);
/////////////////////////////

/////// in my sub cpp -> looped ///////
for (i = 0; i < nChannelCountAI; i++) {

    for (j = nFirstIndex; j < nLastIndex; j++) {
        fft_in[j - nFirstIndex][0] = RawData[i][j];
        fft_in[j - nFirstIndex][1] = 0;
    }

    fftw_execute(p);
}
/////////////////////////////
Rinascita
  • 61
  • 1
  • 2
    In my understanding, when you call `p = fftw_plan_dft_1d`, fftw optimises the FFT algorithm for the provided FFT size. So, you cannot use the corresponding `fftw` object for a different FFT size. Not sure that it corresponds to your question exactly – Damien May 15 '19 at 09:29
  • 2
    Either create a plan for each possible FFT size or consider using zero padding on smaller arrays. – Paul R May 15 '19 at 11:58
  • @PaulR Could you further clarify this? e.g. Assume I created a plan for size `4096`. If I need to perform `dft` on an input of `1024`, I have to pad zeros from `1024`to `4096` in input array. But then I've noticed the calculated peak amplitude and the frequency (peak bin id) also have to be adjusted. But I'm not sure of the relationship. – Anubis Jun 15 '19 at 05:52
  • 1
    @Anubis: zero padding effectively interpolates the spectrum - you don't gain any information but it looks smoother. In your particular example above you would just need a factor of 4 correction to get the correct frequency values. See [this question](https://dsp.stackexchange.com/q/741/39) for more details. – Paul R Jun 15 '19 at 13:04
  • 1
    Thanks, BTW, I didn't ask the original question :) – Anubis Jun 15 '19 at 13:43

0 Answers0