source: src/onset/peakpick.c @ 180eeca

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

src/temporal: derive biquad from filter, use in peakpicker

  • Property mode set to 100644
File size: 5.6 KB
Line 
1/*
2   Copyright (C) 2003 Paul Brossier
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18*/
19
20#include "aubio_priv.h"
21#include "fvec.h"
22#include "mathutils.h"
23#include "lvec.h"
24#include "temporal/filter.h"
25#include "temporal/biquad.h"
26#include "onset/peakpick.h"
27
28/* peak picking parameters, default values in brackets
29 *
30 *         [<----post----|--pre-->]
31 *      .................|.............
32 *      time->           ^now
33 */
34struct _aubio_peakpicker_t {
35        /** thresh: offset threshold [0.033 or 0.01] */
36        smpl_t threshold;       
37        /** win_post: median filter window length (causal part) [8] */
38        uint_t  win_post;                       
39        /** pre: median filter window (anti-causal part) [post-1] */
40        uint_t  win_pre;                               
41        /** threshfn: name or handle of fn for computing adaptive threshold [median]  */
42        aubio_thresholdfn_t thresholdfn;
43        /** picker: name or handle of fn for picking event times [peakpick] */
44        aubio_pickerfn_t pickerfn;
45
46        /** biquad lowpass filter */
47        aubio_filter_t * biquad;
48        /** original onsets */
49        fvec_t * onset_keep;
50        /** modified onsets */
51        fvec_t * onset_proc;
52        /** peak picked window [3] */
53        fvec_t * onset_peek;
54        /** scratch pad for biquad and median */
55        fvec_t * scratch;
56
57  /** number of channels to analyse */
58  uint_t channels;
59
60        /** \bug should be used to calculate filter coefficients */
61        /* cutoff: low-pass filter cutoff [0.34, 1] */
62        /* smpl_t cutoff; */
63
64        /* not used anymore */
65        /* time precision [512/44100  winlength/samplerate, fs/buffer_size */
66        /* smpl_t tau; */
67        /* alpha: normalisation exponent [9] */
68        /* smpl_t alpha; */
69};
70
71
72/** modified version for real time, moving mean adaptive threshold this method
73 * is slightly more permissive than the offline one, and yelds to an increase
74 * of false positives. best  */
75void
76aubio_peakpicker_do (aubio_peakpicker_t * p, fvec_t * onset, fvec_t * out)
77{
78  fvec_t *onset_keep = p->onset_keep;
79  fvec_t *onset_proc = p->onset_proc;
80  fvec_t *onset_peek = p->onset_peek;
81  fvec_t *scratch = p->scratch;
82  smpl_t mean = 0., median = 0.;
83  uint_t length = p->win_post + p->win_pre + 1;
84  uint_t i, j = 0;
85
86  for (i = 0; i < p->channels; i++) {
87    /* store onset in onset_keep */
88    /* shift all elements but last, then write last */
89    for (j = 0; j < length - 1; j++) {
90      onset_keep->data[i][j] = onset_keep->data[i][j + 1];
91      onset_proc->data[i][j] = onset_keep->data[i][j];
92    }
93    onset_keep->data[i][length - 1] = onset->data[i][0];
94    onset_proc->data[i][length - 1] = onset->data[i][0];
95  }
96
97  /* filter onset_proc */
98  /** \bug filtfilt calculated post+pre times, should be only once !? */
99  aubio_filter_do_filtfilt (p->biquad, onset_proc, scratch);
100
101  for (i = 0; i < p->channels; i++) {
102    /* calculate mean and median for onset_proc */
103    mean = fvec_mean_channel (onset_proc, i);
104    /* copy to scratch */
105    for (j = 0; j < length; j++)
106      scratch->data[i][j] = onset_proc->data[i][j];
107    median = p->thresholdfn (scratch, i);
108
109    /* shift peek array */
110    for (j = 0; j < 3 - 1; j++)
111      onset_peek->data[i][j] = onset_peek->data[i][j + 1];
112    /* calculate new peek value */
113    onset_peek->data[i][2] =
114        onset_proc->data[i][p->win_post] - median - mean * p->threshold;
115    out->data[i][0] = (p->pickerfn) (onset_peek, 1);
116    if (out->data[i][0]) {
117      out->data[i][0] = fvec_quadint (onset_peek, 1, i);
118    }
119  }
120}
121
122/** this method returns the current value in the pick peaking buffer
123 * after smoothing
124 */
125smpl_t aubio_peakpicker_get_thresholded_input(aubio_peakpicker_t * p) 
126{
127        return p->onset_peek->data[0][1];
128}
129
130uint_t aubio_peakpicker_set_threshold(aubio_peakpicker_t * p, smpl_t threshold) {
131    p->threshold = threshold;
132        return AUBIO_OK;
133}
134
135smpl_t aubio_peakpicker_get_threshold(aubio_peakpicker_t * p) {
136        return p->threshold;
137}
138
139uint_t aubio_peakpicker_set_thresholdfn(aubio_peakpicker_t * p, aubio_thresholdfn_t thresholdfn) {
140        p->thresholdfn = thresholdfn;
141        return AUBIO_OK;
142}
143
144aubio_thresholdfn_t aubio_peakpicker_get_thresholdfn(aubio_peakpicker_t * p) {
145        return (aubio_thresholdfn_t) (p->thresholdfn);
146}
147
148aubio_peakpicker_t * new_aubio_peakpicker(uint_t channels) {
149        aubio_peakpicker_t * t = AUBIO_NEW(aubio_peakpicker_t);
150        t->threshold = 0.1; /* 0.0668; 0.33; 0.082; 0.033; */
151        t->win_post  = 5;
152        t->win_pre   = 1;
153  //channels = 1;
154  t->channels = channels;
155
156        t->thresholdfn = (aubio_thresholdfn_t)(fvec_median_channel); /* (fvec_mean); */
157        t->pickerfn = (aubio_pickerfn_t)(fvec_peakpick);
158
159        t->scratch = new_fvec(t->win_post+t->win_pre+1, channels);
160        t->onset_keep = new_fvec(t->win_post+t->win_pre+1, channels);
161        t->onset_proc = new_fvec(t->win_post+t->win_pre+1, channels);
162        t->onset_peek = new_fvec(3, channels);
163
164        /* cutoff: low-pass filter with cutoff reduced frequency at 0.34
165   generated with octave butter function: [b,a] = butter(2, 0.34);
166   */
167  t->biquad = new_aubio_filter_biquad (0.15998789, 0.31997577, 0.15998789,
168    -0.59488894, 0.23484048, channels);
169
170        return t;
171}
172
173void del_aubio_peakpicker(aubio_peakpicker_t * p) {
174        del_aubio_filter(p->biquad);
175        del_fvec(p->onset_keep);
176        del_fvec(p->onset_proc);
177        del_fvec(p->onset_peek);
178        del_fvec(p->scratch);
179        AUBIO_FREE(p);
180}
Note: See TracBrowser for help on using the repository browser.