1

I wish to have a 1D dynamic array consisting of integer values and then return/prints its decile values.

The input comes from live audio recording, where I get the RMS = avg signal strength across entire signal with pyaudio

So to build the array I do:

import numpy as np
testMeasure = np.empty(1, dtype=int)
testMeasureArr = []
for i in range(0, int(RATE / CHUNK * TEST_ACTIONS_SEC)):
    data = stream.read(CHUNK)
    rms = audioop.rms(data, 2)
    print(rms)
    np.append(testMeasure, rms)
    testMeasureArr.append(rms)

To test the output result

print ( testMeasure )
print (testMeasureArr)
print ( np.percentile(testMeasure, np.arange(0, 100, 10)) )

But I get, data as such

522
2731
679
718
748
808
551
5660
968
707
2434
799
675
[4575657222473777152]
[1081, 876, 748, 562, 651, 679, 751, 694, 756, 718, 638, 602, 664, 635, 630, 624, 606, 767, 744, 531, 530, 627, 541, 678, 540, 700, 754, 640, 619, 938, 775, 1002, 1165, 1257, 837, 783, 664, 7370, 4087, 731, 690, 5874, 3062, 900, 887, 907, 857, 10319, 1453, 664, 568, 620, 2184, 4620, 610, 2795, 1170, 609, 522, 2731, 679, 718, 748, 808, 551, 5660, 968, 707, 2434, 799, 675]
[4.57565722e+18 4.57565722e+18 4.57565722e+18 4.57565722e+18
 4.57565722e+18 4.57565722e+18 4.57565722e+18 4.57565722e+18
 4.57565722e+18 4.57565722e+18]

So it seems a problem with the numpy dynamic array. Would numpy have only static arrays?

If so how to compute the deciles of a dynamic array? Is there some other library or I have to implement the calculation of deciles?

Alternatively, do I first need to contruct the dynamic array, then create a numpy array, this seems awkward to access the statistical functions.

Note in this case the range/array size is known in advance, as it is:

int(RATE / CHUNK * TEST_ACTIONS_SEC)

What is dynamic, is its values, which are filled as the audio stream is on the fly(broadcast) processed

Thanks

P.S.

Solving for quartile and decile using Python

Are there dynamic arrays in numpy?

user2718593
  • 111
  • 8

1 Answers1

1

You must assign the result of the np.append() function to the array:

testMeasure = np.append(testMeasure, rms)

Also its not a good idea to intitialize the array with np.empty() as you now have some random value from memory at the first position of the array.

However: appending numpy arrays is ineffcient (array is copied each time, see here). When the data size is unknown its best to first fill a list and then convert the list to a np.array:

import numpy as np
testMeasureArr = [] 
for i in range(0, int(RATE / CHUNK * TEST_ACTIONS_SEC)):
    data = stream.read(CHUNK)
    rms = audioop.rms(data, 2)
    testMeasureArr.append(rms)

testMeasure = np.array(testMeasureArr)
print(np.percentile(testMeasure, np.arange(0, 100, 10)))

Note: specifically for your case where you know the exact data size in advance you don't need a 'dynamic' array and can create and fill a 'static' numpy array directly.

NMme
  • 461
  • 3
  • 12
  • Jesus seems to work 1/ I wonder why the print of the numpy array first result is 4575657222473777152, it's memory addess. 2/ if array is very large is this the way to do it? – user2718593 Aug 10 '21 at 09:17
  • I added some clarification. The first entry comes from your array initialization with np.empty(). I would suggest you try the code snippet with creating the np.array after filling the list. – NMme Aug 10 '21 at 09:25
  • Excellent. In this case, can't I take advantage, that I know in advance the size of the array, so I can "malloc()" the memory size and stream the integers values into it as they come? Because otherwise it creates at least twice the required memory space and in addition it needs to dump the array into the numpy array? – user2718593 Aug 10 '21 at 09:37
  • Yes, you don't need an array with dynamic size. I added this to the answer, by 'allocating' the array with zeros that is then filled with the streamed values at each position. Hope this helps. – NMme Aug 10 '21 at 09:54
  • 1
    Thanks, put back for the sake of reference in excellent comment, the previous solution in case of true dynamic size array, "I would suggest you try the code snippet with creating the np.array after filling the list" – user2718593 Aug 10 '21 at 10:12