📬❄️📦Adafruit Holiday Shipping Deadlines 2019: Please place all UPS 3 Day orders by 11am ET Friday December 13 📬❄️📦
0

Overflow Error in Adafruit_waveform Sine function?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Overflow Error in Adafruit_waveform Sine function?

by RussoTuristo on Thu Jun 20, 2019 5:36 pm

Hi Everyone,

I've just started playing with my Gemma M0 board and wanted to implement a simple sine wave generator using adafruit_waveform sine library. The problem is that even running the provided example, I get an overflow error: "OverflowError: value must fit in 2 byte(s)"

the sample code is simply:

Code: Select all | TOGGLE FULL SIZE
wave = sine.sine_wave(16000, 440)


the library code is also extremely simple:
Code: Select all | TOGGLE FULL SIZE
def sine_wave(sample_frequency, pitch):
    """Generate a single sine wav cycle at the given sampling frequency and pitch."""
    length = int(sample_frequency / pitch)
    b = array.array("H", [0] * length)
    for i in range(length):
        b[i] = int(math.sin(math.pi * 2 * i / length) * (2 ** 15) + 2 ** 15)
    return b


The overflow error is thrown any time that unsigned short b[i] is given a value larger greater than 32767, i.e 2^15.

I know I can simply write my own sine function that changes b from "H" to 'i" - but it seems odd that a stock function has this bug.

I am missing something?

RussoTuristo
 
Posts: 4
Joined: Thu Jun 20, 2019 3:35 pm

Re: Overflow Error in Adafruit_waveform Sine function?

by adafruit_support_carter on Fri Jun 21, 2019 3:19 pm

The type code will have a specific size associated with it:
https://docs.python.org/3/library/array.html
which you can not exceed.

Your code is using "H" which should be good for 0 to 65535.
Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 4.1.0-beta.0 on 2019-06-13; Adafruit ItsyBitsy M4 Express with samd51g19
>>> import array
>>> b = array.array("H", [0]*2)
>>> b
array('H', [0, 0])
>>> b[0] = 32767
>>> b[0] = 32768
>>> b[0] = 65535
>>> b[0] = 65536
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: value must fit in 2 byte(s)
>>>


Does the above not work for you?

adafruit_support_carter
 
Posts: 13681
Joined: Tue Nov 29, 2016 2:45 pm

Re: Overflow Error in Adafruit_waveform Sine function?

by kevinjwalters on Sat Jun 22, 2019 9:15 am

The key thing here is "0 to 65535". That sine_wave function is buggy because it is at risk of generating int(1.0 * 32768 + 32768) = 65536. This "+1 over" maxium peak value will only occur for frequencies which cause the array length to be divisible by four (original post said even) number. Floating point accuracy may affect things a little here but I don't think it's adding confusion in this case.

I think I may have seen a CircuitPython interpreter change that added range checking for array.array. That might explain why this latent bug was not noticed before. Possibly #1451 applied in #1860. There's also discussion on teething issues with this in comments of #1875.

This code appears quite a bit. I put in one ticket for one instance of it: sine_wave likely to generate value too large for "H" array.array #9.
Last edited by kevinjwalters on Sat Jun 22, 2019 5:21 pm, edited 1 time in total.

kevinjwalters
 
Posts: 466
Joined: Sun Oct 01, 2017 3:15 pm

Re: Overflow Error in Adafruit_waveform Sine function?

by adafruit_support_carter on Sat Jun 22, 2019 11:29 am

Oh, I may have misunderstood what function we were talking about.

@RussoTuristo By "stock function" do you mean sine_wave() or array.array()?

adafruit_support_carter
 
Posts: 13681
Joined: Tue Nov 29, 2016 2:45 pm

Re: Overflow Error in Adafruit_waveform Sine function?

by RussoTuristo on Sat Jun 22, 2019 7:05 pm

@adafruit_support_carter - I was talking about the sine_wave function. It is, indeed, as @kevinjwalters described, buggy.

RussoTuristo
 
Posts: 4
Joined: Thu Jun 20, 2019 3:35 pm

Re: Overflow Error in Adafruit_waveform Sine function?

by adafruit_support_carter on Mon Jun 24, 2019 12:44 pm

OK. Nevermind what I said then. I'd say follow that issue thread kjw linked. Seems like maybe an off by one or something that got copy-pasta'd around? Yah, github issues are the best way to track and deal with fixing those.

adafruit_support_carter
 
Posts: 13681
Joined: Tue Nov 29, 2016 2:45 pm

Please be positive and constructive with your questions and comments.