I have a signal that consists of a sine wave, on which I apply a rectangular window and then an FFT. I'm only interested in the magnitude of the sine wave, and I want it to stay the same even if the signal shifts a few samples to the right relative to the window (of course assuming the sine wave has the same amplitude from top to bottom). Is this possible and how can this be done best? The application is a kind of pitch detector where I have to detect a small group of different pitches.
2 Answers
You can't make an FFT ignore phase (for strictly real input), except for signals which have frequencies that are exactly periodic in the FFT window length.
Otherwise, you will have to interpolate to get any between bin magnitude peaks. And an accurate magnitude interpolation of any non-bin-centered frequency will depend on phase (easiest if you reference phase to the center of the data window), especially near the DC or Fs/2 bins, but to a much smaller extent, thru the rest of the FFT result as well.
I'm only interested in the magnitude of the sine wave, and I want it to stay the same even if the signal shifts a few samples to the right relative to the window
The absolute magnitude of the DFT result will be independent of the phase shift. Each DFT bin will give you a complex number of the form $A \ e^{j \ \theta}$. Circularly shifting your tone will only affect $\theta$. If you take the absolute magnitude, you will end up with $|A|$, all the while ignoring the phase.