1

let's say I have:

c = array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], float)

then I take a fast fourier transform:

r = rfft(c)

which produces the following complex array:

r = [ 21.+0.j ,  -3.+5.19615242j , -3.+1.73205081j , -3.+0.j ]

the number of elements in the new array is 1/2*N + 1. I'm trying to tell python to change the values of SPECIFIC elements in the new array. I want to tell python to keep the FIRST 50% of the elements and to set the others equal to zero, so instead the result would look like

r =     r = [ 21.+0.j ,  -3.+5.19615242j , 0 , 0 ]

how would I go about this?

user3290682
  • 45
  • 1
  • 5

3 Answers3

1

You can use slice notation and extend the result to the correct length:

r = r[:len(r)/2].extend([0] * (len(r) - len(r)  / 2))

The * syntax just repeats the zero element the specified number of times.

Community
  • 1
  • 1
Sam
  • 1,176
  • 6
  • 19
1

rfft return a numpy array which helps easy manipulation of the array.

c = [1,2,3,4,5,6]
r = rfft(c)
r[r.shape[0]/2:] = 0
r
>> array([21.+0.j, -3.+5.1961j, 0.+0.j , 0.+0.j])
Chitrasen
  • 1,706
  • 18
  • 15
0

You can split the list in half, then append a list of zeros as the same length as the remaining part:

>>> i
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> i[:len(i)/2] + [0]*len(i[len(i)/2:])
[1, 2, 3, 4, 5, 0, 0, 0, 0, 0]
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284