Click here to Skip to main content
15,944,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello !

I have to use the spline method to smooth my data curve.
I need to smooth data x and data z.

I use this link : C# Cubic Spline Interpolation[^]

I tried to integrate it in my code.

Here the part of the code which is concerned by the cubic spline interpolation : (in Void Start)

C#
// To use Spline

x = new float[cpt-1];
y = new float[cpt-1];

for (int i = 0; i < cpt-1; i++) {
    for (int j = 1; j < names.Length+1; j++) {

        x[i] = nv_data[j-1].positions[i].x; // Data positions saved in text file
        y[i] = nv_data[j-1].positions[i].z; // Data positions saved in text file

    }
}

    int n = 20;
    xs = new float[n];

    float stepSize = (x[x.Length - 1] - x[0]) / (n - 1);

    for (int i = 0; i < n; i++)

    {
        xs[i] = xs[0] + i * stepSize;
    }

// These two lines are not correct : CubicSpline and FitAndEval appear in red
CubicSpline spline = new CubicSpline();
float[] ys = spline.FitAndEval(x, y, xs);


I tried to declare it before Void Start :

C#
public CubicSpline spline;

CubicSpline spline = new CubicSpline ();


But it's not good.

P-s : I put the other useful codes (CubicSpline, TriDiagonalMatrix, ArrayUtil) at the same location of this current code.

Thank you in advance
Posted
Updated 5-May-15 2:47am
v3
Comments
Matt T Heffron 5-May-15 13:57pm    
The first thing I noticed is that your loading of the x and y arrays is clearly wrong.
Every iteration of the inner, j loop, overwrites the preceding values in the x[i] and y[i] locations.
If the values you are using for the spline aren't what you think they are, then the resultant spline will not be what you expect.
Coralie B 6-May-15 1:56am    
All data of nv_data[j-1].positions[i].x and nv_data[j-1].positions[i].z won't be saved in x[i] and y[i] ?
Matt T Heffron 6-May-15 12:38pm    
Nope, each of the x[i] can hold a single float value (likewise y[i]).
Each iteration of the for (int j = ... loop assigns the different values of nv_data[j-1].positions[i].x to the same x[i] location, overwriting the previous value there.
Use the "Improve question" (above) to describe what you are trying to do and more people will be able to help.

1 solution

 
Share this answer
 
Comments
Coralie B 5-May-15 8:37am    
Thank you but can you help me more please ?
I'm going to update my question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900