micropython-filters icon indicating copy to clipboard operation
micropython-filters copied to clipboard

fir.py bug

Open fsincere opened this issue 3 years ago • 2 comments

Hi Peter, I try fir.py with MicroPython 1.17 on NUCLEO-L476RG board. There is a problem only when the number of coefficients is a multiple of 4. For exemple : coeffs = array.array('i', (100, 100, 100, 100)) ncoeffs = len(coeffs) data = array.array('i', [0]*(ncoeffs +3)) data[0] = ncoeffs data[1] = 0 fir(data, coeffs, 1) # 100 OK fir(data, coeffs, 0) # 100 OK fir(data, coeffs, 0) # 100 OK fir(data, coeffs, 0) # 134492960 bug

coeffs = array.array('i', (0, 100, 100, 100, 100)) works !

fsincere avatar Jan 25 '22 21:01 fsincere

I can confirm this bug on a Pyboard. I will investigate.

peterhinch avatar Jan 26 '22 13:01 peterhinch

Thanks for the report. I've pushed an update to fix this.

It was a classic array bounds error, performing one iteration too many and accessing a coefficient value from the location after the end of the array. This actually occurred regardless of the number of coefficients. A quirk of the array module caused the value in this location to be zero except when len(coeffs) % 4 == 0. This caused the bug only to be apparent under those conditions.

peterhinch avatar Jan 26 '22 18:01 peterhinch