diff options
author | Jamie Bullock <jamie@jamiebullock.com> | 2014-06-06 09:55:01 +0100 |
---|---|---|
committer | Jamie Bullock <jamie@jamiebullock.com> | 2014-06-06 09:55:01 +0100 |
commit | e028f0d8204722f01495ec28edadbb4bdf1b6f1b (patch) | |
tree | ebe19aef151d7f34755163db7c83edd64cdb45b3 | |
parent | 0c54ec464079b450e07abe57be680ab88249b76a (diff) | |
download | LibXtract-e028f0d8204722f01495ec28edadbb4bdf1b6f1b.tar.gz LibXtract-e028f0d8204722f01495ec28edadbb4bdf1b6f1b.tar.bz2 LibXtract-e028f0d8204722f01495ec28edadbb4bdf1b6f1b.zip |
Add new helper function xtract_smoothed(), e.g. can be used to extract smoothed spectrum
-rw-r--r-- | examples/simpletest/simpletest.cpp | 11 | ||||
-rw-r--r-- | src/helper.c | 26 | ||||
-rw-r--r-- | src/libxtract.c | 3 | ||||
-rw-r--r-- | xtract/libxtract.h | 5 | ||||
-rw-r--r-- | xtract/xtract_helper.h | 14 |
5 files changed, 54 insertions, 5 deletions
diff --git a/examples/simpletest/simpletest.cpp b/examples/simpletest/simpletest.cpp index 40bcfeb..a0ae8d4 100644 --- a/examples/simpletest/simpletest.cpp +++ b/examples/simpletest/simpletest.cpp @@ -31,6 +31,7 @@ #include "xtract/libxtract.h" #include "xtract/xtract_stateful.h" #include "xtract/xtract_scalar.h" +#include "xtract/xtract_helper.h" #include "WaveFile.h" #ifndef M_PI @@ -50,7 +51,7 @@ waveform_type; #define BLOCKSIZE 512 #define MAVG_COUNT 10 -#define HALF_BLOCKSIZE BLOCKSIZE >> 1 +#define HALF_BLOCKSIZE (BLOCKSIZE >> 1) #define SAMPLERATE 44100 #define PERIOD 102 #define MFCC_FREQ_BANDS 13 @@ -202,7 +203,6 @@ int main(void) xtract_free_fft(); xtract[XTRACT_SPECTRAL_CENTROID](spectrum, BLOCKSIZE, NULL, ¢roid); -// printf("\nSpectral Centroid: %f\t", centroid); argd[1] = 10.0; /* peak threshold as % of maximum peak */ xtract[XTRACT_PEAK_SPECTRUM](spectrum, BLOCKSIZE / 2, argd, peaks); @@ -250,6 +250,13 @@ int main(void) xtract_features_from_subframes(subframes_windowed, BLOCKSIZE, XTRACT_SPECTRUM, argd, subframes_spectrum); xtract_free_fft(); + argd[0] = 0.5; /* smoothing factor */ + + /* smooth the amplitude components of the first and second spectra */ + xtract_smoothed(subframes_spectrum, HALF_BLOCKSIZE >> 1, argd, subframes_spectrum); + xtract_smoothed(subframes_spectrum + HALF_BLOCKSIZE, HALF_BLOCKSIZE >> 1, argd, subframes_spectrum + HALF_BLOCKSIZE); + + /* difference between the two spectra */ xtract_difference_vector(subframes_spectrum, BLOCKSIZE, NULL, difference); argd[0] = .25; /* norm order */ diff --git a/src/helper.c b/src/helper.c index 5b7155f..9b10294 100644 --- a/src/helper.c +++ b/src/helper.c @@ -75,6 +75,32 @@ int xtract_features_from_subframes(const double *data, const int N, const int fe } + +/* + * Implements y[n] = k * x[n] + (1-k) * y[n-1] + */ +int xtract_smoothed(const double *data, const int N, const void *argv, double *result) +{ + double gain = *(double *)argv; + double oneminusgain = 1.0 - gain; + int i; + + // reverse filtering first + for (i = N - 2; i >= 0; i--) + { + result[i] = gain * data[i] + oneminusgain * data[i+1]; + } + + // then forward filtering + for (i = 1; i < N; i++) + { + result[i] = gain * result[i] + oneminusgain * result[i-1]; + } + + return XTRACT_SUCCESS; +} + + //inline int xtract_is_denormal(double const d) int xtract_is_denormal(double const d) { diff --git a/src/libxtract.c b/src/libxtract.c index 624e8cd..168b106 100644 --- a/src/libxtract.c +++ b/src/libxtract.c @@ -90,6 +90,7 @@ int(*xtract[])(const double *, const int, const void *, double *) = xtract_lpcc, xtract_subbands, /* xtract_helper.h */ - xtract_windowed + xtract_windowed, + xtract_smoothed }; diff --git a/xtract/libxtract.h b/xtract/libxtract.h index 8c20980..e7047de 100644 --- a/xtract/libxtract.h +++ b/xtract/libxtract.h @@ -71,7 +71,7 @@ extern "C" { * @{ */ -#define XTRACT_FEATURES 61 +#define XTRACT_FEATURES 62 /** \brief Enumeration of features, elements are used as indixes to an array of pointers to feature extracton functions */ enum xtract_features_ { @@ -137,7 +137,8 @@ enum xtract_features_ { XTRACT_LPCC, XTRACT_SUBBANDS, /* Helper functions */ - XTRACT_WINDOWED + XTRACT_WINDOWED, + XTRACT_SMOOTHED }; /** \brief Enumeration of feature initialisation functions */ diff --git a/xtract/xtract_helper.h b/xtract/xtract_helper.h index 9dabfbc..7296f0e 100644 --- a/xtract/xtract_helper.h +++ b/xtract/xtract_helper.h @@ -83,6 +83,20 @@ int xtract_is_denormal(double const d); /** \brief Test whether a number is a power of two */ bool xtract_is_poweroftwo(unsigned int x); + +/** \brief Smooth a vector + * + * \param *data a pointer to an array of doubles + * \param N the number of elements in the array pointed to by *data to be smoothed + * \param *argv a pointer to a double giving the smoothing gain + * \param *result a pointer to the first element an array containing the smoothed data + * + * \note if passing in a spectrum e.g. *result from xtract_spectrum(), then N for xtract_smoothed() should be N / 2 with respect to the N for xtract_spectrum() so only amplitude components are smoothed, not frequencies! + * + */ + int xtract_smoothed(const double *data, const int N, const void *argv, double *result); + + /** @} */ #ifdef __cplusplus |