source: src/onset/peakpick.c @ 6e57c2e

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

src/onset/peakpick.h: remove unused functions, fix aubio_peakpicker_get_thresholded_input

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