Solved / how to make this code snippet to work




###
    
from cmath import exp, pi
    
def FFT1(x):
    
    N = len(x)
    if N <= 1: return x
    
    even = FFT1(x[0::2])
    odd =  FFT1(x[1::2])
    
    return [even[k] + exp(-2j*pi*k/N)*odd[k] for k in range(N/2)] + \
    [even[k] - exp(-2j*pi*k/N)*odd[k] for k in range(N/2)]
    
    

    
    
###
    
def FFT2():
    
    x=[1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]
    qq1=FFT1(x)
    print ('Matrix =',qq1)

i get an error for float and int on last line

is there a way to make it work

thanks

Try

range(N//2)

To make sure you get an integer result


###
    
from cmath import exp, pi
    
def FFT1(x):
    print(x)
    N = len(x)
    if N <= 1: return x
    
    even = FFT1(x[0::2])
    odd =  FFT1(x[1::2])
    print("EVEN,ODD", even, odd)
    print("N=", N)
    l1 =  [even[k] + exp(-2j*pi*k/N)*odd[k] for k in range(N//2)]
    l2 = [even[k] - exp(-2j*pi*k/N)*odd[k] for k in range(N//2)]
    
    return l1 + l2
    
    


    
    
###
    
def FFT2():
    
    x=[1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]
    qq1=FFT1(x)
    print ('Matrix =',qq1)


FFT2()


brings back memories of 1st year engineering maths.

oh … gives me


Matrix = [(4+0j), (1-2.414213562373095j), 0j, (1-0.4142135623730949j), 0j, (0.9999999999999999+0.4142135623730949j), 0j, (0.9999999999999997+2.41421356
2373095j)]

it was a fun time to learn so much
but we did not have python at the time and not much PC!LOL

on which line do you change this range ?

thanks

Changed single slash (“/”) division to double slash integer division. The float result of the former was giving you the int expected error in the range function.

Since Python 3
/ -> true division (=float)
// -> integer division (=int)

The result of an int div is the same as
int(dividend/divisor)
but direct int div is slightly faster.