source: src/spectral/statistics.c @ 1eb8c0e

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5sampler
Last change on this file since 1eb8c0e was feb694b, checked in by Paul Brossier <piem@piem.org>, 9 years ago

src/spectral/: add const qualifiers

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2  Copyright (C) 2007-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 "cvec.h"
23#include "spectral/specdesc.h"
24
25void aubio_specdesc_centroid (aubio_specdesc_t * o, const cvec_t * spec,
26    fvec_t * desc);
27void aubio_specdesc_spread (aubio_specdesc_t * o, const cvec_t * spec,
28    fvec_t * desc);
29void aubio_specdesc_skewness (aubio_specdesc_t * o, const cvec_t * spec,
30    fvec_t * desc);
31void aubio_specdesc_kurtosis (aubio_specdesc_t * o, const cvec_t * spec,
32    fvec_t * desc);
33void aubio_specdesc_slope (aubio_specdesc_t * o, const cvec_t * spec,
34    fvec_t * desc);
35void aubio_specdesc_decrease (aubio_specdesc_t * o, const cvec_t * spec,
36    fvec_t * desc);
37void aubio_specdesc_rolloff (aubio_specdesc_t * o, const cvec_t * spec,
38    fvec_t * desc);
39
40
41smpl_t cvec_sum (const cvec_t * s);
42smpl_t cvec_mean (const cvec_t * s);
43smpl_t cvec_centroid (const cvec_t * s);
44smpl_t cvec_moment (const cvec_t * s, uint_t moment);
45
46smpl_t
47cvec_sum (const cvec_t * s)
48{
49  uint_t j;
50  smpl_t tmp = 0.0;
51  for (j = 0; j < s->length; j++) {
52    tmp += s->norm[j];
53  }
54  return tmp;
55}
56
57smpl_t
58cvec_mean (const cvec_t * s)
59{
60  return cvec_sum (s) / (smpl_t) (s->length);
61}
62
63smpl_t
64cvec_centroid (const cvec_t * spec)
65{
66  smpl_t sum = 0., sc = 0.;
67  uint_t j;
68  sum = cvec_sum (spec); 
69  if (sum == 0.) {
70    return 0.;
71  } else {
72    for (j = 0; j < spec->length; j++) {
73      sc += (smpl_t) j *spec->norm[j];
74    }
75    return sc / sum;
76  }
77}
78
79smpl_t
80cvec_moment (const cvec_t * spec, uint_t order)
81{
82  smpl_t sum = 0., centroid = 0., sc = 0.;
83  uint_t j;
84  sum = cvec_sum (spec); 
85  if (sum == 0.) {
86    return 0.;
87  } else {
88    centroid = cvec_centroid (spec);
89    for (j = 0; j < spec->length; j++) {
90      sc += (smpl_t) POW(j - centroid, order) * spec->norm[j];
91    }
92    return sc / sum;
93  }
94}
95
96void
97aubio_specdesc_centroid (aubio_specdesc_t * o UNUSED, const cvec_t * spec,
98    fvec_t * desc)
99{
100  desc->data[0] = cvec_centroid (spec); 
101}
102
103void
104aubio_specdesc_spread (aubio_specdesc_t * o UNUSED, const cvec_t * spec,
105    fvec_t * desc)
106{
107  desc->data[0] = cvec_moment (spec, 2);
108}
109
110void
111aubio_specdesc_skewness (aubio_specdesc_t * o UNUSED, const cvec_t * spec,
112    fvec_t * desc)
113{
114  smpl_t spread;
115  spread = cvec_moment (spec, 2);
116  if (spread == 0) {
117    desc->data[0] = 0.;
118  } else {
119    desc->data[0] = cvec_moment (spec, 3);
120    desc->data[0] /= POW ( SQRT (spread), 3);
121  }
122}
123
124void
125aubio_specdesc_kurtosis (aubio_specdesc_t * o UNUSED, const cvec_t * spec,
126    fvec_t * desc)
127{
128  smpl_t spread;
129  spread = cvec_moment (spec, 2);
130  if (spread == 0) {
131    desc->data[0] = 0.;
132  } else {
133    desc->data[0] = cvec_moment (spec, 4);
134    desc->data[0] /= SQR (spread);
135  }
136}
137
138void
139aubio_specdesc_slope (aubio_specdesc_t * o UNUSED, const cvec_t * spec,
140    fvec_t * desc)
141{
142  uint_t j;
143  smpl_t norm = 0, sum = 0.; 
144  // compute N * sum(j**2) - sum(j)**2
145  for (j = 0; j < spec->length; j++) {
146    norm += j*j;
147  }
148  norm *= spec->length;
149  // sum_0^N(j) = length * (length + 1) / 2
150  norm -= SQR( (spec->length) * (spec->length - 1.) / 2. );
151  sum = cvec_sum (spec); 
152  desc->data[0] = 0.;
153  if (sum == 0.) {
154    return; 
155  } else {
156    for (j = 0; j < spec->length; j++) {
157      desc->data[0] += j * spec->norm[j]; 
158    }
159    desc->data[0] *= spec->length;
160    desc->data[0] -= sum * spec->length * (spec->length - 1) / 2.;
161    desc->data[0] /= norm;
162    desc->data[0] /= sum;
163  }
164}
165
166void
167aubio_specdesc_decrease (aubio_specdesc_t *o UNUSED, const cvec_t * spec,
168    fvec_t * desc)
169{
170  uint_t j; smpl_t sum;
171  sum = cvec_sum (spec); 
172  desc->data[0] = 0;
173  if (sum == 0.) {
174    return;
175  } else {
176    sum -= spec->norm[0];
177    for (j = 1; j < spec->length; j++) {
178      desc->data[0] += (spec->norm[j] - spec->norm[0]) / j;
179    }
180    desc->data[0] /= sum;
181  }
182}
183
184void
185aubio_specdesc_rolloff (aubio_specdesc_t *o UNUSED, const cvec_t * spec,
186    fvec_t *desc)
187{
188  uint_t j; smpl_t cumsum, rollsum;
189  cumsum = 0.; rollsum = 0.;
190  for (j = 0; j < spec->length; j++) {
191    cumsum += SQR (spec->norm[j]);
192  }
193  if (cumsum == 0) {
194    desc->data[0] = 0.;
195  } else {
196    cumsum *= 0.95;
197    j = 0;
198    while (rollsum < cumsum) { 
199      rollsum += SQR (spec->norm[j]);
200      j++;
201    }
202    desc->data[0] = j;
203  }
204}
Note: See TracBrowser for help on using the repository browser.