# 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.

## 1 Answer

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.

For example, in the signal below, you would detect the period marked in the gray box, and, then, both red an blue aligments would be considered equivalent because, for that frequency, they have the same phase. You want to detect not simply the highest value frequency in the FFT, but the smallest frequency that shows a relevant maximum. Or, more or less equivalently: you want to detect, not the highest valued frequency, but the pitch.

• Thanks for the detailed answer, but I am already doing this. I probably simplified too much in the original question, but I'm not just grabbing the maximum amplitude bin. What I should have said in the original question is that the jittering is independent of the estimated frequency: that is, the estimated frequency will stay constant while the signal jitters. If this was the problem, I would expect the estimated frequency to change at the same time the signal jumps. – dsharlet Oct 14 '13 at 17:35
• Not exactly. In the above example, the blue and red would have the same detected frequency (the greatest), it's only that the phase is ambiguous, so you have a "jitter" which is half (or another integer fraction) of that estimated frequency. – leonbloy Oct 14 '13 at 18:05