[specdesc] improve error message
[aubio.git] / src / spectral / dct_plain.c
1 /*
2   Copyright (C) 2018 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 "fmat.h"
24 #include "spectral/dct.h"
25
26 typedef struct _aubio_dct_plain_t aubio_dct_plain_t;
27
28 struct _aubio_dct_plain_t {
29   uint_t size;
30   fmat_t *dct_coeffs;       /** DCT type II orthonormal transform, size * size */
31   fmat_t *idct_coeffs;      /** DCT type III orthonormal transform, size * size */
32 };
33
34 aubio_dct_plain_t * new_aubio_dct_plain (uint_t size) {
35   aubio_dct_plain_t * s = AUBIO_NEW(aubio_dct_plain_t);
36   uint_t i, j;
37   smpl_t scaling;
38   if (aubio_is_power_of_two (size) == 1 && size > 16) {
39     AUBIO_WRN("dct_plain: using plain dct but size %d is a power of two\n", size);
40   }
41
42   s->size = size;
43
44   s->dct_coeffs = new_fmat (size, size);
45   s->idct_coeffs = new_fmat (size, size);
46
47   /* compute DCT type-II transformation matrix
48      dct_coeffs[j][i] = cos ( j * (i+.5) * PI / n_filters )
49   */
50   scaling = SQRT (2. / size);
51   for (i = 0; i < size; i++) {
52     for (j = 1; j < size; j++) {
53       s->dct_coeffs->data[j][i] =
54           scaling * COS (j * (i + 0.5) * PI / size );
55     }
56     s->dct_coeffs->data[0][i] = 1. / SQRT (size);
57   }
58
59   /* compute DCT type-III transformation matrix
60      idct_coeffs[j][i] = cos ( i * (j+.5) * PI / n_filters )
61   */
62   scaling = SQRT (2. / size);
63   for (j = 0; j < size; j++) {
64     for (i = 1; i < size; i++) {
65       s->idct_coeffs->data[j][i] =
66           scaling * COS (i * (j + 0.5) * PI / size );
67     }
68     s->idct_coeffs->data[j][0] = 1. / SQRT (size);
69   }
70   return s;
71 }
72
73 void del_aubio_dct_plain (aubio_dct_plain_t *s) {
74   del_fmat(s->dct_coeffs);
75   del_fmat(s->idct_coeffs);
76   AUBIO_FREE(s);
77 }
78
79 void aubio_dct_plain_do(aubio_dct_plain_t *s, const fvec_t *input, fvec_t *output) {
80   if (input->length != output->length || input->length != s->size) {
81     AUBIO_WRN("dct_plain: using input length %d, but output length = %d and size = %d",
82         input->length, output->length, s->size);
83   }
84   fmat_vecmul(s->dct_coeffs, input, output);
85 }
86
87 void aubio_dct_plain_rdo(aubio_dct_plain_t *s, const fvec_t *input, fvec_t *output) {
88   if (input->length != output->length || input->length != s->size) {
89     AUBIO_WRN("dct_plain: using input length %d, but output length = %d and size = %d",
90         input->length, output->length, s->size);
91   }
92   fmat_vecmul(s->idct_coeffs, input, output);
93 }