DFT/FFT Imaginary vs real values

  • Thread starter btb4198
  • Start date
  • Tags
    Imaginary
In summary, the DFT and FFT can be used to graph the imaginary number of a function to determine its frequency. However, when graphing the real numbers, the frequency may appear as 0 Hz or 4000 Hz due to a programming error. It is important to note that the index used in the DFT does not correspond directly to a physical frequency, and both positive and negative frequencies must be taken into account.
  • #1
btb4198
572
10
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 ?
 
Engineering news on Phys.org
  • #2
btb4198 said:
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
@ 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
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
DrClaude said:
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
btb4198 said:
Why at 250 Hz?

I am setting it to 2000 Hz?

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

btb4198 said:
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
@ 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
btb4198 said:
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.
btb4198 said:
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
DrClaude said:
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
btb4198 said:
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
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
btb4198 said:
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?

btb4198 said:
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
8000 is the sampling speed
Fs
 
  • #14
DrClaude said:
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
btb4198 said:
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 ?

btb4198 said:
so t would be in ms.

btb4198 said:
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 said:
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
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
btb4198 said:
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
btb4198 said:
F = (index number /1024) * 8000
is not the correct formula?
 
  • #19
Maybe we should try something different. What is the exact input to the FFT?
 
  • #20
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
btb4198 said:
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
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
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
btb4198 said:
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.

btb4198 said:
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)
$$

btb4198 said:
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
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
btb4198 said:
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
jasonRF said:
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.

jasonRF said:
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
btb4198 said:
how do you know the maximum frequency is 4000?
Nyquist frequency

btb4198 said:
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.

btb4198 said:
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
jasonRF said:
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
DrClaude said:
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 ?
 

1. What is the difference between imaginary and real values in DFT/FFT?

The discrete Fourier transform (DFT) and fast Fourier transform (FFT) are mathematical algorithms used in signal processing to convert a signal from its original domain (often time or space) to a representation in the frequency domain. The output of these transforms includes both real and imaginary values, which represent the magnitude and phase of the signal's frequency components, respectively.

2. Why do DFT/FFT outputs have both imaginary and real values?

The use of both imaginary and real values in DFT/FFT outputs allows for a more complete representation of the frequency components of a signal. The real values represent the amplitude or magnitude of the signal's frequency components, while the imaginary values represent the phase or timing of those components. Together, these values provide a more detailed description of the signal in the frequency domain.

3. How are imaginary and real values used in DFT/FFT analysis?

Imaginary and real values are used in various ways in DFT/FFT analysis, depending on the specific application. In general, the real values are used to calculate the magnitude spectrum of a signal, while the imaginary values are used to calculate the phase spectrum. These values can also be used to reconstruct the original signal in the time or space domain.

4. Can imaginary values be negative in DFT/FFT outputs?

Yes, imaginary values can be negative in DFT/FFT outputs. This is because the imaginary values represent the phase of a signal's frequency components, which can be positive or negative. Negative imaginary values indicate a phase shift of 180 degrees, while positive values indicate a phase shift of 0 degrees.

5. How do imaginary and real values affect the accuracy of DFT/FFT analysis?

The accuracy of DFT/FFT analysis is not affected by the presence of imaginary and real values in the outputs. These values are necessary for a complete representation of the signal in the frequency domain and do not impact the accuracy of the analysis. However, errors in the input signal or the DFT/FFT algorithm itself can affect the accuracy of the results.

Similar threads

  • Electrical Engineering
Replies
4
Views
786
Replies
6
Views
965
  • Electrical Engineering
Replies
8
Views
903
  • Electrical Engineering
Replies
10
Views
2K
  • Electrical Engineering
Replies
1
Views
921
  • Electrical Engineering
Replies
7
Views
1K
  • Electrical Engineering
2
Replies
41
Views
3K
  • Electrical Engineering
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
2K
Replies
3
Views
521
Back
Top