aboutsummaryrefslogtreecommitdiff
path: root/src/vector.c
blob: 7968e74a566e5be75acb3bdedc489407435bc048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* libxtract feature extraction library
 *  
 * Copyright (C) 2006 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.
 *
 * 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.
 *
 * 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.
 */


/* xtract_vector.c: defines functions that extract a feature as a single value from an input vector */

#include "xtract/libxtract.h"
#include <math.h>
#include <string.h>
#include <stdlib.h>

#ifdef XTRACT_FFT

#include <fftw3.h>

int xtract_magnitude_spectrum(const float *data, const int N, const void *argv, float *result){

    float *temp, *input, q, sr;
    size_t bytes;
    int n , M = N >> 1;
    fftwf_plan plan;

    temp = (float *)fftwf_malloc(N * sizeof(float));
    input = (float *)malloc(bytes = N * sizeof(float));
    input = memcpy(input, data, bytes);

    q = sr = 0.f;

    sr = *(float *)argv;

    CHECK_SR;

    q = (sr * .5) / M;

    plan = fftwf_plan_r2r_1d(N, input, temp, FFTW_R2HC, FFTW_ESTIMATE);
    
    fftwf_execute(plan);
    
    for(n = 1; n < M; n++){
        result[M + n] = sqrt(SQ(temp[n]) + SQ(temp[N - n])) / N;
        result[n] = n * q;
    }
    
    result[M] = fabs(temp[0]) / N;
    result[0] = q * .5;
    
    fftwf_destroy_plan(plan);
    fftwf_free(temp);
    free(input);
    
    return SUCCESS;
}

int xtract_autocorrelation_fft(const float *data, const int N, const void *argv, float *result){
    
    float *temp, *input;
    size_t bytes;
    int n;
    fftwf_plan plan;

    temp = (float *)fftwf_malloc(N * sizeof(float));
    input = (float *)malloc(bytes = N * sizeof(float));
    input = memcpy(input, data, bytes);

    plan = fftwf_plan_r2r_1d(N, input, temp, FFTW_HC2R, FFTW_ESTIMATE);

    fftwf_execute(plan);
    
    for(n = 0; n < N - 1; n++)
        result[n] = temp[n+1];
    
    fftwf_destroy_plan(plan);
    fftwf_free(temp);
    free(input);

    return SUCCESS;
}

int xtract_mfcc(const float *data, const int N, const void *argv, float *result){

    xtract_mel_filter *f;
    float *input;
    size_t bytes;
    int n, filter;

    f = (xtract_mel_filter *)argv;
    
    input = (float *)malloc(bytes = N * sizeof(float));
    input = memcpy(input, data, bytes);
    
    for(filter = 0; filter < f->n_filters; filter++){
        for(n = 0; n < N; n++){
            result[filter] += input[n] * f->filters[filter][n];
        }
        if(result[filter] < LOG_LIMIT) result[filter] = LOG_LIMIT;
        result[filter] = log(result[filter]);
    }

    for(n = filter + 1; n < N; n++) result[n] = 0; 
    
    xtract_dct(result, f->n_filters, NULL, result);

    free(input);
    
    return SUCCESS;
}

int xtract_dct(const float *data, const int N, const void *argv, float *result){
    
    fftwf_plan plan;
    float *input;
    size_t bytes;

    input = (float *)malloc(bytes = N * sizeof(float));
    input = memcpy(input, data, bytes);
    
    plan = 
        fftwf_plan_r2r_1d(N, input, result, FFTW_REDFT00, FFTW_ESTIMATE);
    
    fftwf_execute(plan);
    fftwf_destroy_plan(plan);
    free(input);

    return SUCCESS;
}

#else

int xtract_magnitude_spectrum(const float *data, const int N, const void *argv, float *result){

    NEEDS_FFTW;

}

int xtract_autocorrelation_fft(const float *data, const int N, const void *argv, float *result){

    NEEDS_FFTW;

}

int xtract_mfcc(const float *data, const int N, const void *argv, float *result){

    NEEDS_FFTW;

}

int xtract_dct(const float *data, const int N, const void *argv, float *result){

    NEEDS_FFTW;

}

#endif

int xtract_autocorrelation(const float *data, const int N, const void *argv, float *result){

    /* Naive time domain implementation  */
    
    int n = N, i;
    
    float corr;

    while(n--){
       corr = 0;
        for(i = 0; i < N - n; i++){
            corr += data[i] * data[i + n];
        }
        result[n] = corr / N;
    }

    return SUCCESS;
}

int xtract_amdf(const float *data, const int N, const void *argv, float *result){

    int n = N, i;
    
    float md, temp;

    while(n--){
       md = 0;
        for(i = 0; i < N - n; i++){
            temp = data[i] - data[i + n];
			temp = (temp < 0 ? -temp : temp);
			md += temp;
        }
        result[n] = md / N;
    }

    return SUCCESS;
}

int xtract_asdf(const float *data, const int N, const void *argv, float *result){
    
    int n = N, i;
    
    float sd;

    while(n--){
       sd = 0;
        for(i = 0; i < N - n; i++){
            /*sd = 1;*/
            sd += SQ(data[i] - data[i + n]);
        }
        result[n] = sd / N;
    }

    return SUCCESS;
}

int xtract_bark_coefficients(const float *data, const int N, const void *argv, float *result){

    int *limits, band, n;

    limits = (int *)argv;
    
    for(band = 0; band < BARK_BANDS; band++){
        for(n = limits[band]; n < limits[band + 1]; n++)
            result[band] += data[n];
    }

    return SUCCESS;
}

int xtract_peaks(const float *data, const int N, const void *argv, float *result){

    float thresh, max, y, y2, 
	  y3, p, width, sr,
	  *input = NULL;
    size_t bytes;
    int n = N, M, rv = SUCCESS;

    thresh = max = y = y2 = y3 = p = width = sr = 0.f;
    
    if(argv != NULL){
        thresh = ((float *)argv)[0];
        sr = ((float *)argv)[1];
    }
    else
        rv = BAD_ARGV;

    if(thresh < 0 || thresh > 100){
        thresh = 0;
        rv = BAD_ARGV;
    }

    CHECK_SR;

    input = (float *)malloc(bytes = N * sizeof(float));

    if(input != NULL)
	input = memcpy(input, data, bytes);
    else
	return MALLOC_FAILED;

    M = N >> 1;
    width = sr / N;

    while(n--)
        max = MAX(max, input[n]);
    
    thresh *= .01 * max;

    result[0] = 0;
    result[M] = 0;

    for(n = 1; n < M; n++){
        if(input[n] >= thresh){
            if(input[n] > input[n - 1] && input[n] > input[n + 1]){
                result[n] = width * (n + (p = .5 * (y = input[n-1] - (y3 = input[n+1])) / (input[n - 1] - 2 * (y2 = input[n]) + input[n + 1])));
                result[M + n] = y2 - .25 * (y - y3) * p;
            }
            else{
                result[n] = 0;
                result[M + n] = 0;
            }
        }
        else{
            result[n] = 0;
            result[M + n] = 0;
        }
    }	  
   
    free(input);
    return (rv ? rv : SUCCESS);
}
	    
int xtract_harmonics(const float *data, const int N, const void *argv, float *result){
    
    int n = (N >> 1), M = n; 

    const float *freqs, *amps;
    float f0, thresh, ratio, nearest, distance;

    freqs = data;
    amps = data + n;
    f0 = *((float *)argv);
    thresh = *((float *)argv+1);

    ratio = nearest = distance = 0.f;

    while(n--){
	if(freqs[n]){
	    ratio = freqs[n] / f0;
	    nearest = round(ratio);
	    distance = fabs(nearest - ratio);
	    if(distance > thresh)
		result[n] = result[M + n] = 0.f;
	    else {
		result[n] = freqs[n];
		result[M + n] = amps[n];
	    }
	}
	else
	    result[n] = result[M + n] = 0.f;
    }
    return SUCCESS;
}