gianfi
Posts: 11
Joined: Tue Oct 04, 2022 8:45 am

average data acquired from a pressure sensor

Fri Mar 17, 2023 7:54 am

Hi, I'm doing an experiment with the MPXV7002DP pressure sensor. I connected it to Raspberry Pi 3 Model A+ via the ads1115 adc. The data read from the sensor fluctuates a lot so I would like to average the data before printing the results. Can anyone tell me how to do it?

tpyo kingg
Posts: 1035
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: average data acquired from a pressure sensor

Fri Mar 17, 2023 8:11 am

You might look at the rollmean() function from the zoo package in R. You can then write an R script much like you would write an AWK or a shell script but using "#!/usr/bin/Rscript" as the shebang. It is also possible to read SQLite directly into R, if that's how you are keeping your data.

vffgaston
Posts: 95
Joined: Wed May 13, 2020 3:33 pm

Re: average data acquired from a pressure sensor

Fri Mar 17, 2023 8:20 am

gianfi wrote:
Fri Mar 17, 2023 7:54 am
Hi, I'm doing an experiment with the MPXV7002DP pressure sensor. I connected it to Raspberry Pi 3 Model A+ via the ads1115 adc. The data read from the sensor fluctuates a lot so I would like to average the data before printing the results. Can anyone tell me how to do it?
Language you are using?
Output (raedout)?
Estability of the pressure value itself?.

gianfi
Posts: 11
Joined: Tue Oct 04, 2022 8:45 am

Re: average data acquired from a pressure sensor

Fri Mar 17, 2023 8:55 am

For the connection of the adc I used this tutorial: https://www.engineersgarage.com/raspber ... terfacing/
The code is written in Python, I was thinking if you could average the data read via a while loop but i couldn't get it done properly.
Sorry but I started using Raspberry Pi and Python recently

ame
Posts: 6714
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: average data acquired from a pressure sensor

Fri Mar 17, 2023 9:11 am

gianfi wrote:
Fri Mar 17, 2023 8:55 am
For the connection of the adc I used this tutorial: https://www.engineersgarage.com/raspber ... terfacing/
The code is written in Python, I was thinking if you could average the data read via a while loop but i couldn't get it done properly.
Sorry but I started using Raspberry Pi and Python recently
Do you know how to perform the average function using basic maths?

If you understand how to do the calculation by hand then you can plan how to write the Python code. If you can't do the calculation by hand then you will find it difficult to write code, or to verify code that someone else has written.

Then, if you are still having difficulty with the code, post your code here and ask for advice.
Hmm. What can I put here?

PiGraham
Posts: 5245
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: average data acquired from a pressure sensor

Fri Mar 17, 2023 9:17 am

You could use a library like scipy

https://www.dsprelated.com/showarticle/164.php

If you only want to stabilise a displayed value and don't care about detailed frequency responses you can implement a basic IIR filter very simply
by adding a proportion 'a' of the view value and a proportion 'b' of the previous result value where a + b = 1.0.
If a is close to 1.0m say a = 0.99, b = 0.01, then the result will gradually approach the new value and rapid fluctuations will be filtered out. You can adjust the a value to get balance between speed of response and stability that suits you.

Because it's an integrative calculation it can be implemented with just a few lines of code.

Code: Select all

a = 0.99
b = 1.0-a
input = ReadInput()
result = input

while 1:
    input=ReadInput()
    result = a * result + b * input
    Display(result)
    
https://tomroelandts.com/articles/low-p ... iir-filter

There are various alternatives. If you want a true rolling average keep a listof past values (a FIFO) and add each new value to the list and to a sum variable. Subtract the stored value at the other end of the list and divide the sum by the number of values added. You only need one add, one subtract and a division for each new value.

tpyo kingg
Posts: 1035
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: average data acquired from a pressure sensor

Fri Mar 17, 2023 9:55 am

I'm not sure how this would compare but when you read the sensor, you could have the script do three things:

First, add the reading to the front of a list using insert(0,reading)
Then, if the list has more than the number of readings you want in your rolling average, pop() the oldest reading out.
Finally, take the average of the remaining list elements and use that for your number.

There are also moving average capabilities in NumPy.

PiGraham
Posts: 5245
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: average data acquired from a pressure sensor

Fri Mar 17, 2023 10:15 am

tpyo kingg wrote:
Fri Mar 17, 2023 9:55 am
I'm not sure how this would compare but when you read the sensor, you could have the script do three things:

First, add the reading to the front of a list using insert(0,reading)
Then, if the list has more than the number of readings you want in your rolling average, pop() the oldest reading out.
Finally, take the average of the remaining list elements and use that for your number.

There are also moving average capabilities in NumPy.
You can save a little time by keeping the last sum and just add the value you insert and subtract the value you pop. Then you don't need to calculate an average of the list. Just divide the sum by the length of the list and that is the average of the new list.

memjr
Posts: 2782
Joined: Fri Aug 21, 2020 5:59 pm

Re: average data acquired from a pressure sensor

Fri Mar 17, 2023 3:03 pm

Code: Select all


def get_sensor_data():
	val = 0
	num_readings = 10
	for _ in range(num_readings)
		val += my_sensor.read()  # whatever actually gets readings from the sensor goes here
		# time.sleep(1)  # add a pause if sensor rquires time between readings.
	return val / num_readings


DeaD_EyE
Posts: 117
Joined: Sat May 17, 2014 9:49 pm

Re: average data acquired from a pressure sensor

Sat Mar 18, 2023 1:19 pm

You could use a class, collections.deque and statistics.mean:

Code: Select all

from collections import deque
from statistics import mean


class RollingMean:
    def __init__(self, window_size):
        self.buffer = deque(maxlen=window_size)

    def __call__(self, value):
        self.buffer.append(value)
        return mean(self.buffer)


rolling_mean = RollingMean(5)
for pressure in range(8, 21):
    pressure /= 10
    pressure_mean = rolling_mean(pressure)
    print(f"Pressure: {pressure:<01.1f} | RollingMean: {pressure_mean:01.2f}")

You can make the class customizable:

Code: Select all

import statistics
import random
from collections import deque


class Rolling:
    def __init__(self, window_size, window_function):
        self.buffer = deque(maxlen=window_size)
        if not callable(window_function):
            raise ValueError("window_function must be a callable")
        self.window_function = window_function

    def __call__(self, value):
        self.buffer.append(value)
        return self.window_function(self.buffer)


rolling_mean = Rolling(5, statistics.mean)
rolling_median = Rolling(5, statistics.median)
rolling_geometric = Rolling(5, statistics.geometric_mean)

pressure_values = [value / 100 for value in range(80, 211)]
random.shuffle(pressure_values)

for pressure in pressure_values:
    pressure_mean = rolling_mean(pressure)
    pressure_median = rolling_median(pressure)
    pressure_geometric = rolling_geometric(pressure)

    print(
        f"Pressure: {pressure:<01.2f} | mean: {pressure_mean:01.3f} | "
        f"median: {pressure_median:01.3f} | geometric: {pressure_geometric:01.3f}"
    )

Return to “Python”