source: src/pitch/pitchyin.c @ 1175883

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 1175883 was e391790, checked in by Paul Brossier <piem@piem.org>, 9 years ago

src/pitch/pitchyin.c, src/pitch/pitchyinfft.c: avoid producing NaN on silence (closes #7)

  • Property mode set to 100644
File size: 4.2 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;
[5284e0d]39  smpl_t confidence;
[2ba3440]40};
41
42/** compute difference function
43 
44  \param input input signal
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
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
53
54*/
[fddfa64]55void aubio_pitchyin_getcum (fvec_t * yinbuf);
[2ba3440]56
57/** detect pitch in a YIN function
58 
59  \param yinbuf input buffer as computed by aubio_pitchyin_getcum
60
61*/
[fddfa64]62uint_t aubio_pitchyin_getpitch (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;
70  return o;
71}
72
[fddfa64]73void
74del_aubio_pitchyin (aubio_pitchyin_t * o)
75{
76  del_fvec (o->yin);
77  AUBIO_FREE (o);
[2ba3440]78}
79
[96fb8ad]80/* outputs the difference function */
[fddfa64]81void
82aubio_pitchyin_diff (fvec_t * input, fvec_t * yin)
83{
[168337e]84  uint_t j, tau;
[a2f3555]85  smpl_t tmp;
[168337e]86  for (tau = 0; tau < yin->length; tau++) {
87    yin->data[tau] = 0.;
88  }
89  for (tau = 1; tau < yin->length; tau++) {
90    for (j = 0; j < yin->length; j++) {
91      tmp = input->data[j] - input->data[j + tau];
92      yin->data[tau] += SQR (tmp);
[a2f3555]93    }
94  }
[96fb8ad]95}
96
97/* cumulative mean normalized difference function */
[fddfa64]98void
99aubio_pitchyin_getcum (fvec_t * yin)
100{
[168337e]101  uint_t tau;
[69d642a]102  smpl_t tmp = 0.;
[168337e]103  yin->data[0] = 1.;
104  //AUBIO_DBG("%f\t",yin->data[0]);
105  for (tau = 1; tau < yin->length; tau++) {
106    tmp += yin->data[tau];
107    yin->data[tau] *= tau / tmp;
108    //AUBIO_DBG("%f\t",yin->data[tau]);
[a2f3555]109  }
[168337e]110  //AUBIO_DBG("\n");
[96fb8ad]111}
112
[fddfa64]113uint_t
114aubio_pitchyin_getpitch (fvec_t * yin)
115{
[168337e]116  uint_t tau = 1;
[fddfa64]117  do {
[168337e]118    if (yin->data[tau] < 0.1) {
119      while (yin->data[tau + 1] < yin->data[tau]) {
[a2f3555]120        tau++;
121      }
122      return tau;
123    }
124    tau++;
[fddfa64]125  } while (tau < yin->length);
[a2f3555]126  //AUBIO_DBG("No pitch found");
127  return 0;
[96fb8ad]128}
129
[09b082d]130
131/* all the above in one */
[fddfa64]132void
133aubio_pitchyin_do (aubio_pitchyin_t * o, fvec_t * input, fvec_t * out)
134{
[2ba3440]135  smpl_t tol = o->tol;
[fddfa64]136  fvec_t *yin = o->yin;
[168337e]137  uint_t j, tau = 0;
[a2f3555]138  sint_t period;
139  smpl_t tmp = 0., tmp2 = 0.;
[168337e]140  yin->data[0] = 1.;
141  for (tau = 1; tau < yin->length; tau++) {
142    yin->data[tau] = 0.;
143    for (j = 0; j < yin->length; j++) {
144      tmp = input->data[j] - input->data[j + tau];
145      yin->data[tau] += SQR (tmp);
146    }
147    tmp2 += yin->data[tau];
[e391790]148    if (tmp2 != 0) {
149      yin->data[tau] *= tau / tmp2;
150    } else {
151      yin->data[tau] = 1.;
152    }
[168337e]153    period = tau - 3;
154    if (tau > 4 && (yin->data[period] < tol) &&
155        (yin->data[period] < yin->data[period + 1])) {
[69d642a]156      out->data[0] = fvec_quadratic_peak_pos (yin, period);
[168337e]157      goto beach;
[a2f3555]158    }
159  }
[69d642a]160  out->data[0] = fvec_quadratic_peak_pos (yin, fvec_min_elem (yin));
[168337e]161beach:
162  return;
[09b082d]163}
164
[5284e0d]165smpl_t
166aubio_pitchyin_get_confidence (aubio_pitchyin_t * o) {
167  o->confidence = 1. - fvec_min (o->yin);
168  return o->confidence;
169}
170
[fddfa64]171uint_t
172aubio_pitchyin_set_tolerance (aubio_pitchyin_t * o, smpl_t tol)
173{
[2ba3440]174  o->tol = tol;
175  return 0;
176}
177
[fddfa64]178smpl_t
179aubio_pitchyin_get_tolerance (aubio_pitchyin_t * o)
180{
[2ba3440]181  return o->tol;
182}
Note: See TracBrowser for help on using the repository browser.