1

I have a large numerical array (results of an audio file's signal which I return to my react native app from a python backend.

And the array looks like this

[1098, 522, 597, 325, 201, 16, ...................... 75, 447, 253, 58, 273]

size of the array is around 535754

I have this code which I use to find raio

peaks = audio_signal_array
const ratio = Math.max(...peaks) / 100
// ratio = 1.1 (example result)
const normalized = peaks.map(point => Math.round(point / ratio))
// normalized = [0, 0, 2, 5, 18, 50, 100, 18] (example result)

Everytime I run this code it throws me the error

error [RangeError: Maximum call stack size exceeded (native stack depth)]

So it would be great if anyone can help me to either fix this code from the javascript side or help me convert this code to python so I can return the ratio and the normalized array to the react native app

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Kasun
  • 473
  • 1
  • 10
  • 32
  • Please do not use irrelevant tags for your question. If the problem is that a javascript program receives some data and does something wrong (such as throwing an error), even though the data is correct, then that is a javascript question - it **does not matter** what language was used to **create** the data. Python experts will have no particular advantage in answering a question like this. – Karl Knechtel Oct 09 '22 at 14:33
  • Does this answer your question? [Maximum call stack size exceeded error](https://stackoverflow.com/questions/6095530/maximum-call-stack-size-exceeded-error) – Karl Knechtel Oct 09 '22 at 14:34

1 Answers1

4

Try

peaks.reduce((a, b) => Math.max(a, b))

instead of

Math.max(...peaks)

On the python side, you can simply do

ratio = max(peaks) / 100

See also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max:

...spread (...) and apply will either fail or return the wrong result if the array has too many elements, because they try to pass the array elements as function parameters.

gog
  • 10,367
  • 2
  • 24
  • 38