source: src/spectral/statistics.c @ 2e50800

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 2e50800 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
RevLine 
[1651b58]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
[d95ff38]26cvec_sum (cvec_t * s)
[1651b58]27{
28  uint_t j;
29  smpl_t tmp = 0.0;
[d95ff38]30  for (j = 0; j < s->length; j++) {
31    tmp += s->norm[j];
32  }
[1651b58]33  return tmp;
34}
35
36smpl_t
[d95ff38]37cvec_mean (cvec_t * s)
[1651b58]38{
[d95ff38]39  return cvec_sum (s) / (smpl_t) (s->length);
[1651b58]40}
41
42smpl_t
[d95ff38]43cvec_centroid (cvec_t * spec)
[1651b58]44{
45  smpl_t sum = 0., sc = 0.;
46  uint_t j;
[d95ff38]47  sum = cvec_sum (spec); 
[1651b58]48  if (sum == 0.) {
49    return 0.;
50  } else {
51    for (j = 0; j < spec->length; j++) {
[d95ff38]52      sc += (smpl_t) j *spec->norm[j];
[1651b58]53    }
54    return sc / sum;
55  }
56}
57
58smpl_t
[d95ff38]59cvec_moment (cvec_t * spec, uint_t order)
[1651b58]60{
61  smpl_t sum = 0., centroid = 0., sc = 0.;
62  uint_t j;
[d95ff38]63  sum = cvec_sum (spec); 
[1651b58]64  if (sum == 0.) {
65    return 0.;
66  } else {
[d95ff38]67    centroid = cvec_centroid (spec);
[1651b58]68    for (j = 0; j < spec->length; j++) {
[d95ff38]69      sc += (smpl_t) POW(j - centroid, order) * spec->norm[j];
[1651b58]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{
[d95ff38]79  desc->data[0] = cvec_centroid (spec); 
[1651b58]80}
81
82void
83aubio_specdesc_spread (aubio_specdesc_t * o UNUSED, cvec_t * spec,
84    fvec_t * desc)
85{
[d95ff38]86  desc->data[0] = cvec_moment (spec, 2);
[1651b58]87}
88
89void
90aubio_specdesc_skewness (aubio_specdesc_t * o UNUSED, cvec_t * spec,
91    fvec_t * desc)
92{
[d95ff38]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);
[1651b58]100  }
101}
102
103void
104aubio_specdesc_kurtosis (aubio_specdesc_t * o UNUSED, cvec_t * spec,
105    fvec_t * desc)
106{
[d95ff38]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);
[1651b58]114  }
115}
116
117void
118aubio_specdesc_slope (aubio_specdesc_t * o UNUSED, cvec_t * spec,
119    fvec_t * desc)
120{
[d95ff38]121  uint_t j;
[1651b58]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. );
[d95ff38]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]; 
[1651b58]137    }
[d95ff38]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;
[1651b58]142  }
143}
144
145void
146aubio_specdesc_decrease (aubio_specdesc_t *o UNUSED, cvec_t * spec,
147    fvec_t * desc)
148{
[d95ff38]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;
[1651b58]158    }
[d95ff38]159    desc->data[0] /= sum;
[1651b58]160  }
161}
162
163void
164aubio_specdesc_rolloff (aubio_specdesc_t *o UNUSED, cvec_t * spec,
165    fvec_t *desc)
166{
[d95ff38]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++;
[1651b58]180    }
[d95ff38]181    desc->data[0] = j;
[1651b58]182  }
183}
Note: See TracBrowser for help on using the repository browser.