gpt4 book ai didi

python - 返回一个实值的、相位加扰的时间序列

转载 作者:太空狗 更新时间:2023-10-30 01:12:52 25 4
gpt4 key购买 nike

我正在尝试使用 Scipy/Numpy 在 Python 中实现时间序列的“相位加扰”。简而言之,我想:

  1. 做一个时间序列。
  2. 使用 FFT 测量频域中的功率和相位。
  3. 扰乱现有相位,随机将相位重新分配给频率。
  4. 使用 IFFT 返回具有加扰相位的实值(即非复数)时间序列,这样时间序列的功率谱保持不变,但时间序列的点与原始时间序列不同。

我有一个表面上似乎有效的脚本(见图表),但我怀疑我遗漏了一些重要的东西。特别是,我返回的相位加扰时间序列具有复值条目而不是实值条目,我不确定如何处理它。如果任何信号处理人员可以参与并指导我,我将不胜感激。

这是一个适用于 Jupyter Notebook 的示例脚本:

%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft, ifft

def phaseScrambleTS(ts):
"""Returns a TS: original TS power is preserved; TS phase is shuffled."""
fs = fft(ts)
pow_fs = np.abs(fs) ** 2.
phase_fs = np.angle(fs)
phase_fsr = phase_fs.copy()
np.random.shuffle(phase_fsr)
fsrp = np.sqrt(pow_fs) * (np.cos(phase_fsr) + 1j * np.sin(phase_fsr))
tsr = ifft(fsrp)
return tsr

ts = np.array([0.02, -1.04, 2.50, 2.21, 1.37, -0.05, 0.06, -0.22, -0.48, -0.31, 0.15, 0.99, 0.39, 0.65, 1.13, 0.77, 1.16, 1.35, 0.92, 1.42, 1.58, 1.33, 0.73, 0.98, 0.66, 0.13, -0.19, 2.05, 1.95, 1.25, 1.37, 0.85, 0.00, 1.37, 2.17, 0.69, 1.38, 0.49, 0.52, 0.62, 1.74, 0.67, 0.61, 1.03, 0.38, 0.64, 0.83, 1.16, 1.10, 1.30, 1.98, 0.92, 1.36, -1.49, -0.80, -0.08, 0.01, -0.04, -0.07, -0.20, 0.82, -0.26, 0.83, 0.09, -0.54, -0.45, 0.82, -0.53, -0.88, -0.54, -0.30, 0.52, 0.54, -0.57, 0.73, -0.04, 0.34, 0.59, -0.67, -0.25, -0.44, 0.07, -1.00, -1.88, -2.55, -0.08, -1.13, -0.94, -0.09, -2.08, -1.56, 0.25, -1.87, 0.52, -0.51, -1.42, -0.80, -1.96, -1.42, -1.27, -1.08, -1.79, -0.73, -2.70, -1.14, -1.71, -0.75, -0.78, -1.87, -0.88, -2.15, -1.92, -2.17, -0.98, -1.52, -1.92], dtype=np.float)

N = ts.shape[0]
TR = 2.
x = np.linspace(0.0, N*TR, N)
plt.plot(x, ts)
plt.ylabel('% Sig. Change')
plt.xlabel('Time')
plt.title('RSFC: Time domain')
plt.show()

ts_ps = phaseScrambleTS(ts)
plt.plot(x, ts, x, ts_ps)
plt.ylabel('% Sig. Change')
plt.xlabel('Time')
plt.title('RSFC, Orig. vs. Phase-Scrambled: Time domain')
plt.show()

fs = fft(ts)
fs_ps = fft(ts_ps)
xf = np.linspace(0.0, 1.0/(2.0*TR), N/2)
plt.plot(xf, 2./N * np.abs(fs[0:N/2]), 'b--', xf, 2./N * np.abs(fs_ps[0:N/2]), 'g:')
plt.grid()
plt.ylabel('Amplitude')
plt.xlabel('Freq.')
plt.title('RSFC, Orig. vs. Phase-Scrambled: Freq. domain, Amp.')
plt.show()

编辑:根据以下解决方案之一,我将偶数情况概括为奇数情况,如下所示。我相信现在不需要检测不可忽略的虚部的条件,但我会留给后代使用。

def phaseScrambleTS(ts):
"""Returns a TS: original TS power is preserved; TS phase is shuffled."""
fs = fft(ts)
pow_fs = np.abs(fs) ** 2.
phase_fs = np.angle(fs)
phase_fsr = phase_fs.copy()
if len(ts) % 2 == 0:
phase_fsr_lh = phase_fsr[1:len(phase_fsr)/2]
else:
phase_fsr_lh = phase_fsr[1:len(phase_fsr)/2 + 1]
np.random.shuffle(phase_fsr_lh)
if len(ts) % 2 == 0:
phase_fsr_rh = -phase_fsr_lh[::-1]
phase_fsr = np.concatenate((np.array((phase_fsr[0],)), phase_fsr_lh,
np.array((phase_fsr[len(phase_fsr)/2],)),
phase_fsr_rh))
else:
phase_fsr_rh = -phase_fsr_lh[::-1]
phase_fsr = np.concatenate((np.array((phase_fsr[0],)), phase_fsr_lh, phase_fsr_rh))
fsrp = np.sqrt(pow_fs) * (np.cos(phase_fsr) + 1j * np.sin(phase_fsr))
tsrp = ifft(fsrp)
if not np.allclose(tsrp.imag, np.zeros(tsrp.shape)):
max_imag = (np.abs(tsrp.imag)).max()
imag_str = '\nNOTE: a non-negligible imaginary component was discarded.\n\tMax: {}'
print imag_str.format(max_imag)
return tsrp.real

最佳答案

Scipy 有 rfftirfft 例程来对实数信号进行傅里叶变换(r 是实数)。下面的函数返回一个正确加扰的信号。我应该注意,所写的例程仅适用于偶数长度的信号,但为奇数长度的信号修复它应该不会太难,只需查看 rfft 文档

from scipy.fftpack import rfft, irfft

def phaseScrambleTS(ts):
"""Returns a TS: original TS power is preserved; TS phase is shuffled."""
fs = rfft(ts)
# rfft returns real and imaginary components in adjacent elements of a real array
pow_fs = fs[1:-1:2]**2 + fs[2::2]**2
phase_fs = np.arctan2(fs[2::2], fs[1:-1:2])
phase_fsr = phase_fs.copy()
np.random.shuffle(phase_fsr)
# use broadcasting and ravel to interleave the real and imaginary components.
# The first and last elements in the fourier array don't have any phase information, and thus don't change
fsrp = np.sqrt(pow_fs[:, np.newaxis]) * np.c_[np.cos(phase_fsr), np.sin(phase_fsr)]
fsrp = np.r_[fs[0], fsrp.ravel(), fs[-1]]
tsr = irfft(fsrp)
return tsr

关于python - 返回一个实值的、相位加扰的时间序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39543002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com