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 | void del_aubio_dct_plain (aubio_dct_plain_t *s); |
---|
35 | |
---|
36 | aubio_dct_plain_t * new_aubio_dct_plain (uint_t size) { |
---|
37 | aubio_dct_plain_t * s = AUBIO_NEW(aubio_dct_plain_t); |
---|
38 | uint_t i, j; |
---|
39 | smpl_t scaling; |
---|
40 | if (aubio_is_power_of_two (size) == 1 && size > 16) { |
---|
41 | AUBIO_WRN("dct_plain: using plain dct but size %d is a power of two\n", size); |
---|
42 | } |
---|
43 | if ((sint_t)size <= 0) { |
---|
44 | AUBIO_ERR("dct_plain: can only create with size > 0, requested %d\n", |
---|
45 | size); |
---|
46 | goto failure; |
---|
47 | } |
---|
48 | |
---|
49 | s->size = size; |
---|
50 | |
---|
51 | s->dct_coeffs = new_fmat (size, size); |
---|
52 | s->idct_coeffs = new_fmat (size, size); |
---|
53 | |
---|
54 | /* compute DCT type-II transformation matrix |
---|
55 | dct_coeffs[j][i] = cos ( j * (i+.5) * PI / n_filters ) |
---|
56 | */ |
---|
57 | scaling = SQRT (2. / size); |
---|
58 | for (i = 0; i < size; i++) { |
---|
59 | for (j = 1; j < size; j++) { |
---|
60 | s->dct_coeffs->data[j][i] = |
---|
61 | scaling * COS (j * (i + 0.5) * PI / size ); |
---|
62 | } |
---|
63 | s->dct_coeffs->data[0][i] = 1. / SQRT (size); |
---|
64 | } |
---|
65 | |
---|
66 | /* compute DCT type-III transformation matrix |
---|
67 | idct_coeffs[j][i] = cos ( i * (j+.5) * PI / n_filters ) |
---|
68 | */ |
---|
69 | scaling = SQRT (2. / size); |
---|
70 | for (j = 0; j < size; j++) { |
---|
71 | for (i = 1; i < size; i++) { |
---|
72 | s->idct_coeffs->data[j][i] = |
---|
73 | scaling * COS (i * (j + 0.5) * PI / size ); |
---|
74 | } |
---|
75 | s->idct_coeffs->data[j][0] = 1. / SQRT (size); |
---|
76 | } |
---|
77 | return s; |
---|
78 | failure: |
---|
79 | del_aubio_dct_plain(s); |
---|
80 | return NULL; |
---|
81 | } |
---|
82 | |
---|
83 | void del_aubio_dct_plain (aubio_dct_plain_t *s) { |
---|
84 | if (s->dct_coeffs) |
---|
85 | del_fmat(s->dct_coeffs); |
---|
86 | if (s->idct_coeffs) |
---|
87 | del_fmat(s->idct_coeffs); |
---|
88 | AUBIO_FREE(s); |
---|
89 | } |
---|
90 | |
---|
91 | void aubio_dct_plain_do(aubio_dct_plain_t *s, const fvec_t *input, fvec_t *output) { |
---|
92 | if (input->length != output->length || input->length != s->size) { |
---|
93 | AUBIO_WRN("dct_plain: using input length %d, but output length = %d and size = %d\n", |
---|
94 | input->length, output->length, s->size); |
---|
95 | } |
---|
96 | fmat_vecmul(s->dct_coeffs, input, output); |
---|
97 | } |
---|
98 | |
---|
99 | void aubio_dct_plain_rdo(aubio_dct_plain_t *s, const fvec_t *input, fvec_t *output) { |
---|
100 | if (input->length != output->length || input->length != s->size) { |
---|
101 | AUBIO_WRN("dct_plain: using input length %d, but output length = %d and size = %d\n", |
---|
102 | input->length, output->length, s->size); |
---|
103 | } |
---|
104 | fmat_vecmul(s->idct_coeffs, input, output); |
---|
105 | } |
---|