2 Copyright (C) 2018 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
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.
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.
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/>.
21 #include "aubio_priv.h"
24 #include "spectral/dct.h"
26 typedef struct _aubio_dct_plain_t aubio_dct_plain_t;
28 struct _aubio_dct_plain_t {
30 fmat_t *dct_coeffs; /** DCT type II orthonormal transform, size * size */
31 fmat_t *idct_coeffs; /** DCT type III orthonormal transform, size * size */
34 aubio_dct_plain_t * new_aubio_dct_plain (uint_t size) {
35 aubio_dct_plain_t * s = AUBIO_NEW(aubio_dct_plain_t);
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);
44 s->dct_coeffs = new_fmat (size, size);
45 s->idct_coeffs = new_fmat (size, size);
47 /* compute DCT type-II transformation matrix
48 dct_coeffs[j][i] = cos ( j * (i+.5) * PI / n_filters )
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 );
56 s->dct_coeffs->data[0][i] = 1. / SQRT (size);
59 /* compute DCT type-III transformation matrix
60 idct_coeffs[j][i] = cos ( i * (j+.5) * PI / n_filters )
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 );
68 s->idct_coeffs->data[j][0] = 1. / SQRT (size);
73 void del_aubio_dct_plain (aubio_dct_plain_t *s) {
74 del_fmat(s->dct_coeffs);
75 del_fmat(s->idct_coeffs);
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);
84 fmat_vecmul(s->dct_coeffs, input, output);
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);
92 fmat_vecmul(s->idct_coeffs, input, output);