python dynamic 1D input array and computation of quartile, decile array of measures

Viewed 65

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?

1 Answers

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.

Related