Debian Codegraph @

src/core/util_xforms.c

raw symbols SCIP
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler and Virtual Machine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 U.S.A.
23 -----------------------------------------------------------------------------*/
24
25 //-----------------------------------------------------------------------------
26 // name: util_xform.c
27 // desc: transforms
28 //
29 // authors: Ge Wang ([email protected])
30 // Perry R. Cook ([email protected])
31 // FFT: CARL music distribution
32 // date: 11.27.2003
33 //-----------------------------------------------------------------------------
34 #include "util_xforms.h"
35 #include <stdlib.h>
36 #include <math.h>
37
38
39
40
41 //-----------------------------------------------------------------------------
42 // name: hanning()
43 // desc: make window
44 //-----------------------------------------------------------------------------
45 void hanning( FLOAT_XFORM * window, unsigned long length )
46 {
47 unsigned long i;
48 double pi, phase = 0, delta;
49
50 pi = 4.*atan(1.0);
51 delta = 2 * pi / (double) length;
52
53 for( i = 0; i < length; i++ )
54 {
55 window[i] = (FLOAT_XFORM)(0.5 * (1.0 - cos(phase)));
56 phase += delta;
57 }
58 }
59
60
61
62
63 //-----------------------------------------------------------------------------
64 // name: hamming()
65 // desc: make window
66 //-----------------------------------------------------------------------------
67 void hamming( FLOAT_XFORM * window, unsigned long length )
68 {
69 unsigned long i;
70 double pi, phase = 0, delta;
71
72 pi = 4.*atan(1.0);
73 delta = 2 * pi / (double) length;
74
75 for( i = 0; i < length; i++ )
76 {
77 window[i] = (FLOAT_XFORM)(0.54 - .46*cos(phase));
78 phase += delta;
79 }
80 }
81
82
83
84
85 //-----------------------------------------------------------------------------
86 // name: blackman()
87 // desc: make window
88 //-----------------------------------------------------------------------------
89 void blackman( FLOAT_XFORM * window, unsigned long length )
90 {
91 unsigned long i;
92 double pi, phase = 0, delta;
93
94 pi = 4.*atan(1.0);
95 delta = 2 * pi / (double) length;
96
97 for( i = 0; i < length; i++ )
98 {
99 window[i] = (FLOAT_XFORM)(0.42 - .5*cos(phase) + .08*cos(2*phase));
100 phase += delta;
101 }
102 }
103
104
105
106
107 //-----------------------------------------------------------------------------
108 // name: bartlett()
109 // desc: make window
110 //-----------------------------------------------------------------------------
111 void bartlett( FLOAT_XFORM * window, unsigned long length )
112 {
113 unsigned long i;
114 FLOAT_XFORM half = (FLOAT_XFORM)length / 2;
115
116 for( i = 0; i < length; i++ )
117 {
118 if( i < half ) window[i] = i / half;
119 else window[i] = (length - i) / half;
120 }
121 }
122
123
124
125
126 //-----------------------------------------------------------------------------
127 // name: apply_window()
128 // desc: apply a window to data
129 //-----------------------------------------------------------------------------
130 void apply_window( FLOAT_XFORM * data, FLOAT_XFORM * window, unsigned long length )
131 {
132 unsigned long i;
133
134 for( i = 0; i < length; i++ )
135 data[i] *= window[i];
136 }
137
138 static FLOAT_XFORM PI ;
139 static FLOAT_XFORM TWOPI ;
140 void bit_reverse( FLOAT_XFORM * x, long N );
141
142 //-----------------------------------------------------------------------------
143 // name: rfft()
144 // desc: real value fft
145 //
146 // these routines from the CARL software, spect.c
147 // check out the CARL CMusic distribution for more source code
148 //
149 // if forward is true, rfft replaces 2*N real data points in x with N complex
150 // values representing the positive frequency half of their Fourier spectrum,
151 // with x[1] replaced with the real part of the Nyquist frequency value.
152 //
153 // if forward is false, rfft expects x to contain a positive frequency
154 // spectrum arranged as before, and replaces it with 2*N real values.
155 //
156 // N MUST be a power of 2.
157 //
158 //-----------------------------------------------------------------------------
159 void rfft( FLOAT_XFORM * x, long N, unsigned int forward )
160 {
161 static int first = 1 ;
162 FLOAT_XFORM c1, c2, h1r, h1i, h2r, h2i, wr, wi, wpr, wpi, temp, theta ;
163 FLOAT_XFORM xr, xi ;
164 long i, i1, i2, i3, i4, N2p1 ;
165
166 if( first )
167 {
168 PI = (FLOAT_XFORM) (4.*atan( 1. )) ;
169 TWOPI = (FLOAT_XFORM) (8.*atan( 1. )) ;
170 first = 0 ;
171 }
172
173 theta = PI/N ;
174 wr = 1. ;
175 wi = 0. ;
176 c1 = 0.5 ;
177
178 if( forward )
179 {
180 c2 = -0.5 ;
181 cfft( x, N, forward ) ;
182 xr = x[0] ;
183 xi = x[1] ;
184 }
185 else
186 {
187 c2 = 0.5 ;
188 theta = -theta ;
189 xr = x[1] ;
190 xi = 0. ;
191 x[1] = 0. ;
192 }
193
194 wpr = (FLOAT_XFORM) (-2.*pow( sin( 0.5*theta ), 2. )) ;
195 wpi = (FLOAT_XFORM) sin( theta ) ;
196 N2p1 = (N<<1) + 1 ;
197
198 for( i = 0 ; i <= N>>1 ; i++ )
199 {
200 i1 = i<<1 ;
201 i2 = i1 + 1 ;
202 i3 = N2p1 - i2 ;
203 i4 = i3 + 1 ;
204 if( i == 0 )
205 {
206 h1r = c1*(x[i1] + xr ) ;
207 h1i = c1*(x[i2] - xi ) ;
208 h2r = -c2*(x[i2] + xi ) ;
209 h2i = c2*(x[i1] - xr ) ;
210 x[i1] = h1r + wr*h2r - wi*h2i ;
211 x[i2] = h1i + wr*h2i + wi*h2r ;
212 xr = h1r - wr*h2r + wi*h2i ;
213 xi = -h1i + wr*h2i + wi*h2r ;
214 }
215 else
216 {
217 h1r = c1*(x[i1] + x[i3] ) ;
218 h1i = c1*(x[i2] - x[i4] ) ;
219 h2r = -c2*(x[i2] + x[i4] ) ;
220 h2i = c2*(x[i1] - x[i3] ) ;
221 x[i1] = h1r + wr*h2r - wi*h2i ;
222 x[i2] = h1i + wr*h2i + wi*h2r ;
223 x[i3] = h1r - wr*h2r + wi*h2i ;
224 x[i4] = -h1i + wr*h2i + wi*h2r ;
225 }
226
227 wr = (temp = wr)*wpr - wi*wpi + wr ;
228 wi = wi*wpr + temp*wpi + wi ;
229 }
230
231 if( forward )
232 x[1] = xr ;
233 else
234 cfft( x, N, forward ) ;
235 }
236
237
238
239
240 //-----------------------------------------------------------------------------
241 // name: cfft()
242 // desc: complex value fft
243 //
244 // these routines from CARL software, spect.c
245 // check out the CARL CMusic distribution for more software
246 //
247 // cfft replaces FLOAT_XFORM array x containing NC complex values
248 // (2*NC FLOAT_XFORM values alternating real, imagininary, etc.)
249 // by its Fourier transform; if forward is true, or by its inverse
250 // Fourier transform ifforward is false, using a recursive Fast Fourier
251 // transform method due to Danielson and Lanczos.
252 //
253 // NC MUST be a power of 2.
254 //-----------------------------------------------------------------------------
255 void cfft( FLOAT_XFORM * x, long NC, unsigned int forward )
256 {
257 FLOAT_XFORM wr, wi, wpr, wpi, theta, scale ;
258 long mmax, ND, m, i, j, delta ;
259 ND = NC<<1 ;
260 bit_reverse( x, ND ) ;
261
262 for( mmax = 2 ; mmax < ND ; mmax = delta )
263 {
264 delta = mmax<<1 ;
265 theta = TWOPI/( forward? mmax : -mmax ) ;
266 wpr = (FLOAT_XFORM) (-2.*pow( sin( 0.5*theta ), 2. )) ;
267 wpi = (FLOAT_XFORM) sin( theta ) ;
268 wr = 1. ;
269 wi = 0. ;
270
271 for( m = 0 ; m < mmax ; m += 2 )
272 {
273 /* register */ FLOAT_XFORM rtemp, itemp ;
274 for( i = m ; i < ND ; i += delta )
275 {
276 j = i + mmax ;
277 rtemp = wr*x[j] - wi*x[j+1] ;
278 itemp = wr*x[j+1] + wi*x[j] ;
279 x[j] = x[i] - rtemp ;
280 x[j+1] = x[i+1] - itemp ;
281 x[i] += rtemp ;
282 x[i+1] += itemp ;
283 }
284
285 wr = (rtemp = wr)*wpr - wi*wpi + wr ;
286 wi = wi*wpr + rtemp*wpi + wi ;
287 }
288 }
289
290 // scale output
291 scale = (FLOAT_XFORM)(forward ? 1./ND : 2.) ;
292 {
293 /* register */ FLOAT_XFORM *xi=x, *xe=x+ND ;
294 while( xi < xe )
295 *xi++ *= scale ;
296 }
297 }
298
299
300
301
302 //-----------------------------------------------------------------------------
303 // name: bit_reverse()
304 // desc: bitreverse places FLOAT_XFORM array x containing N/2 complex values
305 // into bit-reversed order
306 //-----------------------------------------------------------------------------
307 void bit_reverse( FLOAT_XFORM * x, long N )
308 {
309 FLOAT_XFORM rtemp, itemp ;
310 long i, j, m ;
311 for( i = j = 0 ; i < N ; i += 2, j += m )
312 {
313 if( j > i )
314 {
315 rtemp = x[j] ; itemp = x[j+1] ; /* complex exchange */
316 x[j] = x[i] ; x[j+1] = x[i+1] ;
317 x[i] = rtemp ; x[i+1] = itemp ;
318 }
319
320 for( m = N>>1 ; m >= 2 && j >= m ; m >>= 1 )
321 j -= m ;
322 }
323 }
324
325
326
327
328 //-----------------------------------------------------------------------------
329 // name: the_dct()
330 // desc: type ii dct on N reals
331 //-----------------------------------------------------------------------------
332 void the_dct( FLOAT_XFORM * x, unsigned long N, FLOAT_XFORM * out, unsigned long Nout )
333 {
334 unsigned long k, n;
335
336 // sanity check
337 assert( Nout <= N );
338 // zero out
339 memset( out, 0, sizeof(FLOAT_XFORM)*Nout );
340
341 // go for it
342 for( k = 0; k < Nout; k++ )
343 {
344 for( n = 0; n < N; n++ )
345 {
346 out[k] += x[n] * cos( CK_ONE_PI / N * k * (n + .5) );
347 }
348 }
349 }
350
351
352
353
354
355 //-----------------------------------------------------------------------------
356 // name: the_dct_matrix()
357 // desc: type ii dct matrx on N reals
358 //-----------------------------------------------------------------------------
359 void the_dct_matrix( FLOAT_XFORM ** out, unsigned long N )
360 {
361 unsigned long k, n;
362
363 // go for it
364 for( k = 0; k < N; k++ )
365 {
366 for( n = 0; n < N; n++ )
367 {
368 out[k][n] = cos( CK_ONE_PI / N * k * (n + .5) );
369 }
370 }
371 }
372
373
374 //-----------------------------------------------------------------------------
375 // name: the_dct_now()
376 // desc: apply dct matrx on N reals
377 //-----------------------------------------------------------------------------
378 void the_dct_now( FLOAT_XFORM * x, FLOAT_XFORM ** matrix, unsigned long N,
379 FLOAT_XFORM * out, unsigned long Nout )
380 {
381 unsigned long k, n;
382
383 // sanity check
384 assert( Nout <= N );
385 // zero out
386 memset( out, 0, sizeof(FLOAT_XFORM)*Nout );
387
388 // go for it
389 for( k = 0; k < Nout; k++ )
390 {
391 for( n = 0; n < N; n++ )
392 {
393 out[k] = x[n] * matrix[k][n];
394 }
395 }
396 }
397
398
399
400
401 //-----------------------------------------------------------------------------
402 // name: the_inverse_dct()
403 // desc: type iii dct, or inverse dct
404 //-----------------------------------------------------------------------------
405 void the_inverse_dct( FLOAT_XFORM * x, unsigned long N, FLOAT_XFORM * out, unsigned long Nout )
406 {
407 }
408
409
410 //-----------------------------------------------------------------------------
411 // name: the_inverse_dct_matrix()
412 // desc: generates the NxN DCT matrix
413 //-----------------------------------------------------------------------------
414 void the_inverse_dct_matrix( FLOAT_XFORM ** out, unsigned long N )
415 {
416 unsigned long k, n;
417
418 // go for it
419 for( k = 0; k < N; k++ )
420 {
421 for( n = 0; n < N; n++ )
422 {
423 out[k][n] = cos( CK_ONE_PI / N * n * (k + .5) );
424 }
425 }
426 }
427
428
429
430
431 //-----------------------------------------------------------------------------
432 // name: the_inverse_dct_now()
433 // desc: apply inverse dct matrx on N reals
434 //-----------------------------------------------------------------------------
435 void the_inverse_dct_now( FLOAT_XFORM * x, FLOAT_XFORM ** matrix, unsigned long N,
436 FLOAT_XFORM * out, unsigned long Nout )
437 {
438 unsigned long k, n;
439
440 // sanity check
441 assert( Nout <= N );
442 // zero out
443 memset( out, 0, sizeof(FLOAT_XFORM)*Nout );
444
445 // go for it
446 for( k = 0; k < Nout; k++ )
447 {
448 for( n = 0; n < N; n++ )
449 {
450 out[k] = x[0] / 2 + x[n] * matrix[k][n];
451 }
452 }
453 }