1D or 2D fourier transformation

Started by
1 comment, last by Decrius 12 years, 5 months ago
Consider the following D code (a real is like a double in C). f is the original discrete function, F the Fourier transformed

immutable int N = 8;

real[N] f = [0.0, 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0];
real[N] F = 0.0;

for (int n = 0; n < N; n++)
{
for (int k = 0; k < N; k++)
{
real a = 2.0 * PI * cast(real) (k * n) / N;
real r_part = cos(a);
real i_part = sin(a);

F[n] += f[k] * sqrt(r_part * r_part + i_part * i_part);
}
}

foreach (value; F)
{
writeln(value);
}


The Fourier transformed only ever has equal values on each discrete position. In the above example 8 times "3" is printed.

Perhaps I'm missing a division by N, I'm not sure whether this is for the transformation or it's inverse.
Futher I can't suspect any fault in the algorithm, it seems to be equivalent to the definition, unless I'm being stupid.

Note this is FT, not FFT.
Why do I not get sinusoids or sinc's for the transformed output? I want them...

Thanks.
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Advertisement
Your code is incorrect. First, at the very least you must treat F as complex values. Second, you calculate the real and imaginary part of the phasor correct, but you should multiply the input signal by the complex value r_part +i*i_part, not by sqrt(r_part[sup]2[/sup] + i_part[sup]2[/sup]). See the definition of the discrete Fourier transform.

If you think about your current code, you will see that sqrt(cos[sup]2[/sup](a)+ cos[sup]2[/sup](a)), which is always equal to 1 no matter what the value of a is.
Yes, indeed. That is always 1.

I only want to calculate the magnitude, I should've mentioned. I think I do the sqrt too early. It should be outside the "k" loop.

Luckily D has imaginary numbers build-in :)

immutable int N = 8;

creal[N] f = [0.0 + 0i, 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0];
creal[N] F = 0.0 + 0i;

for (int n = 0; n < N; n++)
{
for (int k = 0; k < N; k++)
{
real a = 2.0 * PI * cast(real) (k * n) / N;
F[n] += f[k] * expi(-a);
}
}

foreach (value; F)
{
writeln(value, " ", sqrt(value.re * value.re + value.im * value.im));
}


Thanks.
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora

This topic is closed to new replies.

Advertisement