Real time Moving Averages in Python
<p>I need to calculate a moving average in Python of a sensor data that is coming on on the serial port. All the samples I can find about numpy use data from a file or that is in an array hard coded before the program starts. </p>

<p>In my case I do not have any data when the program starts. The data comes in over time in real time every second. I want to smooth the data as it arrives on the serial port. </p>

<p>I have this working on the Arduino but also need it in Python. Can somebody please point me to a real time (single value over time) sample not a batch sample.</p>

<p>Here's how you would add one reading at a time to a running collection of readings and return the average. I prepopulated the readings list to show it in action, but in your program, you'd just start off with an empty list: <code>readings = []</code></p>

<p>I made the assumption that you want to include the last x readings in your average rather than including all of the readings.  That is what the <code>max_samples</code> parameter is for.</p>

<p>without numpy:</p>

<pre><code>readings = [1, 2, 3, 4, 5, 6, 7, 8, 9]
reading = 10
max_samples = 10

def mean(nums):
    return float(sum(nums)) / max(len(nums), 1)

readings.append(reading)
avg = mean(readings)

print 'current average =', avg
print 'readings used for average:', readings

if len(readings) == max_samples:
    readings.pop(0)

print 'readings saved for next time:', readings
</code></pre>

<p>result:</p>

<pre><code>current average = 5.5
readings used for average: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
readings saved for next time: [2, 3, 4, 5, 6, 7, 8, 9, 10]
</code></pre>

<p>with numpy:</p>

<pre><code>import numpy as np

readings = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
reading = 10
max_samples = 10

readings = np.append(readings, reading)
avg = np.mean(readings)

print 'current average =', avg
print 'readings used for average:', readings

if len(readings) == max_samples:
    readings = np.delete(readings, 0)

print 'readings saved for next time:', readings
</code></pre>

<p>result:</p>

<pre><code>current average = 5.5
readings used for average: [ 1  2  3  4  5  6  7  8  9 10]
readings saved for next time: [ 2  3  4  5  6  7  8  9 10]
</code></pre>

python average