annotate src/helper.c @ 285:89fe52066db1 tip master

MSCV missing ssize_t fix
author Jamie Bullock <jamie@jamiebullock.com>
date Tue, 16 Jul 2019 18:29:20 +0100
parents d383a8c66b5d
children
rev   line source
jamie@141 1 /*
jamie@141 2 * Copyright (C) 2012 Jamie Bullock
jamie@140 3 *
jamie@141 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
jamie@141 5 * of this software and associated documentation files (the "Software"), to
jamie@141 6 * deal in the Software without restriction, including without limitation the
jamie@141 7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
jamie@141 8 * sell copies of the Software, and to permit persons to whom the Software is
jamie@141 9 * furnished to do so, subject to the following conditions:
jamie@107 10 *
jamie@141 11 * The above copyright notice and this permission notice shall be included in
jamie@141 12 * all copies or substantial portions of the Software.
jamie@107 13 *
jamie@141 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jamie@141 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jamie@141 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jamie@141 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jamie@141 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
jamie@141 19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
jamie@141 20 * IN THE SOFTWARE.
jamie@107 21 *
jamie@107 22 */
jamie@107 23
jamie@107 24 /* helper.c: helper functions. */
jamie@107 25
jamie@113 26 #include <stdio.h>
jamie@113 27
jamie@259 28 #include "xtract/libxtract.h"
jamie@107 29
jamie@113 30 #ifdef WORDS_BIGENDIAN
jamie@113 31 #define INDEX 0
jamie@113 32 #else
jamie@113 33 #define INDEX 1
jamie@113 34 #endif
jamie@113 35
jamie@146 36 int xtract_windowed(const double *data, const int N, const void *argv, double *result)
jamie@140 37 {
jamie@107 38
jamie@107 39 int n;
jamie@146 40 const double *window;
jamie@107 41
jamie@107 42 n = N;
jamie@146 43 window = (const double *)argv;
jamie@107 44
jamie@107 45 while(n--)
jamie@107 46 result[n] = data[n] * window[n];
jamie@107 47
jamie@107 48 return XTRACT_SUCCESS;
jamie@107 49
jamie@107 50 }
jamie@107 51
jamie@146 52 int xtract_features_from_subframes(const double *data, const int N, const int feature, const void *argv, double *result)
jamie@140 53 {
jamie@107 54
jamie@146 55 const double *frame1,
jamie@107 56 *frame2;
jamie@146 57 double *result1,
jamie@107 58 *result2;
jamie@107 59
jamie@113 60 int n, rv;
jamie@107 61
jamie@107 62 n = N >> 1;
jamie@107 63
jamie@107 64 frame1 = data;
jamie@107 65 frame2 = data + n;
jamie@107 66 result1 = result;
jamie@107 67 result2 = result + n;
jamie@107 68
jamie@107 69 rv = xtract[feature](frame1, n, argv, result1);
jamie@107 70
jamie@107 71 if(rv == XTRACT_SUCCESS)
jamie@107 72 rv = xtract[feature](frame2, n, argv, result2);
jamie@107 73
jamie@107 74 return rv;
jamie@107 75
jamie@107 76 }
jamie@113 77
jamie@244 78
jamie@244 79 /*
jamie@244 80 * Implements y[n] = k * x[n] + (1-k) * y[n-1]
jamie@244 81 */
jamie@244 82 int xtract_smoothed(const double *data, const int N, const void *argv, double *result)
jamie@244 83 {
jamie@244 84 double gain = *(double *)argv;
jamie@244 85 double oneminusgain = 1.0 - gain;
jamie@244 86 int i;
jamie@244 87
jamie@244 88 // reverse filtering first
jamie@244 89 for (i = N - 2; i >= 0; i--)
jamie@244 90 {
jamie@244 91 result[i] = gain * data[i] + oneminusgain * data[i+1];
jamie@244 92 }
jamie@244 93
jamie@244 94 // then forward filtering
jamie@244 95 for (i = 1; i < N; i++)
jamie@244 96 {
jamie@244 97 result[i] = gain * result[i] + oneminusgain * result[i-1];
jamie@244 98 }
jamie@244 99
jamie@244 100 return XTRACT_SUCCESS;
jamie@244 101 }
jamie@244 102
jamie@244 103
andrea@211 104 //inline int xtract_is_denormal(double const d)
andrea@211 105 int xtract_is_denormal(double const d)
jamie@140 106 {
jamie@113 107 if(sizeof(d) != 2 * sizeof(int))
jamie@113 108 fprintf(stderr, "libxtract: Error: xtract_is_denormal() detects inconsistent wordlength for type 'double'\n");
jamie@113 109
jamie@113 110 int l = ((int *)&d)[INDEX];
jamie@113 111 return (l&0x7ff00000) == 0 && d!=0; //Check for 0 may not be necessary
jamie@113 112 }
jamie@113 113
andrea@211 114 //inline bool xtract_is_poweroftwo(unsigned int x)
andrea@211 115 bool xtract_is_poweroftwo(unsigned int x)
jamie@140 116 {
jamie@140 117 return ((x != 0) && !(x & (x - 1)));
jamie@140 118 }
jamie@140 119