Re: DFT Function



On Thu, 13 Nov 2008 12:06:13 -0800 (PST), Pat <pabrown1975@xxxxxxxxx>
wrote:

On Nov 13, 4:06 am, David C. Ullrich <dullr...@xxxxxxxxxxx> wrote:

It's impossible to say what the problem is. The input to the code
is an array of floats. You say you get the wrong answer with
input cos(5.5 * 6.2831853 * t). That's not an array of floats.
What _are_ you using for input? (Also, what output do you get
and how do you know it's wrong?)

Here is the code that fills the input array:

#define NUM_SAMPLES 256

float x[NUM_SAMPLES];
float fx[NUM_SAMPLES];

for (int i = 0; i < 256; i++)
{
float t = (float)i / NUM_SAMPLES;
x[i] = cosf(5.5f * 2.0f * (float)M_PI * t);
}

full_dft(x, fx, NUM_SAMPLES);

That's more or less what I guessed. The output you get
below is right (except it has remarkably large roundoff error;
I get much more precise results with the crudest possible
Python code!)

Evidently you were expecting a peak around 5.5.
That's simply not how it works - no, the fact that
you're ignoring the sin part is not the explanation,
if you include that you're not going to see a peak
around 5.5 either. Well, you do get something
sort of like that, but it's not nearly as sharp as
what you're going to get with a frequency of 5.

The point is that cos(5.5 t) does not have period
2pi; you're not really taking any sort of FT of
that function, you're only sampling a piece of
a period. (Well, you're sampling 5 and a half
periods, which explains why there's a rough
sort of peak around 5.5.)

In fact cos(5.5(2 pi - t)) = - cos(5.5 2 pi t),
so the actual cosine coefficients of the continuous
function (ass opposed to the discrete sampling
of it) are exactly 0.

When run, the output array (fx) is filled with the following:

0: 0.999978
1: 0.999983
2: 0.999979
3: 0.999979
4: 0.999971
5: 0.999916
6: 1.000051
7: 1.000010
8: 1.000008
9: 1.000000

... and so on; all values of the output array are very close to 1.0.
In contrast, when the input is changed to a frequency of 5.0, the
output array looks like so:

0: 0.000013
1: 0.000013
2: 0.000012
3: 0.000014
4: 0.000022
5: 127.999992
6: 0.000013
7: 0.000002
8: 0.000003
9: 0.000006

... and the rest of the values are very close to 0.0. Just what I
would expect, a large peak at a frequency of 5.

David C. Ullrich

"Understanding Godel isn't about following his formal proof.
That would make a mockery of everything Godel was up to."
(John Jones, "My talk about Godel to the post-grads."
in sci.logic.)
.



Relevant Pages

  • Re: DFT Function
    ... Evidently you were expecting a peak around 5.5. ... you're sampling 5 and a half ... all values of the output array are very close to 1.0. ... David Bernier ...
    (sci.math)
  • Re: whither regenerative amplifiers?
    ... >to assemble an array the size of a large pizza and call the power ... Doing an ADC that has very many bits is tricky at high frequencies. ... it so that only one peak lines up in both. ...
    (sci.electronics.design)
  • Re: whither regenerative amplifiers?
    ... >to assemble an array the size of a large pizza and call the power ... Doing an ADC that has very many bits is tricky at high frequencies. ... it so that only one peak lines up in both. ...
    (sci.electronics.basics)
  • Re: whither regenerative amplifiers?
    ... >to assemble an array the size of a large pizza and call the power ... Doing an ADC that has very many bits is tricky at high frequencies. ... it so that only one peak lines up in both. ...
    (sci.electronics.misc)
  • Re: DirectSound
    ... A neat trick for generating sampled single frequency PCM signals is ... Generate an array of samples for a 1Hz wave at the PCM sampling rate ...
    (comp.lang.pascal.delphi.misc)