summaryrefslogtreecommitdiff
path: root/libm/double/fftr.c
blob: d4ce23463a4c93d501cc4b9035f776ed3776a405 (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
/*							fftr.c
 *
 *	FFT of Real Valued Sequence
 *
 *
 *
 * SYNOPSIS:
 *
 * double x[], sine[];
 * int m;
 *
 * fftr( x, m, sine );
 *
 *
 *
 * DESCRIPTION:
 *
 * Computes the (complex valued) discrete Fourier transform of
 * the real valued sequence x[].  The input sequence x[] contains
 * n = 2**m samples.  The program fills array sine[k] with
 * n/4 + 1 values of sin( 2 PI k / n ).
 *
 * Data format for complex valued output is real part followed
 * by imaginary part.  The output is developed in the input
 * array x[].
 *
 * The algorithm takes advantage of the fact that the FFT of an
 * n point real sequence can be obtained from an n/2 point
 * complex FFT.
 *
 * A radix 2 FFT algorithm is used.
 *
 * Execution time on an LSI-11/23 with floating point chip
 * is 1.0 sec for n = 256.
 *
 *
 *
 * REFERENCE:
 *
 * E. Oran Brigham, The Fast Fourier Transform;
 * Prentice-Hall, Inc., 1974
 *
 */


#include <math.h>

static short n0 = 0;
static short n4 = 0;
static short msav = 0;

extern double PI;

#ifdef ANSIPROT
extern double sin ( double );
static int bitrv(int, int);
#else
double sin();
static int bitrv();
#endif

fftr( x, m0, sine )
double x[];
int m0;
double sine[];
{
int th, nd, pth, nj, dth, m;
int n, n2, j, k, l, r;
double xr, xi, tr, ti, co, si;
double a, b, c, d, bc, cs, bs, cc;
double *p, *q;

/* Array x assumed filled with real-valued data */
/* m0 = log2(n0)			*/
/* n0 is the number of real data samples */

if( m0 != msav )
	{
	msav = m0;

	/* Find n0 = 2**m0	*/
	n0 = 1;
	for( j=0; j<m0; j++ )
		n0 <<= 1;

	n4 = n0 >> 2;

	/* Calculate array of sines */
	xr = 2.0 * PI / n0;
	for( j=0; j<=n4; j++ )
		sine[j] = sin( j * xr );
	}

n = n0 >> 1;	/* doing half length transform */
m = m0 - 1;


/*							fftr.c	*/

/*  Complex Fourier Transform of n Complex Data Points */

/*	First, bit reverse the input data	*/

for( k=0; k<n; k++ )
	{
	j = bitrv( k, m );
	if( j > k )
		{ /* executed approx. n/2 times */
		p = &x[2*k];
		tr = *p++;
		ti = *p;
		q = &x[2*j+1];
		*p = *q;
		*(--p) = *(--q);
		*q++ = tr;
		*q = ti;
		}
	}
	
/*							fftr.c	*/
/*			Radix 2 Complex FFT			*/
n2 = n/2;
nj = 1;
pth = 1;
dth = 0;
th = 0;

for( l=0; l<m; l++ )
	{	/* executed log2(n) times, total */
	j = 0;
	do
		{	/* executed n-1 times, total */
		r = th << 1;
		si = sine[r];
		co = sine[ n4 - r ];
		if( j >= pth )
			{
			th -= dth;
			co = -co;
			}
		else
			th += dth;

		nd = j;

		do
			{ /* executed n/2 log2(n) times, total */
			r = (nd << 1) + (nj << 1);
			p = &x[ r ];
			xr = *p++;
			xi = *p;
			tr = xr * co + xi * si;
			ti = xi * co - xr * si;
			r = nd << 1;
			q = &x[ r ];
			xr = *q++;
			xi = *q;
			*p = xi - ti;
			*(--p) = xr - tr;
			*q = xi + ti;
			*(--q) = xr + tr;
			nd += nj << 1;
			}
		while( nd < n );
		}
	while( ++j < nj );

	n2 >>= 1;
	dth = n2;
	pth = nj;
	nj <<= 1;
	}

/*							fftr.c	*/

/*	Special trick algorithm			*/
/*	converts to spectrum of real series	*/

/* Highest frequency term; add space to input array if wanted */
/*
x[2*n] = x[0] - x[1];
x[2*n+1] = 0.0;
*/

/* Zero frequency term */
x[0] = x[0] + x[1];
x[1] = 0.0;
n2 = n/2;

for( j=1; j<=n2; j++ )
	{	/* executed n/2 times */
	si = sine[j];
	co = sine[ n4 - j ];
	p = &x[ 2*j ];
	xr = *p++;
	xi = *p;
	q = &x[ 2*(n-j) ];
	tr = *q++;
	ti = *q;
	a = xr + tr;
	b = xi + ti;
	c = xr - tr;
	d = xi - ti;
	bc = b * co;
	cs = c * si;
	bs = b * si;
	cc = c * co;
	*p = ( d - bs - cc )/2.0;
	*(--p) = ( a + bc - cs )/2.0;
	*q = -( d + bs + cc )/2.0;
	*(--q) = ( a - bc + cs )/2.0;
	}

return(0);
}

/*							fftr.c	*/

/*	Bit reverser	*/

int bitrv( j, m )
int j, m;
{
register int j1, ans;
short k;

ans = 0;
j1 = j;

for( k=0; k<m; k++ )
	{
	ans = (ans << 1) + (j1 & 1);
	j1 >>= 1;
	}

return( ans );
}