1

I read continuous serial data from sensor. I splitted it and assigned to 5 variables - a,b,c,d,e. I am trying to calculate the average of each variable for every 5s. Since my sensor is 50Hz, I made the range 250. My implementation is as below. So, the question is - is there any way better to do it, especially on setting the time to be exactly 5 seconds instead of presuming it equal to 250 times of loop?

a,b,c,d,e = [[] for i in range(5)]

for i in range(250):
    data = ser.readline().decode() #read from sensor
    msg  = float(data.split(','))
    a.append(msg[0])
    b.append(msg[1])
    c.append(msg[2])
    d.append(msg[3])
    e.append(msg[4])

average_a = sum(a)/len(a)
average_b = sum(b)/len(b)
average_c = sum(c)/len(c)
average_d = sum(d)/len(d)
average_e = sum(e)/len(e)

1 Answers1

0

Try something like this

import datetime
duration = 5 #Collect for 5 seconds
time_start = datetime.datetime.now()
time_end = time_start + datetime.timedelta(seconds=duration)
big_list = [[] for i in range(5)]

#This loops runs for 5 seconds
while datetime.datetime.now() < time_end:
    data = ser.readline().decode() #read from sensor
    #Create a list of floats and append it to a list
    msg = [float(f) for f in data.split(',')]
    for i, m in enumerate(msg):
        big_list[i].append(msg[i])

#Iterate through list of list, and calculate average for each list
for l in big_list:
    print(sum(l)/len(l))

e.g. if data is '1,2,3,4,5' every time

big_list = []
for i in range(250):
    data = '1,2,3,4,5'
    msg = [float(f) for f in data.split(',')]
    big_list.append(msg)

for l in big_list:
    print(sum(l)/len(l))
#1.0
#2.0
#3.0
#4.0
#5.0
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
  • Thank u for your reply. Your solution is much elegant than mine. Anyway, do you have any idea how to do for looping over time? – jusnaini muslimin Apr 03 '19 at 09:34
  • Perfect. Thank you :) – jusnaini muslimin Apr 04 '19 at 05:24
  • I just noticed the output of my code and yours are different. The big_list is (1,2,3,4,5),(1,2,3,4,5)..and the average of your method is 3, which is not what im looking for. I want the average of each element such as a =(1, 1,1,..),b=(2,2,2,..) until e=(5,5,5,..).So for example the average of a=0.33 if a(1,1,1). Could u pls advise sir.. – jusnaini muslimin Apr 04 '19 at 09:33
  • Thank you,it works as required. Btw, i want to write it into file but it keeps giving me error - float has no attribute write. The updated code is the way i do that..could u point me how to fix it? – jusnaini muslimin Apr 04 '19 at 17:31
  • Try this to write your list of averages to file: https://stackoverflow.com/questions/899103/writing-a-list-to-a-file-with-python – Devesh Kumar Singh Apr 05 '19 at 00:34
  • Check that your name of the file is different than any float variable you are assigning @jusnainimuslimin Also if the answer helped you, please consider accepting it :) – Devesh Kumar Singh May 03 '19 at 12:23
  • Got it. Thank u sir – jusnaini muslimin May 03 '19 at 22:48