jamie@141: /* jamie@141: * Copyright (C) 2012 Jamie Bullock jamie@140: * jamie@141: * Permission is hereby granted, free of charge, to any person obtaining a copy jamie@141: * of this software and associated documentation files (the "Software"), to jamie@141: * deal in the Software without restriction, including without limitation the jamie@141: * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or jamie@141: * sell copies of the Software, and to permit persons to whom the Software is jamie@141: * furnished to do so, subject to the following conditions: jamie@107: * jamie@141: * The above copyright notice and this permission notice shall be included in jamie@141: * all copies or substantial portions of the Software. jamie@107: * jamie@141: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR jamie@141: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, jamie@141: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE jamie@141: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER jamie@141: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING jamie@141: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS jamie@141: * IN THE SOFTWARE. jamie@107: * jamie@107: */ jamie@107: jamie@107: /* helper.c: helper functions. */ jamie@107: jamie@113: #include jamie@113: jamie@259: #include "xtract/libxtract.h" jamie@107: jamie@113: #ifdef WORDS_BIGENDIAN jamie@113: #define INDEX 0 jamie@113: #else jamie@113: #define INDEX 1 jamie@113: #endif jamie@113: jamie@146: int xtract_windowed(const double *data, const int N, const void *argv, double *result) jamie@140: { jamie@107: jamie@107: int n; jamie@146: const double *window; jamie@107: jamie@107: n = N; jamie@146: window = (const double *)argv; jamie@107: jamie@107: while(n--) jamie@107: result[n] = data[n] * window[n]; jamie@107: jamie@107: return XTRACT_SUCCESS; jamie@107: jamie@107: } jamie@107: jamie@146: int xtract_features_from_subframes(const double *data, const int N, const int feature, const void *argv, double *result) jamie@140: { jamie@107: jamie@146: const double *frame1, jamie@107: *frame2; jamie@146: double *result1, jamie@107: *result2; jamie@107: jamie@113: int n, rv; jamie@107: jamie@107: n = N >> 1; jamie@107: jamie@107: frame1 = data; jamie@107: frame2 = data + n; jamie@107: result1 = result; jamie@107: result2 = result + n; jamie@107: jamie@107: rv = xtract[feature](frame1, n, argv, result1); jamie@107: jamie@107: if(rv == XTRACT_SUCCESS) jamie@107: rv = xtract[feature](frame2, n, argv, result2); jamie@107: jamie@107: return rv; jamie@107: jamie@107: } jamie@113: jamie@244: jamie@244: /* jamie@244: * Implements y[n] = k * x[n] + (1-k) * y[n-1] jamie@244: */ jamie@244: int xtract_smoothed(const double *data, const int N, const void *argv, double *result) jamie@244: { jamie@244: double gain = *(double *)argv; jamie@244: double oneminusgain = 1.0 - gain; jamie@244: int i; jamie@244: jamie@244: // reverse filtering first jamie@244: for (i = N - 2; i >= 0; i--) jamie@244: { jamie@244: result[i] = gain * data[i] + oneminusgain * data[i+1]; jamie@244: } jamie@244: jamie@244: // then forward filtering jamie@244: for (i = 1; i < N; i++) jamie@244: { jamie@244: result[i] = gain * result[i] + oneminusgain * result[i-1]; jamie@244: } jamie@244: jamie@244: return XTRACT_SUCCESS; jamie@244: } jamie@244: jamie@244: andrea@211: //inline int xtract_is_denormal(double const d) andrea@211: int xtract_is_denormal(double const d) jamie@140: { jamie@113: if(sizeof(d) != 2 * sizeof(int)) jamie@113: fprintf(stderr, "libxtract: Error: xtract_is_denormal() detects inconsistent wordlength for type 'double'\n"); jamie@113: jamie@113: int l = ((int *)&d)[INDEX]; jamie@113: return (l&0x7ff00000) == 0 && d!=0; //Check for 0 may not be necessary jamie@113: } jamie@113: andrea@211: //inline bool xtract_is_poweroftwo(unsigned int x) andrea@211: bool xtract_is_poweroftwo(unsigned int x) jamie@140: { jamie@140: return ((x != 0) && !(x & (x - 1))); jamie@140: } jamie@140: