Curve interpolation

I have code where a curve is generated using random values. and the horizontal line that runs through it. The code is as follows.

import numpy as np
import matplotlib.pylab as pl

data = np.random.uniform(low=-1600, high=-550, size=(288,))
line = [-1290] * 288

pl.figure(figsize = (10,5))
pl.plot(data)
pl.plot(line)

Now I need to find the coordinates for all the intersection points of the curve (data) and the line. A curve consists of linear segments that connect adjacent points. And there are many intersection points where the curve corresponds to the line. Any help would be greatly appreciated. thanks!

+4
source share
3 answers

Shapely, Shapely , . , , Gist by @endolith. kazemakase.

from matplotlib import mlab

def find_crossings(a, value):
    # Normalize the 'signal' to zero.
    sig = a - value

    # Find all indices right before any crossing.
    indices = mlab.find((sig[1:] >= 0) & (sig[:-1] < 0) | (sig[1:] < 0) & (sig[:-1] >= 0))

    # Use linear interpolation to find intersample crossings.
    return [i - sig[i] / (sig[i+1] - sig[i]) for i in indices]

( x), (-1290 ). :

find_crossings(data, -1290)

100 :

x = find_crossings(data, -1290)
plt.figure(figsize=(10,5))
plt.plot(data)
plt.plot(line)
plt.scatter(x, [-1290 for p in x], color='red')
plt.show()

Intersection of the zero sequence of functions

+3

, , , . , , .

:

  • , ,
  • ,
+2

Here is a solution using shapely:

import numpy as np
import matplotlib.pylab as pl
np.random.seed(0)
data = np.random.uniform(low=-1600, high=-550, size=(50,))
line = [-1290] * len(data)

pl.figure(figsize = (10,5))
pl.plot(data)
pl.plot(line)

from shapely import geometry

line = geometry.LineString(np.c_[np.arange(len(data)), data])
hline = geometry.LineString([[-100, -1290], [1000, -1290]])

points = line.intersection(hline)

x = [p.x for p in points]
y = [p.y for p in points]

pl.plot(x, y, "o")

output:

enter image description here

+2
source

Source: https://habr.com/ru/post/1623911/


All Articles