7ea1f2a1893f2d46ad9892a712339b323e53f59c
[aubio.git] / src / spectral / dct_fftw.c
1 /*
2   Copyright (C) 2017 Paul Brossier <piem@aubio.org>
3
4   This file is part of aubio.
5
6   aubio is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   aubio is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "aubio_priv.h"
22 #include "fvec.h"
23 #include "cvec.h"
24 #include "spectral/dct.h"
25
26 #ifdef HAVE_FFTW3
27
28 #include <fftw3.h>
29 #include <pthread.h>
30
31 #ifdef HAVE_FFTW3F
32 #if HAVE_AUBIO_DOUBLE
33 #error "Using aubio in double precision with fftw3 in single precision"
34 #endif /* HAVE_AUBIO_DOUBLE */
35 #else  /* HAVE_FFTW3F */
36 #if !HAVE_AUBIO_DOUBLE
37 #error "Using aubio in single precision with fftw3 in double precision"
38 #endif /* HAVE_AUBIO_DOUBLE */
39 #endif /* HAVE_FFTW3F */
40
41 #ifdef HAVE_FFTW3F
42 #define fftw_malloc            fftwf_malloc
43 #define fftw_free              fftwf_free
44 #define fftw_execute           fftwf_execute
45 #define fftw_plan_dft_r2c_1d   fftwf_plan_dft_r2c_1d
46 #define fftw_plan_dft_c2r_1d   fftwf_plan_dft_c2r_1d
47 #define fftw_plan_r2r_1d       fftwf_plan_r2r_1d
48 #define fftw_plan              fftwf_plan
49 #define fftw_destroy_plan      fftwf_destroy_plan
50 #endif
51
52 // defined in src/spectral/fft.c
53 extern pthread_mutex_t aubio_fftw_mutex;
54
55 extern void aubio_ooura_ddct(int, int, smpl_t *, int *, smpl_t *);
56
57 struct _aubio_dct_t {
58   uint_t size;
59   fvec_t *in, *out;
60   smpl_t *data;
61   fftw_plan pfw, pbw;
62   smpl_t scalers[5];
63 };
64
65 aubio_dct_t * new_aubio_dct (uint_t size) {
66   aubio_dct_t * s = AUBIO_NEW(aubio_dct_t);
67   if (!s) {
68     goto beach;
69   }
70   s->size = size;
71   s->in = new_fvec(size);
72   s->out = new_fvec(size);
73   pthread_mutex_lock(&aubio_fftw_mutex);
74   s->data = (smpl_t *)fftw_malloc(sizeof(smpl_t) * size);
75   s->pfw = fftw_plan_r2r_1d(size, s->in->data,  s->data, FFTW_REDFT10,
76       FFTW_ESTIMATE);
77   s->pbw = fftw_plan_r2r_1d(size, s->data, s->out->data, FFTW_REDFT01,
78       FFTW_ESTIMATE);
79   pthread_mutex_unlock(&aubio_fftw_mutex);
80   s->scalers[0] = SQRT(1./(4.*s->size));
81   s->scalers[1] = SQRT(1./(2.*s->size));
82   s->scalers[2] = 1. / s->scalers[0];
83   s->scalers[3] = 1. / s->scalers[1];
84   s->scalers[4] = .5 / s->size;
85   return s;
86 beach:
87   AUBIO_FREE(s);
88   return NULL;
89 }
90
91 void del_aubio_dct(aubio_dct_t *s) {
92   pthread_mutex_lock(&aubio_fftw_mutex);
93   fftw_destroy_plan(s->pfw);
94   fftw_destroy_plan(s->pbw);
95   fftw_free(s->data);
96   pthread_mutex_unlock(&aubio_fftw_mutex);
97   del_fvec(s->in);
98   del_fvec(s->out);
99   AUBIO_FREE(s);
100 }
101
102 void aubio_dct_do(aubio_dct_t *s, const fvec_t *input, fvec_t *output) {
103   uint_t i;
104   fvec_copy(input, s->in);
105   fftw_execute(s->pfw);
106   //fvec_copy(s->out, output);
107   s->data[0] *= s->scalers[0];
108   for (i = 1; i < s->size; i++) {
109     s->data[i] *= s->scalers[1];
110   }
111   memcpy(output->data, s->data, output->length * sizeof(smpl_t));
112 }
113
114 void aubio_dct_rdo(aubio_dct_t *s, const fvec_t *input, fvec_t *output) {
115   uint_t i;
116   memcpy(s->data, input->data, input->length * sizeof(smpl_t));
117   //s->data[0] *= .5;
118   s->data[0] *= s->scalers[2];
119   for (i = 1; i < s->size; i++) {
120     s->data[i] *= s->scalers[3];
121   }
122   fftw_execute(s->pbw);
123   for (i = 0; i < s->size; i++) {
124     s->out->data[i] *= s->scalers[4];
125   }
126   fvec_copy(s->out, output);
127 }
128
129 #endif //HAVE_FFTW3