# DFT/FFT Imaginary vs real values

1. Sep 22, 2013

### btb4198

I have a working DFT and FFT now that I coded in a program ..

now from testing I can see that with both the FFT and the DFT if I just graph the Imaginary number I will get the right frequency
for example:
F[t] = 10 Sin((2 * PI * 2000 *t)/8000) 0 <t <1024
will get me
a frequency

2000 Hz and 6000 Hz now I do know that 6000 Hz is the - frequency in the sin wave

but I a graph my Real numbers I get
0 Hz
4000Hz
Why is that ?
should I only use my imaginary values and for get about my real value ?

also my magnitude for all the Frequencies are the same 65535 can I get something from that ?

2. Sep 23, 2013

### Staff: Mentor

What are the units of $t$?

The FT of that function should give you a single peak. You made a programming error.

3. Sep 23, 2013

### btb4198

@ PF Patron

so t would be in ms. Oh so i should not be getting 6000 Hz? I would told that the sine wave has a positive frequency and a negative frequency.

4. Sep 23, 2013

### Staff: Mentor

If $t$ is in ms, then you have $\nu = 250\ \mathrm{Hz}$.

And I meant that you will have a peak at a single absolute frequency, so that would be two peaks at $\pm 250\ \mathrm{Hz}$.

5. Sep 23, 2013

### btb4198

Why at 250 Hz?

I am setting it to 2000 Hz?

Also the t does not really matter ? I am setting t from 0 to 1024... the FFT and DFT does not care what unit of time the samples are in, just as long as they are the same length.
so since I am going 0 - 1024 , I guess it more in s and not ms, but I do not see how that is important .

6. Sep 23, 2013

### Staff: Mentor

2000/8 = 250.

The program doesn't care, so long as you assign the right output to the right frequency. Maybe this is where your problem lies. What values of nu are you using for G[nu]?

7. Sep 23, 2013

### btb4198

@ DrClaude
what is nu and what is G[un]?
n -1
so f[r] = Ʃ x(k) e^ ( -j2∏kr/N)
k = 0

if by G[un] you mean f[r]

then for both my FFT and DFT, I am using 0 - 1024 for un...
is that is not what you are talking about then, I am not sure

8. Sep 23, 2013

### Staff: Mentor

Yes, this is what I meant. Sorry, I should've made my notation clear.
There is your problem! First, I guess that should be 1023, not 1024. (Looking at the OP, that should be $0 \le t < 1024$). Second, this is not the range of frequencies you get our of the FFT, and they are not in a simple order. Check for instance the GSL manual.

9. Sep 23, 2013

### btb4198

Yes it is 0 to 1023
So I am not sure what you are trying to say... I looked at that site you sent me but I am missing something..

are you saying I should use less than 0 to 1023?

10. Sep 23, 2013

### Staff: Mentor

These are the indices, but not the frequencies. Here is the relevant information from the link a gave you:

For physical applications it is important to remember that the index appearing in the DFT does not correspond directly to a physical frequency. If the time-step of the DFT is \Delta then the frequency-domain includes both positive and negative frequencies, ranging from -1/(2\Delta) through 0 to +1/(2\Delta). The positive frequencies are stored from the beginning of the array up to the middle, and the negative frequencies are stored backwards from the end of the array.

Here is a table which shows the layout of the array data, and the correspondence between the time-domain data z, and the frequency-domain data x.

index z x = FFT(z)

0 z(t = 0) x(f = 0)
1 z(t = 1) x(f = 1/(n Delta))
2 z(t = 2) x(f = 2/(n Delta))
. ........ ..................
n/2 z(t = n/2) x(f = +1/(2 Delta),
-1/(2 Delta))
. ........ ..................
n-3 z(t = n-3) x(f = -3/(n Delta))
n-2 z(t = n-2) x(f = -2/(n Delta))
n-1 z(t = n-1) x(f = -1/(n Delta))

11. Sep 23, 2013

### btb4198

so I do know that "These are the indices, but not the frequencies. " I am not sure what this has to do with why I get
0 Hz
4000Hz

for the real values ... I take the indices that have do not have 0 in them and do

F = (index number /1024) * 8000

that is how you get the frequencies.

what do you mean by "the time-step of the DFT is \Delta"?

12. Sep 23, 2013

### Staff: Mentor

What is that 8000?

The increment between two consecutive values in the original data. In your case, that means 1 ms.

The first element you get back, f[0], contains the Fourier component for a frequency of 0. The second element, f[1], for a frequency
$$\nu = \frac{1}{n \Delta} = \frac{1}{1024 \times 1\ \mathrm{ms}} \approx 0.977\ \mathrm{Hz}$$
and so on up to f[512], which is for frequency
$$\nu = \frac{1}{2 \Delta} = \frac{1}{2 \times 1\ \mathrm{ms}} = 500\ \mathrm{Hz}$$
which is the highest frequency you can measure for such a time step (1 ms). If you want to measure higher frequencies, you need to sample over a shorter time interval.

For f[512] to f[1023], you will have the results for negative frequencies, starting at $-500\ \mathrm{Hz}$ for f[512], up to $-0.977\ \mathrm{Hz}$ for f[1023]. (Note that the middle element, f[512], contains the result for both the positive and the negative maximum frequency.)

13. Sep 23, 2013

### btb4198

8000 is the sampling speed
Fs

14. Sep 23, 2013

### btb4198

why is 500 Hz the highest frequency i can measure?
Also, it is not really 1 ms.. I did a for loop from 0 to 1024 so i really do not have a time.. it is just was testing so the units could be sec, min, or hours... however that would be a very slow sinwave...

15. Sep 23, 2013

### Staff: Mentor

Look, it is hard to help you if you don't state your problem better. You start by saying that you do not get the right frequency, in Hz, for your DFT. But now it turns out that t doesn't have real units? If you want more help from me, please state more precisely what you need help with.

16. Sep 23, 2013

### btb4198

ok so I am getting the right Frequency 2000 HZ with the imaginary values but not with the real values...

and I wanted to know why ?

17. Sep 23, 2013

### btb4198

If I use the real values I get 0 Hz and 4000Hz
but with the imaginary values I get 2000 Hz and 6000Hz
do you understand what I asking ?

18. Sep 23, 2013

### Staff: Mentor

How do you know these values in Hz since
is not the correct formula?

19. Sep 23, 2013

### Staff: Mentor

Maybe we should try something different. What is the exact input to the FFT?

20. Sep 23, 2013

### btb4198

For ( int t = 0; t < 1024; t++)
{
buffer[t] = (10 * sin( 2 * PI * 2000 *t))/ 8000;
}

2000 Hz is the frequency
8000 Hz is the sampling speed
10 is the amplitude
N is 1024

I get the same values for both my FFT and DFT.