【问题标题】:Create a 2D array (3D plot) from two 1D arrays (2D plots) in Python (for calculating the Hilbert spectrum)在 Python 中从两个 1D 数组(2D 图)创建一个 2D 数组(3D 图)(用于计算希尔伯特谱)
【发布时间】:2016-07-14 13:20:19
【问题描述】:

我想在 Python 中将 Hilbert spectrum 计算为 3D 图(即 2D 数组)。希尔伯特谱是time x frequency -> amplitude 形式的函数,它为每个时间和频率对分配一个幅度值。

计算频谱的方法将一个或多个二维信号作为输入,每个二维信号有两个分量:time -> frequencytime -> amplitude。以单个信号a 为例。 y1 是频率值,y2 是幅度值。

a_x = [1,2,3]
a_y1 = [1,2,1]
a_y2 = [4,5,6]

我想将这两个 2D 图转换成一个 3D 图,这样X x Y1 -> Y2

a(1,1) = 4
a(2,2) = 5
a(3,1) = 6

实际值将是浮点数。到目前为止,我的解决方案是获取y1 中的最大值和最小值,并以预定精度(例如 0.01)初始化网格。在这个例子中:

y1_max = np.amax(a_y1)
y1_min = np.amin(a_y2)
# Initialise 2d array of zeros
hilbert_spectrum = np.zeros((len(a_x), len(np.linspace(y1_min, y1_max, 0.01)))

然后我会这样填写网格:

# Fit the old y1 values into new grid
y1_grid = np.floor((a_y1 - y1_min) / 0.01).astype(np.int)

# Fill the 2D hilbert spectrum
hilbert_spectrum[1, y1_grid[0]] = 4
hilbert_spectrum[2, y1_grid[1]] = 5
hilbert_spectrum[3, y1_grid[2]] = 6

但是,当有多个输入信号时,这会变得复杂。有没有更数学/简洁的方法来做到这一点?输出应该是一个可用于进一步计算的二维数组。

【问题讨论】:

    标签: python numpy spectrum


    【解决方案1】:

    关注:http://matplotlib.org/examples/mplot3d/trisurf3d_demo2.html

    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.tri as mtri
    
    a_x = [1,2,3]
    a_y1 = [1,2,1]
    a_y2 = [4,5,6]
    
    # Triangulate parameter space to determine the triangles
    tri = mtri.Triangulation(a_x, a_y1)
    
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1, projection='3d')
    
    # The triangles in parameter space determine which x, y, z points are
    # connected by an edge
    ax.plot_trisurf(a_x, a_y1, a_y2, triangles=tri.triangles, cmap=plt.cm.Spectral)
    
    plt.show()
    

    【讨论】:

    • 你能告诉我在哪里可以得到实际的二维数组吗?我知道我可以用 matplotlib 绘制点,但我需要得到二维数组而不绘制它。对不起,如果我的问题不够清楚。
    • 我提供了类似 Delaunay 三角测量的方法,您可以从 scipy.spatial.Delaunay 获得。您将获得的数据结构不是二维数组。这是一个图表(参见:en.wikipedia.org/wiki/Graph_(abstract_data_type))。
    • 抱歉,我不知道如何从图中生成二维数组。也许只是我对三角测量缺乏了解。
    猜你喜欢
    • 1970-01-01
    • 2019-04-07
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 2010-09-11
    • 2018-12-11
    相关资源
    最近更新 更多