Signal Recovery Using a Digital Lock-in Amplifier.

At school, in math class, we were taught to describe a curve with an equation based on its shape. For a person this task is considered simple, but if you try to hand it over to a microcontroller, you'll see that even recognizing a sine wave is not that easy.

Let's imagine that we have fed a sinusoidal signal to an ADC input and collected a few dozen samples. If we plot these samples on a sheet of paper, we can easily determine the amplitude, phase and frequency, and using them, write the equation of the curve in the form A*sin(wt+q). With an MCU we can easily calculate the amplitude as the difference between the maximum and minimum values, and the frequency can be calculated in different ways. But how do we calculate the phase?

It turns out there is no simple solution to this problem, and it requires slightly more complex math. Below we'll look at one way to solve it, known as the Digital Lock-in Amplifier.
Signal Recovery Using a Digital Lock-in Amplifier.

The idea is that we essentially pass the data obtained from the ADC through two mixers, where it is multiplied by known reference signals, and then this mixture is passed through a low-pass filter. At this point you need to understand that the reference signals are stored in the MCU memory as arrays for a given IF.

Now that the general concept is clear, let's look at the math of the process. For that we'll need another picture.
Signal Recovery Using a Digital Lock-in Amplifier.

The picture shows that we represent the signal arriving at the ADC as A*sin(wt+q), and the reference signals as B*cos(wt+p) and B*sin(wt+p). I'd like to note that the ADC signal can also be represented as A*cos(wt+q), and that is what we will do.
Signal Recovery Using a Digital Lock-in Amplifier.

Signal Recovery Using a Digital Lock-in Amplifier.

In the first step, as mentioned earlier, we mix the signal from the ADC with the reference signals. To do this, let's recall the necessary trigonometric formulas.
Signal Recovery Using a Digital Lock-in Amplifier.

And apply them.
Signal Recovery Using a Digital Lock-in Amplifier.

Signal Recovery Using a Digital Lock-in Amplifier.

In the next step we have to pass both signals through a low-pass filter, which suppresses the higher harmonics, and the formulas take the following form.
Signal Recovery Using a Digital Lock-in Amplifier.

The resulting components can be used to calculate the amplitude
Signal Recovery Using a Digital Lock-in Amplifier.

and the phase.
Signal Recovery Using a Digital Lock-in Amplifier.

The frequency in this case is known and doesn't need to be calculated. I'll also leave one more picture explaining where the formulas come from.
Signal Recovery Using a Digital Lock-in Amplifier.

It may seem that a single measurement is enough for the calculations, but that is not the case. We have only looked at the general outline of the algorithm, leaving out the implementation details.