diff options
author | Sean Enderby <sean.enderby@gmail.com> | 2014-02-17 12:58:28 +0000 |
---|---|---|
committer | Sean Enderby <sean.enderby@gmail.com> | 2014-02-17 12:58:28 +0000 |
commit | 5e3bd37d8f85d8bf1cb6bf9436c24b83dfad89c6 (patch) | |
tree | e42769acbc978e1cb98a81853d268ddb51e73f23 /src/scalar.c | |
parent | 43e496ad0a0f632696a89265d05807a89104b897 (diff) | |
download | LibXtract-5e3bd37d8f85d8bf1cb6bf9436c24b83dfad89c6.tar.gz LibXtract-5e3bd37d8f85d8bf1cb6bf9436c24b83dfad89c6.tar.bz2 LibXtract-5e3bd37d8f85d8bf1cb6bf9436c24b83dfad89c6.zip |
xtract_hps, made it more in the style of the library and octave error correction bit
Diffstat (limited to 'src/scalar.c')
-rw-r--r-- | src/scalar.c | 95 |
1 files changed, 21 insertions, 74 deletions
diff --git a/src/scalar.c b/src/scalar.c index b7d3806..2e93934 100644 --- a/src/scalar.c +++ b/src/scalar.c @@ -808,58 +808,42 @@ int xtract_nonzero_count(const double *data, const int N, const void *argv, doub } -/*int xtract_hps(const double *data, const int N, const void *argv, double *result) +int xtract_hps(const double *data, const int N, const void *argv, double *result) { + int n, M, i, peak_index, position1_lwr; + double tempProduct, peak, largest1_lwr, ratio1; - int n = N, M, m, l, peak_index, position1_lwr; - double *coeffs2, *coeffs3, *product, L, - largest1_lwr, peak, ratio1, sr; - - sr = *(double*)argv; - if(sr == 0) - sr = 44100.0; - - coeffs2 = (double *)malloc(N * sizeof(double)); - coeffs3 = (double *)malloc(N * sizeof(double)); - product = (double *)malloc(N * sizeof(double)); + n = N / 2; - while(n--) coeffs2[n] = coeffs3[n] = 1; + M = ceil(n / 3.0); - M = N >> 1; - L = N / 3.0; - - while(M--) + if (M <= 1) { - m = M << 1; - coeffs2[M] = (data[m] + data[m+1]) * 0.5; - - if(M < L) - { - l = M * 3; - coeffs3[M] = (data[l] + data[l+1] + data[l+2]) / 3.0; - } + /* Input data is too short. */ + *result = 0; + return XTRACT_NO_RESULT; } - peak_index = peak = 0; - - for(n = 1; n < N; n++) + tempProduct = peak = 0; + for (i = 0; i < M; ++i) { - product[n] = data[n] * coeffs2[n] * coeffs3[n]; - if(product[n] > peak) + tempProduct = data [i] * data [i * 2] * data [i * 3]; + + if (tempProduct > peak) { - peak_index = n; - peak = product[n]; + peak = tempProduct; + peak_index = i; } } largest1_lwr = position1_lwr = 0; - for(n = 0; n < N; n++) + for(i = 0; i < N; ++i) { - if(data[n] > largest1_lwr && n != peak_index) + if(data[i] > largest1_lwr && i != peak_index) { - largest1_lwr = data[n]; - position1_lwr = n; + largest1_lwr = data[i]; + position1_lwr = i; } } @@ -869,44 +853,7 @@ int xtract_nonzero_count(const double *data, const int N, const void *argv, doub peak_index * 0.6 && ratio1 > 0.1) peak_index = position1_lwr; - *result = sr / (double)peak_index; - - free(coeffs2); - free(coeffs3); - free(product); - - return XTRACT_SUCCESS; -}*/ - -int xtract_hps(const double *data, const int N, const void *argv, double *result) -{ - int numBins, numBinsToUse, i, maxIndex; - double tempProduct, currentMax; - - numBins = N / 2; - - numBinsToUse = ceil(numBins / 3.0); - - if (numBinsToUse <= 1) - { - /* Input data is too short. */ - *result = 0; - return XTRACT_NO_RESULT; - } - - tempProduct = currentMax = 0; - for (i = 0; i < numBinsToUse; ++i) - { - tempProduct = data [i] * data [i * 2] * data [i * 3]; - - if (tempProduct > currentMax) - { - currentMax = tempProduct; - maxIndex = i; - } - } - - *result = data [numBins + maxIndex]; + *result = data [n + peak_index]; return XTRACT_SUCCESS; } |