B4J Question xChart lib, DynamicLines - any smoothing option ?

peacemaker

Expert
Licensed User
Longtime User
HI, All

Is there possibility to make DynamicLines like some ... Dynamic smooth curves ?
 

peacemaker

Expert
Licensed User
Longtime User
Your lib is super, so, i think it's better to use it, making extra functions...
Smoothing here i guess, it's making a curve in the center of each point.
Or indeed - instead of the lines should be curves between the points.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
I had to do some smoothing in one of my projects and created this based on "corner cutting".
I have no idea if there is such a term, but it works very well and I use it quite a lot.
You can use it on any set of points, even closed shapes. You can pass the x and y values to the Chart object in XChart.

demo.png
 

Attachments

  • smoothing.zip
    21.5 KB · Views: 109
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
For those interested...

'Corner Cutting Algorithm'

Based on an age-old woodworking technique for transforming a square blank into a circle.
All you need is a saw. Woodworking is one of my hobbies.

Imagine three points. They form a corner. Cut the tip off the corner.
You now have four points, and a smoother connection between the end points.

Cutting close to the tip of the corner makes it less round.
Cutting close to the midpoint of each edge makes it more round.

Final smoothness is determined by the number of times the resulting curve is fed back into the smoothing process.
Each iteration increases the number of points. For 3 starting points: 3, 4, 6, 10 ... 2 + 2 * (Ni - 2)
It goes up quickly (I usually repeat it 3 or 4 times), but the process is fast enough to use in animations.

It works on all point lists, even for complex curves that intersect themselves.
In a virtual trainset project I created, the tracks are sampled from a satellite image by hand and then smoothed this way.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
You didn't mention the oscillograph chart. I haven't used it.
@klaus said there were some smoothing options.

Presumable, you can save a buffer of visible points.
Apply the above algorithm, impose the smoothed curve on the chart.
Then update this as the next point comes in and redraw.
Maybe?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
If values are similar - no smoothing is needed, but for point with big difference - if each chart point is added by a timer one by one - it's impossible to detect what curve bending is needed to draw: no next point, 3rd point, to understand the trend.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I would suggest you to check if a value is out of range.
Every time you get a new value you can check if the previous value is out of range with a given tolerance.
If yes, set its value to the mean value between the last and the value before the suspicious value.

You could also calculate a tendency curve with the last three values.
When you get a new value, calculate the linear tendency with last two points or the polynomial tendency curve with the last three points.
Calculate the expected new value and if the new real value is out of range set it to expected value.

Or you could use the Exponential smoothing, but this is more useful for noisy signals, but your case is more about really wrong out of range values.
The equation is : yd[t] = yd[t-1] + Kt * (ys[t] - yd[t-1])
Where
Kt is the smoothing factor between 0 and 1. 1 means no smoothing
ys[t] = current source value
yd[t] = new smoothed destination value
yd[t-1] = previous smoothed destination value at t - 1
This smoothing is similar to a low pass filter and introduces a kind of delay.

Attached a small test program with Exponential smoothing.
It uses the xChartLite class version 1.5 not yet published.
I first used the xChart library and then checked with the xChartLite library.

1664107027221.png
 

Attachments

  • XChartOscilloscope.zip
    23.8 KB · Views: 84
Upvote 0

klaus

Expert
Licensed User
Longtime User
I played a bit further with the smoothing with values out of range.
The signal is a sine with an amplitude of 1 and a mean value of 25 plus a random noise with an amplitude of 0.5.
And a value out of range every 20 samples.
When a value is out of range, it is set to the expected value with a linear tendency.
y(n) = y(n - 1) + y(n - 1) - y(n - 2)

1664129964543.png



Attached a new project.
 

Attachments

  • XChartOscilloscope.zip
    24.7 KB · Views: 80
Upvote 0

klaus

Expert
Licensed User
Longtime User
And the last version with linear interpolation. It works better than with predictive value.
It works also well when more than one successive values are out of range.
When values are out of range, the adjustment is made when the next correct value arrives.
And the wrong values in between the two correct values are interpolated.
You see it well with the wrong values around 4.

1664197184102.png
 

Attachments

  • XChartOscilloscope.zip
    24.6 KB · Views: 86
Last edited:
Upvote 0
Top