Changeset 7e35b37 for tests


Ignore:
Timestamp:
Mar 3, 2013, 8:09:48 PM (12 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:
474a573
Parents:
abd326c
Message:

tests/src/: improve examples

Location:
tests/src
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • tests/src/test-cvec.c

    rabd326c r7e35b37  
    11#include <aubio.h>
     2#include "utils_tests.h"
    23
    3 int main(){
    4         /* allocate some memory */
    5         uint_t win_s      = 1024;                       /* window size */
    6         cvec_t * sp       = new_cvec (win_s); /* input buffer */
    7         del_cvec(sp);
     4int main ()
     5{
     6  uint_t i, window_size = 16; // window size
     7  utils_init_random();
     8  cvec_t * complex_vector = new_cvec (window_size); // input buffer
     9  uint_t rand_times = 4;
    810
    9         return 0;
     11  while (rand_times -- ) {
     12    // fill with random phas and norm
     13    for ( i = 0; i < complex_vector->length; i++ ) {
     14      complex_vector->norm[i] = ( 2. / RAND_MAX * random() - 1. );
     15      complex_vector->phas[i] = ( 2. / RAND_MAX * random() - 1. ) * M_PI;
     16    }
     17    // print the vector
     18    cvec_print(complex_vector);
     19  }
     20
     21  // set all vector elements to `0`
     22  cvec_zeros(complex_vector);
     23  for ( i = 0; i < complex_vector->length; i++ ) {
     24    assert( complex_vector->norm[i] == 0. );
     25    // assert( complex_vector->phas[i] == 0 );
     26  }
     27  cvec_print(complex_vector);
     28
     29  // set all vector elements to `1`
     30  cvec_ones(complex_vector);
     31  for ( i = 0; i < complex_vector->length; i++ ) {
     32    assert( complex_vector->norm[i] == 1. );
     33    // assert( complex_vector->phas[i] == 0 );
     34  }
     35  cvec_print(complex_vector);
     36  // destroy it
     37  del_cvec(complex_vector);
     38  return 0;
    1039}
    11 
  • tests/src/test-fmat.c

    rabd326c r7e35b37  
    11#include <aubio.h>
     2#include <assert.h>
    23
    3 int main(){
    4         uint_t length = 1024;                     /* length */
    5         uint_t height = 1024;                     /* height */
    6         fmat_t * mat = new_fmat (length, height); /* input buffer */
    7         fmat_print(mat);
    8         del_fmat(mat);
    9         return 0;
     4// create a new matrix and fill it with i * 1. + j * .1, where i is the row,
     5// and j the column.
     6
     7int main ()
     8{
     9  uint_t height = 3, length = 9, i, j;
     10  // create fmat_t object
     11  fmat_t * mat = new_fmat (length, height);
     12  for ( i = 0; i < mat->height; i++ ) {
     13    for ( j = 0; j < mat->length; j++ ) {
     14      // all elements are already initialized to 0.
     15      assert(mat->data[i][j] == 0);
     16      // setting element of row i, column j
     17      mat->data[i][j] = i * 1. + j *.1;
     18    }
     19  }
     20  // print out matrix
     21  fmat_print(mat);
     22  // destroy it
     23  del_fmat(mat);
     24  return 0;
    1025}
    1126
  • tests/src/test-fvec.c

    rabd326c r7e35b37  
    22#include <assert.h>
    33
    4 int main(){
    5   uint_t buffer_size = 1024;
    6   fvec_t * in = new_fvec (buffer_size);
     4int main ()
     5{
     6  uint_t vec_size = 10, i;
     7  fvec_t * vec = new_fvec (vec_size);
    78
    8   assert( in->length                == buffer_size);
     9  // vec->length matches requested size
     10  assert(vec->length == vec_size);
    911
    10   assert( in->data[0]               == 0);
    11   assert( in->data[buffer_size / 2] == 0);
    12   assert( in->data[buffer_size - 1] == 0);
     12  // all elements are initialized to `0.`
     13  for ( i = 0; i < vec->length; i++ ) {
     14    assert(vec->data[i] == 0.);
     15  }
    1316
    14   in->data[buffer_size -1 ] = 1;
    15   assert( in->data[buffer_size - 1] == 1);
     17  // all elements can be set to `0.`
     18  fvec_zeros(vec);
     19  for ( i = 0; i < vec->length; i++ ) {
     20    assert(vec->data[i] == 0.);
     21  }
     22  fvec_print(vec);
    1623
    17   del_fvec(in);
     24  // all elements can be set to `1.`
     25  fvec_ones(vec);
     26  for ( i = 0; i < vec->length; i++ ) {
     27    assert(vec->data[i] == 1.);
     28  }
     29  fvec_print(vec);
     30
     31  // each element can be accessed directly
     32  for ( i = 0; i < vec->length; i++ ) {
     33    vec->data[i] = i;
     34    assert(vec->data[i] == i);
     35  }
     36  fvec_print(vec);
     37
     38  // now destroys the vector
     39  del_fvec(vec);
    1840
    1941  return 0;
  • tests/src/test-lvec.c

    rabd326c r7e35b37  
    11#include <aubio.h>
    22
    3 int main(){
    4         /* allocate some memory */
    5         uint_t win_s      = 1024;                       /* window size */
    6         lvec_t * sp       = new_lvec (win_s); /* input buffer */
    7         del_lvec(sp);
    8 
    9         return 0;
     3int main()
     4{
     5  uint_t win_s = 1024; // window size
     6  lvec_t * sp = new_lvec (win_s); // input buffer
     7  del_lvec(sp);
     8  return 0;
    109}
    1110
  • tests/src/test-mathutils-window.c

    rabd326c r7e35b37  
    11#include <aubio.h>
    2 #include <stdlib.h>
     2#include <math.h>
     3#include <stdio.h>
    34
    4 int main( )
     5int main ()
    56{
    6   uint_t length;
    7   for (length = 2; length <= 5; length++)
    8   {
    9     fvec_t *t = new_aubio_window("rectangle", length);
    10     del_fvec(t);
    11     t = new_aubio_window("hamming", length);
    12     fvec_print(t);
    13     del_fvec(t);
    14     t = new_aubio_window("hanning", length);
    15     fvec_print(t);
    16     del_fvec(t);
    17     t = new_aubio_window("hanningz", length);
    18     fvec_print(t);
    19     del_fvec(t);
    20     t = new_aubio_window("blackman", length);
    21     fvec_print(t);
    22     del_fvec(t);
    23     t = new_aubio_window("blackman_harris", length);
    24     fvec_print(t);
    25     del_fvec(t);
    26     t = new_aubio_window("gaussian", length);
    27     fvec_print(t);
    28     del_fvec(t);
    29     t = new_aubio_window("welch", length);
    30     fvec_print(t);
    31     del_fvec(t);
    32     t = new_aubio_window("parzen", length);
    33     fvec_print(t);
    34     del_fvec(t);
    35     t = new_aubio_window("default", length);
    36     fvec_print(t);
    37     del_fvec(t);
     7  uint_t length = 0;
     8  uint_t n_length = 4, n_types = 10, i, t;
     9  uint_t lengths[4] = { 8, 10, 15, 16 };
     10  char *method = "default";
     11  char *window_types[10] = { "default",
     12    "rectangle", "hamming", "hanning", "hanningz",
     13    "blackman", "blackman_harris", "gaussian", "welch", "parzen"};
     14
     15  for ( t = 0; t < n_types; t ++ ) {
     16    for ( i = 0; i < n_length; i++)
     17    {
     18      length = lengths[i];
     19      method = window_types[t];
     20
     21      fvec_t * window = new_aubio_window(method, length);
     22
     23      fvec_set_window(window, method);
     24      fprintf(stdout, "length: %d, method: %s, window:, ", length, method);
     25      fvec_print(window);
     26
     27      del_fvec(window);
     28    }
    3829  }
    3930  return 0;
  • tests/src/test-mathutils.c

    rabd326c r7e35b37  
    44#include <aubio.h>
    55
    6 int main(){
     6int test_next_power_of_two()
     7{
    78  uint_t a, b;
     9  a = 15; b = aubio_next_power_of_two(a); assert(b == 16);
     10  fprintf(stdout, "aubio_next_power_of_two(%d) = %d\n", a, b);
    811
    9   a = 31; b = aubio_next_power_of_two(a);
    10   fprintf(stdout, "next_power_of_two of %d is %d\n", a, b);
    11   assert(b == 32);
     12  a = 17; b = aubio_next_power_of_two(a); assert(b == 32);
     13  fprintf(stdout, "aubio_next_power_of_two(%d) = %d\n", a, b);
    1214
    13   a = 32; b = aubio_next_power_of_two(a);
    14   fprintf(stdout, "next_power_of_two of %d is %d\n", a, b);
    15   assert(b == 32);
     15  a = 31; b = aubio_next_power_of_two(a); assert(b == 32);
     16  fprintf(stdout, "aubio_next_power_of_two(%d) = %d\n", a, b);
    1617
    17   a = 33; b = aubio_next_power_of_two(a);
    18   fprintf(stdout, "next_power_of_two of %d is %d\n", a, b);
    19   assert(b == 64);
     18  a = 32; b = aubio_next_power_of_two(a); assert(b == 32);
     19  fprintf(stdout, "aubio_next_power_of_two(%d) = %d\n", a, b);
     20
     21  a = 33; b = aubio_next_power_of_two(a); assert(b == 64);
     22  fprintf(stdout, "aubio_next_power_of_two(%d) = %d\n", a, b);
    2023
    2124  return 0;
    2225}
    2326
     27int test_miditofreq()
     28{
     29  smpl_t midi, freq;
     30  for ( midi = 0; midi < 128; midi += 3 ) {
     31    freq = aubio_miditofreq(midi);
     32    fprintf(stdout, "aubio_miditofreq(%.2f) = %.2f\n", midi, freq);
     33  }
     34  midi = 69.5;
     35  freq = aubio_miditofreq(midi);
     36  fprintf(stdout, "aubio_miditofreq(%.2f) = %.2f\n", midi, freq);
     37  midi = -69.5;
     38  freq = aubio_miditofreq(midi);
     39  fprintf(stdout, "aubio_miditofreq(%.2f) = %.2f\n", midi, freq);
     40  midi = -169.5;
     41  freq = aubio_miditofreq(midi);
     42  fprintf(stdout, "aubio_miditofreq(%.2f) = %.2f\n", midi, freq);
     43  midi = 140.;
     44  freq = aubio_miditofreq(midi);
     45  fprintf(stdout, "aubio_miditofreq(%.2f) = %.2f\n", midi, freq);
     46  midi = 0;
     47  freq = aubio_miditofreq(midi);
     48  fprintf(stdout, "aubio_miditofreq(%.2f) = %.2f\n", midi, freq);
     49  midi = 8.2e10;
     50  freq = aubio_miditofreq(midi);
     51  fprintf(stdout, "aubio_miditofreq(%.2f) = %.2f\n", midi, freq);
     52  midi = -5.e10;
     53  freq = aubio_miditofreq(midi);
     54  fprintf(stdout, "aubio_miditofreq(%.2f) = %.2f\n", midi, freq);
     55  return 0;
     56}
     57
     58int test_freqtomidi()
     59{
     60  smpl_t midi, freq;
     61  for ( freq = 0.; freq < 30000.; freq += 440. ) {
     62    midi = aubio_freqtomidi(freq);
     63    fprintf(stdout, "aubio_freqtomidi(%.2f) = %.2f\n", freq, midi);
     64  }
     65  freq = 69.5;
     66  midi = aubio_freqtomidi(freq);
     67  fprintf(stdout, "aubio_freqtomidi(%.2f) = %.2f\n", freq, midi);
     68  freq = -69.5;
     69  midi = aubio_freqtomidi(freq);
     70  fprintf(stdout, "aubio_freqtomidi(%.2f) = %.2f\n", freq, midi);
     71  freq = -169.5;
     72  midi = aubio_freqtomidi(freq);
     73  fprintf(stdout, "aubio_freqtomidi(%.2f) = %.2f\n", freq, midi);
     74  freq = 140.;
     75  midi = aubio_freqtomidi(freq);
     76  fprintf(stdout, "aubio_freqtomidi(%.2f) = %.2f\n", freq, midi);
     77  freq = 0;
     78  midi = aubio_freqtomidi(freq);
     79  fprintf(stdout, "aubio_freqtomidi(%.2f) = %.2f\n", freq, midi);
     80  freq = 8.2e10;
     81  midi = aubio_freqtomidi(freq);
     82  fprintf(stdout, "aubio_freqtomidi(%.2f) = %.2f\n", freq, midi);
     83  freq = -5.;
     84  midi = aubio_freqtomidi(freq);
     85  fprintf(stdout, "aubio_freqtomidi(%.2f) = %.2f\n", freq, midi);
     86  return 0;
     87}
     88
     89int test_aubio_window()
     90{
     91  uint_t window_size = 16;
     92  fvec_t * window = new_aubio_window("default", window_size);
     93  del_fvec(window);
     94
     95  window = new_fvec(window_size);
     96  fvec_set_window(window, "rectangle");
     97  fvec_print(window);
     98
     99  window_size /= 2.;
     100  window = new_aubio_window("triangle", window_size);
     101  fvec_print(window);
     102  del_fvec(window);
     103
     104  window = new_aubio_window("rectangle", 16);
     105  del_fvec (window);
     106  return 0;
     107}
     108
     109int main ()
     110{
     111  test_next_power_of_two();
     112  test_miditofreq();
     113  test_freqtomidi();
     114  return 0;
     115}
Note: See TracChangeset for help on using the changeset viewer.