Exemple #1
0
    /** Calculate the derivatives for a set of points on the spline
     *
     * @param out :: The array to store the derivatives in
     * @param xValues :: The array of x values we wish to know the derivatives of
     * @param nData :: The size of the arrays
     * @param order :: The order of the derivatives o calculate
     */
    void CubicSpline::derivative1D(double* out, const double* xValues, size_t nData, const size_t order) const
    {
      int n = getAttribute("n").asInt();

      boost::scoped_array<double> x(new double[n]);
      boost::scoped_array<double> y(new double[n]);

      //setup the reference points and calculate
      if(m_recalculateSpline) setupInput(x,y,n);
      calculateDerivative(out,xValues,nData,order);

    }
Exemple #2
0
void updateWeights(Neuron* neuron) {
	
	double derivative = calculateDerivative(neuron);
	int i;
	
	for(i = 0 ; i < neuron->inputCount ; i++) {
		
		//printf("Updating weights with input val %f\n",neuron->inputs[i]);
		//printf("Updating weights with errorSignal val %f\n",neuron->errorSignal);
		neuron->weights[i] = neuron->weights[i] + (0.1 * neuron->errorSignal * derivative * neuron->inputs[i]);
		//printf("New weight value for %d: %f\n",i,neuron->weights[i]);
	}
}
Exemple #3
0
ViEigenBaseVector* ViFourierInterpolator::estimateModelSplines(const int &degree, const int &derivative, const qreal *leftSamples, const int &leftSize, const qreal *rightSamples, const int &rightSize, const int &outputSize, const qreal &scaling)
{
	static int i, j, extraEquations, columnOffset1, columnOffset2, rowOffset1, rowOffset2, rightStart, splineCount, leftSplineCount, rightSplineCount, singleCoefficientCount, coefficientCount, derivativeCount, equationCount, size;
	static qreal x1, x2, value1, value2;

	size = leftSize + rightSize;
	if(size <= 1) return NULL;
	rightStart = leftSize + outputSize;

	splineCount = size - 1;
	leftSplineCount = leftSize - 1;
	rightSplineCount = rightSize - 1;

	singleCoefficientCount = (degree * 2) + 1;
	coefficientCount = splineCount * singleCoefficientCount;
	derivativeCount = splineCount - 1;

	extraEquations = 0;

	equationCount = 0;
	equationCount += splineCount * 2; // Spline polynomials. Times 2 since we use the spline with 2 points
	equationCount += derivativeCount * derivative; // Intermediate derivatives should be equal;
	while(equationCount < coefficientCount) // Add first spline == 0
	{
		++equationCount;
		++extraEquations;
	}

	ViEigenBaseMatrix *matrix = mEigen->createMatrix(equationCount, coefficientCount);
	ViEigenBaseVector *vector = mEigen->createVector(equationCount);

	// Add left spline polynomials
	for(i = 0; i < leftSplineCount; ++i)
	{
		rowOffset1 = i * 2;
		rowOffset2 = rowOffset1 + 1;
		columnOffset1 = singleCoefficientCount * i;
		columnOffset2 = columnOffset1 + degree;

		vector->set(rowOffset1, leftSamples[i]);
		vector->set(rowOffset2, leftSamples[i + 1]);

		x1 = i * scaling;
		x2 = (i + 1) * scaling;

		matrix->set(rowOffset1, columnOffset1, 0.5);
		matrix->set(rowOffset2, columnOffset1, 0.5);

		for(j = 1; j <= degree; ++j)
		{
			value1 = x1 * j;
			value2 = x2 * j;

			matrix->setCos(rowOffset1, j + columnOffset1, value1);
			matrix->setCos(rowOffset2, j + columnOffset1, value2);

			matrix->setSin(rowOffset1, j + columnOffset2, value1);
			matrix->setSin(rowOffset2, j + columnOffset2, value2);
		}
	}

	// Add the spline polynomial that runs over the output gap, aka between the last left and first right sample
	rowOffset1 = leftSplineCount * 2;
	rowOffset2 = rowOffset1 + 1;
	columnOffset1 = singleCoefficientCount * leftSplineCount;
	columnOffset2 = columnOffset1 + degree;

	vector->set(rowOffset1, leftSamples[leftSize - 1]);
	vector->set(rowOffset2, rightSamples[0]);

	x1 = (leftSize - 1) * scaling;
	x2 = (leftSize + outputSize) * scaling;

	matrix->set(rowOffset1, columnOffset1, 0.5);
	matrix->set(rowOffset2, columnOffset1, 0.5);

	for(j = 1; j <= degree; ++j)
	{
		value1 = x1 * j;
		value2 = x2 * j;

		matrix->setCos(rowOffset1, j + columnOffset1, value1);
		matrix->setCos(rowOffset2, j + columnOffset1, value2);

		matrix->setSin(rowOffset1, j + columnOffset2, value1);
		matrix->setSin(rowOffset2, j + columnOffset2, value2);
	}

	// Add right spline polynomials
	for(i = 0; i < rightSplineCount; ++i)
	{
		rowOffset1 = (leftSplineCount + i + 1) * 2;
		rowOffset2 = rowOffset1 + 1;
		columnOffset1 = singleCoefficientCount * (i + leftSplineCount + 1);
		columnOffset2 = columnOffset1 + degree;

		vector->set(rowOffset1, rightSamples[i]);
		vector->set(rowOffset2, rightSamples[i + 1]);

		x1 = (rightStart + i) * scaling;
		x2 = (rightStart + i + 1) * scaling;

		matrix->set(rowOffset1, columnOffset1, 0.5);
		matrix->set(rowOffset2, columnOffset1, 0.5);

		for(j = 1; j <= degree; ++j)
		{
			value1 = x1 * j;
			value2 = x2 * j;

			matrix->setCos(rowOffset1, j + columnOffset1, value1);
			matrix->setCos(rowOffset2, j + columnOffset1, value2);

			matrix->setSin(rowOffset1, j + columnOffset2, value1);
			matrix->setSin(rowOffset2, j + columnOffset2, value2);
		}
	}

	// Add the deratives between neighbouring left splines
	for(i = 1; i <= derivative; ++i)
	{
		rowOffset1 = (splineCount * 2) + ((i - 1) * leftSplineCount);
		for(j = 0; j < leftSplineCount; ++j)
		{
			rowOffset2 = rowOffset1 + j;
			columnOffset1 = j * singleCoefficientCount;
			x1 = (j + 1) * scaling;
			calculateDerivative(degree, x1, matrix, rowOffset2, i, columnOffset1, 1);
			calculateDerivative(degree, x1, matrix, rowOffset2, i, columnOffset1 + singleCoefficientCount, -1); // -1: take the derivative on the right-hand side to the left of the equation
		}
	}

	// Add the deratives between neighbouring right splines
	for(i = 1; i <= derivative; ++i)
	{
		rowOffset1 = (splineCount * 2) + ((i - 1) * rightSplineCount) + (leftSplineCount * derivative);
		for(j = 0; j < rightSplineCount; ++j)
		{
			rowOffset2 = rowOffset1 + j;
			columnOffset1 = (j + leftSplineCount) * singleCoefficientCount;
			x1 = (rightStart + j) * scaling;
			calculateDerivative(degree, x1, matrix, rowOffset2, i, columnOffset1, 1);
			calculateDerivative(degree, x1, matrix, rowOffset2, i, columnOffset1 + singleCoefficientCount, -1); // -1: take the derivative on the right-hand side to the left of the equation
		}
	}

	// Set the derivitives for the first spline at point 0 to 0
	if(extraEquations > 0)
	{
		for(i = 1; i <= extraEquations; ++i) matrix->set(equationCount - i, 0, 1);
	}

	ViEigenBaseVector *coefficients = mEigen->estimate(matrix, vector);
	mEigen->clear(matrix);
	mEigen->clear(vector);
	return coefficients;
}
Exemple #4
0
ViEigenBaseVector* ViFourierInterpolator::estimateModelOsculating(const int &degree, const int &derivative, const qreal *leftSamples, const int &leftSize, const qreal *rightSamples, const int &rightSize, const int &outputSize, const qreal &scaling)
{
	static int i, j, offset, start, end, derivativeCount, totalDerivatives, totalSize, size;
	static qreal x, value;

	size = leftSize + rightSize;
	derivativeCount = size - 4; // Average, we don't have derivatives for the first and last point for both sides
	totalDerivatives = derivativeCount * derivative;
	totalSize = size + totalDerivatives;

	ViEigenBaseMatrix *matrix = mEigen->createMatrix(totalSize, (2 * degree) + 1);
	ViEigenBaseVector *vector = mEigen->createVector(totalSize);

	for(i = 0; i < leftSize; ++i)
	{
		vector->set(i, leftSamples[i]);
		x = i * scaling;
		matrix->set(i, 0, 0.5);
		for(j = 1; j <= degree; ++j)
		{
			value = x * j;
			matrix->setCos(i, j, value);
			matrix->setSin(i, j + degree, value);
		}
	}

	for(i = 0; i < rightSize; ++i)
	{
		offset = leftSize + i;
		vector->set(offset, rightSamples[i]);
		x = (offset + outputSize) * scaling;
		matrix->set(offset, 0, 0.5);
		for(j = 1; j <= degree; ++j)
		{
			value = x * j;
			matrix->setCos(offset, j, value);
			matrix->setSin(offset, j + degree, value);
		}
	}

	// Determine for how many samples we cannot calculate a derivitate, and skip those equations.
	// Der1 and der2 must skip 1 sample at the start and 1 at the end, der3 and der4 skip 2, etc
	start = ceil(derivative / 2.0);

	qreal derivativesLeft[leftSize];
	qreal derivativesRight[rightSize];

	end = leftSize - start;
	for(i = 1; i <= derivative; ++i)
	{
		ViDifferentiater::derivative(i, leftSamples, leftSize, derivativesLeft);
		for(j = start; j < end; ++j)
		{
			offset = size + (derivativeCount * (i - 1)) + (j - 1);
			vector->set(offset, derivativesLeft[j]);
			calculateDerivative(degree, j * scaling, matrix, offset, i);
		}
	}

	end = rightSize - start;
	for(i = 1; i <= derivative; ++i)
	{
		ViDifferentiater::derivative(i, rightSamples, rightSize, derivativesRight);
		for(j = start; j < end; ++j)
		{
			offset = (leftSize - 2) + size + (derivativeCount * (i - 1)) + (j - 1);
			vector->set(offset, derivativesRight[j]);
			calculateDerivative(degree, j * scaling, matrix, offset, i);
		}
	}

	ViEigenBaseVector *coefficients = mEigen->estimate(matrix, vector);
	mEigen->clear(matrix);
	mEigen->clear(vector);
	return coefficients;
}