I am dealing with data which are essentially are sinusoidal with various degrees of noise. I have an interest in extracting the phase of the data and rather than simply fitting a sine wave, I would like to extract it from the fourier space as there is evidence that there may be some level of phase drift in our data and I want to avoid fitting a chirp.
I am simulating the system with a pure tone version. Although I can extract the frequency just fine, I struggle to get the value for the phase when dealing with the ArcTan[Re,Im]. Am I missing something? Many thanks!
A = 0.5; (* amplitude *)
fc = 10; (*frequency *)
\[Phi] = 30 \[Pi]/180; (* phase in radians *)
fs = 32 fc; (* sample rate *)
t = Table[ A Sin[2 \[Pi] fc x + \[Phi]], {x, 0, 2, 1/fs}]; (* the data *)
(* extract relevant parts from the fourier: Re, Im, Magnitude and Arctan[Im/Re]*)
{re, im, abs, arg} = #@Fourier[t, FourierParameters -> {-1, 1}] & /@ {Re, Im, Abs, Arg};
(* generate the frequency space based on the sample rate used *)
freqSpace = Table[(n - 1.) fs/Length[abs], {n, Length[abs]}];
ListPlot[{freqSpace, abs}\[Transpose][[;; 50]], FrameLabel -> {"frequency Hz", "|X(f)|"}, Filling -> Axis]
unitiseArg = arg* Unitize@Threshold[abs,0.2]; (* extract the arctan based on located freq peak *)
ListPlot[{freqSpace, 180/\[Pi] unitiseArg}\[Transpose][[;; 50]], GridLines -> {{10}, {\[Phi] 180 /\[Pi], -\[Phi] 180 /\[Pi]}}, FrameLabel -> {"Frequency", "Phase , deg"}]
Wouldn't one expect to read the phase here to be 30 deg rather than 55 deg?
Many thanks!