22
Sep
0

C – From Matlab To C – Some useful functions

Here are 3 basics functions that can be useful if you are translating matlab functions to C language.

The Functions

We translate this functions into C. Some of this functions use variables number of arguments.
In this case, this is generally like func(NB_ARG, [ARGS]);. Please, be very careful to put the arguments in double and not in integer! For example, do not use “2″ but “2.0″.

// Matlab B = 88 * ones(50,1)
double* B = instance(50, 88.0);

// Matlab C = [1 1 2 2 3 3]
double* C = vectorize(6, 1.0 , 1.0 , 2.0 , 2.0 , 3.0 , 3.0);

// Matlab D = [0.1,0.1,0.1, 0.3,0.3, 0.9,0.9,0.9]
double* D = multiinstance(7,3, 3,0.1, 2,0.3, 3,0.9);

// Matlab E = [start;step;end]
double* E = createTab(start, step, end);

// Matlab Titem = interp1(dt1,dt2,dt3,'linear');
double* Titem = interp1(dt1,sizedt1,dt2,dt3,sizedt3);

In C

/** To inter-pole data */
double* interp1(double * x, int sizex, double * y , double * xi, int sizexi){
	// indexes
	int idyi = 0;
	int idx = 0;
	// After computing, to know the current step progress
	double steps = (y[1] - y[0]) / (x[1] - x[0]);

	// fill left points
	while(idyi < sizexi - 1 && xi[idyi] < x[0]){
		yi[idyi] = y[0] - (steps * (x[0] - xi[idyi]) ) ;
		++idyi;
	}

	// fill point in the same interval as the original values
	while(idyi < sizexi && idx < sizex){
		while(idx < sizex && xi[idyi] >= x[idx]){
			++idx;
		}
		if(idx != sizex){
			steps = (y[idx] - y[idx-1]) / (x[idx] - x[idx-1]);
			yi[idyi] = y[idx] - (steps * (x[idx] - xi[idyi]) ) ;
			++idyi;
		}
	}

	// fill the right points
	if(idyi < sizexi){
		steps = (y[sizex - 1] - y[sizex-2]) / (x[sizex-1] - x[sizex-2]);
		while(idyi < sizexi){
			yi[idyi] = y[sizex-1] + (steps * (xi[idyi] - x[sizex-1])) ;
			++idyi;
		}
	}

	return yi;
}

/** Create a tab like matlab [start;step;end] */
double* createTab(double start, double step, double end){
	// Size
	int size = 1 + fabs((end - start)/step);
	// Alloc
	double * tab = (double*)malloc(sizeof(double) * size);
	// Fill
	for(int idx = 0; start <= end && idx < size; start += step, ++idx){
		tab[idx] = start;
	}
	return tab;
}

/** Create a tab with a value in all cases */
double* instance(int size, double val){
	// Alloc
	double * tab = (double*)malloc(sizeof(double) * size);
	// Fill
	while(size--){
		tab[size] = val;
	}
	return tab;
}

/** Multi Parameter Tab */
/** PUT VALUES IN DOUBLE! */
/** ex : multiinstance(7,3, 3,0.1, 2,0.3, 3,0.9) => [0.1,0.1,0.1, 0.3,0.3, 0.9,0.9,0.9] */
double* multiinstance( int totalsize, int seriesSize, ... )
{
	// Variables arguments Input
	va_list arguments;
	// Alloc
	double *tab = (double*)malloc(sizeof(double) * totalsize);

	int idx = 0, step = 0;
	double value = 0.0;

	// Start Using arguments
	va_start ( arguments, seriesSize );

	// From 0 to totalSize
	for(idx = 0 ; idx < totalsize ; ++idx, --step){
		// Cycle?
		if(step == 0){
			// Get new step & value
			step = va_arg ( arguments, int );
			value = va_arg ( arguments, double );
		}
		// Fill
		tab[idx] = value;
	}

	va_end ( arguments );

	return tab;
}

/** Create a vector from value */
/** PUT VALUES IN DOUBLE! */
/** ex : vectorize(2.0,0.0,5.0) => [0,5] */
double* vectorize( int size, ... )
{
	// Variables Arguments
	va_list arguments;
	double *tab = (double*)malloc(sizeof(double) * size);

	//Use argument
	va_start ( arguments, size );

	for(int idx = 0 ; idx < size ; ++idx){
		tab[idx] = va_arg ( arguments, double );
	}

	va_end ( arguments );

	return tab;
}

This functions are given “as is” without any warranty.
Please, put the post URL in your program to say where you find the code.

Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.

Comments are closed.

Celadon theme by the Themes Boutique