source: src/spectral/statistics.c @ 3a83821

feature/cnnfeature/crepefix/ffmpeg5
Last change on this file since 3a83821 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
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
[feb694b]25void aubio_specdesc_centroid (aubio_specdesc_t * o, const cvec_t * spec,
[029bf4e]26    fvec_t * desc);
[feb694b]27void aubio_specdesc_spread (aubio_specdesc_t * o, const cvec_t * spec,
[029bf4e]28    fvec_t * desc);
[feb694b]29void aubio_specdesc_skewness (aubio_specdesc_t * o, const cvec_t * spec,
[029bf4e]30    fvec_t * desc);
[feb694b]31void aubio_specdesc_kurtosis (aubio_specdesc_t * o, const cvec_t * spec,
[029bf4e]32    fvec_t * desc);
[feb694b]33void aubio_specdesc_slope (aubio_specdesc_t * o, const cvec_t * spec,
[029bf4e]34    fvec_t * desc);
[feb694b]35void aubio_specdesc_decrease (aubio_specdesc_t * o, const cvec_t * spec,
[029bf4e]36    fvec_t * desc);
[feb694b]37void aubio_specdesc_rolloff (aubio_specdesc_t * o, const cvec_t * spec,
[029bf4e]38    fvec_t * desc);
39
40
[feb694b]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);
[029bf4e]45
[1651b58]46smpl_t
[feb694b]47cvec_sum (const cvec_t * s)
[1651b58]48{
49  uint_t j;
50  smpl_t tmp = 0.0;
[d95ff38]51  for (j = 0; j < s->length; j++) {
52    tmp += s->norm[j];
53  }
[1651b58]54  return tmp;
55}
56
57smpl_t
[feb694b]58cvec_mean (const cvec_t * s)
[1651b58]59{
[d95ff38]60  return cvec_sum (s) / (smpl_t) (s->length);
[1651b58]61}
62
63smpl_t
[feb694b]64cvec_centroid (const cvec_t * spec)
[1651b58]65{
66  smpl_t sum = 0., sc = 0.;
67  uint_t j;
[d95ff38]68  sum = cvec_sum (spec); 
[1651b58]69  if (sum == 0.) {
70    return 0.;
71  } else {
72    for (j = 0; j < spec->length; j++) {
[d95ff38]73      sc += (smpl_t) j *spec->norm[j];
[1651b58]74    }
75    return sc / sum;
76  }
77}
78
79smpl_t
[feb694b]80cvec_moment (const cvec_t * spec, uint_t order)
[1651b58]81{
82  smpl_t sum = 0., centroid = 0., sc = 0.;
83  uint_t j;
[d95ff38]84  sum = cvec_sum (spec); 
[1651b58]85  if (sum == 0.) {
86    return 0.;
87  } else {
[d95ff38]88    centroid = cvec_centroid (spec);
[1651b58]89    for (j = 0; j < spec->length; j++) {
[d95ff38]90      sc += (smpl_t) POW(j - centroid, order) * spec->norm[j];
[1651b58]91    }
92    return sc / sum;
93  }
94}
95
96void
[feb694b]97aubio_specdesc_centroid (aubio_specdesc_t * o UNUSED, const cvec_t * spec,
[1651b58]98    fvec_t * desc)
99{
[d95ff38]100  desc->data[0] = cvec_centroid (spec); 
[1651b58]101}
102
103void
[feb694b]104aubio_specdesc_spread (aubio_specdesc_t * o UNUSED, const cvec_t * spec,
[1651b58]105    fvec_t * desc)
106{
[d95ff38]107  desc->data[0] = cvec_moment (spec, 2);
[1651b58]108}
109
110void
[feb694b]111aubio_specdesc_skewness (aubio_specdesc_t * o UNUSED, const cvec_t * spec,
[1651b58]112    fvec_t * desc)
113{
[d95ff38]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);
[1651b58]121  }
122}
123
124void
[feb694b]125aubio_specdesc_kurtosis (aubio_specdesc_t * o UNUSED, const cvec_t * spec,
[1651b58]126    fvec_t * desc)
127{
[d95ff38]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);
[1651b58]135  }
136}
137
138void
[feb694b]139aubio_specdesc_slope (aubio_specdesc_t * o UNUSED, const cvec_t * spec,
[1651b58]140    fvec_t * desc)
141{
[d95ff38]142  uint_t j;
[1651b58]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. );
[d95ff38]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]; 
[1651b58]158    }
[d95ff38]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;
[1651b58]163  }
164}
165
166void
[feb694b]167aubio_specdesc_decrease (aubio_specdesc_t *o UNUSED, const cvec_t * spec,
[1651b58]168    fvec_t * desc)
169{
[d95ff38]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;
[1651b58]179    }
[d95ff38]180    desc->data[0] /= sum;
[1651b58]181  }
182}
183
184void
[feb694b]185aubio_specdesc_rolloff (aubio_specdesc_t *o UNUSED, const cvec_t * spec,
[1651b58]186    fvec_t *desc)
187{
[d95ff38]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++;
[1651b58]201    }
[d95ff38]202    desc->data[0] = j;
[1651b58]203  }
204}
Note: See TracBrowser for help on using the repository browser.