2

I am still finding my way in Python so this question may be very basic: I am trying to generate some cosine waves in python and then do a Fourier transform on them. I am wrote the following code:

from pylab import *
from numpy import *
x = linspace(-5,5,100)
t = linspace(0,2000,2001)
w1=cos(2*pi*3*t/2001)
w2=cos(2*pi*10*t/2001)
fourier=0
long(fourier)

Trace = w1+w2
for i in range(1,125,1):
    fourier[i] = sum(cos(2*pi*i*t/2001)*Trace)

print fourier
f=linspace(1,125,125)
plot(f,fourier)
show()

so instead of calculating the values frequency by frequency I want to make a loop that takes each frequency from 1 to 125 and store the outputs in one list so that I can plot them.

Python does not like this and gives a message saying:

fourier[i] = sum(cos(2*pi*i*t/2001)*Trace)
TypeError: 'int' object does not support item assignment

Any idea why?

1
  • you need range(1,126) to match the the dimensions of f for plotting Commented Dec 25, 2014 at 12:01

4 Answers 4

3

may be you want:

fourier = []

you decleared it as integer, you need to declear as list.

your fourior type is integer and you need to make it as list so it can support index assignment

also remove long(fourier) from your code

for i in range(1,126):
    fourier[i] = sum(cos(2*pi*i*t/2001)*Trace)

this will give you index out of index error

correct would be:

for i in range(1,126):    # you dont need last option as 1, because range step is one by default
    fourier.append(sum(cos(2*pi*i*t/2001)*Trace))
 OR
    fourier.insert(i,sum(cos(2*pi*i*t/2001)*Trace))

its better to use list comprehension and you need to set range to 126 to match the dimensions of f for plotting:

fourier = [sum(cos(2*pi*i*t/2001)*Trace) for i in range(1,126)]
Sign up to request clarification or add additional context in comments.

5 Comments

both of these answers will still produce an error when trying to access fourier[i] on a list with No items
@PadraicCunningham yes mentioned it will give list out of index error :)
better to just use a list comp and the range needs to be 126
@PadraicCunningham Happy Christmas :)
happy christmas to you too :)
1

You need an array initialization: fourier=[]

for i in range(1,125):
  fourier.append(sum(cos(2*pi*i*t/2001)*Trace))

Comments

1

There are several ways to build up a list of objects/numbers.

The simplest is a for loop:

# create a list of squared numbers
squares = []
for item in range(10):
    squares.append(item*item)

This can also be done using a "list comprehension":

# produces identical list to the above for loop
squares = [(item * item) for item in range(10)]

Finally, you are using numpy, this allows to do "vector" operations on arrays. The following is a simple example of the above square code.

eg.

numbers = numpy.array(range(10))
# or you could write -- numbers = numpy.arange(10)
squares = numbers * numbers

However, you can also do very complicated vector arithmetic as well. This allows you to create your Fourier transform very easily.

indices = array(range(125), ndmin=2).T
arr_fourier =  (cos(2*pi*indices*t/2001)*Trace).sum(axis=1)

Note that i has been replaced by indices. When multiplying the indices by t we create a transpose of the indices array so that we end up creating a 2d array as a result. At the end of the process you sum over the one of the axes (in this case 1) to reduce the 2d array back down to a 1d array of length 125.

Comments

0
from pylab import *
from numpy import *
x = linspace(-5,5,100)
t = linspace(0,2000,2001)
w1=cos(2*pi*3*t/2001)
w2=cos(2*pi*10*t/2001)
fourier=[]
Trace = w1+w2
    for i in range(1,126):
        fourier.append(sum(cos(2*pi*i*t/2001)*Trace))
print fourier
f=linspace(1,125,125)
plot(f,fourier)
show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.