# Emulate the effect of a lock-in amplifier on a signal with a programming script

I'm working on a gas emission sensing project in the lab which requires the extraction of a signal in a really noisy environment. The features of interests of my signal are much smaller than the noise components. Literature shows experimental setups for similar purposes using lock-in amplifiers to extract the signal. I have never heard of lock-in amplifiers before and my electronics skills are fairly limited. After a quick search on the Internet, I have realized that lock-in amplifiers might be too bulky for my experimental setup and I was wondering if I could emulate the effect of such amplifiers with a programming script.

Let's say I have a noisy sin signal with know parameters:

• Amplitude: 1 V
• Offset: 3 V
• Frequency: 4 Hz
• For this example, noise is generated in the script

Based on what I have read about lock-in amplifiers, I would first have to multiply it by a reference signal of the same frequency to eliminate the effect of everything at a frequency other than 4 Hz. Let's say I take a sin wave, with an amplitude of 1V and a frequency of 4 Hz. The result is the following signal:

The average of the signal array would be 0.5; this value might change slightly depending on the noise. Now if I give any frequency other than 4 Hz to my reference signal, this average would be around 0 which seems to validate the theory.

Then, based on my understanding, the signal would go through a low-pass filter. I have programmed a butterworth filter of 6th order, with a sample rate of 30 Hz and a cutoff frequency of 3 Hz. Please note that I am not sure on how these numbers should be changed based on what I am trying to accomplish.

The resulting signal after the filter looks like this:

Does this make any sense at all in terms of emulating the effect of a lock-in?

Another question I have is with regards to my feature of interests. Let's say the features of interest I am looking to extract, in a perfectly clean signal would look something like this:

Now let's say this signal is completely buried in noise. What would be the best way to extract it and have it as clean as possible? Would a lock-in amplifier do the job?

• Do you have access to the reference signal (frequency and phase, and amplitude if it varies) or do you have to attempt to recover it from the measured signal? A lock-in is essentially a synchronous demodulator. Normally the demodulator has a low pass filter so that the signal of interest is recovered over many reference cycles- so maybe minutes in your case. Commented Mar 24, 2016 at 17:23
• So basically, my signal is a laser signal that goes inside a gas cell which contains the gas of interests. The gas should absorb a certain % of the signal at a very narrow wavelength (this would result in something similar to what is in the red circle). I do have access to a cleaner signal, before it goes inside the cell. I just have to split my signal 50/50. What I am trying to extract is the really small absorption feature that is buried inside the noise after it passes inside the gas cell. Commented Mar 24, 2016 at 17:27
• You should be able to recover the signal that way, but I don't know if the performance would be adequate. We have an O2 analyzer that works with a similar principle. Commented Mar 24, 2016 at 17:30
• @LaGuille - Thanks for the vote, but in general you should wait at least 24 hours. After all, no matter how persuasive my answer is, someone might come along and shoot it full of holes, or even (gasp! heresy!) provide a better one. It's not like we're in a hurry here. Also, see my edit. Commented Mar 24, 2016 at 19:42

First, as Dave Tweed has answered, you generally use a lock-in to recover a small signal buried in noise.

That said, your script is not properly implementing a lock-in amplifier, as evidenced by your second trace. Your problem is that the DC component of your original signal needs to be suppressed (the signal should be AC-coupled). If your reference sine wave has a DC component of zero (which it should) then for a signal with zero degrees of phase shift and an average of zero, the output will be a sine-squared wave (plus noise). Note that this will be rectified, with no signal component negative.This will allow a low-pass filter to recover the amplitude of the desired frequency, but not its shape.

What you seem to be trying to do is simple noise rejection, and there are two possibilities. Either your noise is broadband, with significant noise energy both above and below your frequency of interest, or the noise is only significant above your fundamental.

Assuming the latter, you can process your signal using only a high-pass filter, made arbitrarily sharp and close to your fundamental. If the former, you need a bandpass filter.

In either case, looking at the crossover distortion shown in the last figure is going to be very, very difficult. That's a low-energy, high-frequency artifact, and may not be easily recovered from the noise. If you really want to try, the first thing you need to do is simulate your signal, then perform an FFT on it to establish the frequency response your filter needs in order not to exclude the signal of interest. Then compare this to the noise spectrum and you'll probably see that they overlap.

Other than an extremely large averaging filter (many, many waveforms averaged), I don't see any good way to recover your feature of interest.

EDIT - Having stated that a signal in noise needs a bandpass filter to recover it, I should explain that the multiplier used acts as what is called "mixer" in the RF world, and its effect is to frequency shift the signal by the reference frequency. This is useful in the case of the lock-in amplifier because it shifts the signal frequency to DC. In this case, a bandpass filter on the original signal becomes a low-pass filter on the processed signal, and the trick of the lock-in is that it's MUCH easier to make a very sharp, narrow lowpass filter than it is a very sharp, narrow bandpass filter. To begin with, the lowpass filter response is intrinsically referenced to DC, or zero Hz. This means that there is no central frequency of the filter to drift with time and temperature, which is a major problem with bandpass filters.

On the other hand, since the desired signal is now DC, you cannot recover the signal shape. Every deviation from the fundamental frequency (sine wave) shows up as a frequency deviation in the processed signal. If the artifact of interest is part of the signal at the base frequency, the frequency deviations show up as harmonics, and the closest to the fundamental is at twice the fundamental. This means that any close filtering will eliminate the part of the signal which corresponds to the glitch.

A lock-in amplifier is used to extract an extremely narrowband signal from a noisy channel.

The tiny glitch you're looking for is not narrowband. Instead of a lock-in amplifier, what you need is called an "analog signal averager". One of the best descriptions of this can be found in The Art of Electronics by Horowitz and Hill.

The basic principle is to sample the signal into a large number of "bins", synchronously with its period. After a sufficient number of periods have been averaged, the uncorrelated noise is reduced, and you're left with the original waveform, including all of its harmonics (the glitch).

• I've got the second edition here in front of me and it starts on pg. 760, for those following along at home. And hey, you can still buy 68008's on eBay! Commented Mar 24, 2016 at 18:14

The lock-in amplifier is not a magic bench amplifier that does better than a normal amplifier. Your example (DC offsets aside) seems to assume that. The LIA gets more involved in the experiment than that.

The idea is to deliberately modulate the cause and then use the LIA to recover the effect. OR, if you know the cause is already modulated, detect or predict that, as the reference signal.

So for a gas example, imagine a cell for measuring gas concentration by its absorption. It has a narrow band light source and a light sensor. This would work in the dark, but unfortunately the sensor also picks up environmental light. You don't want the environmental light looking like a change in gas concentration.

So... you periodically modulate the input light to the gas sensor cell.

Now the noisy gas-sensor signal with environmental contamination, and the reference signal, are fed to the LIA. The LIA can amplify the noisy gas-sensor signal, using the reference signal to know whether this was a time of the cycle with more or less light emitted by your source. It does this by multiplying the two, and then integrating. Or in the frequency domain, mixing the desired signal down to baseband and then filtering off the higher frequencies.

The filter must be at a much lower frequency than the modulation, so if you modulated the input to the cell at 4 Hz, you'd filter at 0.25 Hz, or something. As you can imagine, you couldn't measure gas concentration variations as fast as the flashing of the light source. In practice, if you have a choice, you might modulate it much faster than the response time you need.