average data acquired from a pressure sensor
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?
-
- Posts: 1035
- Joined: Mon Apr 09, 2018 5:26 pm
- Location: N. Finland
Re: average data acquired from a pressure sensor
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.
Re: average data acquired from a pressure sensor
Language you are using?gianfi wrote: ↑Fri Mar 17, 2023 7:54 amHi, 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?
Output (raedout)?
Estability of the pressure value itself?.
Re: average data acquired from a pressure sensor
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
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
Re: average data acquired from a pressure sensor
Do you know how to perform the average function using basic maths?gianfi wrote: ↑Fri Mar 17, 2023 8:55 amFor 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
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?
Re: average data acquired from a pressure sensor
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.
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.
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)
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.
-
- Posts: 1035
- Joined: Mon Apr 09, 2018 5:26 pm
- Location: N. Finland
Re: average data acquired from a pressure sensor
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.
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.
Re: average data acquired from a pressure sensor
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.tpyo kingg wrote: ↑Fri Mar 17, 2023 9:55 amI'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.
Re: average data acquired from a pressure sensor
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
Re: average data acquired from a pressure sensor
You could use a class, collections.deque and statistics.mean:
You can make the class customizable:
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}")
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}"
)