Forum - MCS Electronics

Author Message
snow

Joined: 28 Jun 2005
Posts: 176
Location: Ashburton / Mid Canterbury / New Zealand

Posted: Thu Jul 15, 2021 9:56 pm    Post subject: Rolling average to smooth sensor jitter

Hi.

Just a function to smooth sensor jitter. Code pretty dirty but gives the general idea. Improvements welcome.

 Code: Sub GetBatteryVoltage()     'Calculate an rolling average.. Loads data into an array and fills it. Once filled starts overwriting the oldest data with the newest     'Between reads of sensor, Code reads the array and averages the data in it     'Data saggy at the start of filling as array is empty    Local Result as Word    Local Average as Word    Local X as byte    Result=0    Average=0    X=0    If BattReadIndex = NumOfBattReadings then       BattReadIndex = 0                                                  'Wraps around to the beginning of array:    End if    Result=BattVolt                                                          'BattVolt = Getadc(1)    BattReadings(BattReadIndex) =  Result                         'Store it in the array    BattReadIndex = BattReadIndex + 1                             'Advance to the next position in the array:    Result=0    For X = 0 to NumOfBattReadings                                 'Read the array      Result=Result+BattReadings(X)    Next X    Average= Result/NumOfBattReadings                            'Calculate the average:    BatteryVoltage=Average End Sub
i.dobson

Joined: 05 Jan 2006
Posts: 1551
Location: Basel, Switzerland

Posted: Fri Jul 16, 2021 9:05 am    Post subject:

Hi,

I've been using this code to smooth sensor values for several years.

 Code: Const _smoothing_factor = 3                                 'Nr.values for avg Dim Pressure_accumulator(2) As Long Dim Temp_long As Long Dim Accumulator_temp As Long Dim Sensor_value As Word Dim Accumulator As Long  'smooth raw value     Temp_long = Pressure_accumulator(temp_byte)             'Load value into temp var.     Accumulator = Temp_long                                 'This is the old average on LONG form     Shift Accumulator , Right , _smoothing_factor           'Accumulator sum divided by 2^smoothing factor     Pressure_raw(temp_byte) = Accumulator     Temp_long = Temp_long - Accumulator                     'Subtract old value     Temp_long = Temp_long + Sensor_value                    'Add new     Pressure_accumulator(temp_byte) = Temp_long             'Save accumulator for next time

This code avoids using a divide which is slow on AVR's.
This function is more a weighted average, where younger values have more weight in the result.

Regards
Ian Dobson

_________________
Home of AVRTimer, the Bascom timer value calculator.
http://www.planet-ian.com

Walking on water and writing software to specification is easy if they're frozen.
snow

Joined: 28 Jun 2005
Posts: 176
Location: Ashburton / Mid Canterbury / New Zealand

 Posted: Fri Jul 16, 2021 9:54 pm    Post subject: Thanks Ian That's a nice bit of code
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT + 1 Hour Page 1 of 1

 Jump to: Select a forum BASCOM AVR/8051----------------BASCOM-AVRBASCOM-8051BASCOM-ARDUINOShare your working BASCOM-8051 code hereShare your working BASCOM-AVR code hereBASCOM BETA-SLA BASCOM Related----------------EASY TCP/IPAVR-DOSAR7212KokkeKat FAT-free SD card libBASCOM Project Blog Other Stuff----------------VariousPCB'sRoboticsNew WebSiteAnnouncementsAVR Archive----------------BASCOM-AVR ArchiveBASCOM-8051 ArchiveBASCOM-AVR Unsupported versionsEasy TCP/IP ArchiveBASCOM-EDB
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum