Mercurial > hg > libxtract
changeset 192:bb60691d9570
Make xtract_lowest_value() return XTRACT_NO_RESULT if all values in the input data are below or equal to threshold value. Fixes #46
If XTRACT_NO_RESULT is returned, *result will be set to DBL_MAX
author | Jamie Bullock <jamie@jamiebullock.com> |
---|---|
date | Tue, 11 Feb 2014 16:39:41 +0000 |
parents | e1d65ec5fde3 |
children | 1d19c68bdcaf |
files | examples/simpletest/simpletest.c src/scalar.c xtract/xtract_scalar.h |
diffstat | 3 files changed, 27 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/simpletest/simpletest.c Wed Jan 15 19:16:32 2014 +0000 +++ b/examples/simpletest/simpletest.c Tue Feb 11 16:39:41 2014 +0000 @@ -98,6 +98,7 @@ double f0 = 0.0; double flux = 0.0; double centroid = 0.0; + double lowest = 0.0; double spectrum[BLOCKSIZE] = {0}; double windowed[BLOCKSIZE] = {0}; double peaks[BLOCKSIZE] = {0}; @@ -109,6 +110,7 @@ double argd[4] = {0}; double samplerate = 44100.0; int n; + int rv = XTRACT_SUCCESS; xtract_mel_filter mel_filters; fill_wavetable(344.53125f, NOISE); // 344.53125f = 128 samples @ 44100 Hz @@ -122,6 +124,19 @@ xtract[XTRACT_MEAN](wavetable, BLOCKSIZE, NULL, &mean); printf("\nInput mean = %.2f\n\n", mean); /* We expect this to be zero for a square wave */ + /* get the lowest value in the input */ + argd[0] = -.5; + rv = xtract[XTRACT_LOWEST_VALUE](wavetable, BLOCKSIZE, argd, &lowest); + + if (rv == XTRACT_SUCCESS) + { + printf("\nLowest value = %.6f\n\n", lowest); + } + else + { + printf("\nUnable to get lowest value, all values below threshold?\n\n"); + } + exit(0); /* create the window function */ window = xtract_init_window(BLOCKSIZE, XTRACT_HANN); xtract_windowed(wavetable, BLOCKSIZE, window, windowed);
--- a/src/scalar.c Wed Jan 15 19:16:32 2014 +0000 +++ b/src/scalar.c Tue Feb 11 16:39:41 2014 +0000 @@ -27,6 +27,7 @@ #include <string.h> #include <stdio.h> #include <math.h> +#include <limits.h> #include "dywapitchtrack/dywapitchtrack.h" @@ -746,16 +747,18 @@ { int n = N; - double temp; - *result = data[--n]; + *result = DBL_MAX; while(n--) { - if((temp = data[n]) > *(double *)argv) + if(data[n] > *(double *)argv) *result = XTRACT_MIN(*result, data[n]); } + if (*result == DBL_MAX) + return XTRACT_NO_RESULT; + return XTRACT_SUCCESS; }
--- a/xtract/xtract_scalar.h Wed Jan 15 19:16:32 2014 +0000 +++ b/xtract/xtract_scalar.h Tue Feb 11 16:39:41 2014 +0000 @@ -353,8 +353,13 @@ * * \param *data: a pointer to the first element in an array of doubles * \param N: the number of elements to be considered - * \param *argv: a pointer to a double representing the lower limit for the search. i.e. (*result > *argv) returns 1. + * \param *argv: a pointer to a double representing the lower limit for the search. All values in the array pointed to by *data that are below or equal to this threshold will be ignored. * \param *result: a pointer to a value representing the lowest component in *data that falls above a given threshold. + * + * \return XTRACT_SUCCESS is a lowest value was found or XTRACT_NO_VALUE if all values + * in the array pointed to by *data are below or equal to the threshold set with *argv + * + * \note If XTRACT_NO_VALUE is returned, *result will be set to DBL_MAX * */ int xtract_lowest_value(const double *data, const int N, const void *argv, double *result);