FFTW - Cannot see where I am going wrong

Hi, I am using the fftw library to perform a Fourier Transform on data that I read from a file and then output the transform into a new file.

I have compiled the library and it all links and seems to work just fine. However when It comes to actually performing the transform the output is just wrong. Here is what I put in and the output:

What is inputed:
1
2
3
4
0.218418
0.218418
0.218418
0.218418


What is outputted:
1
2
3
4
0.873672
0
0
0


Here is my code that involves reading the data into a file, performing the transform and outputting the data:

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
	vector<float> data;
	

	while(stop == false){

		stringstream cc; stringstream rr;
		cc << column; rr << row;
		string c_column = cc.str(); string r_row = rr.str();
		
		string openfile = "["+c_column+"]["+r_row+"].txt";
		
		ifstream input;
		input.open(openfile.c_str());

		if(!input.is_open()){
			input.close();
			cout << "File not opened, leaving loop\n";
			break;
		}

		float s;
		while (input >> s){
			data.push_back(s);//adds data from file to vector
		}
		input.close();
		
		
		
		int n = data.size(); 
		complex<double>*  in=new complex<double> [n];
		complex<double>* out=new complex<double> [n];
		
		cout << "Data size: "<< data.size()<<endl;
		
		for (int i=0;i<n;i++){
         	in[i]=data[i];
			cout << "Added "<< in[i] <<" to variable in\n";//check numbers are being passed correctly
		}
		
		
		fftw_plan p;
		p = fftw_plan_dft_1d(n,reinterpret_cast<fftw_complex*>(in), reinterpret_cast<fftw_complex*>(out), FFTW_FORWARD, FFTW_ESTIMATE);
		fftw_execute(p);
		 
				
		ofstream output;
		string dataoutfile = "t[" + c_column+ "][" + r_row+ "].txt";
		output.open(dataoutfile.c_str(),ios::app);
		
		for (int i=0;i<n;i++){
			output << scientific << out[i].real() << "\t" << out[i].imag() <<endl; //change scientific to match precision of the data			
			cout << "Value: "<< out[i].real()<<"\t"<<out[i].imag() << " Added into document\n";
		}
		output.close();

		
		
		fftw_destroy_plan(p);
		fftw_cleanup();		
		delete [] in;
		delete [] out;
		data.clear();
		
		if(column == ncolumns-1){
			row++;
			column = 0;
		}else{
			column ++;
		}
	}


I think my error lies on lines 42?

Any help would be very much appreciated. As far as I can tell it should be simple but its not working out that way.

Documentation of the library, only section 2 - one dimensional sections seem relevant to what I am doing.

http://www.fftw.org/doc/
Last edited on
What kind of data do you want to do a fourier tansform on?

My only experience is with FFTreal on audio data. For what I was doing, you choose an fft size (number of sample values) to pass in. You discard the second half of your output because your limited by half your sample rate (nyquist rate). Then you calculate what frequency your bins (real and imaginary pairs / complex numbers) are associated with.

f[i] = i * samplerate / fftsize;

where f[i] is the frequency you have data for at bin i.

I'm not sure what you should expect to get as output when you input

0.218418
0.218418
0.218418
0.218418

I noticed that when using FFTreal, I would not get good output for this type of input without any oscillation. And with only four input values, your only going to get two usable bins, the first bin would be the first 1/4 of the sample rate, and the second, the second quarter.

Also with FFTreal, you input your sample values as real numbers, and you input your imaginary numbers as all 0's. Then it spits out the same two vectors processed.



Last edited on
Your data has no oscilation, which means it has a frequency of 0.

0.873672, is 4 times your input, 0.218418. I guess that is the real component of bin 1? That is supposed to be the amplitude of the component cosine waves of the bin.

Maybe try it on some more interesting data.
Last edited on
Topic archived. No new replies allowed.