4

Code below:

import numpy as np 
from numpy import random_intel
import mkl_fft
import matplotlib.pyplot as plt

n = 10**5
a = np.random_intel.rand(n)
b = mkl_fft.fft(a)
plt.scatter(b.real,b.imag)
plt.show()
print(b)
for i in b :
    if i.real > n/2:
        print("Weird FFT Number is ",i)

Result is : output

You can see:

Weird FFT Number is  (50020.99077289924+0j)

Why FFT with random set came out one particular number?


(Thanks to Paul Panzer & SleuthEye)

With mkl_fft.fft(a-0.5) the final result is: final result


[2019/03/29 Updated]

With normalized data everything went well

b = mkl_fft.fft((a - np.mean(a))/np.std(a))

The average value of (a - np.mean(a))/np.std(a) is near zero

Tiny tiny
  • 43
  • 4

2 Answers2

2

That is the constant or zero frequency mode, which is essentially the mean of your signal. You are sampling uniformly from the unit interval, so the mean is ~0.5. Some fft implementations scale this with the number of points to save a multiplication.

Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
2

The large value in the FFT output happens to be the very first one which corresponds to the DC component. This indicates that the input has a non-zero average value over the entire data set.

Indeed if you look closer at the input data, you might notice that the values are always between 0 and 1, with an average value around 0.5. This is consistent with the rand function implementation which provides pseudo-random samples drawn from a uniform distribution over [0, 1).

You may confirm this to be the case by subtracting the average value with

b = mkl_fft.fft(a - np.mean(a))

and noting that the large initial value b[0] should be near zero.

SleuthEye
  • 14,379
  • 2
  • 32
  • 61
  • _"decreases significantly to be of similar magnitude as the others"_ Don't think so, should be zero up to numerical noise. Whereas the other modes should be distinct from zero. – Paul Panzer Mar 15 '19 at 02:39
  • I based that statement on the fact that typically a random variable with zero expected value would still have some non-zero average value, but you are right that in this specific case where the mean is constructed to be exactly zero, `b[0]` should be very near zero. – SleuthEye Mar 15 '19 at 02:45
  • Yep, I think you are correct if instead of the sample mean you subtract the true mean, which should be 0.5 (-resolution/2 if we want to be pedantic). – Paul Panzer Mar 15 '19 at 03:03
  • I found out a ultimate solution :`b = mkl_fft.fft((a - np.mean(a))/np.std(a))` – Tiny tiny Mar 29 '19 at 02:34