# DFT/FFT Imaginary vs real values

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 ?

## Answers and Replies

DrClaude
Mentor
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 ?
What are the units of ##t##?

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

@ 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.

DrClaude
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}##.

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}##.

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 .

DrClaude
Mentor
Why at 250 Hz?

I am setting it to 2000 Hz?

F[t] = 10 Sin((2 * PI * 2000 *t)/8000)
2000/8 = 250.

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 .
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]?

@ 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

DrClaude
Mentor
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]
Yes, this is what I meant. Sorry, I should've made my notation clear.
then for both my FFT and DFT, I am using 0 - 1024
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.

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.

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?

DrClaude
Mentor
are you saying I should use less than 0 to 1023?
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))

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"?

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

F = (index number /1024) * 8000
What is that 8000?

what do you mean by "the time-step of the DFT is \Delta"?
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.)

8000 is the sampling speed
Fs

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.)

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...

DrClaude
Mentor
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 ?

so t would be in ms.

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...
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.

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 ?

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 ?

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 ?

DrClaude
Mentor
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 ?
How do you know these values in Hz since
F = (index number /1024) * 8000
is not the correct formula?

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

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.

DrClaude
Mentor
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.

Ok, now I get it!

Setting f = FFT(buffer), the FT of buffer should give f = 0. everywhere, expect the imaginary part of f[256] and f[768].

DrClaude
Mentor
Maybe I should explain how to figure out which elements are non-zero.

The frequency of the signal is ##\nu = 2000##, while the sampling spacing is ##\Delta = 1/8000##. You should get peaks at ##\pm 2000##, that is for
$$\frac{k}{n \Delta} = 2000 \Rightarrow k = 256$$
and for
$$\frac{k - n}{n \Delta} = -2000 \Rightarrow k = 768$$
Since the signal is a sine wave, it will be purely imaginary.

(By the way, I actually checked that with code. If your result differ, it means your FFT code is buggy.)

I get 2000 and 6000

should I use (K-n)/(change n) ?

I am using f = (index number / N) * Fs
that is how i get f= 2000 and 6000

also sine wave are purely imaginary?
I did not know that...
so that is why my real values are wrong ?

I get
F = 0 Hz
F = 4000Hz
for my real values

also I am using F = (index number /N) * Fs
fs is sampling F

DrClaude
Mentor
I get 2000 and 6000

should I use (K-n)/(change n) ?

I am using f = (index number / N) * Fs
that is how i get f= 2000 and 6000
Yes, just follow the math, which I gave in details. That is 2000 and -2000, not 6000. With the sampling you have, the maximum frequency is 4000.

also sine wave are purely imaginary?
I did not know that...
so that is why my real values are wrong ?
It comes from the trigonometric expression of the exponential:
$$e^{2 \pi i \nu t} = \cos( 2 \pi\nu t) + i \sin( 2 \pi\nu t)$$

so that is why my real values are wrong ?
I on't know what is wrong with your program, but indeed the real part should be 0.

how do you know the maximum frequency is 4000?

are you saying the F should = 0 ?
are that that all the indices should have 0 in them ?
Δ=1/8000,
what is n ?

ok I do get 0 for one of the real sin F
but I also get 4000Hz