I am having trouble finding a known phase between two data sets, lets call them data set 11 and data set 12. I am obtaining a sample size of 1024 samples, at a sampling rate of 500kHz with a total sample time of 2.048ms.
All data and code used in this question can be found at this repository - https://github.com/smake5730/fft_phase/graphs/contributors
As can be seen from the sample data below the two data sets show the voltage is leading the current by around 90°. (zoomed in)
When I compute the FFT with 1024 bins I get clear peaks. (zoomed in)
I apply the Hanning window during this and do not use zero-padding.
I use the location of the largest peak with the atan2(a/b) method to obtain the phase and find that I get the following below. The first data gives me +90° and the second data gives me -90°. The frequencies are correctly identified.
I have tried adding zero-padding to the end of the signal and it does not solve the issue. I have also tried upsampling and it does not give the correct phase either.
Does anyone have any ideas/hints on what I could try or where I am going wrong?
EDIT As was pointed out below, my code takes individual bins and incorrectly matched bins were used in the second set.