From 4e623f07d088fb98edb6595bf315ab4026d559b7 Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Sat, 9 Jun 2012 08:16:31 +0000 Subject: Clean code, reorg, ajout licence Audacity, test6 pour la mysterieuse fonction todb_a. Ca n'est pas la courbe de ponderation A, c'est just une conversion en dB pour une puissance. La constante 4.34 est un mystère... c'est le log d'une valeur de ref sûrement, mais j'ai pas trouvé laquelle MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: file:///var/svn/2012-violon-leds/trunk@15 6be1fa4d-33ac-4c33-becc-79fcb3794bb6 --- tests/test5/compute.c | 91 ++++++++------------------------------------------- 1 file changed, 13 insertions(+), 78 deletions(-) (limited to 'tests/test5/compute.c') diff --git a/tests/test5/compute.c b/tests/test5/compute.c index 3a94efe..e8b03fc 100644 --- a/tests/test5/compute.c +++ b/tests/test5/compute.c @@ -4,6 +4,9 @@ #include #define MAX_SAMPLES 2048 +static inline float todB_a(const float *x); +void compute_spectrum(float * data, int width, double rate, float *output); + gfloat compute_level(const float *data, size_t nsamples, size_t nchan) { @@ -32,22 +35,26 @@ gfloat compute_level(const float *data, size_t nsamples, size_t nchan) { } // printf("\n"); - compute_spectrom(input, nsamples, rate, output); + compute_spectrum(input, nsamples, rate, output); + //printf("%f\n", output[0]); level=0.f; for (i=1;i<128;i++) { level+=output[i]; } level/=127.f; - //printf("%f\n", level); return level; } -// From Audacity -void compute_spectrom(float * data, int width, double rate, float *output) { +static inline float todB_a(const float *x){ + return (float)((*(int32_t *)x)&0x7fffffff) * 7.17711438e-7f -764.6161886f; +} + +// Adapted from Audacity +void compute_spectrum(float * data, int width, double rate, float *output) { int i; float processed[256]={0.0f}; -//TODO : remove init here +//TODO : remove init here(handy for step by step debug) float in[256]={0.0f}; float out[256]={0.0f}; @@ -57,7 +64,7 @@ void compute_spectrom(float * data, int width, double rate, float *output) { for (i=0; i<256; i++) in[i] = data[start + i]; - // Hanning + // Windowing : Hanning for (i=0; i<256; i++) in[i] *= 0.50 - 0.50 * cos(2 * M_PI * i / (256 - 1)); @@ -83,78 +90,6 @@ void compute_spectrom(float * data, int width, double rate, float *output) { output[i] = processed[i]; } -/* - * PowerSpectrum - * - * This function computes the same as RealFFT, above, but - * adds the squares of the real and imaginary part of each - * coefficient, extracting the power and throwing away the - * phase. - * - * For speed, it does not call RealFFT, but duplicates some - * of its code. - */ - -void PowerSpectrum(float *In, float *Out) -{ - int i; - - float theta = M_PI / 128; - - float tmpReal[128]; - float tmpImag[128]; - float RealOut[128]; - float ImagOut[128]; - - for (i = 0; i < 128; i++) { - tmpReal[i] = In[2 * i]; - tmpImag[i] = In[2 * i + 1]; //FIXME : WTFFFF ? - tmpImag[i]=0; - } - - FFT(128, 0, tmpReal, tmpImag, RealOut, ImagOut); - - float wtemp = sin(0.5 * theta); - - float wpr = -2.0 * wtemp * wtemp; - float wpi = -1.0 * sin(theta); - float wr = 1.0 + wpr; - float wi = wpi; - - int i3; - - float h1r, h1i, h2r, h2i, rt, it; - for (i = 1; i < 128 / 2; i++) { - - i3 = 128 - i; - - h1r = 0.5 * (RealOut[i] + RealOut[i3]); - h1i = 0.5 * (ImagOut[i] - ImagOut[i3]); - h2r = 0.5 * (ImagOut[i] + ImagOut[i3]); - h2i = -0.5 * (RealOut[i] - RealOut[i3]); - - rt = h1r + wr * h2r - wi * h2i; - it = h1i + wr * h2i + wi * h2r; - - Out[i] = rt * rt + it * it; - - rt = h1r - wr * h2r + wi * h2i; - it = -h1i + wr * h2i + wi * h2r; - - Out[i3] = rt * rt + it * it; - - wr = (wtemp = wr) * wpr - wi * wpi + wr; - wi = wi * wpr + wtemp * wpi + wi; - } - - rt = (h1r = RealOut[0]) + ImagOut[0]; - it = h1r - ImagOut[0]; - Out[0] = rt * rt + it * it; - rt = RealOut[128 / 2]; - it = ImagOut[128 / 2]; - Out[128 / 2] = rt * rt + it * it; -} - void audio2hsv_1(gint audio_level, gint *light_h, gint *light_s, gint *light_v) { // Dummy code *light_h=-audio_level; -- cgit v1.2.3