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.


1 Answer 1


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.

  • $\begingroup$ Thanks. I implemented this and it works well, better than my original plan would have worked. I omitted (2) because that would have been expensive in cpu time. $\endgroup$ Jan 14, 2019 at 19:39
  • $\begingroup$ @RossDonelly, I'm glad to hear it, thanks for letting me know. You should click the "accepted" mark if it did the trick. That's a Stack Exchange thing. The smoothing shouldn't really be any heavier than a single bin calculation, but you are right, it is optional. The DFT itself has a smoothing effect. Smoothing it ahead of time simply reduces the impact of outliers. $\endgroup$ Jan 14, 2019 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.