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 | /* default values : alpha=4, beta=3, threshold=0.25 */ |
---|
22 | |
---|
23 | #include "aubio_priv.h" |
---|
24 | #include "fvec.h" |
---|
25 | #include "cvec.h" |
---|
26 | #include "mathutils.h" |
---|
27 | #include "spectral/tss.h" |
---|
28 | |
---|
29 | struct _aubio_tss_t |
---|
30 | { |
---|
31 | smpl_t threshold; |
---|
32 | smpl_t alpha; |
---|
33 | smpl_t beta; |
---|
34 | smpl_t parm; |
---|
35 | smpl_t thrsfact; |
---|
36 | fvec_t *theta1; |
---|
37 | fvec_t *theta2; |
---|
38 | fvec_t *oft1; |
---|
39 | fvec_t *oft2; |
---|
40 | fvec_t *dev; |
---|
41 | }; |
---|
42 | |
---|
43 | void aubio_tss_do(aubio_tss_t *o, cvec_t * input, |
---|
44 | cvec_t * trans, cvec_t * stead) |
---|
45 | { |
---|
46 | uint_t j; |
---|
47 | uint_t test; |
---|
48 | uint_t nbins = input->length; |
---|
49 | smpl_t alpha = o->alpha; |
---|
50 | smpl_t beta = o->beta; |
---|
51 | smpl_t parm = o->parm; |
---|
52 | smpl_t * dev = (smpl_t *)o->dev->data; |
---|
53 | smpl_t * oft1 = (smpl_t *)o->oft1->data; |
---|
54 | smpl_t * oft2 = (smpl_t *)o->oft2->data; |
---|
55 | smpl_t * theta1 = (smpl_t *)o->theta1->data; |
---|
56 | smpl_t * theta2 = (smpl_t *)o->theta2->data; |
---|
57 | /* second phase derivative */ |
---|
58 | for (j=0;j<nbins; j++){ |
---|
59 | dev[j] = aubio_unwrap2pi(input->phas[j] |
---|
60 | -2.0*theta1[j]+theta2[j]); |
---|
61 | theta2[j] = theta1[j]; |
---|
62 | theta1[j] = input->phas[j]; |
---|
63 | } |
---|
64 | |
---|
65 | for (j=0;j<nbins; j++){ |
---|
66 | /* transient analysis */ |
---|
67 | test = (ABS(dev[j]) > parm*oft1[j]); |
---|
68 | trans->norm[j] = input->norm[j] * test; |
---|
69 | trans->phas[j] = input->phas[j] * test; |
---|
70 | } |
---|
71 | |
---|
72 | for (j=0;j<nbins; j++){ |
---|
73 | /* steady state analysis */ |
---|
74 | test = (ABS(dev[j]) < parm*oft2[j]); |
---|
75 | stead->norm[j] = input->norm[j] * test; |
---|
76 | stead->phas[j] = input->phas[j] * test; |
---|
77 | |
---|
78 | /*increase sstate probability for sines */ |
---|
79 | test = (trans->norm[j]==0.); |
---|
80 | oft1[j] = test; |
---|
81 | test = (stead->norm[j]==0.); |
---|
82 | oft2[j] = test; |
---|
83 | test = (trans->norm[j]>0.); |
---|
84 | oft1[j] += alpha*test; |
---|
85 | test = (stead->norm[j]>0.); |
---|
86 | oft2[j] += alpha*test; |
---|
87 | test = (oft1[j]>1. && trans->norm[j]>0.); |
---|
88 | oft1[j] += beta*test; |
---|
89 | test = (oft2[j]>1. && stead->norm[j]>0.); |
---|
90 | oft2[j] += beta*test; |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | uint_t aubio_tss_set_threshold(aubio_tss_t *o, smpl_t threshold){ |
---|
95 | o->threshold = threshold; |
---|
96 | o->parm = o->threshold * o->thrsfact; |
---|
97 | return AUBIO_OK; |
---|
98 | } |
---|
99 | |
---|
100 | aubio_tss_t * new_aubio_tss(uint_t buf_size, uint_t hop_size) |
---|
101 | { |
---|
102 | aubio_tss_t * o = AUBIO_NEW(aubio_tss_t); |
---|
103 | uint_t rsize = buf_size/2+1; |
---|
104 | o->threshold = 0.25; |
---|
105 | o->thrsfact = TWO_PI*hop_size/rsize; |
---|
106 | o->alpha = 3.; |
---|
107 | o->beta = 4.; |
---|
108 | o->parm = o->threshold*o->thrsfact; |
---|
109 | o->theta1 = new_fvec(rsize); |
---|
110 | o->theta2 = new_fvec(rsize); |
---|
111 | o->oft1 = new_fvec(rsize); |
---|
112 | o->oft2 = new_fvec(rsize); |
---|
113 | o->dev = new_fvec(rsize); |
---|
114 | return o; |
---|
115 | } |
---|
116 | |
---|
117 | void del_aubio_tss(aubio_tss_t *s) |
---|
118 | { |
---|
119 | free(s->theta1); |
---|
120 | free(s->theta2); |
---|
121 | free(s->oft1); |
---|
122 | free(s->oft2); |
---|
123 | free(s->dev); |
---|
124 | free(s); |
---|
125 | } |
---|
126 | |
---|
127 | uint_t aubio_tss_set_alpha(aubio_tss_t *o, smpl_t alpha){ |
---|
128 | o->alpha = alpha; |
---|
129 | return AUBIO_OK; |
---|
130 | } |
---|
131 | |
---|
132 | uint_t aubio_tss_set_beta(aubio_tss_t *o, smpl_t beta){ |
---|
133 | o->beta = beta; |
---|
134 | return AUBIO_OK; |
---|
135 | } |
---|
136 | |
---|