Mehdi Souregi
Mehdi Souregi

Reputation: 3265

The numpy rfft dimension vector

I am trying to understand the numpy function rfft.

When I do this :

frame = [5,5,5,5,5,5]
tfdf = numpy.fft.rfft([5,5,5,5,5,5])
print(len(frame))
print(len(tfdf))
print(tfdf)

The result is :

6
4
[30.+0.j  0.+0.j  0.+0.j  0.+0.j]

Why the lenth of tfdf is 4 and not 6 ? Where the 4 comes from ?

Second question if we add the NFFT like this :

NFFT = 20
frame = [5,5,5,5,5,5]
tfdf = numpy.fft.rfft([5,5,5,5,5,5], NFFT)
print(len(frame))
print(len(tfdf))
print(tfdf)

The result is :

6
11
[ 3.00000000e+01+0.00000000e+00j  1.82843788e+01-1.82843788e+01j
  0.00000000e+00-1.53884177e+01j -2.40652626e+00-2.40652626e+00j
  5.00000000e+00-8.88178420e-16j  5.00000000e+00-5.00000000e+00j
  8.88178420e-16-3.63271264e+00j  1.22618638e+00+1.22618638e+00j
  5.00000000e+00+0.00000000e+00j  2.89596110e+00-2.89596110e+00j
  0.00000000e+00+0.00000000e+00j]

Again Where the 11 comes from?

Upvotes: 1

Views: 709

Answers (2)

Sahith Kurapati
Sahith Kurapati

Reputation: 1715

This function computes the one-dimensional discrete Fourier Transform for real input. The documentation says the following:

This function does not compute the negative frequency terms, and the length of the transformed axis of the output is therefore n//2 + 1

Since your original vector has 6 terms, it becomes => 6//2 + 1 = 3 + 1 = 4

Same goes for the 2nd case where you have 20 terms => 20//2 + 1 = 10 + 1 = 11

Upvotes: 3

dc3726
dc3726

Reputation: 306

To answer your first question:

Per the documentation here: https://numpy.org/doc/stable/reference/generated/numpy.fft.rfft.html

If an axis and n are not specified, the rfft method returns a transformed axis with length (n/2)+1 is the array is even, and (n+1)/2 if it is odd.

Since as you show the length of the array frame is 6, which is even, the method will return an array with length (6/2)+1 = 4.

To answer your second question, from similar logic above, you are passing through an n value of 20 which is even so the array returned will be of size (20/2)+1 = 11

Upvotes: 1

Related Questions