Reputation: 177
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?
Upvotes: 2
Views: 88
Reputation: 3059
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()
Upvotes: 0
Reputation: 40778
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.
Upvotes: 1
Reputation: 19753
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)]
Upvotes: 3
Reputation: 9413
You need an array initialization: fourier=[]
for i in range(1,125):
fourier.append(sum(cos(2*pi*i*t/2001)*Trace))
Upvotes: 1