Original Post
I'm just testing something, so no need to lecture me on the algorithm please, thank you. I'm running a DFT on an impulse function. Then printing out the data, then running inverse DFT on the frequency domain values.
I'm not sure why the inverse DFT doesn't return back the impulse function. The algorithm is based straight from the definition. Here is the code :
here is the output:
can anyone see what i'm doing wrong? BTW, i'm reading from this site. Thanks.
regards, D.C
I'm not sure why the inverse DFT doesn't return back the impulse function. The algorithm is based straight from the definition. Here is the code :
#include <iostream>
#include <ctime>
#include <set>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef float Type;
typedef std::vector<Type> Vector;
const Type TWO_PI = Type(3.141519 * 2.0);
struct FrequencyDomain{
Vector realPart;
Vector imaginaryPart;
};
FrequencyDomain calculateDFT(const Vector& sampleData){
FrequencyDomain freqDomain;
for(unsigned k = 0; k < sampleData.size()/2; ++k){
Type realVal = 0;
Type imVal = 0;
for(unsigned i = 0; i < sampleData.size(); ++i){
float theta = TWO_PI * k * i / float(sampleData.size());
realVal += sampleData * cos(theta);
imVal += sampleData * sin( theta );
}
freqDomain.realPart.push_back(realVal);
freqDomain.imaginaryPart.push_back(-imVal);
}
return freqDomain;
}
Vector calculateInverseDFT(const FrequencyDomain& d){
Vector v;
const int SAMPLE_SIZE = d.realPart.size() * 2;
FrequencyDomain f;
//adjust proper sizing
f.realPart = Vector(d.realPart.size());
f.imaginaryPart = Vector(d.imaginaryPart.size());
//calculate the special case
f.realPart[0] = d.realPart[0] / SAMPLE_SIZE;
f.realPart[f.realPart.size()-1] = d.realPart.back() / SAMPLE_SIZE;
//convert to cos amplitudes
for(unsigned i = 1; i < d.realPart.size() - 1; ++i){
f.realPart = d.realPart / (SAMPLE_SIZE/2.0f);
}
//convert to sin amplitudes
for(unsigned i = 0; i < d.imaginaryPart.size(); ++i){
f.imaginaryPart = -d.imaginaryPart / (SAMPLE_SIZE/2.0f);
}
//run calculation
for(int i = 0; i < SAMPLE_SIZE; ++i){
Type realVal = 0;
Type imVal = 0;
for(int k = 0; k < SAMPLE_SIZE/2; ++k){
float theta = TWO_PI * k * i/ float(SAMPLE_SIZE);
realVal += f.realPart[k] * cos( theta );
imVal += f.imaginaryPart[k] * sin( theta );
}
v.push_back(realVal + imVal );
}
return v;
}
template<int MIN, int MAX>
struct RandRange{
int operator()(){ return rand() % (MAX-MIN) + MIN; }
};
template<typename Itr>
void print(Itr begin, Itr end, const std::string& delim = " "){
cout << "[ ";
while(begin != end){ cout << *begin << delim; ++begin; }
cout << "]";
}
int main() {
srand( time(0) );
const int GEN_SIZE = 32;
Vector sampleData(GEN_SIZE,0);
sampleData[15] = 1;
//std::generate_n(sampleData.begin(),GEN_SIZE,RandRange<1,200>());
cout << "Sampled data: ";
print( sampleData.begin(), sampleData.end());
cout << endl;
FrequencyDomain f = calculateDFT(sampleData);
cout << "Real Part: ";
print(f.realPart.begin(), f.realPart.end());
cout << "\nImaginary Part: ";
print(f.imaginaryPart.begin(),f.imaginaryPart.end());
cout << "\n\n";
Vector v = calculateInverseDFT(f);
cout << "Time Domain after inverse: ";
print(v.begin(),v.end());
cout << "\n";
return 0;
}
here is the output:
Sampled data: [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
Real Part: [ 1 -0.980772 0.923827 -0.831354 0.706912 -0.555284 0.3823 -0.194616 -0.000551164 0.195699 -0.38332 0.556201 -0.707694 0.831966 -0.924249 0.980987 ]
Imaginary Part: [ -0 -0.195158 0.382811 -0.555743 0.707302 -0.831661 0.924038 -0.980879 1 -0.980664 0.923616 -0.831048 0.706519 -0.554826 0.38179 -0.194076 ]
Time Domain after inverse: [ 0.000597399 -0.00118974 0.0029381 -0.00577524 0.00959269 -0.014243 0.0195472 -0.0253024 0.0312866 -0.0372698 0.0430225 -0.0483229 0.0529676 -0.0567778 0.0596068 0.938677 0.0619292 -0.061333 0.0595804 -0.0567395 0.0529184 -0.0482652 0.042958 -0.0372013 0.0312163 -0.0252324 0.0194809 -0.014182 0.00954051 -0.00573279 0.00290673 -0.0011705 ]
can anyone see what i'm doing wrong? BTW, i'm reading from this site. Thanks.
regards, D.C