src/spectral/dct_*.c: remove unused cvec.h
[aubio.git] / src / spectral / dct_accelerate.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 "spectral/dct.h"
24
25 #if defined(HAVE_ACCELERATE)
26
27 #if HAVE_AUBIO_DOUBLE
28 #warning "no double-precision dct with accelerate"
29 #endif
30
31 struct _aubio_dct_t {
32   uint_t size;
33   fvec_t *tmp;
34   vDSP_DFT_Setup setup;
35   vDSP_DFT_Setup setupInv;
36 };
37
38 aubio_dct_t * new_aubio_dct (uint_t size) {
39   aubio_dct_t * s = AUBIO_NEW(aubio_dct_t);
40
41   if ((sint_t)size < 16 || !aubio_is_power_of_two(size)) {
42     AUBIO_ERR("dct: can only create with sizes greater than 16 and"
43         "that are powers of two, requested %d\n", size);
44     goto beach;
45   }
46
47   s->setup = vDSP_DCT_CreateSetup(NULL, (vDSP_Length)size, vDSP_DCT_II);
48   s->setupInv = vDSP_DCT_CreateSetup(NULL, (vDSP_Length)size, vDSP_DCT_III);
49   if (s->setup == NULL || s->setupInv == NULL) {
50     goto beach;
51   }
52
53   s->size = size;
54
55   return s;
56
57 beach:
58   del_aubio_dct(s);
59   return NULL;
60 }
61
62 void del_aubio_dct(aubio_dct_t *s) {
63   if (s->setup) vDSP_DFT_DestroySetup(s->setup);
64   if (s->setupInv) vDSP_DFT_DestroySetup(s->setupInv);
65   AUBIO_FREE(s);
66 }
67
68 void aubio_dct_do(aubio_dct_t *s, const fvec_t *input, fvec_t *output) {
69
70   vDSP_DCT_Execute(s->setup, (const float *)input->data, (float *)output->data);
71
72   // apply orthonormal scaling
73   output->data[0] *= SQRT(1./s->size);
74   smpl_t scaler = SQRT(2./s->size);
75
76   aubio_vDSP_vsmul(output->data + 1, 1, &scaler, output->data + 1, 1,
77       output->length - 1);
78
79 }
80
81 void aubio_dct_rdo(aubio_dct_t *s, const fvec_t *input, fvec_t *output) {
82
83   output->data[0] = input->data[0] / SQRT(1./s->size);
84   smpl_t scaler = 1./SQRT(2./s->size);
85
86   aubio_vDSP_vsmul(input->data + 1, 1, &scaler, output->data + 1, 1,
87       output->length - 1);
88
89   vDSP_DCT_Execute(s->setupInv, (const float *)output->data,
90       (float *)output->data);
91
92   scaler = 2./s->size;
93
94   aubio_vDSP_vsmul(output->data, 1, &scaler, output->data, 1, output->length);
95
96 }
97
98 #endif //defined(HAVE_ACCELERATE)