From f669d29aa97931edf1eb5f2f06af834492893076 Mon Sep 17 00:00:00 2001 From: Sean Enderby Date: Mon, 17 Feb 2014 11:20:59 +0000 Subject: quick fix to xtract_irregularity_j and a sort of working xtract_hps --- src/scalar.c | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) (limited to 'src/scalar.c') diff --git a/src/scalar.c b/src/scalar.c index 07dc77b..b7d3806 100644 --- a/src/scalar.c +++ b/src/scalar.c @@ -283,7 +283,7 @@ int xtract_irregularity_k(const double *data, const int N, const void *argv, dou int xtract_irregularity_j(const double *data, const int N, const void *argv, double *result) { - int n = N; + int n = N - 1; double num = 0.0, den = 0.0; @@ -808,7 +808,7 @@ 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 = N, M, m, l, peak_index, position1_lwr; @@ -876,8 +876,40 @@ int xtract_hps(const double *data, const int N, const void *argv, double *result 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]; + + return XTRACT_SUCCESS; +} int xtract_f0(const double *data, const int N, const void *argv, double *result) { -- cgit v1.2.3 From 5e3bd37d8f85d8bf1cb6bf9436c24b83dfad89c6 Mon Sep 17 00:00:00 2001 From: Sean Enderby Date: Mon, 17 Feb 2014 12:58:28 +0000 Subject: xtract_hps, made it more in the style of the library and octave error correction bit --- src/scalar.c | 95 ++++++++++++++---------------------------------------------- 1 file changed, 21 insertions(+), 74 deletions(-) (limited to 'src/scalar.c') 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; } -- cgit v1.2.3