diff options
Diffstat (limited to 'src/window.c')
-rw-r--r-- | src/window.c | 130 |
1 files changed, 75 insertions, 55 deletions
diff --git a/src/window.c b/src/window.c index f2ceecc..bce0770 100644 --- a/src/window.c +++ b/src/window.c @@ -1,21 +1,24 @@ -/* libxtract feature extraction library - * - * Copyright (C) 2006 Jamie Bullock +/* + * Copyright (C) 2012 Jamie Bullock * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, - * USA. */ /* window.c: defines window generation functions (formulae courtesy of Wikipedia (http://en.wikipedia.org/wiki/Window_function) */ @@ -24,7 +27,8 @@ #include "xtract_window_private.h" -void gauss(float *window, const int N, const float sd){ +void gauss(float *window, const int N, const float sd) +{ int n; const float M = N - 1; @@ -32,11 +36,12 @@ void gauss(float *window, const int N, const float sd){ den, exponent; - for (n = 0; n < N; n++) { + for (n = 0; n < N; n++) + { num = n - M / 2.f; den = sd * M / 2.f; - + exponent = -0.5 * powf(num / den, 2); window[n] = exp(exponent); @@ -44,7 +49,8 @@ void gauss(float *window, const int N, const float sd){ } } -void hamming(float *window, const int N){ +void hamming(float *window, const int N) +{ int n; const float M = N - 1; @@ -54,7 +60,8 @@ void hamming(float *window, const int N){ } -void hann(float *window, const int N){ +void hann(float *window, const int N) +{ int n; const float M = N - 1; @@ -64,7 +71,8 @@ void hann(float *window, const int N){ } -void bartlett(float *window, const int N){ +void bartlett(float *window, const int N) +{ int n; const float M = N - 1; @@ -74,7 +82,8 @@ void bartlett(float *window, const int N){ } -void triangular(float *window, const int N){ +void triangular(float *window, const int N) +{ int n; const float M = N - 1; @@ -83,17 +92,19 @@ void triangular(float *window, const int N){ window[n] = 2.f / N * (N / 2.f - fabsf(n - M / 2.f)); } -void bartlett_hann(float *window, const int N){ +void bartlett_hann(float *window, const int N) +{ int n; const float M = N - 1, - a0 = 0.62, - a1 = 0.5, - a2 = 0.38; + a0 = 0.62, + a1 = 0.5, + a2 = 0.38; float term1 = 0.f, term2 = 0.f; - for (n = 0; n < N; n++){ + for (n = 0; n < N; n++) + { term1 = a1 * fabsf(n / M - 0.5); term2 = a2 * cosf(2.0 * PI * (float)n / M); @@ -102,18 +113,20 @@ void bartlett_hann(float *window, const int N){ } } -void blackman(float *window, const int N){ +void blackman(float *window, const int N) +{ int n; const float M = N - 1, - a0 = 0.42, - a1 = 0.5, - a2 = 0.08; + a0 = 0.42, + a1 = 0.5, + a2 = 0.08; float term1 = 0.f, term2 = 0.f; - for (n = 0; n < N; n++) { - + for (n = 0; n < N; n++) + { + term1 = a1 * cosf(2.0 * PI * (float)n / M); term2 = a2 * cosf(4.0 * PI * (float)n / M); @@ -124,54 +137,61 @@ void blackman(float *window, const int N){ #define BIZ_EPSILON 1E-21 // Max error acceptable /* Based on code from mplayer window.c, and somewhat beyond me */ -float besselI0(float x){ +float besselI0(float x) +{ - float temp; - float sum = 1.0; - float u = 1.0; - float halfx = x/2.0; - int n = 1; + float temp; + float sum = 1.0; + float u = 1.0; + float halfx = x/2.0; + int n = 1; - do { + do + { - temp = halfx/(float)n; - u *=temp * temp; - sum += u; - n++; + temp = halfx/(float)n; + u *=temp * temp; + sum += u; + n++; - } while (u >= BIZ_EPSILON * sum); + } + while (u >= BIZ_EPSILON * sum); - return(sum); + return(sum); } -void kaiser(float *window, const int N, const float alpha){ +void kaiser(float *window, const int N, const float alpha) +{ int n; const float M = N - 1; float num; - for (n = 0; n < N; n++) { + for (n = 0; n < N; n++) + { num = besselI0(alpha * sqrtf(1.0 - powf((2.0 * n / M - 1), 2))); window[n] = num / besselI0(alpha); - + } } -void blackman_harris(float *window, const int N){ +void blackman_harris(float *window, const int N) +{ int n; const float M = N - 1, - a0 = 0.35875, - a1 = 0.48829, - a2 = 0.14128, - a3 = 0.01168; + a0 = 0.35875, + a1 = 0.48829, + a2 = 0.14128, + a3 = 0.01168; float term1 = 0.f, term2 = 0.f, term3 = 0.f; - for (n = 0; n < N; n++) { + for (n = 0; n < N; n++) + { term1 = a1 * cosf(2.0 * PI * n / M); term2 = a2 * cosf(4.0 * PI * n / M); |