source: src/spectral/dct_fftw.c @ 30cb440e

feature/crepe_org
Last change on this file since 30cb440e was 63c5015, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[dct] fail fftw creation if size <= 0

  • Property mode set to 100644
File size: 3.7 KB
Line 
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#ifdef HAVE_FFTW3
26
27#include <fftw3.h>
28#include <pthread.h>
29
30#ifdef HAVE_FFTW3F
31#if HAVE_AUBIO_DOUBLE
32#error "Using aubio in double precision with fftw3 in single precision"
33#endif /* HAVE_AUBIO_DOUBLE */
34#else  /* HAVE_FFTW3F */
35#if !HAVE_AUBIO_DOUBLE
36#error "Using aubio in single precision with fftw3 in double precision"
37#endif /* HAVE_AUBIO_DOUBLE */
38#endif /* HAVE_FFTW3F */
39
40#ifdef HAVE_FFTW3F
41#define fftw_malloc            fftwf_malloc
42#define fftw_free              fftwf_free
43#define fftw_execute           fftwf_execute
44#define fftw_plan_dft_r2c_1d   fftwf_plan_dft_r2c_1d
45#define fftw_plan_dft_c2r_1d   fftwf_plan_dft_c2r_1d
46#define fftw_plan_r2r_1d       fftwf_plan_r2r_1d
47#define fftw_plan              fftwf_plan
48#define fftw_destroy_plan      fftwf_destroy_plan
49#endif
50
51// defined in src/spectral/fft.c
52extern pthread_mutex_t aubio_fftw_mutex;
53
54typedef struct _aubio_dct_fftw_t aubio_dct_fftw_t;
55
56struct _aubio_dct_fftw_t {
57  uint_t size;
58  fvec_t *in, *out;
59  smpl_t *data;
60  fftw_plan pfw, pbw;
61  smpl_t scalers[5];
62};
63
64aubio_dct_fftw_t * new_aubio_dct_fftw (uint_t size) {
65  aubio_dct_fftw_t * s = AUBIO_NEW(aubio_dct_fftw_t);
66  if ((sint_t)size <= 0) {
67    AUBIO_ERR("dct_fftw: can only create with size > 0, requested %d\n",
68        size);
69    goto beach;
70  }
71  s->size = size;
72  s->in = new_fvec(size);
73  s->out = new_fvec(size);
74  pthread_mutex_lock(&aubio_fftw_mutex);
75  s->data = (smpl_t *)fftw_malloc(sizeof(smpl_t) * size);
76  s->pfw = fftw_plan_r2r_1d(size, s->in->data,  s->data, FFTW_REDFT10,
77      FFTW_ESTIMATE);
78  s->pbw = fftw_plan_r2r_1d(size, s->data, s->out->data, FFTW_REDFT01,
79      FFTW_ESTIMATE);
80  pthread_mutex_unlock(&aubio_fftw_mutex);
81  s->scalers[0] = SQRT(1./(4.*s->size));
82  s->scalers[1] = SQRT(1./(2.*s->size));
83  s->scalers[2] = 1. / s->scalers[0];
84  s->scalers[3] = 1. / s->scalers[1];
85  s->scalers[4] = .5 / s->size;
86  return s;
87beach:
88  AUBIO_FREE(s);
89  return NULL;
90}
91
92void del_aubio_dct_fftw(aubio_dct_fftw_t *s) {
93  pthread_mutex_lock(&aubio_fftw_mutex);
94  fftw_destroy_plan(s->pfw);
95  fftw_destroy_plan(s->pbw);
96  fftw_free(s->data);
97  pthread_mutex_unlock(&aubio_fftw_mutex);
98  del_fvec(s->in);
99  del_fvec(s->out);
100  AUBIO_FREE(s);
101}
102
103void aubio_dct_fftw_do(aubio_dct_fftw_t *s, const fvec_t *input, fvec_t *output) {
104  uint_t i;
105  fvec_copy(input, s->in);
106  fftw_execute(s->pfw);
107  //fvec_copy(s->out, output);
108  s->data[0] *= s->scalers[0];
109  for (i = 1; i < s->size; i++) {
110    s->data[i] *= s->scalers[1];
111  }
112  memcpy(output->data, s->data, output->length * sizeof(smpl_t));
113}
114
115void aubio_dct_fftw_rdo(aubio_dct_fftw_t *s, const fvec_t *input, fvec_t *output) {
116  uint_t i;
117  memcpy(s->data, input->data, input->length * sizeof(smpl_t));
118  //s->data[0] *= .5;
119  s->data[0] *= s->scalers[2];
120  for (i = 1; i < s->size; i++) {
121    s->data[i] *= s->scalers[3];
122  }
123  fftw_execute(s->pbw);
124  for (i = 0; i < s->size; i++) {
125    s->out->data[i] *= s->scalers[4];
126  }
127  fvec_copy(s->out, output);
128}
129
130#endif //HAVE_FFTW3
Note: See TracBrowser for help on using the repository browser.