cancel
Showing results for 
Search instead for 
Did you mean: 

Hi everyone, I'm a beginner with MCUs. I want to read ADC values from 12 channels continuously and I'd like to implement an averaging process before transmitting them through UART. I'm using Nucleo-L476RG.

AMahm.1
Associate II

Signals are similar across all 12 channels but I'm confused about how to run the averaging algorithm for 12 channels at the same time. Currently, I can get the ADC values from all 12 channels using DMA without the averaging process.

To explain further, my analog signals are like square waves and I want to average ADC values for the plateaus only. What could be the most efficient way to do this?

Thanks in advance :grinning_face_with_sweat:

7 REPLIES 7

Well the DMA isn't going to do the averaging, and software isn't going to do "at the same time" in a parallel sense.

You'll want to use arrays/structures, have an instance for each of the 12 channels. Do it in a way that scales, rather than you coding repetitively.

Not sure how exactly you'd locate the plateaus, you could perhaps keep a running average of N samples, perhaps holding them in an array so you can roll the old values from the average.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Sebastiaan
Senior

For averaging you can indeed keep track of a an array of the past N samples. But another approach might be to use exponential smoothing with a very low smoothing (alpha) factor, e.g. 0.01

smoothing works by setting the average value = (1-alpha) * [previous average value] + alpha * new value. See wikipedia page.

I've personally used audio input from 1 channel ADC, transferred via DMA and processed in chunks of a few thousand samples to calculate the average, and use exponential smoothing to "smooth" that average over the next bulk calculation. For that one, a smoothing factor of 0.2 or 0.1 was appropriate. (the end goal was to calculate the "power" in the audio signal, a.k.a. sound level in decibel.)

TDK
Guru

> To explain further, my analog signals are like square waves and I want to average ADC values for the plateaus only. What could be the most efficient way to do this?

The best algorithm will very much depend on the details here, how much noise there is, how you define a plateau vs allowable noise.

In any case, you'll likely need to store the last X samples, and make some quantitative measure of a plateau. Perhaps if the value doesn't change by more than 10 counts over a period on 20 samples, then use the average of those 20 samples.

If you feel a post has answered your question, please click "Accept as Solution".

Thanks for the suggestion. The signal looks like this and I want to detect and calculate the average (of N samples) for each plateau.

Thanks for the suggestion. I have a MATLAB algorithm for detecting plateaus from this signal but I'm having difficulties implementing the same algorithm for 12 channels with STM32. But the MATLAB algorithm is also not perfect. So, I'm looking for other ways to detect the plateau.

Your image did not come through for me. I checked on two devices.

If you feel a post has answered your question, please click "Accept as Solution".

Idk, now it's not even visible for me as well. I've uploaded it here: https://ibb.co/whW5b1Y