aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/scalar.c63
-rw-r--r--xtract/xtract_scalar.h6
2 files changed, 23 insertions, 46 deletions
diff --git a/src/scalar.c b/src/scalar.c
index 07dc77b..2e93934 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;
@@ -810,56 +810,40 @@ 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 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,16 +853,11 @@ int xtract_hps(const double *data, const int N, const void *argv, double *result
peak_index * 0.6 && ratio1 > 0.1)
peak_index = position1_lwr;
- *result = sr / (double)peak_index;
-
- free(coeffs2);
- free(coeffs3);
- free(product);
+ *result = data [n + peak_index];
return XTRACT_SUCCESS;
}
-
int xtract_f0(const double *data, const int N, const void *argv, double *result)
{
diff --git a/xtract/xtract_scalar.h b/xtract/xtract_scalar.h
index dc0e5cf..e18aea6 100644
--- a/xtract/xtract_scalar.h
+++ b/xtract/xtract_scalar.h
@@ -386,10 +386,8 @@ int xtract_sum(const double *data, const int N, const void *argv, double *result
/** \brief Extract the Pitch of an input vector using Harmonic Product Spectrum (HPS) analysis
*
- * \warning {This function doesn't work properly}
- *
- * \param *data: a pointer to the first element in an array of doubles representing the magnitude coefficients from the spectrum of an audio vector, (e.g. the first half of the array pointed to by *result from xtract_spectrum().
- * \param N: the number of elements to be considered
+ * \param *data: a pointer to the first element in an array of doubles representing the spectrum of an audio vector (e.g. *result from xtract_spectrum). It is expected that the first half of the array pointed to by *data will contain amplitudes for each frequecy bin, and the second half will contain the respective frequencies
+ * \param N: The length of the vector pointed to by *data.
* \param *argv: a pointer to NULL
* \param *result: the pitch of N values from the array pointed to by *data
*/