view FChTransformUtils.cpp @ 0:d912b9d53e50

Import original code from the downloaded VampFChTCore-v1.1beta archive
author Chris Cannam
date Tue, 02 Oct 2018 10:44:42 +0100
parents
children 7ec763dc767c
line wrap: on
line source
/*
  copyright (C) 2012 I. Irigaray, M. Rocamora

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "FChTransformUtils.h"
#include <math.h>

void cumtrapz(const double *x, const double *y, size_t N, double *accum)
/*Trapezoidal Integrator: 1/2(b-a)(F(a)+F(b))*/
{

accum[0]=0.0;
for (size_t i = 1; i < N; i++) {
    accum[i]=accum[i-1]+0.5*(x[i]-x[i-1])*(y[i]+y[i-1]);
}

}


void interp1(const double *x1, const double *y1, size_t N1, const double *x2, double *y2, size_t N2){
/*1-D linear interpolation*/

	for (size_t i = 0; i < N2; i++) {
		/*Smaller or equal than the smallest, or larger or equal than the largest.*/
		if ( x2[i] <= x1[0] ) {
			y2[i] = y1[0];
		} else if ( x2[i] >= x1[N1-1] ) {
			y2[i] = y1[N1-1];
		} else {
			/*Search every value of x2 in x1*/
			size_t j = 1;
			size_t salir = 0;
			while ((j<N1)&&(!salir)) {			
				if ( x2[i] <= x1[j] ) {
					//y2[i] = ( x2[i]*( y1[j] - y1[j-1] ) + x1[j]*y1[j-1] - x1[j-1]*y1[j] ) / ( x1[j] - x1[j-1] );
					y2[i] = y1[j-1] + ( ( y1[j] - y1[j-1] )*(x2[i] - x1[j-1] ) )/ ( x1[j] - x1[j-1] );
					salir = 1;
				} // if
				j++;
			} // for
		} // else
	} // for

}

void interp1q(const double *y1, const size_t *x2_int, const double *x2_frac, double *y2, size_t N2){

	for(size_t i=0;i<N2;i++){
		y2[i] = y1[x2_int[i]]*(1.0-x2_frac[i])+y1[x2_int[i]+1]*x2_frac[i];
	} // for

}

void hanning_window(double *p_window, size_t n, bool normalize) {

	double accum=0;
    for (size_t i = 0; i < n; i++) {
        p_window[i] = 0.5*(1.0-cos(2*M_PI*(double)(i+1)/((double)n+1.0)));
        accum += p_window[i];
    }
	if (normalize) {
    	for (size_t i = 0; i < n; i++) { //window normalization
    		p_window[i] = p_window[i]/accum;
    	}
	}

}