振幅和相位谱。移相保持振幅不变

2024-10-01 17:22:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有数据,有相等的间隔和在相关点的相应测量。作为一个例子,下面是我的数据摘录:

y =[2.118, 2.1289, 2.1374, 2.1458, 2.1542, 2.1615, 2.1627, 2.165 2.1687...]

点间距为0.1

所以,我需要从数据中得到振幅谱(振幅对频率)和相位谱(相位角对频率)。 加上π/2的负相移。在

在移相和保持振幅不变的情况下,我需要做逆fft并得到新的信号。我想用Python来做这个。在

你能给我举个例子吗。在

我所使用的代码,是从另一个SO问题中获取的,但是我做了一些修改

## Perform FFT WITH SCIPY
signalFFT = np.fft.fft(y)

## Get Power Spectral Density
signalPSD = np.abs(signalFFT) ** 2
signalPhase = np.angle(signalFFT)

## Shift the phase py +90 degrees
new_signalPhase =(180/np.pi)*np.angle(signalFFT)+90 


## Get frequencies corresponding to signal 
fftFreq = np.fft.fftfreq(len(signalPSD), 0.1)

## Get positive half of frequencies
i = fftFreq>0

##
plt.figurefigsize=(8,4)
#plt.plot(fftFreq[i], 10*np.log10(signalPSD[i]));

plt.plot(fftFreq[i], new_signalPhase[i]);
plt.ylim(-200, 200);
plt.xlabel('Frequency Hz');
plt.ylabel('Phase Angle')
plt.grid()
plt.show()

问题是,我想重新生成信号,具有相同的振幅,但移相。我知道答案是smth与ifft有关,但我应该如何准备数据呢?你能告诉我进一步的步骤吗。在

output


Tags: 数据fftnewget信号npplt例子
1条回答
网友
1楼 · 发布于 2024-10-01 17:22:29

这是您的代码,经过一些修改。我们应用傅里叶变换,对变换后的信号进行相移,然后进行傅里叶逆变换,得到相移的时域信号。在

注意,变换是用rfft()irfft()完成的,相移是通过简单地将变换后的数据乘以厘米路径.rect(1.,阶段)。相移相当于将复变换信号乘以exp(i*phase)。在

在图中的左面板中,我们显示原始信号和移位信号。新信号提前90度。在右面板中,我们显示了左轴上的功率谱。在这个例子中,我们有一个单一频率的功率。相位在右轴上绘制。但是,由于我们只有一个频率的功率,所以相位谱在其他地方都显示出噪声。在

#!/usr/bin/python

import matplotlib.pyplot as plt
import numpy as np
import cmath

# Generate a model signal
t0 = 1250.0
dt = 0.152
freq = (1./dt)/128

t = np.linspace( t0, t0+1024*dt, 1024, endpoint=False )
signal = np.sin( t*(2*np.pi)*freq )

## Fourier transform of real valued signal
signalFFT = np.fft.rfft(signal)

## Get Power Spectral Density
signalPSD = np.abs(signalFFT) ** 2
signalPSD /= len(signalFFT)**2

## Get Phase
signalPhase = np.angle(signalFFT)

## Phase Shift the signal +90 degrees
newSignalFFT = signalFFT * cmath.rect( 1., np.pi/2 )

## Reverse Fourier transform
newSignal = np.fft.irfft(newSignalFFT)

## Uncomment this line to restore the original baseline
# newSignal += signalFFT[0].real/len(signal)


# And now, the graphics          -

## Get frequencies corresponding to signal 
fftFreq = np.fft.rfftfreq(len(signal), dt)

plt.figure( figsize=(10, 4) )

ax1 = plt.subplot( 1, 2, 1 )
ax1.plot( t, signal, label='signal')
ax1.plot( t, newSignal, label='new signal')
ax1.set_ylabel( 'Signal' )
ax1.set_xlabel( 'time' )
ax1.legend()

ax2 = plt.subplot( 1, 2, 2 )
ax2.plot( fftFreq, signalPSD )
ax2.set_ylabel( 'Power' )
ax2.set_xlabel( 'frequency' )

ax2b = ax2.twinx()
ax2b.plot( fftFreq, signalPhase, alpha=0.25, color='r' )
ax2b.set_ylabel( 'Phase', color='r' )


plt.tight_layout()

plt.show()

这是图形输出。在

Raw and shifted signals, power and phase spectrum

相关问题 更多 >

    热门问题