source: src/pitch/pitchyin.c @ a114fe0

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since a114fe0 was 3e48568, checked in by Eduard Müller <mueller.eduard@googlemail.com>, 7 years ago

yin pitch confidence tweaks

return pitch candidate search's confidence in yin algorithms and not the
global minimum, so that the confidence reflects the returned pitch value

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[96fb8ad]1/*
[e6a78ea]2  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
[96fb8ad]3
[e6a78ea]4  This file is part of aubio.
[96fb8ad]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/>.
[96fb8ad]18
19*/
20
[7f0fd1d]21/* This algorithm was developed by A. de Cheveigné and H. Kawahara and
[96fb8ad]22 * published in:
23 *
24 * de Cheveigné, A., Kawahara, H. (2002) "YIN, a fundamental frequency
25 * estimator for speech and music", J. Acoust. Soc. Am. 111, 1917-1930. 
26 *
27 * see http://recherche.ircam.fr/equipes/pcm/pub/people/cheveign.html
28 */
29
30#include "aubio_priv.h"
[6c7d49b]31#include "fvec.h"
[96fb8ad]32#include "mathutils.h"
[2d8cffa]33#include "pitch/pitchyin.h"
[96fb8ad]34
[fddfa64]35struct _aubio_pitchyin_t
36{
37  fvec_t *yin;
[2ba3440]38  smpl_t tol;
[3e48568]39  uint_t peak_pos;
[2ba3440]40};
41
42/** compute difference function
[ce3ff2b]43
44  \param input input signal
[2ba3440]45  \param yinbuf output buffer to store difference function (half shorter than input)
46
47*/
[fddfa64]48void aubio_pitchyin_diff (fvec_t * input, fvec_t * yinbuf);
[2ba3440]49
[ce3ff2b]50/** in place computation of the YIN cumulative normalised function
51
52  \param yinbuf input signal (a square difference function), also used to store function
[2ba3440]53
54*/
[fddfa64]55void aubio_pitchyin_getcum (fvec_t * yinbuf);
[2ba3440]56
57/** detect pitch in a YIN function
[ce3ff2b]58
[2ba3440]59  \param yinbuf input buffer as computed by aubio_pitchyin_getcum
60
61*/
[ce3ff2b]62uint_t aubio_pitchyin_getpitch (const fvec_t * yinbuf);
[2ba3440]63
[fddfa64]64aubio_pitchyin_t *
65new_aubio_pitchyin (uint_t bufsize)
66{
67  aubio_pitchyin_t *o = AUBIO_NEW (aubio_pitchyin_t);
[168337e]68  o->yin = new_fvec (bufsize / 2);
[2ba3440]69  o->tol = 0.15;
[3e48568]70  o->peak_pos = 0;
[2ba3440]71  return o;
72}
73
[fddfa64]74void
75del_aubio_pitchyin (aubio_pitchyin_t * o)
76{
77  del_fvec (o->yin);
78  AUBIO_FREE (o);
[2ba3440]79}
80
[96fb8ad]81/* outputs the difference function */
[fddfa64]82void
83aubio_pitchyin_diff (fvec_t * input, fvec_t * yin)
84{
[168337e]85  uint_t j, tau;
[a2f3555]86  smpl_t tmp;
[168337e]87  for (tau = 0; tau < yin->length; tau++) {
88    yin->data[tau] = 0.;
89  }
90  for (tau = 1; tau < yin->length; tau++) {
91    for (j = 0; j < yin->length; j++) {
92      tmp = input->data[j] - input->data[j + tau];
93      yin->data[tau] += SQR (tmp);
[a2f3555]94    }
95  }
[96fb8ad]96}
97
98/* cumulative mean normalized difference function */
[fddfa64]99void
100aubio_pitchyin_getcum (fvec_t * yin)
101{
[168337e]102  uint_t tau;
[69d642a]103  smpl_t tmp = 0.;
[168337e]104  yin->data[0] = 1.;
105  //AUBIO_DBG("%f\t",yin->data[0]);
106  for (tau = 1; tau < yin->length; tau++) {
107    tmp += yin->data[tau];
108    yin->data[tau] *= tau / tmp;
109    //AUBIO_DBG("%f\t",yin->data[tau]);
[a2f3555]110  }
[168337e]111  //AUBIO_DBG("\n");
[96fb8ad]112}
113
[fddfa64]114uint_t
[ce3ff2b]115aubio_pitchyin_getpitch (const fvec_t * yin)
[fddfa64]116{
[168337e]117  uint_t tau = 1;
[fddfa64]118  do {
[168337e]119    if (yin->data[tau] < 0.1) {
120      while (yin->data[tau + 1] < yin->data[tau]) {
[a2f3555]121        tau++;
122      }
123      return tau;
124    }
125    tau++;
[fddfa64]126  } while (tau < yin->length);
[a2f3555]127  //AUBIO_DBG("No pitch found");
128  return 0;
[96fb8ad]129}
130
[09b082d]131/* all the above in one */
[fddfa64]132void
[ce3ff2b]133aubio_pitchyin_do (aubio_pitchyin_t * o, const fvec_t * input, fvec_t * out)
[fddfa64]134{
[813f4c7]135  const smpl_t tol = o->tol;
136  fvec_t* yin = o->yin;
137  const smpl_t *input_data = input->data;
138  const uint_t length = yin->length;
139  smpl_t *yin_data = yin->data;
140  uint_t j, tau;
[a2f3555]141  sint_t period;
[813f4c7]142  smpl_t tmp, tmp2 = 0.;
143
144  yin_data[0] = 1.;
145  for (tau = 1; tau < length; tau++) {
146    yin_data[tau] = 0.;
147    for (j = 0; j < length; j++) {
148      tmp = input_data[j] - input_data[j + tau];
149      yin_data[tau] += SQR (tmp);
[168337e]150    }
[813f4c7]151    tmp2 += yin_data[tau];
[e391790]152    if (tmp2 != 0) {
153      yin->data[tau] *= tau / tmp2;
154    } else {
155      yin->data[tau] = 1.;
156    }
[168337e]157    period = tau - 3;
[813f4c7]158    if (tau > 4 && (yin_data[period] < tol) &&
159        (yin_data[period] < yin_data[period + 1])) {
[3e48568]160      o->peak_pos = (uint_t)period;
161      out->data[0] = fvec_quadratic_peak_pos (yin, o->peak_pos);
162      return;
[a2f3555]163    }
164  }
[3e48568]165  o->peak_pos = (uint_t)fvec_min_elem (yin);
166  out->data[0] = fvec_quadratic_peak_pos (yin, o->peak_pos);
[09b082d]167}
168
[5284e0d]169smpl_t
170aubio_pitchyin_get_confidence (aubio_pitchyin_t * o) {
[3e48568]171  return 1. - o->yin->data[o->peak_pos];
[5284e0d]172}
173
[fddfa64]174uint_t
175aubio_pitchyin_set_tolerance (aubio_pitchyin_t * o, smpl_t tol)
176{
[2ba3440]177  o->tol = tol;
178  return 0;
179}
180
[fddfa64]181smpl_t
182aubio_pitchyin_get_tolerance (aubio_pitchyin_t * o)
183{
[2ba3440]184  return o->tol;
185}
Note: See TracBrowser for help on using the repository browser.