• Status: Solved
• Priority: Medium
• Security: Public
• Views: 999

# Upsample array using spline.

I need to create an new array with about 6 times as many elements as the original. The values of the new array need to be calculated from the original array using a spline function.
0
evenrik
• 2
• 2
• 2
• +4
2 Solutions

Commented:
1. Declare the array of required size

#define INPUT_ELEMENTS 10
double myarray [60];

2. if you wish to make size modifications at run time, use malloc to create your array and use realloc to adjust the size
e.g.

double * myarray;

myarray = (double *) malloc (sizeof (double) * INPUT_ELEMENTS);
...
myarray = (double *) realloc ( (void *) myarray , sizeof (double) * INPUT_ELEMENTS * 6 );
0

Commented:
>need to create an ***new**** array with about 6 times as many elements as the original

An easy solution to avoid to use malloc:
#define MAX_ELEMENTS 10
double myarray [MAX_ELEMENTS];
double newarray [MAX_ELEMENTS*6];

A solution with malloc, with not fixed length, doesn't need realloc:

double *myarray, *newarray;
int elements;  /* enter the number of elements somewhere and store in this variable */

myarray = (double *) malloc (sizeof (double) * elements);
newarray = (double *) malloc (sizeof (double) * elements * 6);

Of course, you will have to free memory somewhere after using arrays:
free(myarray);
free(newarray);
0

Author Commented:
Thank you for your responses, but I obviously did not describe the problem well.
The problem is not getting the memory for the new array, it is calculating the values for the elements of the new array. The original array values would have a curve if plotted. The new array must have the same shape with interpolated values for the new points inbetween the original points. The new points must be on a curve, not just a straight line between the original points.
0

Commented:
You still need to refine your question - Are you are looking for an algorithm or are you looking for the code for an algorithm or is it something else?
0

Commented:
evenrik,

The problem is that spline interpolation is actually not a specific algorithm except in certain fields. There are qubic splines, bezier splines, hermite splines etc. The maths guys could give you a longer list. Do you have any idea what sort of spline you are looking for? What field are you working in? Physics, maths, graphics, they all treat splines in differenty ways.

Give us more details of your needs.

You could get this question moved to the Maths board or post a link to it there? I can move it if you want.

Paul
0

Commented:
hi

i f u want help on spline and nurbs u could use the following link

contains useful programs on splines

http://www.nar-associates.com/nurbs/c_code.html

cheers
0

Author Commented:
I need source code that does "Cubic Spline Interpolation".
I am porting code into c that uses a spline function that does "Cubic Spline Interpolation".
Due to copyright restrictions I cannot just port the existing code.
0

Commented:
0

Commented:
btw what is the nature of your data points? 1D, 2D or 3D?
0

Commented:
Hi evenrik,
"Cubic Spline Interpolation" is a pretty good search term for Google et al.

Cheers!

Stefan
0

## Featured Post

• 2
• 2
• 2
• +4
Tackle projects and never again get stuck behind a technical roadblock.