Doing Fourier Transform

Jul 5, 2013 at 7:22am
Hello! I am having problems doing the fourier transform:

http://en.wikipedia.org/wiki/Discrete_Fourier_transform

My code is:

esc = 2;
maxi = 0;
fu = 500;
fo = 3000;

for (f=fu;f<=fo;f + esc)
{
Y((f-fu)/esc + 1) = 0;
for (b=1;b<=N;b++)
{
Y((f-fu)/esc + 1) = Y((f-fu)/esc + 1) + y(b)*exp(-j*2*pi*f*b/N);
}
if( Y((f-fu)/esc + 1)>maxi)
maxi = Y((f-fu)/esc + 1);
fr = f;
}
}

Don't worry about the exp and complex numbers because they work correctly. N is the number of samples of the input (2500). Esc is the difference between different frequences. So the samples of the output are (1250).

Thank you!
Jul 5, 2013 at 7:36am
you are not changing the value of the variable f.
Jul 5, 2013 at 9:13am
Problem solved, I had to use discret values for f and later convert to the real frequences that i needed.

Thanks anyway!
Jul 5, 2013 at 12:44pm
(in case someone passes by in the future...)

As you have noticed, with for-loops you should really only ever use control variables of an integral type. (Something which seems be missing from this site's page on control structures.)
http://www.cplusplus.com/doc/tutorial/control/

While I'm here, if you'd like to tag your code for readility, see:

How to use code tags
http://www.cplusplus.com/articles/jEywvCM9/

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main() {
    cout << "Hello nicely formatted world!\n";

    return 0;
}


Andy

PS Out of interest, what kind of container are you using?

(I assume Y and y are some sort of container.)
Last edited on Jul 5, 2013 at 1:48pm
Topic archived. No new replies allowed.