简体   繁体   中英

real time data acquisition/processing using Python

I am attempting to write a code that will acquire information from a buffer (returned as a multi-dimentional array), extract certain elements from said array.

So here's what I have:

Drest = np.array([])              #Set up array for data to be read to (not sure if this is needed)
t_end = time.time() + 5            
while time.time() < t_end:
Drest = ftc.getData()             #fts is the buffer that I'm connecting to.
print("Drest: %s" %Drest)

This returns an output which looks like this:

[[  6.79609478e-01   6.79609478e-01   6.79609478e-01   6.79609478e-01
6.79609478e-01   6.79609478e-01   6.79609478e-01   6.79609478e-01
6.79609478e-01   6.79609478e-01   6.79609478e-01   6.79609478e-01
6.79609478e-01   6.79609478e-01   6.79609478e-01   6.79609478e-01]
[  6.81910694e-01   6.81910694e-01   6.81910694e-01   6.81910694e-01
6.81910694e-01   6.81910694e-01   6.81910694e-01   6.81910694e-01
6.81910694e-01   6.81910694e-01   6.81910694e-01   6.81910694e-01
6.81910694e-01   6.81910694e-01   6.81910694e-01   6.81910694e-01]]

Question 1 As the data comes in, I would like to add a "0" as a first element of each array, so I essentially have a first column full of zeros. I have attempted this, but it doesn't add it to every one of, just the very first element, not the first in each element of each array.

block = 1
np.append(block, [Drest])

Question 2 I also need to create a mean of every other "column" from the multi-dimensional array, so I have tried this, but I can't for the life of me get this to work!

 for i in range(0, len(Drest), 2):
    HbO2 = Drest[i]
    HbO2Rest = sum(HbO2)/float(len(HbO2))

Thanks in anticipation of your help :)

Question 1: As the data comes in, I would like to add a "0" as a first element of each array, so I essentially have a first column full of zeros. I have attempted this, but it doesn't add it to every one of, just the very first element, not the first in each element of each array.

import numpy as np
New_Drest = np.asarray([0 for x in xrange(len(Drest))])
Modified_Drest = np.concat(New_Drest,Drest)

Question 2 : I also need to create a mean of every other "column" from the multi-dimensional array, so I have tried this, but I can't for the life of me get this to work!

Numpy has a mean api.

 for i in xrange(len(Drest)):
    if i % 2 == 0:
        HbO2 = Drest[i]
        HbO2Rest = np.mean(HbO2)

Python encourages to use xrange instead of range.

Question 1

Assuming that Drest an array or array:

b = [x.insert(0,0) for x in Drest]

Question 2

you can do about the same using slicing using the previously created array...

c = [sum(y[1:])/len(y[1:])*1.0 for y in b]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM