File: fft.c

package info (click to toggle)
meschach 1.2b-7
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,272 kB
  • ctags: 1,757
  • sloc: ansic: 21,958; makefile: 509; sh: 11
file content (144 lines) | stat: -rw-r--r-- 3,892 bytes parent folder | download | duplicates (4)
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

/**************************************************************************
**
** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
**
**			     Meschach Library
** 
** This Meschach Library is provided "as is" without any express 
** or implied warranty of any kind with respect to this software. 
** In particular the authors shall not be liable for any direct, 
** indirect, special, incidental or consequential damages arising 
** in any way from use of the software.
** 
** Everyone is granted permission to copy, modify and redistribute this
** Meschach Library, provided:
**  1.  All copies contain this copyright notice.
**  2.  All modified copies shall carry a notice stating who
**      made the last modification and the date of such modification.
**  3.  No charge is made for this software or works derived from it.  
**      This clause shall not be construed as constraining other software
**      distributed on the same medium as this software, nor is a
**      distribution fee considered a charge.
**
***************************************************************************/


/*
	Fast Fourier Transform routine
	Loosely based on the Fortran routine in Rabiner & Gold's
	"Digital Signal Processing"
*/

static char rcsid[] = "$Id: fft.c,v 1.3 1994/01/13 05:45:33 des Exp $";

#include        <stdio.h>
#include        "matrix.h"
#include        "matrix2.h"
#include        <math.h>


/* fft -- d.i.t. fast Fourier transform 
        -- radix-2 FFT only
        -- vector extended to a power of 2 */
void    fft(x_re,x_im)
VEC     *x_re, *x_im;
{
    int         i, ip, j, k, li, n, length;
    Real      *xr, *xi;
    Real	theta, pi = 3.1415926535897932384;
    Real      w_re, w_im, u_re, u_im, t_re, t_im;
    Real      tmp, tmpr, tmpi;

    if ( ! x_re || ! x_im )
        error(E_NULL,"fft");
    if ( x_re->dim != x_im->dim )
        error(E_SIZES,"fft");

    n = 1;
    while ( x_re->dim > n )
        n *= 2;
    x_re = v_resize(x_re,n);
    x_im = v_resize(x_im,n);
    printf("# fft: x_re =\n");  v_output(x_re);
    printf("# fft: x_im =\n");  v_output(x_im);
    xr   = x_re->ve;
    xi   = x_im->ve;

    /* Decimation in time (DIT) algorithm */
    j = 0;
    for ( i = 0; i < n-1; i++ )
    {
        if ( i < j )
        {
            tmp = xr[i];
            xr[i] = xr[j];
            xr[j] = tmp;
            tmp = xi[i];
            xi[i] = xi[j];
            xi[j] = tmp;
        }
        k = n / 2;
        while ( k <= j )
        {
            j -= k;
            k /= 2;
        }
        j += k;
    }

    /* Actual FFT */
    for ( li = 1; li < n; li *= 2 )
    {
        length = 2*li;
        theta  = pi/li;
        u_re   = 1.0;
        u_im   = 0.0;
        if ( li == 1 )
        {
            w_re = -1.0;
            w_im =  0.0;
        }
        else if ( li == 2 )
        {
            w_re =  0.0;
            w_im =  1.0;
        }
        else
        {
            w_re = cos(theta);
            w_im = sin(theta);
        }
        for ( j = 0; j < li; j++ )
        {
            for ( i =  j; i < n; i += length )
            {
                ip = i + li;
                /* step 1 */
                t_re = xr[ip]*u_re - xi[ip]*u_im;
                t_im = xr[ip]*u_im + xi[ip]*u_re;
                /* step 2 */
                xr[ip] = xr[i]  - t_re;
                xi[ip] = xi[i]  - t_im;
                /* step 3 */
                xr[i] += t_re;
                xi[i] += t_im;
            }
            tmpr = u_re*w_re - u_im*w_im;
            tmpi = u_im*w_re + u_re*w_im;
            u_re = tmpr;
            u_im = tmpi;
        }
    }
}

/* ifft -- inverse FFT using the same interface as fft() */
void	ifft(x_re,x_im)
VEC	*x_re, *x_im;
{
    /* we just use complex conjugates */

    sv_mlt(-1.0,x_im,x_im);
    fft(x_re,x_im);
    sv_mlt(-1.0/((double)(x_re->dim)),x_im,x_im);
}