Recent

Author Topic: TAChart spline series: how to return Y value for a given X value?  (Read 13279 times)

ironphil

  • Jr. Member
  • **
  • Posts: 58
TAChart spline series: how to return Y value for a given X value?
« on: September 11, 2011, 01:26:54 am »
I am using both the Bspline and cubic spline series with TAChart and they work like a charm. However, since I use the spline to modelize some background, I eventually need to get the Y value of the spline at a specific X value (which is not necessarily at a source point) in order to subtract the background.

Is there a way to retrieve the Y value of the curve for a given X value?

Ask

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 687
Re: TAChart spline series: how to return Y value for a given X value?
« Reply #1 on: September 11, 2011, 12:26:13 pm »
Use Calculate function since r32282

ironphil

  • Jr. Member
  • **
  • Posts: 58
Re: TAChart spline series: how to return Y value for a given X value?
« Reply #2 on: September 12, 2011, 01:39:39 am »
Perfect! It works just as expected. Thank you.

bobihot

  • New Member
  • *
  • Posts: 46
Re: TAChart spline series: how to return Y value for a given X value?
« Reply #3 on: April 07, 2014, 01:52:01 am »
Hi,
I need of this, but can not solve by self.
Are You can give me some demo code?
( To get Y value, from X )
Thanks

wp

  • Hero Member
  • *****
  • Posts: 13411
Re: TAChart spline series: how to return Y value for a given X value?
« Reply #4 on: April 07, 2014, 10:09:12 am »
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  x, y: Double;
begin
  x := 1.0;
  y := Chart1CubicSplineSeries1.Calculate(x);
  ShowMessage(Format('The spline value for x = %f is %f', [x, y]));
end;
 

Is that what you want?

bobihot

  • New Member
  • *
  • Posts: 46
Re: TAChart spline series: how to return Y value for a given X value?
« Reply #5 on: April 07, 2014, 10:49:28 am »
That this, but with TBSplineSeries do not work. Also NearestgraphPoint.
With TCubicSplineSeries they work fine?
Thanks

I was try with this sample:
« Last Edit: April 07, 2014, 11:01:40 am by bobihot »

wp

  • Hero Member
  • *****
  • Posts: 13411
Re: TAChart spline series: how to return Y value for a given X value?
« Reply #6 on: April 07, 2014, 11:03:26 am »
You are right, a "TBSplineSerie.Calculate()" had not been implemented. Let me see if I can fix this. But be patient, I have to dive into code that I have not written myself...
« Last Edit: April 07, 2014, 11:15:41 am by wp »

wp

  • Hero Member
  • *****
  • Posts: 13411
Re: TAChart spline series: how to return Y value for a given X value?
« Reply #7 on: April 12, 2014, 09:25:43 pm »
Trunk rev 44706 contains now a Calculate method for TBSplineSeries which allows to calculate the interpolated spline value for any x value within the series extent. The calculation is done by interpolation between tabulated values, so please don't expect ultimate precision...

Anyway, this does not solve your problem of tracking the interpolated values with the crosshair cursor. And I am a bit confused what to do about that. The problem is that TBSplineSeries, in my opinion, behaves correctly with respect to the DataPointCrosshairTool. In all TAChart series, the cursor jumps only to "real" data points. The exception is CubicSplineSeries where the cursor follows the interpolated points and does not lock to the "real" data points. In my feeling, this behavior is wrong, and I am thinking about removing the curve tracking feature of the CubicSplineSeries, at least optionally, to get a consistent behavior of all series.


bobihot

  • New Member
  • *
  • Posts: 46
Re: TAChart spline series: how to return Y value for a given X value?
« Reply #8 on: April 13, 2014, 12:43:20 pm »
Thanks- I will be try now.
CubicSplineSeries pass by initial points and do not have difference to spline line. Instead of B-spline, where passed inside.
Now I try and write conclusion :)
If I understand what-where can to do, will try to change some thing.
In my case I need to recover noise signal and this mean that maybe B-spline is a real signal, which is need to track.
Very, very thanks!  :)

P.M.
Why new snapshots is with fpc-2.6.2 ? I exchange with 2.6.4 and work very well.
« Last Edit: April 13, 2014, 01:20:55 pm by bobihot »

wp

  • Hero Member
  • *****
  • Posts: 13411
Re: TAChart spline series: how to return Y value for a given X value?
« Reply #9 on: April 13, 2014, 02:54:43 pm »
In the attachment you can find a modified version of your demo which, as a workaround, uses an invisible line series that copies the calculated B-Spline points by means of a UserDefinedChartSource recalculating the interpolated points of the BSplineSeries at some intervals.

I also tried a TFunctionSeries but this crashes due to NaN's being returned from BSplineSeries.Calculate for non-existing x values.

Relativity

  • Full Member
  • ***
  • Posts: 103
Re: TAChart spline series: how to return Y value for a given X value?
« Reply #10 on: June 01, 2016, 04:04:15 pm »
Quote
I also tried a TFunctionSeries but this crashes due to NaN's being returned from BSplineSeries.Calculate for non-existing x values.

wp, what do you mean with "non-existing x values" ?
Do you mean x values that have not been inserted in the ListChartSource used as source for the BSplineSeries ?
"How'm I gonna get through?"
  -- Pet Shop Boys

wp

  • Hero Member
  • *****
  • Posts: 13411
Re: TAChart spline series: how to return Y value for a given X value?
« Reply #11 on: June 01, 2016, 05:38:03 pm »
Sorry - I can't rember exactly, two years is a long time... But I think that the BSplineSeries is calculated only within a given extent of x values, in the example between 10 and 16. For all other x values the Calculate method of TBSplineSeries returns NaN ("Not a Number") which causes errors in some cases.

Relativity

  • Full Member
  • ***
  • Posts: 103
Re: TAChart spline series: how to return Y value for a given X value?
« Reply #12 on: June 02, 2016, 11:11:28 am »
I get a runtime error when I use TBSplineSeries.Calculate in the following code:

Code: [Select]
   BSpline.Degree := 3;
   ListChartSource1 := TListChartSource.Create(Chart1);
   ListChartSource2 := TListChartSource.Create(Chart1);
   ListChartSource3 := TListChartSource.Create(Chart1);


   for i := -200 to 1500 do
     begin
       ListChartSource1.Add(i,rohDataSum[i]);
       ListChartSource2.Add(i,rohDataSum[i]/520.02);
     end;

  BSpline.Source:= ListChartSource1;
  CubicSpline.Source := ListChartSource1;
  CoMoIps.Source := ListChartSource1;

  j:=BSpline.GetYValue(202); // THIS WORKS
  j:= BSpline.Calculate(202); // HERE I GET RUNTIME ERROR         

The statement before the last one:

Code: [Select]
j:=BSpline.GetYValue(202);

works perfectly and returns the right value (I've checked with the debugger), but the last statement:

Code: [Select]
j:=BSpline.Calculate(202);

gives runtime error.

Can anybody help me ?
"How'm I gonna get through?"
  -- Pet Shop Boys

wp

  • Hero Member
  • *****
  • Posts: 13411
Re: TAChart spline series: how to return Y value for a given X value?
« Reply #13 on: June 02, 2016, 11:42:54 am »
Can anybody help me ?
Not without any more details. Your code looks correct, but there are many reasons why something could go wrong. Please condense the topic into a small compilable project which shows the issue and which you upload here (only pas, lfm, lpi and lpr files, packed into a single zip).

Relativity

  • Full Member
  • ***
  • Posts: 103
Re: TAChart spline series: how to return Y value for a given X value?
« Reply #14 on: June 02, 2016, 01:40:58 pm »
Here is a project with an example of the problem: GetYValue works but Calculate doesn't.
Thank you for everything.
"How'm I gonna get through?"
  -- Pet Shop Boys

 

TinyPortal © 2005-2018