source: src/spectral/statistics.c @ 48b6a52

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

src/spectral/: added statistics.c, containing some cuidado spectral shape descriptors

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