0
import cmath
import math
import random
import time

P = []
V = []
Vin = []

def Compute_wn_win(n,V,Vin):
    for i in range (0,n):
        V.append(complex(math.cos(2*math.pi*i/n),math.sin(2*math.pi*i/n)))
        Vin.append(1/(complex(math.cos(2*math.pi*i/n),math.sin(2*math.pi*i/n))))    

Compute_wn_win(8,V,Vin)

for i in range(0,8):
    random_number = random.uniform(-1.0,1.0)
    P.append(random_number)

def FFT(P,V,n):
    if(n==1):
        return P[0]
    else:
        Peven = []
        Podd = []
        for i in range(0,n/2):
            Peven.append(P[2*i])
            Podd.append(P[(2*i)+1])
        Vsquared = []
        for i in range(0,n/2):
            Vsquared.append(V[i]*V[i])
        Sole = FFT(Peven,Vsquared,n/2)
        Solo = FFT(Podd,Vsquared,n/2)
        Sol = [0 for x in range(0,n)]
        for i in range(0,n/2):
            Sol[i] = Sole[i]+V[i]*Solo[i]
            Sol[i+n/2] = Sole[i]-V[i]*Solo[i]
        return Sol
Sol = FFT(P,V,8)

I am new to Python. I have the following code. However I get the following error for lines Sole = FFT(Peven,Vsquared,n/2) and Sol[i] = Sole[i]+V[i]*Solo[i]. However, I have defined, Sole, Solo and Sol as list data type so I don't understand why it mentions that float datatype does not have an attribute getitem

Exact Error is

Traceback (most recent call last):
  File "/Users/globetrekker/Documents/CS5050/Assignment7/Test_py.py", line 40, in <module>
    Sol = FFT(P,V,8)
  File "/Users/globetrekker/Documents/CS5050/Assignment7/Test_py.py", line 33, in FFT
    Sole = FFT(Peven,Vsquared,n//2)
  File "/Users/globetrekker/Documents/CS5050/Assignment7/Test_py.py", line 33, in FFT
    Sole = FFT(Peven,Vsquared,n//2)
  File "/Users/globetrekker/Documents/CS5050/Assignment7/Test_py.py", line 37, in FFT
    Sol[i] = Sole[i]+V[i]*Solo[i]
TypeError: 'float' object has no attribute '__getitem__'
  • 1
    It looks like you are trying to do list comprehension with range. But you are passing a float as the second argument from range. `for i in range(0,n/2):`. n/2 is not always an integer. It throws the error `TypeError float can not be interpreted as an integer` – Riley Hughes Oct 24 '18 at 20:20
  • can you edit this and add the exact error you are getting? I do not see where you tried to use *getitem* – Riley Hughes Oct 24 '18 at 20:26

2 Answers2

1

This is introducing a float:

for i in range(0,n/2):

Checkout: I keep getting this error for my simple python program: "TypeError: 'float' object cannot be interpreted as an integer"

Kevin Searle
  • 103
  • 7
1

Sole and Solo are the return values from recursive calls to FFT(), but FFT()'s base case (when n == 1) returns a float, not a list, so the step above the base case will fail by trying to index a float. Presumably, you want to change return P[0] in the base case to return [P[0]].

jwodder
  • 46,316
  • 9
  • 83
  • 106