Output error: WHat's wrong with my code???

My program compiles no prob, but my output makes no sense. Is it just a formattting thing or are my parameters messed up. Please help! I'm really unfamiliar with the FFT function and I'm just attempting a black box apprach to it.

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
/*Description: Program takes data from user-specified file and computes FFTs 
(Fast Fourier Transforms) on data. Outputs FFTs to FFTdata.xlsx, which is saved 
in default domain. For program to work, user-specified file must be saved in 
default domain also.(Default domain: where program executible file is located.*/

#include <iostream>
#include <fstream>
#include <math.h>

using namespace std;

void FFT(double* data, unsigned long nn);
//takes data array and calculates FFT's on data array
void swap(double& a, double& b);
//swaps values

int main()
{
	char filename[101];
	double data[65];
    ifstream existtest, infile;
	ofstream opentest, outfile;			
	
	cout << "Copyright: 3 Aug 2011 Karla Guardado\n\n";
    cout << "Program takes data from user-specified file and computes FFTs\n";
    cout << "(Fast Fourier Transforms) on data. Outputs FFTs to FFTdata.xls.\n";
    cout << endl;
    
    cout << "Enter file name. (Type name, extension included, then press <ENTER>.)";
    cout << endl << endl << "File name: ";
	cin >> filename;
	cout << endl << endl;
	
	existtest.open(filename);
	
	if (existtest.fail()) {
		existtest.close();
		cout << "ERROR: File does not exsist. Exiting.\n" << endl;
		system ("pause");
		return 0;
	}
	
	existtest.close();
    opentest.open(filename);
	
	if (opentest.fail()) {
		opentest.close();
        cout << "ERROR: File failed to open. Exiting.";
		system ("pause");
		return 0;
	}
    opentest.close();
    infile.open(filename);
    
    int i = 0; double num;
    while (infile >> num) { data[i] = num; i++;}

    unsigned long nom = 16; FFT(data, nom);
    
    outfile.open("FFTdata.csv");
	outfile << data;
	outfile.close();
	
	system ("pause");
	return 0;
}

void FFT(double* data, unsigned long nn)
//takes data array and calculates FFT's on data array
{
    unsigned long n, mmax, m, j, istep, i;     
    double wtemp, wr, wpr, wpi, wi, theta;     
    double tempr, tempi;       
    
    //reverse-binary reindexing     
    n = nn<<1;     
    j=1;     
    for (i=1; i<n; i+=2) {         
        if (j>i) {             
            swap(data[j-1], data[i-1]);             
            swap(data[j], data[i]);         
        }         
        m = nn;         
        while (m>=2 && j>m) {             
            j -= m;             
            m >>= 1;         
        }         
        j += m;     
    }       
                       
    //here begins the Danielson-Lanczos section     
    mmax=2;     
    while (n>mmax) {         
        istep = mmax<<1;         
        theta = -(2*M_PI/mmax);         
        wtemp = sin(0.5*theta);         
        wpr = -2.0*wtemp*wtemp;         
        wpi = sin(theta);         
        wr = 1.0;         
        wi = 0.0;         
        for (m=1; m < mmax; m += 2) {             
            for (i=m; i <= n; i += istep) {                 
            j=i+mmax;                 
            tempr = wr*data[j-1] - wi*data[j];                 
            tempi = wr * data[j] + wi*data[j-1];                   
            data[j-1] = data[i-1] - tempr;                 
            data[j] = data[i] - tempi;                 
            data[i-1] += tempr;                 
            data[i] += tempi;             
            }             
            wtemp=wr;             
            wr += wr*wpr - wi*wpi;             
            wi += wi*wpr + wtemp*wpi;         
        }   
        mmax=istep;     
        } 
    }

void swap(double& a, double& b)
//swaps values
{
    double temp;
    
    temp = a;
    a = b;
    b = temp;
}
The stream does not write an array automatically (instead it writes the pointer). Line 61 should look like 56.

I appreciate the response. So I modified the code and Everthing now works except that my ouput size is huge! Running this on a 64 point data set after the FFT function data grows to 520! and I don;t understand the output. I have no idea what I did. Help please!

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
/*Description: Program takes data from user-specified file and computes FFTs 
(Fast Fourier Transforms) on data. Outputs FFTs to FFTdata.xlsx, which is saved 
in default domain. For program to work, user-specified file must be saved in 
default domain also.(Default domain: where program executible file is located.*/

#include <iostream>
#include <fstream>
#include <math.h>

using namespace std;

void FFT(double* data, unsigned long nn);
//takes data array and calculates FFT's on data array
void swap(double& a, double& b);
//swaps values

int main()
{
    char filename[101];
    double data[65];
    ifstream existtest, infile;
    ofstream opentest, outfile;			
	
    cout << "Copyright: 3 Aug 2011 Karla Guardado\n\n";
    cout << "Program takes data from user-specified file and computes FFTs\n";
    cout << "(Fast Fourier Transforms) on data. Outputs FFTs to FFTdata.xls.\n";
    cout << endl;
    
    cout << "Enter file name. (Type name, extension included, then press <ENTER>.)";
    cout << endl << endl << "File name: ";
    cin >> filename;
    cout << endl;
	
    existtest.open(filename);
	
    if (existtest.fail()) {
        existtest.close();
        out << "ERROR: File does not exsist. Exiting.\n" << endl;
        system ("pause");
        return 0;
    }
	
    existtest.close();
    opentest.open(filename);
	
    if (opentest.fail()) {
         opentest.close();
         cout << "ERROR: File failed to open. Exiting.";
         system ("pause");
         return 0;
    }
    opentest.close();
    infile.open(filename);
    
    int i = 0; double num;
    while (infile >> num) { data[i] = num; i++;}

    unsigned long nom = 16; FFT(data, nom);
    
    outfile.open("FFTdata.csv");
    cout<< "Working...\n\n";
    
    i = 0; while (i < sizeof(data)) { outfile << data[i]; }
	
    outfile.close();
	cout << "Done.\n\n";
    
    system ("pause");
	return 0;
}

void FFT(double* data, unsigned long nn)
//takes data array and calculates FFT's on data array
{
    unsigned long n, mmax, m, j, istep, i;     
    double wtemp, wr, wpr, wpi, wi, theta;     
    double tempr, tempi;       
    
    //reverse-binary reindexing     
    n = nn<<1;     
    j=1;     
    for (i=1; i<n; i+=2) {         
        if (j>i) {             
            swap(data[j-1], data[i-1]);             
            swap(data[j], data[i]);         
        }         
        m = nn;         
        while (m>=2 && j>m) {             
            j -= m;             
            m >>= 1;         
        }         
        j += m;     
    }       
                       
    //here begins the Danielson-Lanczos section     
    mmax=2;     
    while (n>mmax) {         
        istep = mmax<<1;         
        theta = -(2*M_PI/mmax);         
        wtemp = sin(0.5*theta);         
        wpr = -2.0*wtemp*wtemp;         
        wpi = sin(theta);         
        wr = 1.0;         
        wi = 0.0;         
        for (m=1; m < mmax; m += 2) {             
            for (i=m; i <= n; i += istep) {                 
            j=i+mmax;                 
            tempr = wr*data[j-1] - wi*data[j];                 
            tempi = wr * data[j] + wi*data[j-1];                   
            data[j-1] = data[i-1] - tempr;                 
            data[j] = data[i] - tempi;                 
            data[i-1] += tempr;                 
            data[i] += tempi;             
            }             
            wtemp=wr;             
            wr += wr*wpr - wi*wpi;             
            wi += wi*wpr + wtemp*wpi;         
        }   
        mmax=istep;     
        } 
    }

void swap(double& a, double& b)
//swaps values
{
    double temp;
    
    temp = a;
    a = b;
    b = temp;
}
I thought FFT algs normally did their calculation in place, so the number of o/p points = number of i/p points?

This line looks suspect (line 53 above)

i = 0; while (i < sizeof(data)) { outfile << data[i]; }

I don't see an increment operator, so it will just repeatedly output data[0]

Andy
Thnaks! ^ I should have known it was something silly :)
:-) They're the most difficult things to spot!!

Andy
When it comes to index you should use a for loop: Line 56
1
2
    double num;
    for (int i = 0; infile >> num; i++) { data[i] = num;}
and line 63:
1
2
3
4
for (i = 0; i < (sizeof(data) / sizeof(data[0])); ++i)
{
  outfile << data[i];
}
for debugging purposes it's better to write each expressin on one line.

On line 63: sizeof(data) results in the amount of bytes (which is 65 * sizeof(double)) used for this array not the amount of elements. This (sizeof(data) / sizeof(data[0])) does.

Are you sure that you want always store the whole array?

for debugging purposes it's better to write each expressin on one line.


I absolutely agree!!!

People who write their conditions on the same line as the functions they control need to have hot coals applied until they stop doing it!

When you're in the middle of an extended debugging session, and discover a suspicious bit of code, only to realise you can't set a breakpoint which trigger on the right conditon,can be very, very frustrating. Esp. if getting to that point required you to work though an extended repro scenario!

(Yes, you can use conditional breakpoints to work round the problem. But they slow down the execution significanlty more than regular breakpoints.)

Andy

P.S. Apparently there are debuggers which can set breakpoints on char offset + line number; but neither gcc or Visual Studio are up to it. (Unless it's just appeared in VC10?)

P.P.S. I have hit this problem far more than I feel I should have!
Last edited on
Topic archived. No new replies allowed.