1 | /* |
---|
2 | Copyright (C) 2013 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 "cvec.h" |
---|
24 | #include "mathutils.h" |
---|
25 | #include "spectral/fft.h" |
---|
26 | #include "pitch/pitchspecacf.h" |
---|
27 | |
---|
28 | /** pitch specacf structure */ |
---|
29 | struct _aubio_pitchspecacf_t |
---|
30 | { |
---|
31 | fvec_t *win; /**< temporal weighting window */ |
---|
32 | fvec_t *winput; /**< windowed spectrum */ |
---|
33 | aubio_fft_t *fft; /**< fft object to compute*/ |
---|
34 | fvec_t *fftout; /**< Fourier transform output */ |
---|
35 | fvec_t *sqrmag; /**< square magnitudes */ |
---|
36 | fvec_t *acf; /**< auto correlation function */ |
---|
37 | smpl_t tol; /**< tolerance */ |
---|
38 | smpl_t confidence; /**< confidence */ |
---|
39 | }; |
---|
40 | |
---|
41 | aubio_pitchspecacf_t * |
---|
42 | new_aubio_pitchspecacf (uint_t bufsize) |
---|
43 | { |
---|
44 | aubio_pitchspecacf_t *p = AUBIO_NEW (aubio_pitchspecacf_t); |
---|
45 | p->fft = new_aubio_fft (bufsize); |
---|
46 | if (!p->fft) goto beach; |
---|
47 | p->win = new_aubio_window ("hanningz", bufsize); |
---|
48 | p->winput = new_fvec (bufsize); |
---|
49 | p->fftout = new_fvec (bufsize); |
---|
50 | p->sqrmag = new_fvec (bufsize); |
---|
51 | p->acf = new_fvec (bufsize / 2 + 1); |
---|
52 | p->tol = 1.; |
---|
53 | p->confidence = 0.; |
---|
54 | return p; |
---|
55 | |
---|
56 | beach: |
---|
57 | AUBIO_FREE(p); |
---|
58 | return NULL; |
---|
59 | } |
---|
60 | |
---|
61 | void |
---|
62 | aubio_pitchspecacf_do (aubio_pitchspecacf_t * p, const fvec_t * input, fvec_t * output) |
---|
63 | { |
---|
64 | uint_t l, tau; |
---|
65 | fvec_t *fftout = p->fftout; |
---|
66 | // window the input |
---|
67 | for (l = 0; l < input->length; l++) { |
---|
68 | p->winput->data[l] = p->win->data[l] * input->data[l]; |
---|
69 | } |
---|
70 | // get the real / imag parts of its fft |
---|
71 | aubio_fft_do_complex (p->fft, p->winput, fftout); |
---|
72 | for (l = 0; l < input->length / 2 + 1; l++) { |
---|
73 | p->sqrmag->data[l] = SQR(fftout->data[l]); |
---|
74 | } |
---|
75 | // get the real / imag parts of the fft of the squared magnitude |
---|
76 | aubio_fft_do_complex (p->fft, p->sqrmag, fftout); |
---|
77 | // copy real part to acf |
---|
78 | for (l = 0; l < fftout->length / 2 + 1; l++) { |
---|
79 | p->acf->data[l] = fftout->data[l]; |
---|
80 | } |
---|
81 | // get the minimum |
---|
82 | tau = fvec_min_elem (p->acf); |
---|
83 | // get the interpolated minimum |
---|
84 | output->data[0] = fvec_quadratic_peak_pos (p->acf, tau) * 2.; |
---|
85 | } |
---|
86 | |
---|
87 | void |
---|
88 | del_aubio_pitchspecacf (aubio_pitchspecacf_t * p) |
---|
89 | { |
---|
90 | del_fvec (p->win); |
---|
91 | del_fvec (p->winput); |
---|
92 | del_aubio_fft (p->fft); |
---|
93 | del_fvec (p->sqrmag); |
---|
94 | del_fvec (p->fftout); |
---|
95 | del_fvec (p->acf); |
---|
96 | AUBIO_FREE (p); |
---|
97 | } |
---|
98 | |
---|
99 | smpl_t |
---|
100 | aubio_pitchspecacf_get_confidence (const aubio_pitchspecacf_t * o) { |
---|
101 | // no confidence for now |
---|
102 | return o->confidence; |
---|
103 | } |
---|
104 | |
---|
105 | uint_t |
---|
106 | aubio_pitchspecacf_set_tolerance (aubio_pitchspecacf_t * p, smpl_t tol) |
---|
107 | { |
---|
108 | p->tol = tol; |
---|
109 | return 0; |
---|
110 | } |
---|
111 | |
---|
112 | smpl_t |
---|
113 | aubio_pitchspecacf_get_tolerance (const aubio_pitchspecacf_t * p) |
---|
114 | { |
---|
115 | return p->tol; |
---|
116 | } |
---|