1
\$\begingroup\$

As a part of signal conditioning stage I am trying to find the moving mean/moving average of a signal at the output of instrumentation amplifier (used LM324-N for instrumentation amplifier).

Is it possible to find the moving average of a signal using analog circuitry?

Some constraints -

  1. Signal characteristics -
    • Frequency range - 0 to 2 Hz
    • Amplitude dynamic range - 0 to 3.5 V
  2. Window size to calculate the moving average = 5 sec (max)

  3. Preferably op-amp to be used, so as not to overload the output of instrumentation amplifier

  4. If op-amp to be used, it must be single supply (preferably LM324-N)

A rough diagram of my setup

Edit 1:-

The signal-

  • Frequency Range = This is unfiltered signal, but will be band-limited to 0 to 2 Hz
  • Amplitude is of 10 bit ADC, Reference = 5V
  • Baseline shifts at approx. = 10/20/30/40/48 sec
  • -

Signal plot

Imp. Note - I am using moving average filter for baseline drift correction, and have already achieved in software. The doubt was if this can be done in pure analog circuit alone?

\$\endgroup\$
  • \$\begingroup\$ What is the S/N ratio before and after you expect and what is the noise spectrum? What group delay is tolerated as the breakpoint introduces a phase shift whose slope is time delay? ( often seen on stock market charts) These parameters are needed to optimize your results. i.e. to match the filter of your spectrum pass filter and block the noise spectrum. Both combined create ideal moving average. Your hand drawing shows about 6f e.g. 2Hz and 12Hz noise with 0dB SNR ( signal=noise level) \$\endgroup\$ – Sunnyskyguy EE75 Mar 29 at 7:06
  • 2
    \$\begingroup\$ This is one case where MCUs are superior. A simple low cost MCU with a very simple program can do this. And you can get a high accuracy analog signal out of it by rectifying PWM with a single op amp. What's the reason you can't use a MCU? \$\endgroup\$ – Lundin Mar 29 at 7:59
  • \$\begingroup\$ @Lundin To minimize the cost \$\endgroup\$ – Kulbhushan Chand Mar 29 at 8:35
  • 2
    \$\begingroup\$ Development cost or BOM cost? The solution I propose is a few dollars of BOM, but you'll need a couple of weeks of programmer time. \$\endgroup\$ – Lundin Mar 29 at 8:38
  • 1
    \$\begingroup\$ Smells like an XY problem. \$\endgroup\$ – Andy aka Mar 29 at 8:44
4
\$\begingroup\$

Getting a precise analogue of a digital moving average filter is somewhere between very difficult and impossible using analogue circuitry, as it involves delaying a signal for a long time.

There are various approximations you could use, which might (or might not) be acceptable to your particular application.

The first is a simple lowpass RC filter. Instead of the 'box car' impulse response of a moving average, it gives you a decaying exponential. For many signals and applications, this will result in similar dynamic behaviour. It is however a weighted average.

The second is a gated integrator, zeroed at time = 0, and read out at time = t_window. While this implements the unweighted average over the window time, it only produces one sample at the end. You could run a number of these with differing start times to produce samples more frequently, but this rapidly becomes silly in terms of duplicated hardware.

The exact analogue of an MA filter is to use an integrator on the input signal, and an analogue delay line going to a subtractor on the same integrator. 5 seconds length of cable is obviously impractical, but there used to be made 'CMOS bucket brigade charge coupled analogue delay lines'. I don't know whether these are still available, it's likely they've been completely superceded by low cost ADCs, MCUs and DACs with better and more flexible performance. Even with a high fidelity delay line, integrator drift will give you a long term problem.

If you are used to playing around with impulse responses, then you might consider the following way of tailoring the RC filter to be a bit more box-car like and a bit less exponential. Consider the impulse response as the sum of several exponentials with differing amplitudes and time constants. This image shows (in blue) an RC exponential response, and (in orange) what you get after subtracting 0.66 of an RC response of half the time constant, the result scaled up a bit. This flattens the first part of the curve and moves the weight a bit further out along the response. Just a thought. I'll let you play with adding more of these. There's not a lot that can be done about the long exponential tail however.

enter image description here

\$\endgroup\$
  • \$\begingroup\$ I think RC circuit may not work in case the baseline/mean of the signal changes suddenly. \$\endgroup\$ – Kulbhushan Chand Mar 29 at 9:09
  • \$\begingroup\$ An RC works exactly as an RC works. The question is, does it provide the response you are looking for? Are you looking for exactly a MA response, in which case the only solution is an ADC and MCU? Or are you looking for something that can be implemented with an LM324-N, and if so, what compromises are you prepared to make with the response? You might consider a 3rd order bessel lowpass filter, this can be implemented with a single LM324-N. It would have a higher latency than the RC I've described, but be more box-car like, and have a shorter tail. But it's still a weighted average. \$\endgroup\$ – Neil_UK Mar 29 at 9:29
  • \$\begingroup\$ I wanted the varying baseline of the signal (moving average), which further will be used to adjust some of the amplifier parameters and thus to keep the signal within the bounds. "So yes, I require almost moving average". I have used Sallen-Key LPF with Fc = 2Hz as anti-aliasing filter at the output. Like you said I can try RC or another filter with Fc very low (0.1 Hz) and see if that works. \$\endgroup\$ – Kulbhushan Chand Mar 29 at 9:53
  • \$\begingroup\$ @KulbhushanChand I think you have some misconceptions about how your moving average filter will behave. Have you done simulations? \$\endgroup\$ – Scott Seidman Mar 29 at 13:54
  • \$\begingroup\$ @KulbhushanChand What do you mean by the baseline "changes suddenly"? Your question said nothing about a "baseline" or the idea that you wanted different behavior if it changed suddenly. You need to edit the question to add all pertinent details. \$\endgroup\$ – Elliot Alderson Mar 29 at 14:25
1
\$\begingroup\$

Is it possible to find the moving average of a signal using analog circuitry?

"Is it possible" tends to be a poor question, because usually, things are possible but not practical. The short answer is (so long as you are talking about causal filters) yes, it's possible, but you probably don't want to do this.

Let's start with the idea that a moving average is a simple finite impulse response (FIR) filter, and the simplest form of the moving average impulse response would just be a series of points in discete time space of height 1/N, where N is the width of your filter -- i.e., a simple rectangle function. The frequency response would be a sinc function.

Now that you have the frequency response, there are algorithms that will let you transform this to the continuous time (see http://www.ijcst.com/vol23/3/karan.pdf, for example). Then, you would have a transfer function, consisting of zeros and poles, and there are a variety of tools that would let you design such a filter with op amps.

The resulting filters would be complicated, hard to build to good tolerance, expensive, and most likely, won't work particularly well when you're done, because of tolerance issues.

The real answer is to think about what you're trying to accomplish, not how you're accomplishing it. You're most likely trying to do something that requires a low-pass filtering operation. If so, design and implement the appropriate low-pass filter.

Alternatively, if you really need your moving average, the easiest path forward is probably to sample the signal, do your moving average, convert it back to analog, and Bob's your uncle.

\$\endgroup\$
0
\$\begingroup\$

A moving average is a Low Pass Filter (LPF). There are many ways to design this filter by specs.

For example.

Pass Band = -3 dB @ 4Hz, 0 dB ripple
Types: = 0.5 deg linear phase, Bessel, Gausian to 12dB, etc.
Stop Band = -40 dB @ 12Hz
-2.5dB @ 2Hz

Shown here with 5Vp 12Hz noise added to 5Vp sweep signal 0.5 to 2Hz. enter image description here

For single supply, bias to Vin- with input 50% input attenuation and then gain of 2 for non-inverting.

\$\endgroup\$
  • \$\begingroup\$ The dotted line in my hand-drawing is actually the moving average which I'm expecting. The signal is the high frequency (2 Hz) waveform. Looks like you have used 4th order two-stage Sallen-Key Low Pass Filter, which may not work in case the baseline/mean of the signal changes suddenly. \$\endgroup\$ – Kulbhushan Chand Mar 29 at 8:48
  • \$\begingroup\$ You ought to know a sudden baseline shift is a step function and if you wish to block that then you need a HPF spec or DC bandstop filter. aka "ac coupler" (-1) Did you not understand my specs match your question. You must define better specs. \$\endgroup\$ – Sunnyskyguy EE75 Mar 29 at 14:01
  • \$\begingroup\$ "Your" moving average does not block a step function "baseline" or DC shift. My answer suppresses your hand drawn signal as requested. Do you really have a PhD? \$\endgroup\$ – Sunnyskyguy EE75 Mar 29 at 14:07
  • \$\begingroup\$ A moving average with no group delay must average forward data with backwards data or in other words filter delayed input and average with previous and present data by the amount of group delay. But you still have not defined you expected inputs and results enough. (-1) Your sketch shows no baseline shift response. You must learn to define spectral density of the signal and the noise. \$\endgroup\$ – Sunnyskyguy EE75 Mar 29 at 14:29
  • \$\begingroup\$ " which may not work in case the baseline/mean of the signal changes suddenly. – Kulbhushan Chand" My answer is valid since you have yet to define this requirement. A better moving average looks forward and backward in time so there is no group delay. But a baseline shift has yet to be defined what is expected. Why not yet? I defined the parameters that give the flattest group delay response with an example. But your specs are poorly defined.. When you do this define passband & stopband in amplitude and phase , the solution is trivial. \$\endgroup\$ – Sunnyskyguy EE75 Mar 30 at 17:19

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.