I have a clean square-wave signal and a noisy signal from an accelerometer, perfectly synchronized to each-other but out of phase. I want to find the phase difference between them in real time, in software (I am building a dynamic balancer). I plan to convert the clean signal to a (complex) phasor in real time and multiply that by the noisy signal, then integrate over a moving window. I should be able to measure the phase angle of the integral to get the answer. Does this sound like the right approach? It would seem to cancel out the other frequencies automatically. I have considered measuring the frequency of the clean signal and filtering the noisy signal using a band-pass filter around that frequency, but it seems unnecessary.
Here is how I would do it:
1) Find the frequency of the clean signal, this should be trivial in the time domain for a clean square wave.
2) Heavily smooth the noisy signal with a symmetric filter. (IIR or FIR)
3) Calculate a single DFT bin for each signal using the length of a whole number of cycles for your frame. This will make the harmonics leakless, and make your fundamental measurement more accurate. The bin number you calculate should be the number of periods in your frame.
4) Calculate the phase for each signal relative to the frame at the fundamental frequency. (Straight from the DFT bin value)
5) Take the difference in the two phases and translate if from an angle to a time using the frequency.
This is going to be way more efficient than some cross-correlation type calculation.