source: src/spectral/statistics.c @ 5d16185

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

src/spectral: switch to mono

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