Reputation: 11
I am working on a project with sensor data. I write the sensor data at first into a csv file to save it. After that I read the file and receive the sensor data as a list as follows:
[ 0 1 1 0 0 516]
[ 1 1 1 0 0 498]
[ 2 1 1 0 0 499]
[ 3 1 1 0 0 500]
[ 4 1 1 0 0 487]
[ 5 1 1 0 0 502]
...
There are no commas between the elements. I need the last element of this list to transfer the unit (analog data). I tested several things e.g. to create an array and convert it back to list and so on...
This is how I saved the data (working with a raspberry pi):
device.start(samplingRate, acqChannelsEDA)
with open('/home/pi/pywork/EDAdata.csv', 'w') as edafile:
# create csv writer
writer = csv.writer(edafile)
start = time.time()
end = time.time()
while (end - start) < running_time:
# Read samples
sampledata = device.read(nSamples)
writer.writerow(sampledata)
print(sampledata)
# print(eda_tf)
end = time.time()
edafile.close()
The output for print(sampledata) is:
[[8 1 1 0 0 0]]
[[9 1 1 0 0 0]]
[[10 1 1 0 0 0]]
[[11 1 1 0 0 0]]
[[12 1 1 0 0 0]]
[[13 1 1 0 0 0]]
[[14 1 1 0 0 0]]
[[15 1 1 0 0 0]]
This is the code from read(self, nSamples=100)
. The method to get sampledata
.
dataAcquired = numpy.zeros((nSamples, 5 + nChannels), dtype=int)
for sample in range(nSamples):
Data = self.receive(number_bytes)
decodedData = list(struct.unpack(number_bytes * "B ", Data))
crc = decodedData[-1] & 0x0F
decodedData[-1] = decodedData[-1] & 0xF0
x = 0
for i in range(number_bytes):
for bit in range(7, -1, -1):
x = x << 1
if (x & 0x10):
x = x ^ 0x03
x = x ^ ((decodedData[i] >> bit) & 0x01)
if (crc == x & 0x0F):
dataAcquired[sample, 0] = decodedData[-1] >> 4
dataAcquired[sample, 1] = decodedData[-2] >> 7 & 0x01
dataAcquired[sample, 2] = decodedData[-2] >> 6 & 0x01
dataAcquired[sample, 3] = decodedData[-2] >> 5 & 0x01
dataAcquired[sample, 4] = decodedData[-2] >> 4 & 0x01
if nChannels > 0:
dataAcquired[sample, 5] = ((decodedData[-2] & 0x0F) << 6) | (decodedData[-3] >> 2)
if nChannels > 1:
dataAcquired[sample, 6] = ((decodedData[-3] & 0x03) << 8) | decodedData[-4]
if nChannels > 2:
dataAcquired[sample, 7] = (decodedData[-5] << 2) | (decodedData[-6] >> 6)
if nChannels > 3:
dataAcquired[sample, 8] = ((decodedData[-6] & 0x3F) << 4) | (decodedData[-7] >> 4)
if nChannels > 4:
dataAcquired[sample, 9] = ((decodedData[-7] & 0x0F) << 2) | (decodedData[-8] >> 6)
if nChannels > 5:
dataAcquired[sample, 10] = decodedData[-8] & 0x3F
else:
raise Exception(ExceptionCode.CONTACTING_DEVICE)
return dataAcquired
Upvotes: 1
Views: 84
Reputation: 1032
To get last element of a list, you should use negative indexing, e.g.
arr = [10, 20, 30, 40, 50]
print (arr[-1])
print (arr[-2])
Output:
50
40
Upvotes: 1