Roberto said:
I developed a DFT routine using a language named "ATP Model Language".
It is based on FORTRAN and specific to electromagnetic transients
simulations, a study field in electrical engineering. Few months ago,
I cought up on C++ readings and, nowadays, fell very bad to retarn to
"FORTRAN". My Discret Fourier Trasmoration Routine (DFT) is used to
filtering data from electrical system by digital relays. Could you
help me with some tips to develop a routine like this one using C++
OOP? Or maybe help me to translate a "function-oriented" (Fortran) in
C++?
Thanks,
Roberto Dias
There are several methods to go about your project:
1. Use somebody else's software.
2. Use existing libraries and just write "glue-ware".
3. Write your own from scratch.
The first two require the use of a web search engine,
which is off-topic to this newsgroup. For example,
one might use the keywords "Discrete Fourier Transformation
Filter C++ Library" (make sure the spelling is correct).
You could reorder the terms in order of importance:
"Fourier Transformation Descrete C++ Library".
Writing your own
----------------
From lurking in this newsgroup and after reading the
FAQ and Welcome.Txt, you know that the best method
is to start out small and work up.
We all know that a good starting point is with a simple
program:
#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;
int main(void)
{
cout << "It works!" << endl;
return EXIT_SUCCESS;
}
The Next Step: Input
---------------------
An FFT (Fast Fourier Transform) or a DFT requires data.
There are two routes here:
1. Place test data inside the program.
2. Input data from a file.
In the embedded systems arena, I would go with #1. Often
times, the platforms don't support files. However, in
this case we'll go with route 2.
So, add input capability to your main program. For simplicity,
just input the data and spit it back out.
#include <iostream>
#include <fstream>
#include <cstdlib>
using std::cerr;
using std::cout;
using std::endl;
using std::ifstream;
#define DATA_TYPE int // Change this to match your data type.
const char INPUT_FILENAME[] = "dft.data";
int main(void)
{
// Assume ASCII represented data i.e. "12" not 12.
ifstream inp(INPUT_FILENAME);
// Always check for errors after opening a file.
if (!inp)
{
cerr << "Error opening input file, "
<< INPUT_FILENAME
<< endl;
return EXIT_FAILURE;
}
DATA_TYPE value;
// Input one value and spit it out:
if (inp >> value)
{
cout << "Value read: " << value << endl;
}
else
{
cerr << "Error reading input data." << endl;
return EXIT_FAILURE;
}
// Read all the values and spit them out:
while (inp >> value)
{
cout << value << endl;
}
// A proper program closes the file before exiting.
inp.close();
return EXIT_SUCCESS;
}
Next: Adding Computation
-------------------------
After the above program works, we know that we can
input data properly, which is very important.
The next step is to perform the computation. But
this requires some planning or design. Here are
some routes:
1. DFT wants all data at once.
2. DFT can handle one value at a time.
For route #2, we just add a function call inside
the input loop. For route #1, we append each value
to a vector, then after all values are read in, we
pass the vector to the DFT:
#include <vector>
//...
using std::vector;
#define DFT_RESULT_TYPE int // or whatever your's is.
// Create a function declaration.
DFT_RESULT_TYPE My_Dft(vector<DATA_TYPE>& dft_values);
//...
int main(void)
{
vector<DATA_TYPE> dft_values;
DFT_RESULT_TYPE result;
//...
while (inp >> value)
{
dft_values.push_back(value);
}
// Process the data.
result = My_Dft(dft_values);
// Output the result
cout << "DFT result: " << result << endl;
// ...
return EXIT_SUCCESS;
}
DFT_RESULT_TYPE My_Dft(vector<DATA_TYPE>& dft_values)
{
cout << "Processing " << dft_values.size()
<< " values" << endl;
return 12; // Some test value.
}
After the above modifications have been made and tested,
you now can go on and add details to the DFT routine.
Follow this method of writing simple test programs,
getting them to work, then building up.
Search the Web for "Test Driven Development".
Hope that helps.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book