Changeset eb7f743


Ignore:
Timestamp:
Oct 2, 2009, 6:11:07 AM (15 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:
352fd5f
Parents:
1498ced
Message:

src/mathutils.{c,h}: loop over channels when possible, improve documentation, indent

Location:
src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/mathutils.c

    r1498ced reb7f743  
    2626#include "config.h"
    2727
    28 fvec_t * new_aubio_window(uint_t size, aubio_window_type wintype) {
     28fvec_t *
     29new_aubio_window (uint_t size, aubio_window_type wintype)
     30{
    2931  // create fvec of size x 1 channel
    3032  fvec_t * win = new_fvec( size, 1);
     
    7981}
    8082
    81 smpl_t aubio_unwrap2pi(smpl_t phase) {
     83smpl_t
     84aubio_unwrap2pi (smpl_t phase)
     85{
    8286  /* mod(phase+pi,-2pi)+pi */
    83   return phase + TWO_PI * (1. + FLOOR(-(phase+PI)/TWO_PI));
    84 }
    85 
    86 smpl_t fvec_mean(fvec_t *s) {
    87   uint_t i,j;
     87  return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI));
     88}
     89
     90smpl_t
     91fvec_mean (fvec_t * s)
     92{
     93  uint_t i, j;
    8894  smpl_t tmp = 0.0;
    89   for (i=0; i < s->channels; i++)
    90     for (j=0; j < s->length; j++)
     95  for (i = 0; i < s->channels; i++)
     96    for (j = 0; j < s->length; j++)
    9197      tmp += s->data[i][j];
    92   return tmp/(smpl_t)(s->length);
    93 }
    94 
    95 smpl_t fvec_sum(fvec_t *s) {
    96   uint_t i,j;
     98  return tmp / (smpl_t) (s->length);
     99}
     100
     101smpl_t
     102fvec_sum (fvec_t * s)
     103{
     104  uint_t i, j;
    97105  smpl_t tmp = 0.0;
    98   for (i=0; i < s->channels; i++)
    99     for (j=0; j < s->length; j++)
     106  for (i = 0; i < s->channels; i++) {
     107    for (j = 0; j < s->length; j++) {
    100108      tmp += s->data[i][j];
     109    }
     110  }
    101111  return tmp;
    102112}
    103113
    104 smpl_t fvec_max(fvec_t *s) {
    105   uint_t i,j;
     114smpl_t
     115fvec_max (fvec_t * s)
     116{
     117  uint_t i, j;
    106118  smpl_t tmp = 0.0;
    107   for (i=0; i < s->channels; i++)
    108     for (j=0; j < s->length; j++)
    109       tmp = (tmp > s->data[i][j])? tmp : s->data[i][j];
     119  for (i = 0; i < s->channels; i++) {
     120    for (j = 0; j < s->length; j++) {
     121      tmp = (tmp > s->data[i][j]) ? tmp : s->data[i][j];
     122    }
     123  }
    110124  return tmp;
    111125}
    112126
    113 smpl_t fvec_min(fvec_t *s) {
    114   uint_t i,j;
     127smpl_t
     128fvec_min (fvec_t * s)
     129{
     130  uint_t i, j;
    115131  smpl_t tmp = s->data[0][0];
    116   for (i=0; i < s->channels; i++)
    117     for (j=0; j < s->length; j++)
    118       tmp = (tmp < s->data[i][j])? tmp : s->data[i][j]  ;
     132  for (i = 0; i < s->channels; i++) {
     133    for (j = 0; j < s->length; j++) {
     134      tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j];
     135    }
     136  }
    119137  return tmp;
    120138}
    121139
    122 uint_t fvec_min_elem(fvec_t *s) {
    123   uint_t i,j=0, pos=0.;
     140uint_t
     141fvec_min_elem (fvec_t * s)
     142{
     143  uint_t i, j = 0, pos = 0.;
    124144  smpl_t tmp = s->data[0][0];
    125   for (i=0; i < s->channels; i++)
    126     for (j=0; j < s->length; j++) {
    127       pos = (tmp < s->data[i][j])? pos : j;
    128       tmp = (tmp < s->data[i][j])? tmp : s->data[i][j]  ;
    129     }
     145  for (i = 0; i < s->channels; i++) {
     146    for (j = 0; j < s->length; j++) {
     147      pos = (tmp < s->data[i][j]) ? pos : j;
     148      tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j];
     149    }
     150  }
    130151  return pos;
    131152}
    132153
    133 uint_t fvec_max_elem(fvec_t *s) {
    134   uint_t i,j=0, pos=0.;
     154uint_t
     155fvec_max_elem (fvec_t * s)
     156{
     157  uint_t i, j, pos;
    135158  smpl_t tmp = 0.0;
    136   for (i=0; i < s->channels; i++)
    137     for (j=0; j < s->length; j++) {
    138       pos = (tmp > s->data[i][j])? pos : j;
    139       tmp = (tmp > s->data[i][j])? tmp : s->data[i][j]  ;
    140     }
     159  for (i = 0; i < s->channels; i++) {
     160    for (j = 0; j < s->length; j++) {
     161      pos = (tmp > s->data[i][j]) ? pos : j;
     162      tmp = (tmp > s->data[i][j]) ? tmp : s->data[i][j];
     163    }
     164  }
    141165  return pos;
    142166}
    143167
    144 void fvec_shift(fvec_t *s) {
    145   uint_t i,j;
    146   //smpl_t tmp = 0.0;
    147   for (i=0; i < s->channels; i++)
    148     for (j=0; j < s->length / 2 ; j++) {
    149       //tmp = s->data[i][j];
    150       //s->data[i][j] = s->data[i][j+s->length/2];
    151       //s->data[i][j+s->length/2] = tmp;
    152       ELEM_SWAP(s->data[i][j],s->data[i][j+s->length/2]);
    153     }
    154 }
    155 
    156 smpl_t fvec_local_energy(fvec_t * f) {
    157   smpl_t locE = 0.;
    158   uint_t i,j;
    159   for (i=0;i<f->channels;i++)
    160     for (j=0;j<f->length;j++)
    161       locE+=SQR(f->data[i][j]);
    162   return locE;
    163 }
    164 
    165 smpl_t fvec_local_hfc(fvec_t * f) {
    166   smpl_t locE = 0.;
    167   uint_t i,j;
    168   for (i=0;i<f->channels;i++)
    169     for (j=0;j<f->length;j++)
    170       locE+=(i+1)*f->data[i][j];
    171   return locE;
    172 }
    173 
    174 smpl_t fvec_alpha_norm(fvec_t * DF, smpl_t alpha) {
     168void
     169fvec_shift (fvec_t * s)
     170{
     171  uint_t i, j;
     172  for (i = 0; i < s->channels; i++) {
     173    for (j = 0; j < s->length / 2; j++) {
     174      ELEM_SWAP (s->data[i][j], s->data[i][j + s->length / 2]);
     175    }
     176  }
     177}
     178
     179smpl_t
     180fvec_local_energy (fvec_t * f)
     181{
     182  smpl_t energy = 0.;
     183  uint_t i, j;
     184  for (i = 0; i < f->channels; i++) {
     185    for (j = 0; j < f->length; j++) {
     186      energy += SQR (f->data[i][j]);
     187    }
     188  }
     189  return energy;
     190}
     191
     192smpl_t
     193fvec_local_hfc (fvec_t * v)
     194{
     195  smpl_t hfc = 0.;
     196  uint_t i, j;
     197  for (i = 0; i < v->channels; i++) {
     198    for (j = 0; j < v->length; j++) {
     199      hfc += (i + 1) * v->data[i][j];
     200    }
     201  }
     202  return hfc;
     203}
     204
     205void
     206fvec_min_removal (fvec_t * v)
     207{
     208  smpl_t v_min = fvec_min (v);
     209  fvec_add (v,  - v_min );
     210}
     211
     212smpl_t
     213fvec_alpha_norm (fvec_t * o, smpl_t alpha)
     214{
     215  uint_t i, j;
    175216  smpl_t tmp = 0.;
    176   uint_t i,j;
    177   for (i=0;i<DF->channels;i++)
    178     for (j=0;j<DF->length;j++)
    179       tmp += POW(ABS(DF->data[i][j]),alpha);
    180   return POW(tmp/DF->length,1./alpha);
    181 }
    182 
    183 void
    184 fvec_min_removal (fvec_t * o)
    185 {
    186   uint_t i, j;
    187   smpl_t mini = fvec_min (o);
    188217  for (i = 0; i < o->channels; i++) {
    189218    for (j = 0; j < o->length; j++) {
    190       o->data[i][j] -= mini;
    191     }
    192   }
    193 }
    194 
    195 void fvec_alpha_normalise(fvec_t * mag, uint_t alpha) {
    196   smpl_t alphan = 1.;
    197   uint_t length = mag->length, i=0, j;
    198   alphan = fvec_alpha_norm(mag,alpha);
    199   for (j=0;j<length;j++){
    200     mag->data[i][j] /= alphan;
    201   }
    202 }
    203 
    204 void fvec_add(fvec_t * mag, smpl_t threshold) {
    205   uint_t length = mag->length, i=0, j;
    206   for (j=0;j<length;j++) {
    207     mag->data[i][j] += threshold;
     219      tmp += POW (ABS (o->data[i][j]), alpha);
     220    }
     221  }
     222  return POW (tmp / o->length, 1. / alpha);
     223}
     224
     225void
     226fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
     227{
     228  uint_t i, j;
     229  smpl_t norm = fvec_alpha_norm (o, alpha);
     230  for (i = 0; i < o->channels; i++) {
     231    for (j = 0; j < o->length; j++) {
     232      o->data[i][j] /= norm;
     233    }
     234  }
     235}
     236
     237void
     238fvec_add (fvec_t * o, smpl_t val)
     239{
     240  uint_t i, j;
     241  for (i = 0; i < o->channels; i++) {
     242    for (j = 0; j < o->length; j++) {
     243      o->data[i][j] += val;
     244    }
    208245  }
    209246}
     
    217254}
    218255
    219 smpl_t fvec_moving_thres(fvec_t * vec, fvec_t * tmpvec,
    220     uint_t post, uint_t pre, uint_t pos) {
    221   smpl_t * medar = (smpl_t *)tmpvec->data[0];
     256smpl_t
     257fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
     258    uint_t post, uint_t pre, uint_t pos)
     259{
     260  smpl_t *medar = (smpl_t *) tmpvec->data[0];
    222261  uint_t k;
    223   uint_t win_length =  post+pre+1;
    224   uint_t length =  vec->length;
     262  uint_t win_length = post + pre + 1;
     263  uint_t length = vec->length;
    225264  /* post part of the buffer does not exist */
    226   if (pos<post+1) {
    227     for (k=0;k<post+1-pos;k++)
    228       medar[k] = 0.; /* 0-padding at the beginning */
    229     for (k=post+1-pos;k<win_length;k++)
    230       medar[k] = vec->data[0][k+pos-post];
    231   /* the buffer is fully defined */
    232   } else if (pos+pre<length) {
    233     for (k=0;k<win_length;k++)
    234       medar[k] = vec->data[0][k+pos-post];
    235   /* pre part of the buffer does not exist */
     265  if (pos < post + 1) {
     266    for (k = 0; k < post + 1 - pos; k++)
     267      medar[k] = 0.;            /* 0-padding at the beginning */
     268    for (k = post + 1 - pos; k < win_length; k++)
     269      medar[k] = vec->data[0][k + pos - post];
     270    /* the buffer is fully defined */
     271  } else if (pos + pre < length) {
     272    for (k = 0; k < win_length; k++)
     273      medar[k] = vec->data[0][k + pos - post];
     274    /* pre part of the buffer does not exist */
    236275  } else {
    237     for (k=0;k<length-pos+post;k++)
    238       medar[k] = vec->data[0][k+pos-post];
    239     for (k=length-pos+post;k<win_length;k++)
    240       medar[k] = 0.; /* 0-padding at the end */
    241   }
    242   return fvec_median(tmpvec);
     276    for (k = 0; k < length - pos + post; k++)
     277      medar[k] = vec->data[0][k + pos - post];
     278    for (k = length - pos + post; k < win_length; k++)
     279      medar[k] = 0.;            /* 0-padding at the end */
     280  }
     281  return fvec_median (tmpvec);
    243282}
    244283
     
    301340  if (x2 == pos) return (x->data[0][pos] <= x->data[0][x0]) ? pos : x0;
    302341  s0 = x->data[0][x0];
    303   s1 = x->data[0][pos]     ;
     342  s1 = x->data[0][pos];
    304343  s2 = x->data[0][x2];
    305344  return pos + 0.5 * (s2 - s0 ) / (s2 - 2.* s1 + s0);
    306 }
    307 
    308 smpl_t aubio_quadfrac(smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf) {
    309   smpl_t tmp = s0 + (pf/2.) * (pf * ( s0 - 2.*s1 + s2 ) - 3.*s0 + 4.*s1 - s2);
    310   return tmp;
    311345}
    312346
     
    320354}
    321355
    322 smpl_t aubio_freqtomidi(smpl_t freq) {
     356smpl_t
     357aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf)
     358{
     359  smpl_t tmp =
     360      s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2);
     361  return tmp;
     362}
     363
     364smpl_t
     365aubio_freqtomidi (smpl_t freq)
     366{
    323367  /* log(freq/A-2)/log(2) */
    324   smpl_t midi = freq/6.875;
    325   midi = LOG(midi)/0.69314718055995;
     368  smpl_t midi = freq / 6.875;
     369  midi = LOG (midi) / 0.69314718055995;
    326370  midi *= 12;
    327371  midi -= 3;
     
    329373}
    330374
    331 smpl_t aubio_miditofreq(smpl_t midi) {
    332   smpl_t freq = (midi+3.)/12.;
    333   freq = EXP(freq*0.69314718055995);
     375smpl_t
     376aubio_miditofreq (smpl_t midi)
     377{
     378  smpl_t freq = (midi + 3.) / 12.;
     379  freq = EXP (freq * 0.69314718055995);
    334380  freq *= 6.875;
    335381  return freq;
    336382}
    337383
    338 smpl_t aubio_bintofreq(smpl_t bin, smpl_t samplerate, smpl_t fftsize) {
    339   smpl_t freq = samplerate/fftsize;
    340   return freq*bin;
    341 }
    342 
    343 smpl_t aubio_bintomidi(smpl_t bin, smpl_t samplerate, smpl_t fftsize) {
    344   smpl_t midi = aubio_bintofreq(bin,samplerate,fftsize);
    345   return aubio_freqtomidi(midi);
    346 }
    347 
    348 smpl_t aubio_freqtobin(smpl_t freq, smpl_t samplerate, smpl_t fftsize) {
    349   smpl_t bin = fftsize/samplerate;
    350   return freq*bin;
    351 }
    352 
    353 smpl_t aubio_miditobin(smpl_t midi, smpl_t samplerate, smpl_t fftsize) {
    354   smpl_t freq = aubio_miditofreq(midi);
    355   return aubio_freqtobin(freq,samplerate,fftsize);
    356 }
    357 
    358 /** returns 1 if wassilence is 0 and RMS(ibuf)<threshold
    359  * \bug mono
    360  */
    361 uint_t aubio_silence_detection(fvec_t * ibuf, smpl_t threshold) {
    362   smpl_t loudness = 0;
    363   uint_t i=0,j;
    364   for (j=0;j<ibuf->length;j++) {
    365     loudness += SQR(ibuf->data[i][j]);
    366   }
    367   loudness = SQRT(loudness);
    368   loudness /= (smpl_t)ibuf->length;
    369   loudness = LIN2DB(loudness);
    370 
    371   return (loudness < threshold);
    372 }
    373 
    374 /** returns level log(RMS(ibuf)) if < threshold, 1 otherwise
    375  * \bug mono
    376  */
    377 smpl_t aubio_level_detection(fvec_t * ibuf, smpl_t threshold) {
    378   smpl_t loudness = 0;
    379   uint_t i=0,j;
    380   for (j=0;j<ibuf->length;j++) {
    381     loudness += SQR(ibuf->data[i][j]);
    382   }
    383   loudness = SQRT(loudness);
    384   loudness /= (smpl_t)ibuf->length;
    385   loudness = LIN2DB(loudness);
    386 
    387   if (loudness < threshold)
     384smpl_t
     385aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
     386{
     387  smpl_t freq = samplerate / fftsize;
     388  return freq * bin;
     389}
     390
     391smpl_t
     392aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
     393{
     394  smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize);
     395  return aubio_freqtomidi (midi);
     396}
     397
     398smpl_t
     399aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
     400{
     401  smpl_t bin = fftsize / samplerate;
     402  return freq * bin;
     403}
     404
     405smpl_t
     406aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize)
     407{
     408  smpl_t freq = aubio_miditofreq (midi);
     409  return aubio_freqtobin (freq, samplerate, fftsize);
     410}
     411
     412smpl_t
     413aubio_db_spl (fvec_t * o)
     414{
     415  smpl_t val = SQRT (fvec_local_energy (o));
     416  val /= (smpl_t) o->length;
     417  return LIN2DB (val);
     418}
     419
     420uint_t
     421aubio_silence_detection (fvec_t * o, smpl_t threshold)
     422{
     423  return (aubio_db_spl (o) < threshold);
     424}
     425
     426smpl_t
     427aubio_level_detection (fvec_t * o, smpl_t threshold)
     428{
     429  smpl_t db_spl = aubio_db_spl (o);
     430  if (db_spl < threshold) {
    388431    return 1.;
    389   else
    390     return loudness;
    391 }
    392 
    393 smpl_t aubio_zero_crossing_rate(fvec_t * input) {
    394   uint_t i=0,j;
     432  } else {
     433    return db_spl;
     434  }
     435}
     436
     437smpl_t
     438aubio_zero_crossing_rate (fvec_t * input)
     439{
     440  uint_t i = 0, j;
    395441  uint_t zcr = 0;
    396   for ( j = 1; j < input->length; j++ ) {
     442  for (j = 1; j < input->length; j++) {
    397443    // previous was strictly negative
    398     if( input->data[i][j-1] < 0. ) {
     444    if (input->data[i][j - 1] < 0.) {
    399445      // current is positive or null
    400       if ( input->data[i][j] >= 0. ) {
     446      if (input->data[i][j] >= 0.) {
    401447        zcr += 1;
    402448      }
    403     // previous was positive or null
     449      // previous was positive or null
    404450    } else {
    405451      // current is strictly negative
    406       if ( input->data[i][j] < 0. ) {
     452      if (input->data[i][j] < 0.) {
    407453        zcr += 1;
    408454      }
    409455    }
    410456  }
    411   return zcr/(smpl_t)input->length;
    412 }
    413 
    414 void aubio_autocorr(fvec_t * input, fvec_t * output) {
    415   uint_t i = 0, j = 0, length = input->length;
    416   smpl_t * data = input->data[0];
    417   smpl_t * acf = output->data[0];
    418   smpl_t tmp =0.;
    419   for(i=0;i<length;i++){
    420     for(j=i;j<length;j++){
    421       tmp += data[j-i]*data[j];
    422     }
    423     acf[i] = tmp /(smpl_t)(length-i);
    424     tmp = 0.0;
    425   }
    426 }
    427 
    428 void aubio_cleanup(void) {
     457  return zcr / (smpl_t) input->length;
     458}
     459
     460void
     461aubio_autocorr (fvec_t * input, fvec_t * output)
     462{
     463  uint_t i, j, k, length = input->length;
     464  smpl_t *data, *acf;
     465  smpl_t tmp = 0;
     466  for (k = 0; k < input->channels; k++) {
     467    data = input->data[k];
     468    acf = output->data[k];
     469    for (i = 0; i < length; i++) {
     470      tmp = 0.;
     471      for (j = i; j < length; j++) {
     472        tmp += data[j - i] * data[j];
     473      }
     474      acf[i] = tmp / (smpl_t) (length - i);
     475    }
     476  }
     477}
     478
     479void
     480aubio_cleanup (void)
     481{
    429482#if HAVE_FFTW3
    430   fftw_cleanup();
     483  fftw_cleanup ();
    431484#else
    432485#if HAVE_FFTW3F
    433   fftwf_cleanup();
     486  fftwf_cleanup ();
    434487#endif
    435488#endif
  • src/mathutils.h

    r1498ced reb7f743  
    2121/** @file
    2222 *  various math functions
    23  *
    24  *  \todo multichannel (each function should return -or set- an array sized to
    25  *  the number of channel in the input vector)
    26  *
    27  *  \todo appropriate switches depending on types.h content
    2823 */
    2924
     
    3631
    3732/** Window types
    38  *
    39  * inspired from
    40  *
    41  *  - dafx : http://profs.sci.univr.it/%7Edafx/Final-Papers/ps/Bernardini.ps.gz
    42  *  - freqtweak : http://freqtweak.sf.net/
    43  *  - extace : http://extace.sf.net/
    44  */
    45 typedef enum {
    46         aubio_win_rectangle,         
    47         aubio_win_hamming,
    48         aubio_win_hanning,
    49         aubio_win_hanningz,
    50         aubio_win_blackman,
    51         aubio_win_blackman_harris,
    52         aubio_win_gaussian,
    53         aubio_win_welch,
    54         aubio_win_parzen
     33 
     34  References:
     35   
     36    - <a href="http://en.wikipedia.org/wiki/Window_function">Window
     37function</a> on Wikipedia
     38    - Amalia de Götzen, Nicolas Bernardini, and Daniel Arfib. Traditional (?)
     39implementations of a phase vocoder: the tricks of the trade. In Proceedings of
     40the International Conference on Digital Audio Effects (DAFx-00), pages 37–44,
     41Uni- versity of Verona, Italy, 2000.
     42  (<a href="http://profs.sci.univr.it/%7Edafx/Final-Papers/ps/Bernardini.ps.gz">
     43  ps.gz</a>)
     44
     45*/
     46typedef enum
     47{
     48  aubio_win_rectangle,
     49  aubio_win_hamming,
     50  aubio_win_hanning,
     51  aubio_win_hanningz,
     52  aubio_win_blackman,
     53  aubio_win_blackman_harris,
     54  aubio_win_gaussian,
     55  aubio_win_welch,
     56  aubio_win_parzen
    5557} aubio_window_type;
    5658
    5759/** create window */
    58 fvec_t * new_aubio_window(uint_t size, aubio_window_type wintype);
    59 
    60 /** principal argument
    61  *
    62  * mod(phase+PI,-TWO_PI)+PI
    63  */
     60fvec_t *new_aubio_window (uint_t size, aubio_window_type wintype);
     61
     62/** compute the principal argument
     63
     64  This function maps the input phase to its corresponding value wrapped in the
     65range \f$ [-\pi, \pi] \f$.
     66
     67  \param phase unwrapped phase to map to the unit circle
     68 
     69  \return equivalent phase wrapped to the unit circle
     70
     71*/
    6472smpl_t aubio_unwrap2pi (smpl_t phase);
    6573
    66 /** calculates the mean of a vector
    67  *
    68  * \bug mono
    69  */
    70 smpl_t fvec_mean(fvec_t *s);
    71 /** returns the max of a vector
    72  *
    73  * \bug mono
    74  */
    75 smpl_t fvec_max(fvec_t *s);
    76 /** returns the min of a vector
    77  *
    78  * \bug mono
    79  */
    80 smpl_t fvec_min(fvec_t *s);
    81 /** returns the index of the min of a vector
    82  *
    83  * \bug mono
    84  */
    85 uint_t fvec_min_elem(fvec_t *s);
    86 /** returns the index of the max of a vector
    87  *
    88  * \bug mono
    89  */
    90 uint_t fvec_max_elem(fvec_t *s);
    91 /** implement 'fftshift' like function
    92  *
    93  * a[0]...,a[n/2],a[n/2+1],...a[n]
    94  *
    95  * becomes
    96  *
    97  * a[n/2+1],...a[n],a[0]...,a[n/2]
    98  */
    99 void fvec_shift(fvec_t *s);
    100 /** returns sum */
    101 smpl_t fvec_sum(fvec_t *s);
    102 
    103 /** returns energy
    104  *
    105  * \bug mono
    106  */
    107 smpl_t fvec_local_energy(fvec_t * f);
    108 /** returns High Frequency Energy Content
    109  *
    110  * \bug mono */
    111 smpl_t fvec_local_hfc(fvec_t * f);
    112 /** return alpha norm.
    113  *
    114  * alpha=2 means normalise variance.
    115  * alpha=1 means normalise abs value.
    116  * as alpha goes large, tends to normalisation
    117  * by max value.
    118  *
    119  * \bug should not use POW :(
    120  */
    121 smpl_t fvec_alpha_norm(fvec_t * DF, smpl_t alpha);
    122 /**  min removal */
    123 void fvec_min_removal(fvec_t * mag);
    124 /**  alpha normalisation */
    125 void fvec_alpha_normalise(fvec_t * mag, uint_t alpha);
    126 /** add a constant to all members of a vector */
    127 void fvec_add(fvec_t * mag, smpl_t threshold);
    128 
    129 /** compute adaptive threshold of input vector */
    130 void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
    131     uint_t win_post, uint_t win_pre);
    132 /**  adaptative thresholding
    133  *
    134  * y=fn_thresh(fn,x,post,pre)
    135  * compute adaptive threshold at each time
    136  *    fn : a function name or pointer, eg 'median'
    137  *    x:   signal vector
    138  *    post: window length, causal part
    139  *    pre: window length, anti-causal part
    140  * Returns:
    141  *    y:   signal the same length as x
    142  *
    143  * Formerly median_thresh, used compute median over a
    144  * window of post+pre+1 samples, but now works with any
    145  * function that takes a vector or matrix and returns a
    146  * 'representative' value for each column, eg
    147  *    medians=fn_thresh(median,x,8,8) 
    148  *    minima=fn_thresh(min,x,8,8) 
    149  * see SPARMS for explanation of post and pre
    150  */
    151 smpl_t fvec_moving_thres(fvec_t * vec, fvec_t * tmp,
    152     uint_t win_post, uint_t win_pre, uint_t win_pos);
    153 
    154 /** returns the median of the vector
    155  *
    156  *  This Quickselect routine is based on the algorithm described in
    157  *  "Numerical recipes in C", Second Edition,
    158  *  Cambridge University Press, 1992, Section 8.5, ISBN 0-521-43108-5
    159  *
    160  *  This code by Nicolas Devillard - 1998. Public domain,
    161  *  available at http://ndevilla.free.fr/median/median/
    162  */
    163 smpl_t fvec_median(fvec_t * input);
     74/** compute the mean of a vector
     75
     76  \param s vector to compute norm from
     77
     78  \return the mean of v
     79
     80*/
     81smpl_t fvec_mean (fvec_t * s);
     82
     83/** find the max of a vector
     84
     85  \param s vector to get the max from
     86
     87  \return the value of the minimum of v
     88
     89*/
     90smpl_t fvec_max (fvec_t * s);
     91
     92/** find the min of a vector
     93
     94  \param s vector to get the min from
     95
     96  \return the value of the maximum of v
     97
     98*/
     99smpl_t fvec_min (fvec_t * s);
     100
     101/** find the index of the min of a vector
     102
     103  \param s vector to get the index from
     104
     105  \return the index of the minimum element of v
     106
     107*/
     108uint_t fvec_min_elem (fvec_t * s);
     109
     110/** find the index of the max of a vector
     111
     112  \param s vector to get the index from
     113
     114  \return the index of the maximum element of v
     115
     116*/
     117uint_t fvec_max_elem (fvec_t * s);
     118
     119/** swap the left and right halves of a vector
     120 
     121  This function swaps the left part of the signal with the right part of the
     122signal. Therefore
     123
     124  \f$ a[0], a[1], ..., a[\frac{N}{2}], a[\frac{N}{2}+1], ..., a[N-1], a[N] \f$
     125 
     126  becomes
     127 
     128  \f$ a[\frac{N}{2}+1], ..., a[N-1], a[N], a[0], a[1], ..., a[\frac{N}{2}] \f$
     129
     130  This operation, known as 'fftshift' in the Matlab Signal Processing Toolbox,
     131can be used before computing the FFT to simplify the phase relationship of the
     132resulting spectrum. See Amalia de Götzen's paper referred to above.
     133 
     134*/
     135void fvec_shift (fvec_t * v);
     136
     137/** compute the sum of all elements of a vector
     138
     139  \param v vector to compute the sum of
     140
     141  \return the sum of v
     142
     143*/
     144smpl_t fvec_sum (fvec_t * v);
     145
     146/** compute the energy of a vector
     147
     148  This function compute the sum of the squared elements of a vector.
     149 
     150  \param v vector to get the energy from
     151
     152  \return the energy of v
     153 
     154*/
     155smpl_t fvec_local_energy (fvec_t * v);
     156
     157/** compute the High Frequency Content of a vector
     158
     159  The High Frequency Content is defined as \f$ \sum_0^{N-1} (k+1) v[k] \f$.
     160 
     161  \param v vector to get the energy from
     162
     163  \return the HFC of v
     164 
     165*/
     166smpl_t fvec_local_hfc (fvec_t * v);
     167
     168/** computes the p-norm of a vector
     169 
     170  Computes the p-norm of a vector for \f$ p = \alpha \f$
     171
     172  \f$ L^p = ||x||_p = (|x_1|^p + |x_2|^p + ... + |x_n|^p ) ^ \frac{1}{p} \f$
     173 
     174  If p = 1, the result is the Manhattan distance.
     175
     176  If p = 2, the result is the Euclidean distance.
     177
     178  As p tends towards large values, \f$ L^p \f$ tends towards the maximum of the
     179input vector.
     180
     181  References:
     182 
     183    - <a href="http://en.wikipedia.org/wiki/Lp_space">\f$L^p\f$ space</a> on
     184  Wikipedia
     185
     186  \param v vector to compute norm from
     187  \param p order of the computed norm
     188
     189  \return the p-norm of v
     190 
     191*/
     192smpl_t fvec_alpha_norm (fvec_t * v, smpl_t p);
     193
     194/**  alpha normalisation
     195
     196  This function divides all elements of a vector by the p-norm as computed by
     197fvec_alpha_norm().
     198
     199  \param v vector to compute norm from
     200  \param p order of the computed norm
     201
     202*/
     203void fvec_alpha_normalise (fvec_t * v, smpl_t p);
     204
     205/** add a constant to each elements of a vector
     206
     207  \param v vector to add constant to
     208  \param c constant to add to v
     209
     210*/
     211void fvec_add (fvec_t * v, smpl_t c);
     212
     213/** remove the minimum value of the vector to each elements
     214 
     215  \param v vector to remove minimum from
     216
     217*/
     218void fvec_min_removal (fvec_t * v);
     219
     220/** compute moving median theshold of a vector
     221
     222  This function computes the moving median threshold value of at the given
     223position of a vector, taking the median amongs post elements before and up to
     224pre elements after pos.
     225 
     226  \param v input vector
     227  \param tmp temporary vector of length post+1+pre
     228  \param post length of causal part to take before pos
     229  \param pre length of anti-causal part to take after pos
     230  \param pos index to compute threshold for
     231
     232  \return moving median threshold value
     233
     234*/
     235smpl_t fvec_moving_thres (fvec_t * v, fvec_t * tmp, uint_t post, uint_t pre,
     236    uint_t pos);
     237
     238/** apply adaptive threshold to a vector
     239
     240  For each points at position p of an input vector, this function remove the
     241moving median threshold computed at p.
     242
     243  \param v input vector
     244  \param tmp temporary vector of length post+1+pre
     245  \param post length of causal part to take before pos
     246  \param pre length of anti-causal part to take after pos
     247
     248*/
     249void fvec_adapt_thres (fvec_t * v, fvec_t * tmp, uint_t post, uint_t pre);
     250
     251/** returns the median of a vector
     252
     253  The QuickSelect routine is based on the algorithm described in "Numerical
     254recipes in C", Second Edition, Cambridge University Press, 1992, Section 8.5,
     255ISBN 0-521-43108-5
     256
     257  This implementation of the QuickSelect routine is based on Nicolas
     258Devillard's implementation, available at http://ndevilla.free.fr/median/median/
     259and in the Public Domain.
     260
     261  \param v vector to get median from
     262
     263  \return the median of v
     264 
     265*/
     266smpl_t fvec_median (fvec_t * v);
    164267
    165268/** finds exact peak index by quadratic interpolation*/
    166 smpl_t fvec_quadint(fvec_t * x, uint_t pos, uint_t span);
     269smpl_t fvec_quadint (fvec_t * x, uint_t pos, uint_t span);
    167270
    168271/** Quadratic interpolation using Lagrange polynomial.
    169  *
    170  * inspired from ``Comparison of interpolation algorithms in real-time sound
    171  * processing'', Vladimir Arnost,
    172  *
    173  * estimate = s0 + (pf/2.)*((pf-3.)*s0-2.*(pf-2.)*s1+(pf-1.)*s2);
    174  *    where
    175  *    \param s0,s1,s2 are 3 known points on the curve,
    176  *    \param pf is the floating point index [0;2]
    177  */
    178 smpl_t aubio_quadfrac(smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf);
    179 
    180 /** returns 1 if X1 is a peak and positive */
    181 uint_t fvec_peakpick(fvec_t * input, uint_t pos);
     272 
     273  Inspired from ``Comparison of interpolation algorithms in real-time sound
     274processing'', Vladimir Arnost,
     275 
     276  \param s0,s1,s2 are 3 consecutive samples of a curve
     277  \param pf is the floating point index [0;2]
     278 
     279  \return s0 + (pf/2.)*((pf-3.)*s0-2.*(pf-2.)*s1+(pf-1.)*s2);
     280
     281*/
     282smpl_t aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf);
     283
     284/** return 1 if v[p] is a peak and positive, 0 otherwise
     285
     286  This function returns 1 if a peak is found at index p in the vector v. The
     287peak is defined as follows:
     288
     289  - v[p] is positive
     290  - v[p-1] < v[p]
     291  - v[p] > v[p+1]
     292
     293  \param v input vector
     294  \param p position of supposed for peak
     295
     296  \return 1 if a peak is found, 0 otherwise
     297
     298*/
     299uint_t fvec_peakpick (fvec_t * v, uint_t p);
    182300
    183301/** convert frequency bin to midi value */
    184 smpl_t aubio_bintomidi(smpl_t bin, smpl_t samplerate, smpl_t fftsize);
     302smpl_t aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize);
     303
    185304/** convert midi value to frequency bin */
    186 smpl_t aubio_miditobin(smpl_t midi, smpl_t samplerate, smpl_t fftsize);
     305smpl_t aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize);
     306
    187307/** convert frequency bin to frequency (Hz) */
    188 smpl_t aubio_bintofreq(smpl_t bin, smpl_t samplerate, smpl_t fftsize);
     308smpl_t aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize);
     309
    189310/** convert frequency (Hz) to frequency bin */
    190 smpl_t aubio_freqtobin(smpl_t freq, smpl_t samplerate, smpl_t fftsize);
     311smpl_t aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize);
     312
    191313/** convert frequency (Hz) to midi value (0-128) */
    192 smpl_t aubio_freqtomidi(smpl_t freq);
     314smpl_t aubio_freqtomidi (smpl_t freq);
     315
    193316/** convert midi value (0-128) to frequency (Hz) */
    194 smpl_t aubio_miditofreq(smpl_t midi);
    195 
    196 /** check if current buffer level is under a given threshold */
    197 uint_t aubio_silence_detection(fvec_t * ibuf, smpl_t threshold);
    198 /** get the current buffer level */
    199 smpl_t aubio_level_detection(fvec_t * ibuf, smpl_t threshold);
    200 /**
    201  * calculate normalised autocorrelation function
    202  */
    203 void aubio_autocorr(fvec_t * input, fvec_t * output);
    204 /**
    205  * zero-crossing rate (number of zero cross per sample)
    206  */
    207 smpl_t aubio_zero_crossing_rate(fvec_t * input);
    208 /**
    209  * clean up cached memory at the end of program
    210  *
    211  * use this function at the end of programs to purge all
    212  * cached memory. so far this function is only used to clean
    213  * fftw cache.
    214  */
    215 void aubio_cleanup(void);
     317smpl_t aubio_miditofreq (smpl_t midi);
     318
     319/** compute sound pressure level (SPL) in dB
     320
     321  This quantity is often wrongly called 'loudness'.
     322
     323  \param v vector to compute dB SPL from
     324
     325  \return level of v in dB SPL
     326
     327*/
     328smpl_t aubio_db_spl (fvec_t * v);
     329
     330/** check if buffer level in dB SPL is under a given threshold
     331 
     332  \param v vector to get level from
     333  \param threshold threshold in dB SPL
     334
     335  \return 0 if level is under the given threshold, 1 otherwise
     336
     337*/
     338uint_t aubio_silence_detection (fvec_t * v, smpl_t threshold);
     339
     340/** get buffer level if level >= threshold, 1. otherwise
     341
     342  \param v vector to get level from
     343  \param threshold threshold in dB SPL
     344
     345  \return level in dB SPL if level >= threshold, 1. otherwise
     346
     347*/
     348smpl_t aubio_level_detection (fvec_t * v, smpl_t threshold);
     349
     350/** compute normalised autocorrelation function
     351
     352  \param input vector to compute autocorrelation from
     353  \param output vector to store autocorrelation function to
     354
     355*/
     356void aubio_autocorr (fvec_t * input, fvec_t * output);
     357
     358/** zero-crossing rate (ZCR)
     359
     360  The zero-crossing rate is the number of times a signal changes sign,
     361  divided by the length of this signal.
     362
     363  \param v vector to compute ZCR from
     364
     365  \return zero-crossing rate of v
     366
     367*/
     368smpl_t aubio_zero_crossing_rate (fvec_t * v);
     369
     370/** clean up cached memory at the end of program
     371 
     372  This function should be used at the end of programs to purge all cached
     373  memory. So far it is only useful to clean FFTW's cache.
     374
     375*/
     376void aubio_cleanup (void);
    216377
    217378#ifdef __cplusplus
Note: See TracChangeset for help on using the changeset viewer.