如何关闭 FFT 图中的水平线

问题描述 投票:0回答:1

我试图显示观察数据的趋势以及相应的

fft
fft
中不断出现一条水平线,这是我不需要在图表中显示的部分。

为了自己制作,我在下面给出了一个MWE(伪数据)。

import numpy as np
from scipy.fft import fft, fftfreq
import matplotlib.pyplot as plt

observations = np.array([ 3.78998207e-01,  3.05199629e-01,  2.29614343e-01,  1.86568613e-01,
                         1.83449462e-01,  1.77892746e-01,  1.66237352e-01,  1.81950778e-01,
                         9.88351226e-02,  1.29674430e-01,  7.08703360e-02,  3.64963487e-02,
                         2.75641060e-02,  6.21573753e-02,  8.51043646e-02,  5.32184940e-02,
                         6.47005530e-02, -6.41628893e-02, -1.86618020e-01, -4.08624200e-02,
                        -2.71649960e-02, -8.22041576e-03,  9.13242105e-03,  1.67080717e-01,
                        -1.37465317e-01,  2.74977101e-04,  4.47602122e-02,  8.27649668e-02,
                        -5.60661808e-02, -2.26248880e-01, -1.54768403e-01, -4.46428484e-02,
                        4.57611677e-02,  9.83215698e-02,  9.22357256e-02, -1.23436114e-01,
                      -2.76981909e-01, -1.98824586e-01, -2.33452893e-01, -2.57550630e-01,
                      -9.13919527e-02,  2.64029442e-02, -5.44394568e-02,  4.02010984e-01,
                        3.27256645e-01,  2.14259077e-01,  5.08021357e-01,  5.55141121e-01,
                        6.11203693e-01,  5.34086779e-01,  2.19652659e-01,  1.71635054e-01,
                        1.30867565e-01,  1.25133212e-01,  1.02010973e-01,  1.16727950e-02,
                        2.84545455e-02, -1.73553706e-02, -1.33998184e-01, -1.36456573e-01,
                      -1.68706794e-01, -1.28378379e-01, -1.43710423e-01, -2.02454545e-01,
                      -4.30164457e-01, -5.19982175e-01, -3.74452537e-01, -3.64076796e-01,
                      -3.20950700e-01, -2.34052515e-01, -1.37158482e-01,  2.80797054e-02,
                        7.04379682e-02,  1.13920696e-01,  1.26391389e-01,  9.31688808e-02,
                        1.46000000e-01,  1.18380338e-01,  5.18909438e-02,  1.11584791e-01,
                        6.43582617e-02, -6.36856386e-02, -9.16134931e-02, -1.02616820e-01,
                      -4.43179890e-01, -1.28223431e+00, -1.86160058e+00, -1.43772912e+00,
                      -1.21047880e+00, -7.21282278e-01, -1.65349241e-01,  4.58791266e-02,
                        2.42897190e-01,  3.26587994e-01,  3.15827382e-01, -5.29090909e-02,
                        8.97887313e-03,  2.61194000e-02, -2.24566234e-01, -9.18572710e-02])

observed_fft = fft(observations)

fs = 100
n = observations.size
fft_fre = fftfreq(n, d=1/fs)
x_time = np.arange(len(observations))

fig, axs = plt.subplots(2)
axs[0].plot(x_time, observations)
axs[1].plot(fft_fre, np.abs(observed_fft))

输出: enter image description here

我知道出现这条水平线是因为

FFT
中的
fft_fre
频率包括正频率和负频率,因此围绕零频率的对称图(并且我需要显示
+ve
ve
频率)。 但是,是否有解决方法可以关闭连接最后一个负频率和第一个正频率的线路?

python numpy matplotlib fft
1个回答
0
投票

是的,有一个解决方法。

移动 FFT 输出:

fft_fre_shifted = fftshift(fft_fre)
fft_magnitude_shifted = np.abs(fftshift(observed_fft))

然后确定中点:

if n % 2 == 0:
    midpoint = n // 2
else:
    midpoint = (n // 2) + 1

然后在中点插入一个 Nan 来打破数据,这样第一个正频率和最后一个负频率就不相连了:

fft_fre_plot = np.insert(fft_fre_shifted, midpoint, np.nan)
fft_magnitude_plot = np.insert(fft_magnitude_shifted, midpoint, np.nan)

你应该得到这个:

New plot without horizontal line

© www.soinside.com 2019 - 2024. All rights reserved.