source: src/pitch/pitchyinfast.c @ 3e48568

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretch
Last change on this file since 3e48568 was 3e48568, checked in by Eduard Müller <mueller.eduard@googlemail.com>, 2 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: 5.4 KB
Line 
1/*
2  Copyright (C) 2003-2017 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/* This algorithm was developed by A. de Cheveigné and H. Kawahara and
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"
31#include "fvec.h"
32#include "mathutils.h"
33#include "cvec.h"
34#include "spectral/fft.h"
35#include "pitch/pitchyinfast.h"
36
37struct _aubio_pitchyinfast_t
38{
39  fvec_t *yin;
40  smpl_t tol;
41  uint_t peak_pos;
42  fvec_t *tmpdata;
43  fvec_t *sqdiff;
44  fvec_t *kernel;
45  fvec_t *samples_fft;
46  fvec_t *kernel_fft;
47  aubio_fft_t *fft;
48};
49
50aubio_pitchyinfast_t *
51new_aubio_pitchyinfast (uint_t bufsize)
52{
53  aubio_pitchyinfast_t *o = AUBIO_NEW (aubio_pitchyinfast_t);
54  o->yin = new_fvec (bufsize / 2);
55  o->tmpdata = new_fvec (bufsize);
56  o->sqdiff = new_fvec (bufsize / 2);
57  o->kernel = new_fvec (bufsize);
58  o->samples_fft = new_fvec (bufsize);
59  o->kernel_fft = new_fvec (bufsize);
60  o->fft = new_aubio_fft (bufsize);
61  o->tol = 0.15;
62  o->peak_pos = 0;
63  return o;
64}
65
66void
67del_aubio_pitchyinfast (aubio_pitchyinfast_t * o)
68{
69  del_fvec (o->yin);
70  del_fvec (o->tmpdata);
71  del_fvec (o->sqdiff);
72  del_fvec (o->kernel);
73  del_fvec (o->samples_fft);
74  del_fvec (o->kernel_fft);
75  del_aubio_fft (o->fft);
76  AUBIO_FREE (o);
77}
78
79/* all the above in one */
80void
81aubio_pitchyinfast_do (aubio_pitchyinfast_t * o, const fvec_t * input, fvec_t * out)
82{
83  const smpl_t tol = o->tol;
84  fvec_t* yin = o->yin;
85  const uint_t length = yin->length;
86  uint_t B = o->tmpdata->length;
87  uint_t W = o->yin->length; // B / 2
88  fvec_t tmp_slice, kernel_ptr;
89  uint_t tau;
90  sint_t period;
91  smpl_t tmp2 = 0.;
92
93  // compute r_t(0) + r_t+tau(0)
94  {
95    fvec_t *squares = o->tmpdata;
96    fvec_weighted_copy(input, input, squares);
97#if 0
98    for (tau = 0; tau < W; tau++) {
99      tmp_slice.data = squares->data + tau;
100      tmp_slice.length = W;
101      o->sqdiff->data[tau] = fvec_sum(&tmp_slice);
102    }
103#else
104    tmp_slice.data = squares->data;
105    tmp_slice.length = W;
106    o->sqdiff->data[0] = fvec_sum(&tmp_slice);
107    for (tau = 1; tau < W; tau++) {
108      o->sqdiff->data[tau] = o->sqdiff->data[tau-1];
109      o->sqdiff->data[tau] -= squares->data[tau-1];
110      o->sqdiff->data[tau] += squares->data[W+tau-1];
111    }
112#endif
113    fvec_add(o->sqdiff, o->sqdiff->data[0]);
114  }
115  // compute r_t(tau) = -2.*ifft(fft(samples)*fft(samples[W-1::-1]))
116  {
117    fvec_t *compmul = o->tmpdata;
118    fvec_t *rt_of_tau = o->samples_fft;
119    aubio_fft_do_complex(o->fft, input, o->samples_fft);
120    // build kernel, take a copy of first half of samples
121    tmp_slice.data = input->data;
122    tmp_slice.length = W;
123    kernel_ptr.data = o->kernel->data + 1;
124    kernel_ptr.length = W;
125    fvec_copy(&tmp_slice, &kernel_ptr);
126    // reverse them
127    fvec_rev(&kernel_ptr);
128    // compute fft(kernel)
129    aubio_fft_do_complex(o->fft, o->kernel, o->kernel_fft);
130    // compute complex product
131    compmul->data[0]  = o->kernel_fft->data[0] * o->samples_fft->data[0];
132    for (tau = 1; tau < W; tau++) {
133      compmul->data[tau]    = o->kernel_fft->data[tau] * o->samples_fft->data[tau];
134      compmul->data[tau]   -= o->kernel_fft->data[B-tau] * o->samples_fft->data[B-tau];
135    }
136    compmul->data[W]    = o->kernel_fft->data[W] * o->samples_fft->data[W];
137    for (tau = 1; tau < W; tau++) {
138      compmul->data[B-tau]  = o->kernel_fft->data[B-tau] * o->samples_fft->data[tau];
139      compmul->data[B-tau] += o->kernel_fft->data[tau] * o->samples_fft->data[B-tau];
140    }
141    // compute inverse fft
142    aubio_fft_rdo_complex(o->fft, compmul, rt_of_tau);
143    // compute square difference r_t(tau) = sqdiff - 2 * r_t_tau[W-1:-1]
144    for (tau = 0; tau < W; tau++) {
145      yin->data[tau] = o->sqdiff->data[tau] - 2. * rt_of_tau->data[tau+W];
146    }
147  }
148
149  // now build yin and look for first minimum
150  fvec_zeros(out);
151  yin->data[0] = 1.;
152  for (tau = 1; tau < length; tau++) {
153    tmp2 += yin->data[tau];
154    if (tmp2 != 0) {
155      yin->data[tau] *= tau / tmp2;
156    } else {
157      yin->data[tau] = 1.;
158    }
159    period = tau - 3;
160    if (tau > 4 && (yin->data[period] < tol) &&
161        (yin->data[period] < yin->data[period + 1])) {
162      o->peak_pos = (uint_t)period;
163      out->data[0] = fvec_quadratic_peak_pos (yin, o->peak_pos);
164      return;
165    }
166  }
167  // use global minimum
168  o->peak_pos = (uint_t)fvec_min_elem (yin);
169  out->data[0] = fvec_quadratic_peak_pos (yin, o->peak_pos);
170}
171
172smpl_t
173aubio_pitchyinfast_get_confidence (aubio_pitchyinfast_t * o) {
174  return 1. - o->yin->data[o->peak_pos];
175}
176
177uint_t
178aubio_pitchyinfast_set_tolerance (aubio_pitchyinfast_t * o, smpl_t tol)
179{
180  o->tol = tol;
181  return 0;
182}
183
184smpl_t
185aubio_pitchyinfast_get_tolerance (aubio_pitchyinfast_t * o)
186{
187  return o->tol;
188}
Note: See TracBrowser for help on using the repository browser.