我的目标是从一个经验观测的时间序列中创建一个代孕时间序列,在这个序列中,我希望保持自相关和功率分布在频域,但将相位随机化。
完成这项任务的步骤如下:
。
下面是我的代码的当前状态,其中创建了一个随机时间序列,用于表示或再现。
import numpy as np
TS = np.random.normal(0, 1, 3000) # The time-series
FFT = np.fft.rfft # FFT of time-series
Random_phases = np.exp(np.random.uniform(0,np.pi,len(TS)/2+1)*1.0j) # Generate random phases
FFT = FFT * Random_phases # Randomize the phases in the FFT
TS = np.fft.irfft(FFT) # Tranforms the frequency-domain back into the time-domain = Surrogate time-series代码打印的错误使我陷入以下困境:
Traceback (most recent call last):
File "/Users/...", line 7, in <module>
Random_phases = np.exp(np.random.uniform(0,np.pi,len(TS)/2+1)*1.0j) # Generate random phases
File "mtrand.pyx", line 1121, in numpy.random.mtrand.RandomState.uniform
File "_common.pyx", line 615, in numpy.random._common.cont
TypeError: 'float' object cannot be interpreted as an integer我理解计算中某个地方的代码Random_phases = np.exp(np.random.uniform(0,np.pi,len(TS)/2+1)*1.0j)的第7行产生浮点数而不是整数结果,这导致了问题的出现。对吗?
在不干扰频域相位的实际随机化的情况下,怎样才能克服这个问题呢?
发布于 2022-06-22 14:01:23
len(TS)/2+1是一个浮点数,而uniform需要一个int作为size参数。试试int(len(TS)/2+1)或len(TS)//2+1
np.fft.rfft只是函数,而不是按原样调用的。想必你想用时间序列(即np.fft.rfft(TS) )来称呼它。
把所有东西拼凑在一起:
FFT = np.fft.rfft(TS) # FFT of time-series
Random_phases = np.exp(np.random.uniform(0,np.pi,len(TS)//2+1)*1.0j) # Generate random phases
FFT = FFT * Random_phases # Randomize the phases in the FFT
TS = np.fft.irfft(FFT) # Tranforms the frequency-domain back into the time-domain = Surrogate time-serieshttps://stackoverflow.com/questions/72716727
复制相似问题