Changeset 8e5c051


Ignore:
Timestamp:
Dec 4, 2009, 1:41:59 AM (14 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
fc61225
Parents:
d207300
Message:

src/mathutils.{c,h}: switch to mono

Location:
src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/mathutils.c

    rd207300 r8e5c051  
    4646new_aubio_window (char_t * window_type, uint_t size)
    4747{
    48   // create fvec of size x 1 channel
    49   fvec_t * win = new_fvec( size, 1);
    50   smpl_t * w = win->data[0];
     48  fvec_t * win = new_fvec (size);
     49  smpl_t * w = win->data;
    5150  uint_t i;
    5251  aubio_window_type wintype;
     
    134133fvec_mean (fvec_t * s)
    135134{
    136   uint_t i, j;
     135  uint_t j;
    137136  smpl_t tmp = 0.0;
    138   for (i = 0; i < s->channels; i++)
    139     for (j = 0; j < s->length; j++)
    140       tmp += s->data[i][j];
     137  for (j = 0; j < s->length; j++) {
     138    tmp += s->data[j];
     139  }
    141140  return tmp / (smpl_t) (s->length);
    142141}
    143142
    144143smpl_t
    145 fvec_mean_channel (fvec_t * s, uint_t i)
     144fvec_sum (fvec_t * s)
    146145{
    147146  uint_t j;
    148147  smpl_t tmp = 0.0;
    149   for (j = 0; j < s->length; j++)
    150       tmp += s->data[i][j];
    151   return tmp / (smpl_t) (s->length);
    152 }
    153 
    154 smpl_t
    155 fvec_sum (fvec_t * s)
    156 {
    157   uint_t i, j;
     148  for (j = 0; j < s->length; j++) {
     149    tmp += s->data[j];
     150  }
     151  return tmp;
     152}
     153
     154smpl_t
     155fvec_max (fvec_t * s)
     156{
     157  uint_t j;
    158158  smpl_t tmp = 0.0;
    159   for (i = 0; i < s->channels; i++) {
    160     for (j = 0; j < s->length; j++) {
    161       tmp += s->data[i][j];
    162     }
     159  for (j = 0; j < s->length; j++) {
     160    tmp = (tmp > s->data[j]) ? tmp : s->data[j];
    163161  }
    164162  return tmp;
     
    166164
    167165smpl_t
    168 fvec_max (fvec_t * s)
    169 {
    170   uint_t i, j;
    171   smpl_t tmp = 0.0;
    172   for (i = 0; i < s->channels; i++) {
    173     for (j = 0; j < s->length; j++) {
    174       tmp = (tmp > s->data[i][j]) ? tmp : s->data[i][j];
    175     }
    176   }
    177   return tmp;
    178 }
    179 
    180 smpl_t
    181166fvec_min (fvec_t * s)
    182167{
    183   uint_t i, j;
    184   smpl_t tmp = s->data[0][0];
    185   for (i = 0; i < s->channels; i++) {
    186     for (j = 0; j < s->length; j++) {
    187       tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j];
    188     }
     168  uint_t j;
     169  smpl_t tmp = s->data[0];
     170  for (j = 0; j < s->length; j++) {
     171    tmp = (tmp < s->data[j]) ? tmp : s->data[j];
    189172  }
    190173  return tmp;
     
    194177fvec_min_elem (fvec_t * s)
    195178{
    196   uint_t i, j, pos = 0.;
    197   smpl_t tmp = s->data[0][0];
    198   for (i = 0; i < s->channels; i++) {
    199     for (j = 0; j < s->length; j++) {
    200       pos = (tmp < s->data[i][j]) ? pos : j;
    201       tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j];
    202     }
     179  uint_t j, pos = 0.;
     180  smpl_t tmp = s->data[0];
     181  for (j = 0; j < s->length; j++) {
     182    pos = (tmp < s->data[j]) ? pos : j;
     183    tmp = (tmp < s->data[j]) ? tmp : s->data[j];
    203184  }
    204185  return pos;
     
    208189fvec_max_elem (fvec_t * s)
    209190{
    210   uint_t i, j, pos = 0;
     191  uint_t j, pos = 0;
    211192  smpl_t tmp = 0.0;
    212   for (i = 0; i < s->channels; i++) {
    213     for (j = 0; j < s->length; j++) {
    214       pos = (tmp > s->data[i][j]) ? pos : j;
    215       tmp = (tmp > s->data[i][j]) ? tmp : s->data[i][j];
    216     }
     193  for (j = 0; j < s->length; j++) {
     194    pos = (tmp > s->data[j]) ? pos : j;
     195    tmp = (tmp > s->data[j]) ? tmp : s->data[j];
    217196  }
    218197  return pos;
     
    222201fvec_shift (fvec_t * s)
    223202{
    224   uint_t i, j;
    225   for (i = 0; i < s->channels; i++) {
    226     for (j = 0; j < s->length / 2; j++) {
    227       ELEM_SWAP (s->data[i][j], s->data[i][j + s->length / 2]);
    228     }
     203  uint_t j;
     204  for (j = 0; j < s->length / 2; j++) {
     205    ELEM_SWAP (s->data[j], s->data[j + s->length / 2]);
    229206  }
    230207}
     
    234211{
    235212  smpl_t energy = 0.;
    236   uint_t i, j;
    237   for (i = 0; i < f->channels; i++) {
    238     for (j = 0; j < f->length; j++) {
    239       energy += SQR (f->data[i][j]);
    240     }
     213  uint_t j;
     214  for (j = 0; j < f->length; j++) {
     215    energy += SQR (f->data[j]);
    241216  }
    242217  return energy;
     
    247222{
    248223  smpl_t hfc = 0.;
    249   uint_t i, j;
    250   for (i = 0; i < v->channels; i++) {
    251     for (j = 0; j < v->length; j++) {
    252       hfc += (i + 1) * v->data[i][j];
    253     }
     224  uint_t j;
     225  for (j = 0; j < v->length; j++) {
     226    hfc += (j + 1) * v->data[j];
    254227  }
    255228  return hfc;
     
    266239fvec_alpha_norm (fvec_t * o, smpl_t alpha)
    267240{
    268   uint_t i, j;
     241  uint_t j;
    269242  smpl_t tmp = 0.;
    270   for (i = 0; i < o->channels; i++) {
    271     for (j = 0; j < o->length; j++) {
    272       tmp += POW (ABS (o->data[i][j]), alpha);
    273     }
     243  for (j = 0; j < o->length; j++) {
     244    tmp += POW (ABS (o->data[j]), alpha);
    274245  }
    275246  return POW (tmp / o->length, 1. / alpha);
     
    279250fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
    280251{
    281   uint_t i, j;
     252  uint_t j;
    282253  smpl_t norm = fvec_alpha_norm (o, alpha);
    283   for (i = 0; i < o->channels; i++) {
    284     for (j = 0; j < o->length; j++) {
    285       o->data[i][j] /= norm;
    286     }
     254  for (j = 0; j < o->length; j++) {
     255    o->data[j] /= norm;
    287256  }
    288257}
     
    291260fvec_add (fvec_t * o, smpl_t val)
    292261{
    293   uint_t i, j;
    294   for (i = 0; i < o->channels; i++) {
    295     for (j = 0; j < o->length; j++) {
    296       o->data[i][j] += val;
    297     }
     262  uint_t j;
     263  for (j = 0; j < o->length; j++) {
     264    o->data[j] += val;
    298265  }
    299266}
    300267
    301268void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
    302     uint_t post, uint_t pre, uint_t channel) {
    303   uint_t length = vec->length, i=channel, j;
     269    uint_t post, uint_t pre) {
     270  uint_t length = vec->length, j;
    304271  for (j=0;j<length;j++) {
    305     vec->data[i][j] -= fvec_moving_thres(vec, tmp, post, pre, j, i);
     272    vec->data[j] -= fvec_moving_thres(vec, tmp, post, pre, j);
    306273  }
    307274}
     
    309276smpl_t
    310277fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
    311     uint_t post, uint_t pre, uint_t pos, uint_t channel)
    312 {
    313   uint_t i = channel, k;
    314   smpl_t *medar = (smpl_t *) tmpvec->data[i];
     278    uint_t post, uint_t pre, uint_t pos)
     279{
     280  uint_t k;
     281  smpl_t *medar = (smpl_t *) tmpvec->data;
    315282  uint_t win_length = post + pre + 1;
    316283  uint_t length = vec->length;
     
    320287      medar[k] = 0.;            /* 0-padding at the beginning */
    321288    for (k = post + 1 - pos; k < win_length; k++)
    322       medar[k] = vec->data[0][k + pos - post];
     289      medar[k] = vec->data[k + pos - post];
    323290    /* the buffer is fully defined */
    324291  } else if (pos + pre < length) {
    325292    for (k = 0; k < win_length; k++)
    326       medar[k] = vec->data[0][k + pos - post];
     293      medar[k] = vec->data[k + pos - post];
    327294    /* pre part of the buffer does not exist */
    328295  } else {
    329296    for (k = 0; k < length - pos + post; k++)
    330       medar[k] = vec->data[0][k + pos - post];
     297      medar[k] = vec->data[k + pos - post];
    331298    for (k = length - pos + post; k < win_length; k++)
    332299      medar[k] = 0.;            /* 0-padding at the end */
    333300  }
    334   return fvec_median_channel (tmpvec, i);
    335 }
    336 
    337 smpl_t fvec_median_channel (fvec_t * input, uint_t channel) {
     301  return fvec_median (tmpvec);
     302}
     303
     304smpl_t fvec_median (fvec_t * input) {
    338305  uint_t n = input->length;
    339   smpl_t * arr = (smpl_t *) input->data[channel];
     306  smpl_t * arr = (smpl_t *) input->data;
    340307  uint_t low, high ;
    341308  uint_t median;
     
    386353}
    387354
    388 smpl_t fvec_quadint (fvec_t * x, uint_t pos, uint_t i) {
     355smpl_t fvec_quadint (fvec_t * x, uint_t pos) {
    389356  smpl_t s0, s1, s2;
    390357  uint_t x0 = (pos < 1) ? pos : pos - 1;
    391358  uint_t x2 = (pos + 1 < x->length) ? pos + 1 : pos;
    392   if (x0 == pos) return (x->data[i][pos] <= x->data[i][x2]) ? pos : x2;
    393   if (x2 == pos) return (x->data[i][pos] <= x->data[i][x0]) ? pos : x0;
    394   s0 = x->data[i][x0];
    395   s1 = x->data[i][pos];
    396   s2 = x->data[i][x2];
     359  if (x0 == pos) return (x->data[pos] <= x->data[x2]) ? pos : x2;
     360  if (x2 == pos) return (x->data[pos] <= x->data[x0]) ? pos : x0;
     361  s0 = x->data[x0];
     362  s1 = x->data[pos];
     363  s2 = x->data[x2];
    397364  return pos + 0.5 * (s2 - s0 ) / (s2 - 2.* s1 + s0);
    398365}
    399366
    400367uint_t fvec_peakpick(fvec_t * onset, uint_t pos) {
    401   uint_t i=0, tmp=0;
    402   /*for (i=0;i<onset->channels;i++)*/
    403   tmp = (onset->data[i][pos] > onset->data[i][pos-1]
    404       &&  onset->data[i][pos] > onset->data[i][pos+1]
    405       &&  onset->data[i][pos] > 0.);
     368  uint_t tmp=0;
     369  tmp = (onset->data[pos] > onset->data[pos-1]
     370      &&  onset->data[pos] > onset->data[pos+1]
     371      &&  onset->data[pos] > 0.);
    406372  return tmp;
    407373}
     
    512478aubio_zero_crossing_rate (fvec_t * input)
    513479{
    514   uint_t i = 0, j;
     480  uint_t j;
    515481  uint_t zcr = 0;
    516482  for (j = 1; j < input->length; j++) {
    517483    // previous was strictly negative
    518     if (input->data[i][j - 1] < 0.) {
     484    if (input->data[j - 1] < 0.) {
    519485      // current is positive or null
    520       if (input->data[i][j] >= 0.) {
     486      if (input->data[j] >= 0.) {
    521487        zcr += 1;
    522488      }
     
    524490    } else {
    525491      // current is strictly negative
    526       if (input->data[i][j] < 0.) {
     492      if (input->data[j] < 0.) {
    527493        zcr += 1;
    528494      }
     
    535501aubio_autocorr (fvec_t * input, fvec_t * output)
    536502{
    537   uint_t i, j, k, length = input->length;
     503  uint_t i, j, length = input->length;
    538504  smpl_t *data, *acf;
    539505  smpl_t tmp = 0;
    540   for (k = 0; k < input->channels; k++) {
    541     data = input->data[k];
    542     acf = output->data[k];
    543     for (i = 0; i < length; i++) {
    544       tmp = 0.;
    545       for (j = i; j < length; j++) {
    546         tmp += data[j - i] * data[j];
    547       }
    548       acf[i] = tmp / (smpl_t) (length - i);
     506  data = input->data;
     507  acf = output->data;
     508  for (i = 0; i < length; i++) {
     509    tmp = 0.;
     510    for (j = i; j < length; j++) {
     511      tmp += data[j - i] * data[j];
    549512    }
     513    acf[i] = tmp / (smpl_t) (length - i);
    550514  }
    551515}
  • src/mathutils.h

    rd207300 r8e5c051  
    4141*/
    4242smpl_t fvec_mean (fvec_t * s);
    43 
    44 /** compute the mean of a vector channel
    45 
    46   \param s vector to compute mean from
    47   \param i channel to compute mean from
    48 
    49   \return the mean of v
    50 
    51 */
    52 smpl_t fvec_mean_channel (fvec_t * s, uint_t i);
    5343
    5444/** find the max of a vector
     
    205195*/
    206196smpl_t fvec_moving_thres (fvec_t * v, fvec_t * tmp, uint_t post, uint_t pre,
    207     uint_t pos, uint_t channel);
     197    uint_t pos);
    208198
    209199/** apply adaptive threshold to a vector
     
    218208
    219209*/
    220 void fvec_adapt_thres (fvec_t * v, fvec_t * tmp, uint_t post, uint_t pre,
    221     uint_t channel);
     210void fvec_adapt_thres (fvec_t * v, fvec_t * tmp, uint_t post, uint_t pre);
    222211
    223212/** returns the median of a vector
     
    232221
    233222  \param v vector to get median from
    234   \param channel channel to get median from
    235223
    236224  \return the median of v
    237225 
    238226*/
    239 smpl_t fvec_median_channel (fvec_t * v, uint_t channel);
     227smpl_t fvec_median (fvec_t * v);
    240228
    241229/** finds exact peak index by quadratic interpolation*/
    242 smpl_t fvec_quadint (fvec_t * x, uint_t pos, uint_t channel);
     230smpl_t fvec_quadint (fvec_t * x, uint_t pos);
    243231
    244232/** Quadratic interpolation using Lagrange polynomial.
Note: See TracChangeset for help on using the changeset viewer.