source: src/onset/peakpick.c @ 349e455

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchpitchshiftsamplertimestretchyinfft+
Last change on this file since 349e455 was 349e455, checked in by Paul Brossier <piem@piem.org>, 11 years ago

src/onset/peakpick.c: indent

  • Property mode set to 100644
File size: 5.7 KB
Line 
1/*
2  Copyright (C) 2003-2009 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 "mathutils.h"
24#include "lvec.h"
25#include "temporal/filter.h"
26#include "temporal/biquad.h"
27#include "onset/peakpick.h"
28
29/* peak picking parameters, default values in brackets
30 *
31 *     [<----post----|--pre-->]
32 *  .................|.............
33 *  time->           ^now
34 */
35struct _aubio_peakpicker_t
36{
37        /** thresh: offset threshold [0.033 or 0.01] */
38  smpl_t threshold;
39        /** win_post: median filter window length (causal part) [8] */
40  uint_t win_post;
41        /** pre: median filter window (anti-causal part) [post-1] */
42  uint_t win_pre;
43        /** threshfn: name or handle of fn for computing adaptive threshold [median]  */
44  aubio_thresholdfn_t thresholdfn;
45        /** picker: name or handle of fn for picking event times [peakpick] */
46  aubio_pickerfn_t pickerfn;
47
48        /** biquad lowpass filter */
49  aubio_filter_t *biquad;
50        /** original onsets */
51  fvec_t *onset_keep;
52        /** modified onsets */
53  fvec_t *onset_proc;
54        /** peak picked window [3] */
55  fvec_t *onset_peek;
56        /** scratch pad for biquad and median */
57  fvec_t *scratch;
58
59  /** number of channels to analyse */
60  uint_t channels;
61
62        /** \bug should be used to calculate filter coefficients */
63  /* cutoff: low-pass filter cutoff [0.34, 1] */
64  /* smpl_t cutoff; */
65
66  /* not used anymore */
67  /* time precision [512/44100  winlength/samplerate, fs/buffer_size */
68  /* smpl_t tau; */
69  /* alpha: normalisation exponent [9] */
70  /* smpl_t alpha; */
71};
72
73
74/** modified version for real time, moving mean adaptive threshold this method
75 * is slightly more permissive than the offline one, and yelds to an increase
76 * of false positives. best  */
77void
78aubio_peakpicker_do (aubio_peakpicker_t * p, fvec_t * onset, fvec_t * out)
79{
80  fvec_t *onset_keep = p->onset_keep;
81  fvec_t *onset_proc = p->onset_proc;
82  fvec_t *onset_peek = p->onset_peek;
83  fvec_t *scratch = p->scratch;
84  smpl_t mean = 0., median = 0.;
85  uint_t length = p->win_post + p->win_pre + 1;
86  uint_t i, j = 0;
87
88  for (i = 0; i < p->channels; i++) {
89    /* store onset in onset_keep */
90    /* shift all elements but last, then write last */
91    for (j = 0; j < length - 1; j++) {
92      onset_keep->data[i][j] = onset_keep->data[i][j + 1];
93      onset_proc->data[i][j] = onset_keep->data[i][j];
94    }
95    onset_keep->data[i][length - 1] = onset->data[i][0];
96    onset_proc->data[i][length - 1] = onset->data[i][0];
97  }
98
99  /* filter onset_proc */
100  /** \bug filtfilt calculated post+pre times, should be only once !? */
101  aubio_filter_do_filtfilt (p->biquad, onset_proc, scratch);
102
103  for (i = 0; i < p->channels; i++) {
104    /* calculate mean and median for onset_proc */
105    mean = fvec_mean_channel (onset_proc, i);
106    /* copy to scratch */
107    for (j = 0; j < length; j++)
108      scratch->data[i][j] = onset_proc->data[i][j];
109    median = p->thresholdfn (scratch, i);
110
111    /* shift peek array */
112    for (j = 0; j < 3 - 1; j++)
113      onset_peek->data[i][j] = onset_peek->data[i][j + 1];
114    /* calculate new peek value */
115    onset_peek->data[i][2] =
116        onset_proc->data[i][p->win_post] - median - mean * p->threshold;
117    out->data[i][0] = (p->pickerfn) (onset_peek, 1);
118    if (out->data[i][0]) {
119      out->data[i][0] = fvec_quadint (onset_peek, 1, i);
120    }
121  }
122}
123
124/** this method returns the current value in the pick peaking buffer
125 * after smoothing
126 */
127smpl_t
128aubio_peakpicker_get_thresholded_input (aubio_peakpicker_t * p)
129{
130  return p->onset_peek->data[0][1];
131}
132
133uint_t
134aubio_peakpicker_set_threshold (aubio_peakpicker_t * p, smpl_t threshold)
135{
136  p->threshold = threshold;
137  return AUBIO_OK;
138}
139
140smpl_t
141aubio_peakpicker_get_threshold (aubio_peakpicker_t * p)
142{
143  return p->threshold;
144}
145
146uint_t
147aubio_peakpicker_set_thresholdfn (aubio_peakpicker_t * p,
148    aubio_thresholdfn_t thresholdfn)
149{
150  p->thresholdfn = thresholdfn;
151  return AUBIO_OK;
152}
153
154aubio_thresholdfn_t
155aubio_peakpicker_get_thresholdfn (aubio_peakpicker_t * p)
156{
157  return (aubio_thresholdfn_t) (p->thresholdfn);
158}
159
160aubio_peakpicker_t *
161new_aubio_peakpicker (uint_t channels)
162{
163  aubio_peakpicker_t *t = AUBIO_NEW (aubio_peakpicker_t);
164  t->threshold = 0.1;           /* 0.0668; 0.33; 0.082; 0.033; */
165  t->win_post = 5;
166  t->win_pre = 1;
167  //channels = 1;
168  t->channels = channels;
169
170  t->thresholdfn = (aubio_thresholdfn_t) (fvec_median_channel); /* (fvec_mean); */
171  t->pickerfn = (aubio_pickerfn_t) (fvec_peakpick);
172
173  t->scratch = new_fvec (t->win_post + t->win_pre + 1, channels);
174  t->onset_keep = new_fvec (t->win_post + t->win_pre + 1, channels);
175  t->onset_proc = new_fvec (t->win_post + t->win_pre + 1, channels);
176  t->onset_peek = new_fvec (3, channels);
177
178  /* cutoff: low-pass filter with cutoff reduced frequency at 0.34
179     generated with octave butter function: [b,a] = butter(2, 0.34);
180   */
181  t->biquad = new_aubio_filter_biquad (0.15998789, 0.31997577, 0.15998789,
182      -0.59488894, 0.23484048, channels);
183
184  return t;
185}
186
187void
188del_aubio_peakpicker (aubio_peakpicker_t * p)
189{
190  del_aubio_filter (p->biquad);
191  del_fvec (p->onset_keep);
192  del_fvec (p->onset_proc);
193  del_fvec (p->onset_peek);
194  del_fvec (p->scratch);
195  AUBIO_FREE (p);
196}
Note: See TracBrowser for help on using the repository browser.