【问题标题】:points of intersection of horizontal line with a function [duplicate]水平线与函数的交点[重复]
【发布时间】:2019-08-22 22:15:47
【问题描述】:

我有这段代码生成以下(图像),我将如何继续检测线与函数的交叉点?`

import numpy as np
import matplotlib.pyplot as plt

y = 0.4*np.ones(100)                
x = np.arange(0, 100)           

t = np.linspace(0,100,100)
Fs = 6000
f = 200
func = np.sin(2 * np.pi * f * t / Fs)

idx = np.where(func == y) # how i think i should do to detect intersections

print(idx)

plt.plot(x, y)        # the horizontal line
plt.plot(t,func)      # the function
plt.show()

【问题讨论】:

  • 这简直等价于func(t) - y = 0的求解题。对于您在此处给出的函数,解决方案是周期性的。

标签: python-3.x math signal-processing


【解决方案1】:

您可以使用以下表达式来获取最接近交点的数组t 的索引。

idx = np.argwhere(np.diff(np.sign(y - func))).flatten()

此表达式选择列表中符号发生变化的索引。然而,这只是实际交点的近似值。减小t 的步长以提高精度。


由于方程相对简单,另一种方法是手动求解并实现绘图的封闭式公式。

您有公式y = 0.4 和y = sin(2*pi*t*f/Fs)。交点的值为t,因此0.4 = sin(2*pi*t*f/Fs)。求解t 给出了两个答案:

t = (arcsin(0.4) + 2*pi*k) / (2*pi*f/Fs)
t = (pi - arcsin(0.4) + 2*pi*k) / (2*pi*f/Fs)

其中k 是任意整数。简而言之,循环遍历给定范围内的所有所需整数,并使用上述两个等式计算坐标t。您将获得一组点 (t,0.4),您可以将其绘制在图表上。

【讨论】:

    猜你喜欢
    • 2021-02-04
    • 1970-01-01
    • 2016-12-11
    • 2013-04-07
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 2014-08-24
    • 2021-07-05
    相关资源
    最近更新 更多