[650e39b] | 1 | /* |
---|
[e6a78ea] | 2 | Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org> |
---|
[650e39b] | 3 | |
---|
[e6a78ea] | 4 | This file is part of aubio. |
---|
[650e39b] | 5 | |
---|
[e6a78ea] | 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/>. |
---|
[650e39b] | 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | #include "aubio_priv.h" |
---|
[6c7d49b] | 22 | #include "fvec.h" |
---|
| 23 | #include "cvec.h" |
---|
[650e39b] | 24 | #include "mathutils.h" |
---|
[32d6958] | 25 | #include "spectral/fft.h" |
---|
[2d8cffa] | 26 | #include "pitch/pitchyinfft.h" |
---|
[650e39b] | 27 | |
---|
[91879d9] | 28 | /** pitch yinfft structure */ |
---|
[fddfa64] | 29 | struct _aubio_pitchyinfft_t |
---|
| 30 | { |
---|
| 31 | fvec_t *win; /**< temporal weighting window */ |
---|
| 32 | fvec_t *winput; /**< windowed spectrum */ |
---|
| 33 | cvec_t *res; /**< complex vector to compute square difference function */ |
---|
| 34 | fvec_t *sqrmag; /**< square difference function */ |
---|
| 35 | fvec_t *weight; /**< spectral weighting window (psychoacoustic model) */ |
---|
| 36 | cvec_t *fftout; /**< Fourier transform output */ |
---|
| 37 | aubio_fft_t *fft; /**< fft object to compute square difference function */ |
---|
| 38 | fvec_t *yinfft; /**< Yin function */ |
---|
[22d33e2] | 39 | smpl_t tol; /**< Yin tolerance */ |
---|
[5284e0d] | 40 | smpl_t confidence; /**< confidence */ |
---|
[650e39b] | 41 | }; |
---|
| 42 | |
---|
[fddfa64] | 43 | static const smpl_t freqs[] = { 0., 20., 25., 31.5, 40., 50., 63., 80., 100., |
---|
[10cf306] | 44 | 125., 160., 200., 250., 315., 400., 500., 630., 800., 1000., 1250., |
---|
| 45 | 1600., 2000., 2500., 3150., 4000., 5000., 6300., 8000., 9000., 10000., |
---|
[fddfa64] | 46 | 12500., 15000., 20000., 25100 |
---|
| 47 | }; |
---|
[650e39b] | 48 | |
---|
[fddfa64] | 49 | static const smpl_t weight[] = { -75.8, -70.1, -60.8, -52.1, -44.2, -37.5, |
---|
[10cf306] | 50 | -31.3, -25.6, -20.9, -16.5, -12.6, -9.6, -7.0, -4.7, -3.0, -1.8, -0.8, |
---|
| 51 | -0.2, -0.0, 0.5, 1.6, 3.2, 5.4, 7.8, 8.1, 5.3, -2.4, -11.1, -12.8, |
---|
[fddfa64] | 52 | -12.2, -7.4, -17.8, -17.8, -17.8 |
---|
| 53 | }; |
---|
[650e39b] | 54 | |
---|
[fddfa64] | 55 | aubio_pitchyinfft_t * |
---|
| 56 | new_aubio_pitchyinfft (uint_t bufsize) |
---|
[650e39b] | 57 | { |
---|
[fddfa64] | 58 | aubio_pitchyinfft_t *p = AUBIO_NEW (aubio_pitchyinfft_t); |
---|
[168337e] | 59 | p->winput = new_fvec (bufsize); |
---|
| 60 | p->fft = new_aubio_fft (bufsize); |
---|
| 61 | p->fftout = new_cvec (bufsize); |
---|
| 62 | p->sqrmag = new_fvec (bufsize); |
---|
| 63 | p->res = new_cvec (bufsize); |
---|
| 64 | p->yinfft = new_fvec (bufsize / 2 + 1); |
---|
[fddfa64] | 65 | p->tol = 0.85; |
---|
| 66 | p->win = new_aubio_window ("hanningz", bufsize); |
---|
[168337e] | 67 | p->weight = new_fvec (bufsize / 2 + 1); |
---|
| 68 | uint_t i = 0, j = 1; |
---|
| 69 | smpl_t freq = 0, a0 = 0, a1 = 0, f0 = 0, f1 = 0; |
---|
| 70 | for (i = 0; i < p->weight->length; i++) { |
---|
| 71 | freq = (smpl_t) i / (smpl_t) bufsize *(smpl_t) 44100.; |
---|
| 72 | while (freq > freqs[j]) { |
---|
| 73 | j += 1; |
---|
[10cf306] | 74 | } |
---|
[168337e] | 75 | a0 = weight[j - 1]; |
---|
| 76 | f0 = freqs[j - 1]; |
---|
| 77 | a1 = weight[j]; |
---|
| 78 | f1 = freqs[j]; |
---|
| 79 | if (f0 == f1) { // just in case |
---|
| 80 | p->weight->data[i] = a0; |
---|
| 81 | } else if (f0 == 0) { // y = ax+b |
---|
| 82 | p->weight->data[i] = (a1 - a0) / f1 * freq + a0; |
---|
| 83 | } else { |
---|
| 84 | p->weight->data[i] = (a1 - a0) / (f1 - f0) * freq + |
---|
| 85 | (a0 - (a1 - a0) / (f1 / f0 - 1.)); |
---|
| 86 | } |
---|
| 87 | while (freq > freqs[j]) { |
---|
| 88 | j += 1; |
---|
| 89 | } |
---|
| 90 | //AUBIO_DBG("%f\n",p->weight->data[i]); |
---|
| 91 | p->weight->data[i] = DB2LIN (p->weight->data[i]); |
---|
| 92 | //p->weight->data[i] = SQRT(DB2LIN(p->weight->data[i])); |
---|
[650e39b] | 93 | } |
---|
| 94 | return p; |
---|
| 95 | } |
---|
| 96 | |
---|
[fddfa64] | 97 | void |
---|
| 98 | aubio_pitchyinfft_do (aubio_pitchyinfft_t * p, fvec_t * input, fvec_t * output) |
---|
| 99 | { |
---|
[168337e] | 100 | uint_t tau, l; |
---|
[650e39b] | 101 | uint_t halfperiod; |
---|
[22d33e2] | 102 | smpl_t tmp, sum; |
---|
[fddfa64] | 103 | cvec_t *res = (cvec_t *) p->res; |
---|
| 104 | fvec_t *yin = (fvec_t *) p->yinfft; |
---|
[168337e] | 105 | l = 0; |
---|
| 106 | tmp = 0.; |
---|
| 107 | sum = 0.; |
---|
| 108 | for (l = 0; l < input->length; l++) { |
---|
| 109 | p->winput->data[l] = p->win->data[l] * input->data[l]; |
---|
| 110 | } |
---|
| 111 | aubio_fft_do (p->fft, p->winput, p->fftout); |
---|
| 112 | for (l = 0; l < p->fftout->length; l++) { |
---|
| 113 | p->sqrmag->data[l] = SQR (p->fftout->norm[l]); |
---|
| 114 | p->sqrmag->data[l] *= p->weight->data[l]; |
---|
| 115 | } |
---|
| 116 | for (l = 1; l < p->fftout->length; l++) { |
---|
| 117 | p->sqrmag->data[(p->fftout->length - 1) * 2 - l] = |
---|
| 118 | SQR (p->fftout->norm[l]); |
---|
| 119 | p->sqrmag->data[(p->fftout->length - 1) * 2 - l] *= |
---|
| 120 | p->weight->data[l]; |
---|
| 121 | } |
---|
| 122 | for (l = 0; l < p->sqrmag->length / 2 + 1; l++) { |
---|
| 123 | sum += p->sqrmag->data[l]; |
---|
| 124 | } |
---|
| 125 | sum *= 2.; |
---|
| 126 | aubio_fft_do (p->fft, p->sqrmag, res); |
---|
| 127 | yin->data[0] = 1.; |
---|
| 128 | for (tau = 1; tau < yin->length; tau++) { |
---|
| 129 | yin->data[tau] = sum - res->norm[tau] * COS (res->phas[tau]); |
---|
| 130 | tmp += yin->data[tau]; |
---|
| 131 | yin->data[tau] *= tau / tmp; |
---|
| 132 | } |
---|
| 133 | tau = fvec_min_elem (yin); |
---|
| 134 | if (yin->data[tau] < p->tol) { |
---|
| 135 | /* no interpolation */ |
---|
| 136 | //return tau; |
---|
| 137 | /* 3 point quadratic interpolation */ |
---|
| 138 | //return fvec_quadint_min(yin,tau,1); |
---|
| 139 | /* additional check for (unlikely) octave doubling in higher frequencies */ |
---|
| 140 | if (tau > 35) { |
---|
| 141 | output->data[0] = fvec_quadint (yin, tau); |
---|
[10cf306] | 142 | } else { |
---|
[168337e] | 143 | /* should compare the minimum value of each interpolated peaks */ |
---|
| 144 | halfperiod = FLOOR (tau / 2 + .5); |
---|
| 145 | if (yin->data[halfperiod] < p->tol) |
---|
| 146 | output->data[0] = fvec_quadint (yin, halfperiod); |
---|
| 147 | else |
---|
| 148 | output->data[0] = fvec_quadint (yin, tau); |
---|
[10cf306] | 149 | } |
---|
[168337e] | 150 | } else { |
---|
| 151 | output->data[0] = 0.; |
---|
[22d33e2] | 152 | } |
---|
[650e39b] | 153 | } |
---|
| 154 | |
---|
[fddfa64] | 155 | void |
---|
| 156 | del_aubio_pitchyinfft (aubio_pitchyinfft_t * p) |
---|
| 157 | { |
---|
| 158 | del_fvec (p->win); |
---|
| 159 | del_aubio_fft (p->fft); |
---|
| 160 | del_fvec (p->yinfft); |
---|
| 161 | del_fvec (p->sqrmag); |
---|
| 162 | del_cvec (p->res); |
---|
| 163 | del_cvec (p->fftout); |
---|
| 164 | del_fvec (p->winput); |
---|
| 165 | del_fvec (p->weight); |
---|
| 166 | AUBIO_FREE (p); |
---|
[650e39b] | 167 | } |
---|
[22d33e2] | 168 | |
---|
[5284e0d] | 169 | smpl_t |
---|
| 170 | aubio_pitchyinfft_get_confidence (aubio_pitchyinfft_t * o) { |
---|
| 171 | o->confidence = 1. - fvec_min (o->yinfft); |
---|
| 172 | return o->confidence; |
---|
| 173 | } |
---|
| 174 | |
---|
[fddfa64] | 175 | uint_t |
---|
| 176 | aubio_pitchyinfft_set_tolerance (aubio_pitchyinfft_t * p, smpl_t tol) |
---|
| 177 | { |
---|
[22d33e2] | 178 | p->tol = tol; |
---|
| 179 | return 0; |
---|
| 180 | } |
---|
| 181 | |
---|
[fddfa64] | 182 | smpl_t |
---|
| 183 | aubio_pitchyinfft_get_tolerance (aubio_pitchyinfft_t * p) |
---|
| 184 | { |
---|
[22d33e2] | 185 | return p->tol; |
---|
| 186 | } |
---|