src/pitch/pitchspecacf.c: set confidence to 0. for now
[aubio.git] / src / pitch / pitchspecacf.c
1 /*
2   Copyright (C) 2013 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 #include "aubio_priv.h"
22 #include "fvec.h"
23 #include "cvec.h"
24 #include "mathutils.h"
25 #include "spectral/fft.h"
26 #include "pitch/pitchspecacf.h"
27
28 /** pitch specacf structure */
29 struct _aubio_pitchspecacf_t
30 {
31   fvec_t *win;        /**< temporal weighting window */
32   fvec_t *winput;     /**< windowed spectrum */
33   aubio_fft_t *fft;   /**< fft object to compute*/
34   fvec_t *fftout;     /**< Fourier transform output */
35   fvec_t *sqrmag;     /**< square magnitudes */
36   fvec_t *acf;        /**< auto correlation function */
37   smpl_t tol;         /**< tolerance */
38   smpl_t confidence;  /**< confidence */
39 };
40
41 aubio_pitchspecacf_t *
42 new_aubio_pitchspecacf (uint_t bufsize)
43 {
44   aubio_pitchspecacf_t *p = AUBIO_NEW (aubio_pitchspecacf_t);
45   p->win = new_aubio_window ("hanningz", bufsize);
46   p->winput = new_fvec (bufsize);
47   p->fft = new_aubio_fft (bufsize);
48   p->fftout = new_fvec (bufsize);
49   p->sqrmag = new_fvec (bufsize);
50   p->acf = new_fvec (bufsize / 2 + 1);
51   p->tol = 1.;
52   p->confidence = 0.;
53   return p;
54 }
55
56 void
57 aubio_pitchspecacf_do (aubio_pitchspecacf_t * p, fvec_t * input, fvec_t * output)
58 {
59   uint_t l;
60   fvec_t *fftout = p->fftout;
61   // window the input
62   for (l = 0; l < input->length; l++) {
63     p->winput->data[l] = p->win->data[l] * input->data[l];
64   }
65   // get the real / imag parts of its fft
66   aubio_fft_do_complex (p->fft, p->winput, fftout);
67   for (l = 0; l < input->length / 2 + 1; l++) {
68     p->sqrmag->data[l] = SQR(fftout->data[l]);
69   }
70   // get the real / imag parts of the fft of the squared magnitude
71   aubio_fft_do_complex (p->fft, p->sqrmag, fftout);
72   // copy real part to acf
73   for (l = 0; l < fftout->length / 2 + 1; l++) {
74     p->acf->data[l] = fftout->data[l];
75   }
76   // get the minimum
77   uint_t tau = fvec_min_elem (p->acf);
78   // get the interpolated minimum
79   output->data[0] = fvec_quadratic_peak_pos (p->acf, tau) * 2.;
80 }
81
82 void
83 del_aubio_pitchspecacf (aubio_pitchspecacf_t * p)
84 {
85   del_fvec (p->win);
86   del_fvec (p->winput);
87   del_aubio_fft (p->fft);
88   del_fvec (p->sqrmag);
89   del_fvec (p->fftout);
90   AUBIO_FREE (p);
91 }
92
93 smpl_t
94 aubio_pitchspecacf_get_confidence (aubio_pitchspecacf_t * o) {
95   // no confidence for now
96   return o->confidence;
97 }
98
99 uint_t
100 aubio_pitchspecacf_set_tolerance (aubio_pitchspecacf_t * p, smpl_t tol)
101 {
102   p->tol = tol;
103   return 0;
104 }
105
106 smpl_t
107 aubio_pitchspecacf_get_tolerance (aubio_pitchspecacf_t * p)
108 {
109   return p->tol;
110 }