From 04e68c262f042093ba2f9366f87871d0e4b2a76a Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Thu, 8 Sep 2011 13:44:43 +0200 Subject: [PATCH] moved window functions into own files --- Makefile | 2 +- decode.c | 43 +------------------------------------------ window.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ window.h | 12 ++++++++++++ 4 files changed, 60 insertions(+), 43 deletions(-) create mode 100644 window.c create mode 100644 window.h diff --git a/Makefile b/Makefile index b88ec41..105447f 100644 --- a/Makefile +++ b/Makefile @@ -22,5 +22,5 @@ clean: encode: encode.o mmap_file.o -decode: decode.o mmap_file.o pcm.o wav.o alsa.o +decode: decode.o mmap_file.o pcm.o wav.o alsa.o window.o diff --git a/decode.c b/decode.c index 2707bb2..0c8d39e 100644 --- a/decode.c +++ b/decode.c @@ -8,6 +8,7 @@ #include #include "mmap_file.h" #include "pcm.h" +#include "window.h" float lerp(float a, float b, float x) { @@ -30,48 +31,6 @@ float limit(float min, float max, float x) return tmp > max ? max : tmp; } -float sinc(float x) -{ - return 0 == x ? 1.0 : sinf(M_PI * x) / (M_PI * x); -} -float hann(float n, float N) -{ - return 0.5 * (1.0 - cosf(2.0 * M_PI * n / (N - 1.0))); -} -float hamming(float n, float N) -{ - return 0.54 - 0.46 * cosf(2.0 * M_PI * n / (N - 1.0)); -} -float lanczos(float n, float N) -{ - return sinc(2.0 * n / (N - 1.0) - 1.0); -} -float gauss(float n, float N) -{ - float o = 0.35; - return expf(- 1.0/2.0 * powf((n - (N - 1.0) / 2.0) / (o * (N - 1.0) / 2.0), 2.0)); -} - -float i0f(float x) -{ - // converges for -3*M_PI:3*M_PI in less than 20 iterations - float sum = 1.0, val = 1.0, c = 0.0; - for (int n = 1; n < 20; n++) { - float tmp = x / (2 * n); - val *= tmp * tmp; - float y = val - c; - float t = sum + y; - c = (t - sum) - y; - sum = t; - } - return sum; -} -float kaiser(float n, float N) -{ - float a = 2.0; - return i0f(M_PI * a * sqrtf(1.0 - powf((2.0 * n) / (N - 1.0) - 1.0, 2.0))) / i0f(M_PI * a); -} - typedef struct { float complex *b; float *s; diff --git a/window.c b/window.c new file mode 100644 index 0000000..2bd42ef --- /dev/null +++ b/window.c @@ -0,0 +1,46 @@ + +#include +#include "window.h" + +float sinc(float x) +{ + return 0 == x ? 1.0 : sinf(M_PI * x) / (M_PI * x); +} +float hann(float n, float N) +{ + return 0.5 * (1.0 - cosf(2.0 * M_PI * n / (N - 1.0))); +} +float hamming(float n, float N) +{ + return 0.54 - 0.46 * cosf(2.0 * M_PI * n / (N - 1.0)); +} +float lanczos(float n, float N) +{ + return sinc(2.0 * n / (N - 1.0) - 1.0); +} +float gauss(float n, float N) +{ + float o = 0.35; + return expf(- 1.0/2.0 * powf((n - (N - 1.0) / 2.0) / (o * (N - 1.0) / 2.0), 2.0)); +} + +float i0f(float x) +{ + // converges for -3*M_PI:3*M_PI in less than 20 iterations + float sum = 1.0, val = 1.0, c = 0.0; + for (int n = 1; n < 20; n++) { + float tmp = x / (2 * n); + val *= tmp * tmp; + float y = val - c; + float t = sum + y; + c = (t - sum) - y; + sum = t; + } + return sum; +} +float kaiser(float n, float N) +{ + float a = 2.0; + return i0f(M_PI * a * sqrtf(1.0 - powf((2.0 * n) / (N - 1.0) - 1.0, 2.0))) / i0f(M_PI * a); +} + diff --git a/window.h b/window.h new file mode 100644 index 0000000..53f51d7 --- /dev/null +++ b/window.h @@ -0,0 +1,12 @@ + +#ifndef WINDOW_H +#define WINDOW_H +float sinc(float); +float hann(float, float); +float hamming(float, float); +float lanczos(float, float); +float gauss(float, float); +float kaiser(float, float); + +#endif +