Solved

# Upsample array using spline.

Posted on 2005-05-10
955 Views
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
Question by:evenrik

LVL 45

Expert Comment

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

LVL 55

Expert Comment

>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 Comment

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

LVL 45

Expert Comment

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

LVL 16

Expert Comment

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

LVL 4

Assisted Solution

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 Comment

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

LVL 9

Accepted Solution

0

LVL 9

Expert Comment

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

LVL 12

Expert Comment

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

Cheers!

Stefan
0

## Featured Post

### Suggested Solutions

scanf and assignment suppression 13 129
Way to scan the path 41 159
Difference between struct tm and time_t 5 99
An API detour question 7 38
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

#### Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!