DFT/FFT Imaginary vs real values

  • Thread starter btb4198
  • Start date
  • #1
btb4198
557
9
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

  • #2
DrClaude
Mentor
7,797
4,334
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.
 
  • #3
btb4198
557
9
@ 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
DrClaude
Mentor
7,797
4,334
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
btb4198
557
9
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 .
 
  • #6
DrClaude
Mentor
7,797
4,334
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]?
 
  • #7
btb4198
557
9
@ 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
DrClaude
Mentor
7,797
4,334
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.
 
  • #9
btb4198
557
9
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?
 
  • #10
DrClaude
Mentor
7,797
4,334
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))
 
  • #11
btb4198
557
9
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
DrClaude
Mentor
7,797
4,334
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.)
 
  • #13
btb4198
557
9
8000 is the sampling speed
Fs
 
  • #14
btb4198
557
9
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...
 
  • #15
DrClaude
Mentor
7,797
4,334
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.
 
  • #16
btb4198
557
9
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 ?
 
  • #17
btb4198
557
9
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
DrClaude
Mentor
7,797
4,334
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?
 
  • #19
DrClaude
Mentor
7,797
4,334
Maybe we should try something different. What is the exact input to the FFT?
 
  • #20
btb4198
557
9
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.
 
  • #21
DrClaude
Mentor
7,797
4,334
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].
 
  • #22
DrClaude
Mentor
7,797
4,334
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.)
 
  • #23
btb4198
557
9
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
 
  • #24
DrClaude
Mentor
7,797
4,334
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.
 
  • #25
btb4198
557
9
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
 
  • #26
jasonRF
Science Advisor
Gold Member
1,494
560
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.

Since your 8000 is outside both sets of parentheses, your amplitude is 10/8000 and your sample rate is effectively 1 Hz. You are evaluating sin at integer multiples of PI; due to finite precision these numbers are not all zero, but should all be small. the resulting signal may have some periodic structure due to the precision effects (in matlab single precision I see a sawtooth structure with amplitudes the order of 1e-9), but you are essentially looking at numerical noise here. Try fixing your parentheses.

By the way, you have been asking these types of questions for about a month now:
https://www.physicsforums.com/showthread.php?t=706713
https://www.physicsforums.com/showthread.php?t=708525

so I am lead to wonder what reading have you done on DFT, FFT, and discrete signals and systems? At some point you have to sit down and work through simple examples with pencil and paper. Then you can test your code with those simple examples to make sure it doesn't have bugs.

jason
 
  • #27
DrClaude
Mentor
7,797
4,334
Since your 8000 is outside both sets of parentheses, your amplitude is 10/8000 and your sample rate is effectively 1 Hz.
I think that was a typo. In the OP, the /8000 is inside the sine.

I am lead to wonder what reading have you done on DFT, FFT, and discrete signals and systems
My impression now is not much...
 
  • #28
DrClaude
Mentor
7,797
4,334
how do you know the maximum frequency is 4000?
Nyquist frequency

are you saying the F should = 0 ?
are that that all the indices should have 0 in them ?
I'm saying that all the elements of the array containing the Fourier transform should be zero, except two (corresponding to ±2000 Hz) in their imaginary parts.

what is n ?
The number of sampling points.

You don't seem to be paying enough attention to what I posted before. And in light of what jason pointed out, it is clear that you did not take the time to properly understand FT. Try and study the theory first and come back to PF if you have specific questions. This is the last you will hear from me on this thread.
 
  • #29
btb4198
557
9
Since your 8000 is outside both sets of parentheses, your amplitude is 10/8000 and your sample rate is effectively 1 Hz. You are evaluating sin at integer multiples of PI; due to finite precision these numbers are not all zero, but should all be small. the resulting signal may have some periodic structure due to the precision effects (in matlab single precision I see a sawtooth structure with amplitudes the order of 1e-9), but you are essentially looking at numerical noise here. Try fixing your parentheses.

By the way, you have been asking these types of questions for about a month now:
https://www.physicsforums.com/showthread.php?t=706713
https://www.physicsforums.com/showthread.php?t=708525

so I am lead to wonder what reading have you done on DFT, FFT, and discrete signals and systems? At some point you have to sit down and work through simple examples with pencil and paper. Then you can test your code with those simple examples to make sure it doesn't have bugs.

jason

Jason that was a taypo.

Also I have a done a lot of reading on FFT and DFT
and I have done both a FFT and a DFT by hand...
and I have wrote on my code for both of them...
 
  • #30
btb4198
557
9
Nyquist frequency


I'm saying that all the elements of the array containing the Fourier transform should be zero, except two (corresponding to ±2000 Hz) in their imaginary parts.


The number of sampling points.

You don't seem to be paying enough attention to what I posted before. And in light of what jason pointed out, it is clear that you did not take the time to properly understand FT. Try and study the theory first and come back to PF if you have specific questions. This is the last you will hear from me on this thread.

I do think I understand FFT very well.
I can do one by hand.
I just do not see why I an not getting non zero for my real values.

are you sure you are getting non zero for your real values ?
 

Suggested for: DFT/FFT Imaginary vs real values

  • Last Post
Replies
4
Views
1K
  • Last Post
Replies
6
Views
1K
  • Last Post
Replies
9
Views
1K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
3
Views
838
  • Last Post
Replies
9
Views
2K
  • Last Post
Replies
6
Views
2K
  • Last Post
Replies
5
Views
2K
  • Last Post
Replies
1
Views
6K
  • Last Post
Replies
4
Views
3K
Top