I have been attempting to make a basic, slow, DFT in Matlab and have noticed peculiar behavior that I don't understand.

I have been trying to plot the phase of a 100Hz sinewave captured at 250kHz sample rate with a record length of 10k points. I find that the real part of the DFT is negative at 100 Hz, which I think makes sense due to a sinewave having negative correlation with a cosine wave. However, the imaginary part at 100Hz is also negative, which again makes sense because a sinewave is shifted -90 degrees from a cosine wave.

When I take the inverse tangent of the real/imaginary, obviously this is a positive number. Resulting in a +90 phase shift instead of a -90. Am I misunderstanding something, or do I just have incorrect results?

edit: I've realized the issue is that due to spectral leakage and the fact that this operation there are non-zero magnitudes that need to be filtered out before calculating the phase.

Is there a good way to choose a threshold for when to zero out DFT results? It seems a hard value to choose as if I throw in an arbitrary waveform then I have no information beforehand to choose a proper threshold value. My initial idea is to calculate the bin size based on the time length of the input, and then use a lookup table to change the threshold to be 10%, 1%, 0.1%, etc, of the maximum magnitude in the DFT.

  • 1
    $\begingroup$ I think you need to use atan2. $\endgroup$
    – MBaz
    Jan 23 '19 at 18:13

4 full cycles of a sine wave (starting at a phase of 0) is an odd function in aperture, so the real part of DFT result bin 4 should be zero (or nearly so with numerical noise).

atan2(-1,0) should result in -pi/2


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.