File: fftfun.C

package info (click to toggle)
mixviews 1.10-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 2,440 kB
  • ctags: 6,314
  • sloc: cpp: 31,647; ansic: 2,100; makefile: 1,782; sh: 17
file content (219 lines) | stat: -rw-r--r-- 5,668 bytes parent folder | download
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
// fftfun.C

/******************************************************************************
 *
 *  MiXViews - an X window system based sound & data editor/processor
 *
 *  Copyright (c) 1993, 1994 Regents of the University of California
 *
 *  Author:     Douglas Scott
 *  Date:       December 13, 1994
 *
 *  Permission to use, copy and modify this software and its documentation
 *  for research and/or educational purposes and without fee is hereby granted,
 *  provided that the above copyright notice appear in all copies and that
 *  both that copyright notice and this permission notice appear in
 *  supporting documentation. The author reserves the right to distribute this
 *  software and its documentation.  The University of California and the author
 *  make no representations about the suitability of this software for any 
 *  purpose, and in no event shall University of California be liable for any
 *  damage, loss of data, or profits resulting from its use.
 *  It is provided "as is" without express or implied warranty.
 *
 ******************************************************************************/


#ifdef __GNUG__
#pragma implementation
#endif

#include <math.h>
#include "fftfun.h"
#include "fftdata.h"
#include "request.h"
#include "requester.h"
#include "windowtable.h"

class FFTRequester : public TitledRequester {
public:
	FFTRequester(FFT_Function *);
protected:
	redefined void configureRequest(Request *);
	redefined boolean confirmValues();
private:
	FFT_Function* client;
	int foffset;
	double frate;
};

FFTRequester::FFTRequester(FFT_Function* f)
	: TitledRequester("Fast-Fourier Transform Analysis of Selected Region:"),
	  client(f), foffset(100), frate(0.0) {}

void
FFTRequester::configureRequest(Request* request) {
	request->appendValue("Frame offset (samples):",
			    &foffset, Range(1, client->target()->length() / 2),
			    false);
	request->appendValue("Frame rate (frames/second):", &frate,
	                     PositiveNumbers);
	request->appendChoice("Number of FFT points:",
	                      "|64|128|256|512|1024|2048|",
	                      &client->npoints);
}

boolean
FFTRequester::confirmValues() {
	// { 1, 2, 4, .. } => { 64, 128, 256, ... }
	client->npoints <<= 6;
	return client->setBaseValues(client->npoints * 2, frate, foffset);
}

//********

FFT_Function::FFT_Function(Data* data, int points, int frameoffset)
		: ArrayFunction(data, points*2, frameoffset),
		  xreal(nil), ximag(nil), window(nil), npoints(points) {
	initialize();
}

// FIX ME:  put default values into ArrayFunction args

FFT_Function::FFT_Function(Data* data) : ArrayFunction(data),
	xreal(nil), ximag(nil), window(nil), npoints(P_512) {}

FFT_Function::~FFT_Function() {
	delete [] xreal;
	delete [] ximag;
	Resource::unref(window);
}

Requester *
FFT_Function::createRequester() {
	return new FFTRequester(this);
}

void
FFT_Function::initialize() {
	window = new HanningWindow(framesize());
	window->ref();
	xreal = new double[framesize()];
	ximag = new double[framesize()];
	Super::initialize();
	setAnalysis(
	    new FFTData(analysisLength(), npoints, framerate(), sampRate())
	);
}

int
FFT_Function::operator () (double* input, Data* frame) {
	int fsize = framesize();
	int i;
	for(i=0; i < fsize; i++) {
		*(xreal+i) = *input++;
		*(ximag+i) = 0.0;
	}
	window->applyTo(xreal);			// window the input
	fft(fsize, xreal, ximag);		// do the fft
	// calculate magnitude
	int nvals = fsize / 2;			// only convert and write first half
	for(i=0; i < nvals; i++)
		frame->set(hypot(xreal[i], ximag[i]), 0, i);
	return true;
}

// private static functions for doing transform

void
FFT_Function::fft(int nvalues, double* xreal, double* ximag) {
	static double logTwo = M_LN2;		/* log(2.0) */
	int l = int(log( (double)(abs(nvalues)) ) / logTwo + 0.001);
	int n = int(pow(2.0, double(l)) + 0.5);

	fftalgorithm( (int)(nvalues/abs(nvalues)),l,n,xreal,ximag);

	descramble( ((nvalues>=0)?n:1),l,n,xreal,ximag);
}

void
FFT_Function::fftalgorithm(int wexpsign, int l, int n, double* xr, double* xi) {
	static double PI_x_2 = 2.0 * M_PI;

/* printf("fft: wes,l,n = %d,%d,%d\n",wexpsign,l,n);
*/	int ia = n/2;
	int ib = 1;

	for (int i = 0; i < l; ++i) {
/* printf("level %d\n",i);
*/		int ic = 0;
		int id = ia;

		for (int k = 0; k < ib; ++k) {
			int ie = scramble( l,n,(int)(ic/ia) );
/* printf("ic = %d, ia = %d\n",ic,ia);
printf("W power = %d\n",ie);
*/			double wexp = -wexpsign*PI_x_2*(double)(ie)/(double)(n);
			double zr = cos(wexp);
			double zi = sin(wexp);

			for (int m = ic; m < id; ++m) {
				double ar = xr[m];
				double ai = xi[m];

				double br = xr[m+ia] * zr - xi[m+ia] * zi;
				double bi = xr[m+ia] * zi + xi[m+ia] * zr;

/* printf("x(%d) = x(%d) + W^(%d) * x(%d)\n",m,m,ie,m+ia);
*/				xr[m] = ar + br;
				xi[m] = ai + bi;

/* printf("x(%d) = x(%d) - W^(%d) * x(%d)\n",m+ia,m,ie,m+ia);
*/				xr[m+ia] = ar - br;
				xi[m+ia] = ai - bi;
			}

			ic += 2*ia;
			id += 2*ia;
		}

		ia /= 2;
		ib *= 2;
	}

}

void
FFT_Function::descramble(int scalediv, int l, int n, double* xreal, double* ximag) {
	for (int m1 = 0; m1 < n; ++m1) {
		int m2 = scramble(l,n,m1);
		if (m1 == m2) {
			xreal[m1] /= scalediv;
			ximag[m1] /= scalediv;
		}
		else if (m1 > m2) {
			double swap = xreal[m1]/scalediv;
			xreal[m1] = xreal[m2]/scalediv;
			xreal[m2] = swap;

			swap = ximag[m1]/scalediv;
			ximag[m1] = ximag[m2]/scalediv;
			ximag[m2] = swap;
		}
	}
}

int
FFT_Function::scramble(int l, int n, int min) {
	int mout = 0;
	int mplus = 1;
	int mminus = n/2;
	for (int i = 0; i < l; ++i) {
		if (min >= mminus) {
			mout += mplus;
			min -= mminus;
		}
		mplus *= 2;
		mminus /= 2;
	}
	return mout;
}