This is a followup question to one I asked earlier based on the chat after the answer given by @hotpaw2. I have a signal which will be a single cosine wave, with a phase offset. My task is to extract (with very high accuracy required) the amplitude and phase of this single freuqnecy component.
On paper, the following relations hold assuming a properly normalized fourier transform T:
Unsurprisingly, there is a bit more to the DFT than simply taking the transform and picking off the relevent components. In particular, the discussion suggested to me that I was not entirely clear on what the phase offset is being measured with respect to, and that there are significant edge effects that can destroy the accuracy of the result if data is not properly windowed.
I have been googling around but most of the discussion is fairly technical and light on example, so I was hoping that someone can shed some light on things. In particular, I came across one example suggesting that instead of doing a simple transform, I should be shifting it first:
import numpy as np
import pylab as pl
f = 30.0
w = 2.0*np.pi*f
phase = np.pi/2
num_t = 10*f
t, dt = np.linspace(0, 1, num_t, endpoint=False, retstep=True)
signal = np.cos(w*t+phase)#+np.random.normal(0,0.25,len(t))
amp = np.fft.fftshift(np.fft.rfft(np.fft.ifftshift(signal)))
freqs = np.fft.fftshift(np.fft.rfftfreq(t.shape[-1],dt))
index = np.where(freqs==30)
print index[0][0]
print(np.angle(amp))[index[0][0]]
print (np.abs(amp))[index[0][0]]*(2.0/len(t))
pl.subplot(211)
pl.semilogy(freqs,np.abs(amp))
pl.subplot(212)
pl.plot(freqs,(np.angle(amp)))
pl.show()
So: first set of questions: can someone explain the point of using fftshift, and what exactly it is doing to the data? Why does using an inverse shift, transforming, and then shifting then require a frequency component set which is only shifted once, with no inverse opration? Is this approach the correct one (ignoring for the moment the issue of a window).
second set of questions: if I window my data, presumably this will affect the amplitude and likely the phase(?) of the result. Is there an analytical way to correct for the amplitude change for a given window shape? I can find a few tables that list correction factors, but I haven't really seen a good explanation yet.
In the linked question, it was stated that phase should be measured near the center of the hump of the window function. But because the window function is a time-domain function and I want the phase for a specific frequency, I don't quite understand what that means.
Any light that could be shed on the matter (perhaps in the form of references, since clearly I need to do some more reading) would be most appreciated.