python - Average data over time? -
i’d calculate average val1, val2 , signal value signal data in sample data @ 5 second intervals. in other words, using sample data included, i’d calculate average values between 1st data point (in case being) 01:45:18 through 01:45:22, 01:45:23 through 01:45:27, 01:45:28 through 01:45:32, , 01:45:33 through remainder of data.
ideally, i’d store averaged information in variables such as: dec_average, ra_average, , n_average
any suggestions or ideas on how achieve this? here’s code have far.
import sys import os import matplotlib.pyplot plt matplotlib.dates import strpdate2num import numpy np import matplotlib.colors import matplotlib.cm sat_id,dec,ra,n = np.loadtxt("mydata.asc", usecols=(3,5,7,9), unpack=true)
sample data:
timestamp: 01:45:18 satid 02 val1 36 val2 188 signal 34
timestamp: 01:45:19 satid 02 val1 36 val2 188 signal 34
timestamp: 01:45:20 satid 02 val1 36 val2 188 signal 35
timestamp: 01:45:21 satid 02 val1 36 val2 188 signal 34
timestamp: 01:45:22 satid 02 val1 36 val2 188 signal 35
timestamp: 01:45:23 satid 02 val1 36 val2 188 signal 35
timestamp: 01:45:24 satid 02 val1 36 val2 188 signal 36
timestamp: 01:45:25 satid 02 val1 36 val2 188 signal 35
timestamp: 01:45:26 satid 02 val1 36 val2 188 signal 36
timestamp: 01:45:27 satid 02 val1 37 val2 188 signal 36
timestamp: 01:45:28 satid 02 val1 37 val2 188 signal 36
timestamp: 01:45:29 satid 02 val1 37 val2 188 signal 36
timestamp: 01:45:30 satid 02 val1 38 val2 188 signal 37
timestamp: 01:45:31 satid 02 val1 38 val2 188 signal 36
timestamp: 01:45:32 satid 02 val1 39 val2 188 signal 37
timestamp: 01:45:33 satid 02 val1 39 val2 188 signal 37
timestamp: 01:45:34 satid 02 val1 39 val2 188 signal 37
timestamp: 01:45:35 satid 02 val1 39 val2 188 signal 38
first step capture important data each entry, values of val1
, val2
, , signal
, have done.
then every set of 5 entries, need grab average of each field, can done adding each value of fields , dividing five. using numpy
can achieved using np.average()
, passing in array want average, in our case, first 5 elements first time, next 5 second, , so-on, dec
can done follows.
we create list dec_average
store averages of each set of entries.
dec_average = []
this run through dec
array, averaging each set of 5 elements long there @ least 5 left, , appending average dec_average
.
for in range(5, len(dec) + 1, 5): dec_average.append(np.average(dec[(i - 5):i]))
once finish running through loop, if number of elements in array not multiple of five, there still left average. in order average of those, need last x
items x
remainder of division of length of array five; hence, modulus.
if (len(dec)) % 5 != 0: dec_average.append(np.average(dec[-(len(dec) % 5):]))
putting these 3 pieces of code results in system of calculating average every 5 items of list, , if there less 5 items left in end, getting average of remaining, , appending each average array. can extended suit other data entries.
Comments
Post a Comment