# Using the FFT to align two instances of the same signal

I'm working on a program that has a software oscilloscope-like viewer for audio signals. The scope basically takes in blocks of signals at a regular rate and adds them to its existing signal data. When requested by the system, it renders the signal to the screen.

As you can imagine, the naive implementation of this results in annoying jittering of the signal because it is not being drawing at the same phase every frame.

My first attempt to solve this problem was to simply find the maximum of the signal in each successive frame and put the maximum at some fixed reference location. This mostly worked, as long as the signal didn't have two maximums of nearly the same value, which unfortunately does happen fairly regularly.

I also happen to be attempting to estimate the fundamental frequency of the signal via the FFT, which works quite well. The estimate of the fundamental frequency is very stable. My basic approach is to find the maximum quadratic interpolated magnitude of the DFT bins, but with some extra checks to see if the maximum is a harmonic of some lower fundamental frequency.

I believe I can use the phase information from here to solve the problem of the signal jittering. I've implemented what I think should work, which is to get the phase of the interpolated bin where the fundamental frequency was found, and I then shift the signal accordingly to always align the signal to the same phase.

This actually does clearly work somewhat, because the signal is stabilized quite well, despite multiple nearby maximums of the signal. The problem is that it appears to have two specific stabilized locations that it will flicker between. This is still a huge improvement over not trying to fix this at all, but I'd like to get it perfectly stabilized if possible. I don't understand why there would be any ambiguities in my procedure above.

A few more hints:

• This issue occurs even with computed input test signals that should not contain any noise.
• The two solutions don't appear to be some interesting offset from eachother as far as I can tell.
• I'm also visually reporting the estimated frequency of the signal, and this value is stable even with the signal visualization is not, i.e. they are not changing together.

My question is: is this ambiguity of solutions a mathematical issue? It's possible it's a code bug but I've verified as much as humanly possible that the code is correctly implementing what I'm trying to do.

Hard to know without knowing more details. One possible explanation: assume your signal has a second (or higher) armonic that has greater amplitude than that of the fundamental $\omega_0$ (or put in another way: it's the sum of a sinusoid of frequence $\omega_0$ plus a sinusoid of double freq $\omega_1 = 2 \omega_0$, and the second signal has more amplitude). In this scenario, you'd get $\omega_1$ as you main frequency, which is not you really want. 