我正在编写一个脚本来模拟随机白噪声,以研究其转变为信号形式时的行为。
下面是我遇到的问题:
def white_noise(n: int, N: int, slope: int = grad):
"""
- Creates a data set of white noise with size n, N;
- Filters this dataset with the corresponding slope;
This slope is usually equal to -5/3 or -2/3
- Makes sure the slope is equal to the requested slope in the double log scale.
@param n: size of random array
@param N: number of random arrays
@param slope: slope of the gradient
@return: white_noise, filtered white_noise and the original signal
"""
x = np.linspace(0, 1, n)
slope_loglog = (10 ** (slope * np.log10(x) + 1))
white_noise = rnd.rand(n, N) ** 2 # squaring the white noise to retrieve the power spectrum
white_noise_filtered = []
white_noise_signal = []
white_noise_retransformed = []
for k in range(N):
white_noise_filtered.append(white_noise[:,k] * slope_loglog)
white_noise_signal.append(fft.ifft(white_noise[:,k] * slope_loglog))
white_noise_filtered, white_noise_signal, white_noise_retransformed = \
np.asarray((white_noise_filtered, white_noise_signal, white_noise_retransformed))
white_noise_filtered = white_noise_filtered.transpose()
white_noise_signal = white_noise_signal.transpose().imag
return white_noise, white_noise_filtered, white_noise_signal, white_noise_retransformed, slope_loglog
在我应用快速傅里叶逆变换(1D)的步骤中,我的模拟噪声生成了一个复杂的数组(在处理傅里叶变换时可以预料到)。我不明白的是为什么所有复杂值的实数部分等于“ Inf”。这些应该具有有限的值,那么我在做什么错了?
(注意:slope_loglog
与梯度为-5/3的对数-对数转换数据有关。)
由于我需要先安装ift,然后再使用fft才能再次获得原始噪声(或偏移),所以我需要了解为什么我的脚本会这样做。仅采用阵列“ white_noise_signal”的虚部不会产生原始噪声。
答案 0 :(得分:1)
这与FFT / IFFT无关
np.log10(0) = -np.inf
和
10 ** np.inf = np.inf
因此,如果您使用负斜率,即slope = -1
10 ** (slope * np.log10(0)) = np.inf
将其乘以 any 信号,您将得到inf
。
此外,如果您跳过以下列表之间的列表,则可以显着简化代码:
def white_noise(n: int, N: int, slope: int = -5/3):
"""
- Creates a data set of white noise with size n, N;
- Filters this dataset with the corresponding slope;
This slope is usually equal to -5/3 or -2/3
- Makes sure the slope is equal to the requested slope in the double log scale.
@param n: size of random array
@param N: number of random arrays
@param slope: slope of the gradient
@return: white_noise, filtered white_noise and the original signal
"""
x = np.linspace(0, 1, n)
slope_loglog = (10 ** (slope * np.log10(x) + 1))
white_noise = np.random.rand(n, N) ** 2 # cubing the white noise to retrieve the power spectrum
white_noise_filtered = (white_noise * slope_loglog).T
white_noise_signal = (np.fft.ifft(white_noise, axis=-1) * slope_loglog).T.imag
return white_noise, white_noise_filtered, white_noise_signal, slope_loglog