aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Bullock <jamie@jamiebullock.com>2014-06-03 21:17:51 +0100
committerJamie Bullock <jamie@jamiebullock.com>2014-06-03 21:18:22 +0100
commit78a4b5718ecfa7cb91457bc9b4deff543ec10373 (patch)
tree6d2592547c45fbc2027998a564b5b7a1a1c8bd07
parentc866e1676d423ca1c6d4aa859dd54df487ab25d4 (diff)
downloadLibXtract-78a4b5718ecfa7cb91457bc9b4deff543ec10373.tar.gz
LibXtract-78a4b5718ecfa7cb91457bc9b4deff543ec10373.tar.bz2
LibXtract-78a4b5718ecfa7cb91457bc9b4deff543ec10373.zip
Add xtract_peak() determines whether the 'current' value is a peak
-rw-r--r--src/scalar.c33
-rw-r--r--xtract/libxtract.h1
-rw-r--r--xtract/xtract_scalar.h14
3 files changed, 48 insertions, 0 deletions
diff --git a/src/scalar.c b/src/scalar.c
index e0d2578..54cb459 100644
--- a/src/scalar.c
+++ b/src/scalar.c
@@ -1002,3 +1002,36 @@ int xtract_midicent(const double *data, const int N, const void *argv, double *r
return XTRACT_SUCCESS;
}
+int xtract_peak_picker(const double *data, const int N, const void *argv, double *result)
+{
+ double threshold = *(double *)argv;
+ double current = data[N - 1];
+ double average = 0.0;
+ double maximum = -DBL_MAX;
+
+ for (uint32_t n = 0; n < N; ++n)
+ {
+ average += data[n];
+ if (data[n] > maximum)
+ {
+ maximum = data[n];
+ }
+ }
+
+ average /= (double)N;
+
+ if (current != maximum)
+ {
+ return XTRACT_NO_RESULT;
+ }
+
+ if (current < average + threshold)
+ {
+ return XTRACT_NO_RESULT;
+ }
+
+ return XTRACT_SUCCESS;
+
+}
+
+
diff --git a/xtract/libxtract.h b/xtract/libxtract.h
index 1f4a787..8c20980 100644
--- a/xtract/libxtract.h
+++ b/xtract/libxtract.h
@@ -172,6 +172,7 @@ enum xtract_return_codes_ {
XTRACT_MALLOC_FAILED,
XTRACT_BAD_ARGV,
XTRACT_BAD_VECTOR_SIZE,
+ XTRACT_BAD_STATE,
XTRACT_DENORMAL_FOUND,
XTRACT_NO_RESULT, /* This usually occurs when the correct calculation cannot take place because required data is missing or would result in a NaN or infinity/-infinity. Under these curcumstances 0.f is usually given by *result */
XTRACT_FEATURE_NOT_IMPLEMENTED,
diff --git a/xtract/xtract_scalar.h b/xtract/xtract_scalar.h
index 4e39fd9..1c79cc9 100644
--- a/xtract/xtract_scalar.h
+++ b/xtract/xtract_scalar.h
@@ -457,6 +457,20 @@ int xtract_midicent(const double *data, const int N, const void *argv, double *r
*/
int xtract_nonzero_count(const double *data, const int N, const void *argv, double *result);
+/**
+ * \brief Return XTRACT_SUCCESS if the 'current' value is considered a peak
+ *
+ * @param data a pointer to an array containing time series as provided by *result from xtract_last_n() where the Nth value is considered the 'current' value
+ * @param N an integer representing the number of elements in the time series
+ * @param argv a pointer to a double representing the threshold, whereby the current value will be considered a peak if it is above the average of the last N values (*data) by the threshold
+ * @param result a pointer to a copy of the current value if the current value is considered a peak
+ *
+ *
+ * @return XTRACT_SUCCESS if a peak was found or XTRACT_NO_RESULT if not
+ */
+int xtract_peak(const double *data, const int N, const void *argv, double *result);
+
+
/** @} */
#ifdef __cplusplus