From 814baa0931ede0cd5e66439a6b1613d3157ea07b Mon Sep 17 00:00:00 2001 From: benetherington Date: Fri, 3 Jun 2022 15:24:42 -0400 Subject: [PATCH] Update for newest itertools Change import path per https://github.com/adafruit/Adafruit_CircuitPython_IterTools/issues/13 Use chain and repeat to assure itertools.islice can fetch the last item in the Array. Otherwise, this throws StopIteration. --- teaandtechtime_fft.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/teaandtechtime_fft.py b/teaandtechtime_fft.py index 7e43923..5522b85 100644 --- a/teaandtechtime_fft.py +++ b/teaandtechtime_fft.py @@ -42,7 +42,7 @@ # imports from math import pi, sin, cos, sqrt, pow, log -from adafruit_itertools import islice, count +from adafruit_itertools.adafruit_itertools import islice, count, chain, repeat import array __version__ = "0.0.0-auto.0" @@ -52,8 +52,14 @@ def fft(x): N = len(x) if N <= 1: return x - even = fft(list(islice(x,0,N,2))) - odd = fft(list(islice(x,1,N,2))) + even = fft(list(islice( + chain(x, repeat(0)), + 0, N, 2 + ))) + odd = fft(list(islice( + chain(x, repeat(0)), + 1, N, 2 + ))) T = [cos(2*pi*k/N)*odd[k].real+sin(2*pi*k/N)*odd[k].imag + (cos(2*pi*k/N)*odd[k].imag-sin(2*pi*k/N)*odd[k].real)*1j for k in range(N//2)] return [even[k].real + T[k].real + (even[k].imag + T[k].imag)*1j for k in range(N//2)] + \ [even[k].real - T[k].real + (even[k].imag - T[k].imag)*1j for k in range(N//2)]