0

I have accelerometer data (x,y,z) which is being updated every 50ms. I need to store 80 values of the data into a 3D numpy array (1, 80, 3). For example:

[[[x,y,z] (at 0ms)
  [x,y,z] (at 50ms)
  ...
  [x,y,z]]] (at 4000ms)

After getting the first 80 values, I need to update the array with upcoming values, for example:

[[[x,y,z] (at 50ms)
  [x,y,z] (at 100ms)
  ...
  [x,y,z]]] (at 4050ms)

I'm sure there is a way to update the array without needing to manually write 80 variables to store the data into, but I can't think of it. Would really appreciate some help here.

3
  • Hello, Airidas I would like to know what is the type of data in the input. Is it a np.array, a list or what else? Commented May 8, 2020 at 1:26
  • I send the values over wifi as a char array and on the receiving end I convert them to float variables: ax = gauti_duomenys[0], same for y and z values. Commented May 8, 2020 at 1:29
  • append then concatenate the array np.concatenate() Commented May 8, 2020 at 1:33

2 Answers 2

2

It sounds like you want your array to always be 80 long, so what I would suggest is roll the array and then update the last value.

import numpy as np

data = np.arange(80*3).reshape(80, 3)
data
>>> array([[  0,   1,   2],
           [  3,   4,   5],
           [  6,   7,   8],
           ...,
           [231, 232, 233],
           [234, 235, 236],
           [237, 238, 239]])

data = np.roll(data, -1, axis=0)
data
>>> array([[  3,   4,   5], # this is second row (index 1) in above array
           [  6,   7,   8], # third row
           [  9,  10,  11], # etc.
           ...,
           [234, 235, 236],
           [237, 238, 239],
           [  0,   1,   2]]) # the first row has been rolled to the last position

# now update last position with new data
data[-1] = [x, y, z] # new xyz data
data
>>> data
>>> array([[  3,   4,   5], 
           [  6,   7,   8],
           [  9,  10,  11],
           ...,
           [234, 235, 236],
           [237, 238, 239],
           [ 76,  76,  76]]) # new data updates in correct position in array
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I was looking for. Just changed .reshape(80 , 3) to .reshape(-1, 80, 3) , data[-1] = [x, y, z] to data[0][-1] = [x, y, z] and data = np.roll(data, -1, axis=0) to data = np.roll(data, -1, axis=1) and it's working as expected. Thank you very much!
0

You can use vstack (initializing the array for the first iteration):

data=[x,y,x]  # first iteration
data=np.vstack([data,[x,y,z]]) # for the rest

print(data) # you would have a Nx3 array

For the update every N seconds it is easier if you use a FIFO or a ring buffer: https://pypi.org/project/numpy_ringbuffer/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.