# Detect straight lines & their angle on a waveform

## Recommended Posts

Hello,

I am interested in detecting the straight lines in an arbitrary graph and the line's angle. As in the attached photo, the waveform has many "straight sections". Detecting a straight section while it is at an angle is a bit tricky. Any ideas on how to do it?

##### Share on other sites

You are looking for the tangent to an inflection point.(second order derivative d2x/d2t=0)

##### Share on other sites

I created a curve (see attached file) that is made from a straight inclined line (0-190) a curve (190-812), another inclined straight line 813-987) and a third inclined straight line (988-1000). I made an effort and managed to locate when the slope changes but how can I tell which is the curved and which is the straight part of the plot? The second derivative does not show something useful.

Differentiator and Smoother.2.vi

Savitsky-Golay Differentiator Core.vi

Edited by Ano Ano
##### Share on other sites

Your VI is missing a subVI so I cannot run it, but you will likely need to use the two derivatives together.  The zero crossings of the second derivative will give you the points of inflection as ShaunR pointed out.  Use these locations/indices to reference points on the original function and the first derivative.  You can likely filter out your segments by looking at the first derivative values and determine which points of inflection are important or are on your curved segement.  The value of the first derivative at that point will give you your slope for the tangent line.

Edited by Jordan Kuehn
##### Share on other sites

Somewhere you will have to create threshold and you will have to decide what is strait for what you are doing. We can draw a line on the ground and have everyone agree that it is a strait line. However the Earth is a sphere, so the surface is curved therefore any line drawn on the ground cannot be strait. The line is therefore strait and not strait depending on your frame of reference.

Using the built-in derivative function may give you too much noise. You may have better luck using taking the slope of points some number of samples from each other. This works better for the sensor data I get.

##### Share on other sites
1 hour ago, Tim_S said:

Somewhere you will have to create threshold and you will have to decide what is strait for what you are doing. We can draw a line on the ground and have everyone agree that it is a strait line. However the Earth is a sphere, so the surface is curved therefore any line drawn on the ground cannot be strait. The line is therefore strait and not strait depending on your frame of reference.

Using the built-in derivative function may give you too much noise. You may have better luck using taking the slope of points some number of samples from each other. This works better for the sensor data I get.

You don't need to threshold/ An inflection point is a change in sign of the d2y/d2t from positive to negative or negative to positive. Since you only need two points to draw a line, Just find the two points either side of the inflection (where they change sign) and draw your line through them.If you need a better estimate because your datapoints are a bit sparse then you can do a linear fit on a few points on either side of it.

Lets see who can come up with a "cool" way of detecting sign changes and extracting the points to fit

Edited by ShaunR
##### Share on other sites

A very good way to take an N-order derivative of a noisy signal is the Savitski-Golay filter, which is in LabVIEW. Read up on it, you will see it is much better than the naive method of taking the derivative (the simple forward, backward, and central approximations)

##### Share on other sites
4 hours ago, ShaunR said:

You don't need to threshold/ An inflection point is a change in sign of the d2y/d2t from positive to negative or negative to positive.

With perfect data I would agree. With real-world data, not so much. I've had too many cases where the SNR is 3:1 for what trying to measure and all that little electrical noise creates inflection points even after running through filtering. (Had to explain that cheap sensors are not the place to save money on and bubblegum is not a good medium for splicing signal cables.)

5 hours ago, ShaunR said:

Lets see who can come up with a "cool" way of detecting sign changes and extracting the points to fit

Got some data in mind?

5 minutes ago, MarkCG said:

A very good way to take an N-order derivative of a noisy signal is the Savitski-Golay filter, which is in LabVIEW. Read up on it, you will see it is much better than the naive method of taking the derivative (the simple forward, backward, and central approximations)

I'll have to take a look at that. I've used a moving average filter with good success, which (quickly reading Wikipedia) looks to be a specialized case.

##### Share on other sites
11 hours ago, MarkCG said:

A very good way to take an N-order derivative of a noisy signal is the Savitski-Golay filter, which is in LabVIEW. Read up on it, you will see it is much better than the naive method of taking the derivative (the simple forward, backward, and central approximations)

I can also recommend Savitski-Golay, which is basically just fitting to a Nth-order polynomial, and estimates multiple derivatives at once.  Apply some “straight-line” criteria such as all derivatives beyond the first being “small”.

##### Share on other sites

Hello,

I reached to a good point in detecting when its a straight line (T) and curve (F) in an array. But my mind is blocked on how to extract Nth arrays of straight lines!

Please check the attached code and tell me what I am doing wrong

Differentiator and Smoother.2.vi

Savitsky-Golay Differentiator Core.vi

Second derivative 5.vi

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×