Hi Jonas. It's common to express the spectrum on polar format, like X(w) = |X(w)|*exp(j*phi(w)), while the FFT routine pops out numbers on carthesian format, X(w) = Re[X]+j*Im[x]. Try to convert your spectrum to polar format, plot the magnitude spectrum (i.e. |X(w)| as function of angular frequency w) and see if the results are closer to what you expect. Rune
Using Re[x] or Im[x] from FFT result
Started by ●September 16, 2004
Reply by ●September 17, 20042004-09-17
Thank you all for your replies. However, I cannot tell from your answers wheather my results are correct or not. To illustrate my problem I have collected my data in a diagram. I send a 8 kHz sine wave through my speakers and record it with my mic. Then i do DFT using the FFTW3 library, and plot the result i the diagram below: http://www.rundberg.com/jonas/fftresults.gif The sine peak (taken from Im[x]) is at 7988 Hz and the cosine peak (taken from Re[x]) is at 8031 Hz. That is 43 Hz or 2 bins away from each other. Is this a result to expect? Maybe is has to do with the library used? Can I use the average (8010 Hz) of the two peaks? Thank you / Jonas Rundberg
Reply by ●September 17, 20042004-09-17
I think there's a fundamental misunderstanding here: if you want to get the peak location of your input frequency you'll have to treat the numbers as what they are: complex numbers, not separate sines and cosines. IOW: You need them *both* to find your peak, by taking the magnitude of the complex number stored in your DFT bins. That's also what we've been trying to say with the previous posts. Does that help? -- Stephan M. Bernsee http://www.dspdimension.com
Reply by ●September 17, 20042004-09-17
Jonas Rundberg wrote:> Thank you all for your replies. > > However, I cannot tell from your answers wheather my results are > correct or not. To illustrate my problem I have collected my data in a > diagram. > I send a 8 kHz sine wave through my speakers and record it with my > mic. Then i do DFT using the FFTW3 library, and plot the result i the > diagram below: > http://www.rundberg.com/jonas/fftresults.gif > The sine peak (taken from Im[x]) is at 7988 Hz and the cosine peak > (taken from Re[x]) is at 8031 Hz. That is 43 Hz or 2 bins away from > each other. > > Is this a result to expect? Maybe is has to do with the library used? > Can I use the average (8010 Hz) of the two peaks? > > Thank you > / Jonas RundbergAt each frequency, the amplitude is sqrt(Re[x]^2 + Im[x]^2) and the phase is arctan(re[x]/Im[x]). As Rune pointed out in the sister thread, you are looking at two frequencies. One is nearly pure sine and the other, pure cosine. (That's not really a coincidence, but let's not go into it.) Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
Reply by ●September 17, 20042004-09-17
"Stephan M. Bernsee" <spam@dspdimension.com> wrote in message news:2qvod6F137plfU1@uni-berlin.de...> > I think there's a fundamental misunderstanding here: if you want to get > the peak location of your input frequency you'll have to treat the > numbers as what they are: complex numbers, not separate sines and > cosines. IOW: You need them *both* to find your peak, by taking the > magnitude of the complex number stored in your DFT bins. That's also > what we've been trying to say with the previous posts. > > Does that help?Jonas, The other thing to note is that your frequency sampling interval appears to be around 22Hz. So, you can't expect to resolve the peak better than that without some interpolation. No matter, it appears you do get a double peak from the data shown. So you might question the setup and the signal source. I'd also question the length of the data and the method of calculating the FFT. If the data is indeed around 45msec long then you'd have around 22Hz resolution. That seems a rather short time for a physical system like a speaker - so are you sure you're taking the measurement at steady state? Also, if the data is shorter than 45msec and you've zero-padded to get 22Hz sample intervals in frequency, then you have worse resolution than 22Hz. Fred
Reply by ●September 17, 20042004-09-17
Jerry Avins wrote: ...> phase is arctan(Re[x]/Im[x]). ...Pardon the error! Glen Hermannsfeld caught it. he wrote, "I thought phase was atan(Im[x]/Re[x])", and of course he's right. Phase goes to zero when Im[x] does. Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
Reply by ●September 17, 20042004-09-17
Jerry Avins wrote:> Jerry Avins wrote: > > ... > >> phase is arctan(Re[x]/Im[x]). ... > > > Pardon the error! Glen Hermannsfeld caught it. he wrote, > "I thought phase was atan(Im[x]/Re[x])", and of course he's right. > Phase goes to zero when Im[x] does. > > JerryAnd the next level is to notice that ATAN2 is usually recommended to allow the phase to cover the full circle rather than just half of it. Consider what happens when you negate both the real and imaginary parts.
Reply by ●September 17, 20042004-09-17
Gordon Sande wrote: (snip)>> "I thought phase was atan(Im[x]/Re[x])", and of course he's right. >> Phase goes to zero when Im[x] does.> And the next level is to notice that ATAN2 is usually recommended > to allow the phase to cover the full circle rather than just > half of it. Consider what happens when you negate both the real > and imaginary parts.Well, it originally said arctan and I changed it to atan. I would say it depends on whether you are a mathematician or engineer. Im[] and Re[] are mathematics notation, and arctan is a multivalued function in mathematics. I would say, then, atan2(imag(x),real(x)) for Fortran, PL/I, and maybe C99. -- glen
Reply by ●September 17, 20042004-09-17
Gordon Sande wrote:> > > Jerry Avins wrote: > >> Jerry Avins wrote: >> >> ... >> >>> phase is arctan(Re[x]/Im[x]). ... >> >> >> >> Pardon the error! Glen Hermannsfeld caught it. he wrote, >> "I thought phase was atan(Im[x]/Re[x])", and of course he's right. >> Phase goes to zero when Im[x] does. >> >> Jerry > > > And the next level is to notice that ATAN2 is usually recommended > to allow the phase to cover the full circle rather than just > half of it. Consider what happens when you negate both the real > and imaginary parts.I think of ATAN2 as a computational tool, rather than a mathematical function. Does that make me a stick-in-the-mud? Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
Reply by ●September 17, 20042004-09-17
atan2 is generally a better choice if the library is well written. It handles a lot of error cases so you don't have to. In article <414b4695$0$2668$61fed72c@news.rcn.com>, Jerry Avins <jya@ieee.org> wrote:>Gordon Sande wrote: >> >> >> Jerry Avins wrote: >> >>> Jerry Avins wrote: >>> >>> ... >>> >>>> phase is arctan(Re[x]/Im[x]). ... >>> >>> >>> >>> Pardon the error! Glen Hermannsfeld caught it. he wrote, >>> "I thought phase was atan(Im[x]/Re[x])", and of course he's right. >>> Phase goes to zero when Im[x] does. >>> >>> Jerry >> >> >> And the next level is to notice that ATAN2 is usually recommended >> to allow the phase to cover the full circle rather than just >> half of it. Consider what happens when you negate both the real >> and imaginary parts. > >I think of ATAN2 as a computational tool, rather than a mathematical >function. Does that make me a stick-in-the-mud? > >Jerry






