Mercurial > hg > libxtract
comparison src/vector.c @ 171:661c1c732269
Copy out input values for Ooura so the in-place transform doesn't overwrite *data
author | Jamie Bullock <jamie@jamiebullock.com> |
---|---|
date | Tue, 18 Jun 2013 13:21:02 -0700 |
parents | 71870680f7c1 |
children | a44feda65b99 |
comparison
equal
deleted
inserted
replaced
170:251df503d0fe | 171:661c1c732269 |
---|---|
24 /* xtract_vector.c: defines functions that extract a feature as a single value from an input vector */ | 24 /* xtract_vector.c: defines functions that extract a feature as a single value from an input vector */ |
25 | 25 |
26 #include <math.h> | 26 #include <math.h> |
27 #include <string.h> | 27 #include <string.h> |
28 #include <stdlib.h> | 28 #include <stdlib.h> |
29 #include <assert.h> | |
29 | 30 |
30 #include "fft.h" | 31 #include "fft.h" |
31 | 32 |
32 #include "../xtract/libxtract.h" | 33 #include "../xtract/libxtract.h" |
33 #include "xtract_macros_private.h" | 34 #include "xtract_macros_private.h" |
51 double real = 0.0; | 52 double real = 0.0; |
52 double imag = 0.0; | 53 double imag = 0.0; |
53 unsigned int n = 0; | 54 unsigned int n = 0; |
54 unsigned int m = 0; | 55 unsigned int m = 0; |
55 unsigned int M = N >> 1; | 56 unsigned int M = N >> 1; |
56 #ifndef USE_OOURA | 57 #ifdef USE_OOURA |
58 double *fft = NULL; | |
59 #else | |
57 DSPDoubleSplitComplex *fft = NULL; | 60 DSPDoubleSplitComplex *fft = NULL; |
58 #endif | 61 #endif |
59 | 62 |
60 q = *(double *)argv; | 63 q = *(double *)argv; |
61 vector = (int)*((double *)argv+1); | 64 vector = (int)*((double *)argv+1); |
78 #ifdef USE_OOURA | 81 #ifdef USE_OOURA |
79 /* ooura is in-place | 82 /* ooura is in-place |
80 * the output format is | 83 * the output format is |
81 * a[0] - DC, a[1] - nyquist, a[2...N-1] - remaining bins | 84 * a[0] - DC, a[1] - nyquist, a[2...N-1] - remaining bins |
82 */ | 85 */ |
83 rdft(N, 1, data, ooura_data_spectrum.ooura_ip, | 86 fft = malloc(N * sizeof(double)); |
87 assert(fft != NULL); | |
88 memcpy(fft, data, N * sizeof(double)); | |
89 | |
90 rdft(N, 1, fft, ooura_data_spectrum.ooura_ip, | |
84 ooura_data_spectrum.ooura_w); | 91 ooura_data_spectrum.ooura_w); |
85 #else | 92 #else |
86 fft = &vdsp_data_spectrum.fft; | 93 fft = &vdsp_data_spectrum.fft; |
87 vDSP_ctozD((DSPDoubleComplex *)data, 2, fft, 1, N >> 1); | 94 vDSP_ctozD((DSPDoubleComplex *)data, 2, fft, 1, N >> 1); |
88 vDSP_fft_zripD(vdsp_data_spectrum.setup, fft, 1, | 95 vDSP_fft_zripD(vdsp_data_spectrum.setup, fft, 1, |
108 if(n==1 && withDC) /* discard Nyquist */ | 115 if(n==1 && withDC) /* discard Nyquist */ |
109 { | 116 { |
110 ++n; | 117 ++n; |
111 } | 118 } |
112 | 119 |
113 real = data[n*2]; | 120 real = fft[n*2]; |
114 imag = data[n*2+1]; | 121 imag = fft[n*2+1]; |
115 #else | 122 #else |
116 real = fft->realp[n]; | 123 real = fft->realp[n]; |
117 imag = fft->realp[n]; | 124 imag = fft->realp[n]; |
118 #endif | 125 #endif |
119 | 126 |
153 if(n==1 && withDC) /* discard Nyquist */ | 160 if(n==1 && withDC) /* discard Nyquist */ |
154 { | 161 { |
155 ++n; | 162 ++n; |
156 } | 163 } |
157 | 164 |
158 real = data[n*2]; | 165 real = fft[n*2]; |
159 imag = data[n*2+1]; | 166 imag = fft[n*2+1]; |
160 #else | 167 #else |
161 real = fft->realp[n]; | 168 real = fft->realp[n]; |
162 imag = fft->realp[n]; | 169 imag = fft->realp[n]; |
163 #endif | 170 #endif |
164 | 171 |
184 if(n==1 && withDC) /* discard Nyquist */ | 191 if(n==1 && withDC) /* discard Nyquist */ |
185 { | 192 { |
186 ++n; | 193 ++n; |
187 } | 194 } |
188 | 195 |
189 real = data[n*2]; | 196 real = fft[n*2]; |
190 imag = data[n*2+1]; | 197 imag = fft[n*2+1]; |
191 #else | 198 #else |
192 real = fft->realp[n]; | 199 real = fft->realp[n]; |
193 imag = fft->realp[n]; | 200 imag = fft->realp[n]; |
194 #endif | 201 #endif |
195 | 202 |
222 #ifdef USE_OOURA | 229 #ifdef USE_OOURA |
223 if(n==1 && withDC) /* discard Nyquist */ | 230 if(n==1 && withDC) /* discard Nyquist */ |
224 { | 231 { |
225 ++n; | 232 ++n; |
226 } | 233 } |
227 real = data[n*2]; | 234 real = fft[n*2]; |
228 imag = data[n*2+1]; | 235 imag = fft[n*2+1]; |
229 #else | 236 #else |
230 real = fft->realp[n]; | 237 real = fft->realp[n]; |
231 imag = fft->realp[n]; | 238 imag = fft->realp[n]; |
232 #endif | 239 #endif |
233 *marker = sqrt(XTRACT_SQ(real) + XTRACT_SQ(imag)) / (double)N; | 240 *marker = sqrt(XTRACT_SQ(real) + XTRACT_SQ(imag)) / (double)N; |
241 if(normalise) | 248 if(normalise) |
242 { | 249 { |
243 for(n = 0; n < M; n++) | 250 for(n = 0; n < M; n++) |
244 result[n] /= max; | 251 result[n] /= max; |
245 } | 252 } |
253 | |
254 #ifdef USE_OOURA | |
255 free(fft); | |
256 #endif | |
246 | 257 |
247 return XTRACT_SUCCESS; | 258 return XTRACT_SUCCESS; |
248 } | 259 } |
249 | 260 |
250 int xtract_autocorrelation_fft(const double *data, const int N, const void *argv, double *result) | 261 int xtract_autocorrelation_fft(const double *data, const int N, const void *argv, double *result) |
504 result[n] = 0; | 515 result[n] = 0; |
505 result[N + n] = 0; | 516 result[N + n] = 0; |
506 } | 517 } |
507 } | 518 } |
508 | 519 |
509 free(input); | 520 // free(input); |
510 return (rv ? rv : XTRACT_SUCCESS); | 521 return (rv ? rv : XTRACT_SUCCESS); |
511 } | 522 } |
512 | 523 |
513 int xtract_harmonic_spectrum(const double *data, const int N, const void *argv, double *result) | 524 int xtract_harmonic_spectrum(const double *data, const int N, const void *argv, double *result) |
514 { | 525 { |