[650e39b] | 1 | /* |
---|
[a5f4b7d] | 2 | Copyright (C) 2003-2013 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 | fvec_t *sqrmag; /**< square difference function */ |
---|
| 34 | fvec_t *weight; /**< spectral weighting window (psychoacoustic model) */ |
---|
[4a95f83] | 35 | fvec_t *fftout; /**< Fourier transform output */ |
---|
[fddfa64] | 36 | aubio_fft_t *fft; /**< fft object to compute square difference function */ |
---|
| 37 | fvec_t *yinfft; /**< Yin function */ |
---|
[22d33e2] | 38 | smpl_t tol; /**< Yin tolerance */ |
---|
[5284e0d] | 39 | smpl_t confidence; /**< confidence */ |
---|
[9c9202f] | 40 | uint_t short_period; /** shortest period under which to check for octave error */ |
---|
[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 * |
---|
[9c9202f] | 56 | new_aubio_pitchyinfft (uint_t samplerate, 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); |
---|
[4a95f83] | 61 | p->fftout = new_fvec (bufsize); |
---|
[168337e] | 62 | p->sqrmag = new_fvec (bufsize); |
---|
| 63 | p->yinfft = new_fvec (bufsize / 2 + 1); |
---|
[fddfa64] | 64 | p->tol = 0.85; |
---|
| 65 | p->win = new_aubio_window ("hanningz", bufsize); |
---|
[168337e] | 66 | p->weight = new_fvec (bufsize / 2 + 1); |
---|
| 67 | uint_t i = 0, j = 1; |
---|
| 68 | smpl_t freq = 0, a0 = 0, a1 = 0, f0 = 0, f1 = 0; |
---|
| 69 | for (i = 0; i < p->weight->length; i++) { |
---|
[9c9202f] | 70 | freq = (smpl_t) i / (smpl_t) bufsize *(smpl_t) samplerate; |
---|
[168337e] | 71 | while (freq > freqs[j]) { |
---|
| 72 | j += 1; |
---|
[10cf306] | 73 | } |
---|
[168337e] | 74 | a0 = weight[j - 1]; |
---|
| 75 | f0 = freqs[j - 1]; |
---|
| 76 | a1 = weight[j]; |
---|
| 77 | f1 = freqs[j]; |
---|
| 78 | if (f0 == f1) { // just in case |
---|
| 79 | p->weight->data[i] = a0; |
---|
| 80 | } else if (f0 == 0) { // y = ax+b |
---|
| 81 | p->weight->data[i] = (a1 - a0) / f1 * freq + a0; |
---|
| 82 | } else { |
---|
| 83 | p->weight->data[i] = (a1 - a0) / (f1 - f0) * freq + |
---|
| 84 | (a0 - (a1 - a0) / (f1 / f0 - 1.)); |
---|
| 85 | } |
---|
| 86 | while (freq > freqs[j]) { |
---|
| 87 | j += 1; |
---|
| 88 | } |
---|
| 89 | //AUBIO_DBG("%f\n",p->weight->data[i]); |
---|
| 90 | p->weight->data[i] = DB2LIN (p->weight->data[i]); |
---|
| 91 | //p->weight->data[i] = SQRT(DB2LIN(p->weight->data[i])); |
---|
[650e39b] | 92 | } |
---|
[9c9202f] | 93 | // check for octave errors above 1300 Hz |
---|
| 94 | p->short_period = (uint_t)ROUND(samplerate / 1300.); |
---|
[650e39b] | 95 | return p; |
---|
| 96 | } |
---|
| 97 | |
---|
[fddfa64] | 98 | void |
---|
| 99 | aubio_pitchyinfft_do (aubio_pitchyinfft_t * p, fvec_t * input, fvec_t * output) |
---|
| 100 | { |
---|
[168337e] | 101 | uint_t tau, l; |
---|
[a5f4b7d] | 102 | uint_t length = p->fftout->length; |
---|
[650e39b] | 103 | uint_t halfperiod; |
---|
[a5f4b7d] | 104 | fvec_t *fftout = p->fftout; |
---|
| 105 | fvec_t *yin = p->yinfft; |
---|
| 106 | smpl_t tmp = 0., sum = 0.; |
---|
| 107 | // window the input |
---|
[168337e] | 108 | for (l = 0; l < input->length; l++) { |
---|
| 109 | p->winput->data[l] = p->win->data[l] * input->data[l]; |
---|
| 110 | } |
---|
[a5f4b7d] | 111 | // get the real / imag parts of its fft |
---|
| 112 | aubio_fft_do_complex (p->fft, p->winput, fftout); |
---|
| 113 | // get the squared magnitude spectrum, applying some weight |
---|
| 114 | p->sqrmag->data[0] = SQR(fftout->data[0]); |
---|
[8da0033] | 115 | p->sqrmag->data[0] *= p->weight->data[0]; |
---|
[4a95f83] | 116 | for (l = 1; l < length / 2; l++) { |
---|
[a5f4b7d] | 117 | p->sqrmag->data[l] = SQR(fftout->data[l]) + SQR(fftout->data[length - l]); |
---|
[168337e] | 118 | p->sqrmag->data[l] *= p->weight->data[l]; |
---|
[a5f4b7d] | 119 | p->sqrmag->data[length - l] = p->sqrmag->data[l]; |
---|
[168337e] | 120 | } |
---|
[a5f4b7d] | 121 | p->sqrmag->data[length / 2] = SQR(fftout->data[length / 2]); |
---|
[4a95f83] | 122 | p->sqrmag->data[length / 2] *= p->weight->data[length / 2]; |
---|
[a5f4b7d] | 123 | // get sum of weighted squared mags |
---|
[4a95f83] | 124 | for (l = 0; l < length / 2 + 1; l++) { |
---|
[168337e] | 125 | sum += p->sqrmag->data[l]; |
---|
| 126 | } |
---|
| 127 | sum *= 2.; |
---|
[a5f4b7d] | 128 | // get the real / imag parts of the fft of the squared magnitude |
---|
| 129 | aubio_fft_do_complex (p->fft, p->sqrmag, fftout); |
---|
[168337e] | 130 | yin->data[0] = 1.; |
---|
| 131 | for (tau = 1; tau < yin->length; tau++) { |
---|
[a5f4b7d] | 132 | // compute the square differences |
---|
| 133 | yin->data[tau] = sum - fftout->data[tau]; |
---|
| 134 | // and the cumulative mean normalized difference function |
---|
[168337e] | 135 | tmp += yin->data[tau]; |
---|
| 136 | yin->data[tau] *= tau / tmp; |
---|
| 137 | } |
---|
[a5f4b7d] | 138 | // find best candidates |
---|
[168337e] | 139 | tau = fvec_min_elem (yin); |
---|
| 140 | if (yin->data[tau] < p->tol) { |
---|
[a5f4b7d] | 141 | // no interpolation, directly return the period as an integer |
---|
| 142 | //output->data[0] = tau; |
---|
| 143 | //return; |
---|
| 144 | |
---|
| 145 | // 3 point quadratic interpolation |
---|
[dc7f2cc] | 146 | //return fvec_quadratic_peak_pos (yin,tau,1); |
---|
[168337e] | 147 | /* additional check for (unlikely) octave doubling in higher frequencies */ |
---|
[9c9202f] | 148 | if (tau > p->short_period) { |
---|
[dc7f2cc] | 149 | output->data[0] = fvec_quadratic_peak_pos (yin, tau); |
---|
[10cf306] | 150 | } else { |
---|
[168337e] | 151 | /* should compare the minimum value of each interpolated peaks */ |
---|
| 152 | halfperiod = FLOOR (tau / 2 + .5); |
---|
| 153 | if (yin->data[halfperiod] < p->tol) |
---|
[dc7f2cc] | 154 | output->data[0] = fvec_quadratic_peak_pos (yin, halfperiod); |
---|
[168337e] | 155 | else |
---|
[dc7f2cc] | 156 | output->data[0] = fvec_quadratic_peak_pos (yin, tau); |
---|
[10cf306] | 157 | } |
---|
[168337e] | 158 | } else { |
---|
| 159 | output->data[0] = 0.; |
---|
[22d33e2] | 160 | } |
---|
[650e39b] | 161 | } |
---|
| 162 | |
---|
[fddfa64] | 163 | void |
---|
| 164 | del_aubio_pitchyinfft (aubio_pitchyinfft_t * p) |
---|
| 165 | { |
---|
| 166 | del_fvec (p->win); |
---|
| 167 | del_aubio_fft (p->fft); |
---|
| 168 | del_fvec (p->yinfft); |
---|
| 169 | del_fvec (p->sqrmag); |
---|
[4a95f83] | 170 | del_fvec (p->fftout); |
---|
[fddfa64] | 171 | del_fvec (p->winput); |
---|
| 172 | del_fvec (p->weight); |
---|
| 173 | AUBIO_FREE (p); |
---|
[650e39b] | 174 | } |
---|
[22d33e2] | 175 | |
---|
[5284e0d] | 176 | smpl_t |
---|
| 177 | aubio_pitchyinfft_get_confidence (aubio_pitchyinfft_t * o) { |
---|
| 178 | o->confidence = 1. - fvec_min (o->yinfft); |
---|
| 179 | return o->confidence; |
---|
| 180 | } |
---|
| 181 | |
---|
[fddfa64] | 182 | uint_t |
---|
| 183 | aubio_pitchyinfft_set_tolerance (aubio_pitchyinfft_t * p, smpl_t tol) |
---|
| 184 | { |
---|
[22d33e2] | 185 | p->tol = tol; |
---|
| 186 | return 0; |
---|
| 187 | } |
---|
| 188 | |
---|
[fddfa64] | 189 | smpl_t |
---|
| 190 | aubio_pitchyinfft_get_tolerance (aubio_pitchyinfft_t * p) |
---|
| 191 | { |
---|
[22d33e2] | 192 | return p->tol; |
---|
| 193 | } |
---|