dzakens
dzakens

Reputation: 327

fftw3 proper using

#include <stdio.h>
#include <Windows.h>
#include <string.h>
#include <stdlib.h>
#include "fftw3.h"


int main(void)
{
    FILE *fp;

    int rozmiar_pliku;
    char standard[5] = {0};
    char format[5] = {0};
    int samplerate;

    int k,i;

    fftw_complex in[128];
    fftw_complex out[128];
    fftw_plan p;


    fp = fopen("Kalimba.wav","rb" );

    //printf("%d\n",fp);

    if (fp)
    {
        fread(standard,1,4,fp);
        printf("%s\n",standard);
        printf("RIFF\n");
        if (!strcmp(standard,"RIFF" ))
        {

            fread(&rozmiar_pliku,4,1,fp);

            printf("size: %d\n", rozmiar_pliku);
        }

        fread(format,1,4,fp);

        printf("format: %s\n",format);

        fseek(fp,24,SEEK_SET);

        fread(&samplerate,1,4,fp);

        printf("sample rate: %d\n",samplerate);


        fseek(fp,44,SEEK_SET);


        for(i=0;i<128;++i)
        {
            in[i][0]=getc(fp);
            in[i][1]=in[i][0];

        }


/*
        p = fftw_plan_dft_1d(128, in, out, FFTW_FORWARD, FFTW_ESTIMATE);

        fftw_execute(p);


        for(int j=0;j<128;++j)
            printf("%lf+i*%lf",out[j][0],out[j][1]);

        fftw_destroy_plan(p);
        fftw_free(in);
        fftw_free(out);
*/
    }

    return 0;
}

I'm trying to read wave file and perform FFT by using FFTW3. If i uncomment part which is commented there's nothing show on screen. If I leave it commented :

RIFF
RIFF
size: 61392422
format: WAVE
sample rate: 44100

If uncommented nothing appears. I don't know why it is going like this. Any use of fftw3 cause this situation.

Upvotes: 0

Views: 777

Answers (2)

alpereira7
alpereira7

Reputation: 1550

As it is recommended in the documentation, you should declare in and out using fftw_malloc.

You can allocate them in any way that you like, but we recommend using fftw_malloc.

Then, you'll need to initialize in after creating the plan.

You must create the plan before initializing the input, because FFTW_MEASURE overwrites the in/out arrays. (Technically, FFTW_ESTIMATE does not touch your arrays, but you should always create plans first just to be sure.)

The result with some other modifications, is

#include <stdio.h>
#include <stdlib.h>
#include "fftw3.h"

int main(void)
{
  FILE *fp;

  int rozmiar_pliku;
  char standard[5] = {0};
  char format[5] = {0};
  int samplerate;

  int i;

  fftw_complex *in, *out;
  fftw_plan p;

  fp = fopen("audioFile1.wav","rb" );
  if (fp)
  {
    fread(standard,1,4,fp);
    printf("%s\n",standard);
    printf("RIFF\n");
    if (!strcmp(standard,"RIFF" ))
    {
      fread(&rozmiar_pliku,4,1,fp);
      printf("size: %d\n", rozmiar_pliku);
    }
    fread(format,1,4,fp);
    printf("format: %s\n",format);
    fseek(fp,24,SEEK_SET);
    fread(&samplerate,1,4,fp);
    printf("sample rate: %d\n",samplerate);
    fseek(fp,44,SEEK_SET);

    // Allocate in and out buffers using fftw_alloc
    in  = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * 128);
    out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * 128);

    // Create plan before initializing in
    p = fftw_plan_dft_1d(128, in, out, FFTW_FORWARD, FFTW_ESTIMATE);

    // Initialize in after creating plan
    for(i=0;i<128;++i)
    {
      in[i][0]=getc(fp);
      in[i][1]=in[i][0];
    }

    fftw_execute(p);

    for(int j=0;j<128;++j)
      printf("%lf+i*%lf\n",out[j][0],out[j][1]);

    fftw_destroy_plan(p);
    fftw_free(in); fftw_free(out);
  }

  return 0;
}

Upvotes: 0

Vitality
Vitality

Reputation: 21505

in and out are statically declared arrays. Try passing &in[0] and &out[0] to match the type expected by fftw_plan_dft_1d.

Upvotes: 0

Related Questions