2c96dfd0919b382ae6789c28dc35fa496623b84d
[aubio.git] / examples / tests / test-fft.c
1 #include <stdlib.h>
2 #include <math.h>
3 #include <complex.h>
4 #include <aubio.h>
5
6 #define NEW_ARRAY(_t,_n)                (_t*)malloc((_n)*sizeof(_t))
7
8
9 int main(){
10         uint_t i,j;
11         /* allocate some memory */
12         uint_t win_s      = 1024;                       /* window size */
13         uint_t channels   = 1;                          /* number of channel */
14         fvec_t * in       = new_fvec (win_s, channels); /* input buffer */
15         cvec_t * fftgrain = new_cvec (win_s, channels); /* fft norm and phase */
16         fvec_t * out      = new_fvec (win_s, channels); /* output buffer */
17   
18         /* allocate fft and other memory space */
19         aubio_fft_t * fft      = new_aubio_fft(win_s);      /* fft interface */
20         smpl_t * w             = NEW_ARRAY(smpl_t,win_s); /* window */
21         /* complex spectral data */
22         fft_data_t ** spec     = NEW_ARRAY(fft_data_t*,channels); 
23         for (i=0; i < channels; i++)
24                 spec[i] = NEW_ARRAY(fft_data_t,win_s);
25         /* initialize the window (see mathutils.c) */
26         aubio_window(w,win_s,aubio_win_hanningz);
27   
28         /* fill input with some data */
29   
30         /* execute stft */
31         for (i=0; i < channels; i++) {
32                 aubio_fft_do (fft,in->data[i],spec[i],win_s);
33                 /* put norm and phase into fftgrain */
34                 aubio_fft_getnorm(fftgrain->norm[i], spec[i], win_s/2+1);
35                 aubio_fft_getphas(fftgrain->phas[i], spec[i], win_s/2+1);
36         }
37   
38         /* execute inverse fourier transform */
39         for (i=0; i < channels; i++) {
40                 for (j=0; j<win_s/2+1; j++) {
41                         spec[i][j]  = cexp(I*aubio_unwrap2pi(fftgrain->phas[i][j]));
42                         spec[i][j] *= fftgrain->norm[i][j];
43                 }
44                 aubio_fft_rdo(fft,spec[i],out->data[i],win_s);
45         }
46
47         del_fvec(in);
48         del_fvec(out);
49         del_cvec(fftgrain);
50         free(w);
51         del_aubio_fft(fft);
52         for (i=0; i < channels; i++)
53                 free(spec[i]);
54         free(spec); 
55         aubio_cleanup();
56         return 0;
57 }