Chris@0
|
1 /*
|
Chris@0
|
2 copyright (C) 2012 I. Irigaray, M. Rocamora
|
Chris@0
|
3
|
Chris@0
|
4 This program is free software: you can redistribute it and/or modify
|
Chris@0
|
5 it under the terms of the GNU General Public License as published by
|
Chris@0
|
6 the Free Software Foundation, either version 3 of the License, or
|
Chris@0
|
7 (at your option) any later version.
|
Chris@0
|
8
|
Chris@0
|
9 This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 GNU General Public License for more details.
|
Chris@0
|
13
|
Chris@0
|
14 You should have received a copy of the GNU General Public License
|
Chris@0
|
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
Chris@8
|
16 */
|
Chris@0
|
17
|
Chris@0
|
18
|
Chris@0
|
19 #include "FChTransformUtils.h"
|
Chris@0
|
20 #include <math.h>
|
Chris@0
|
21
|
Chris@0
|
22 void cumtrapz(const double *x, const double *y, size_t N, double *accum)
|
Chris@0
|
23 /*Trapezoidal Integrator: 1/2(b-a)(F(a)+F(b))*/
|
Chris@0
|
24 {
|
Chris@8
|
25 accum[0]=0.0;
|
Chris@8
|
26 for (size_t i = 1; i < N; i++) {
|
Chris@8
|
27 accum[i]=accum[i-1]+0.5*(x[i]-x[i-1])*(y[i]+y[i-1]);
|
Chris@8
|
28 }
|
Chris@0
|
29 }
|
Chris@0
|
30
|
Chris@0
|
31
|
Chris@0
|
32 void interp1(const double *x1, const double *y1, size_t N1, const double *x2, double *y2, size_t N2){
|
Chris@0
|
33 /*1-D linear interpolation*/
|
Chris@0
|
34
|
Chris@8
|
35 for (size_t i = 0; i < N2; i++) {
|
Chris@8
|
36 /*Smaller or equal than the smallest, or larger or equal than the largest.*/
|
Chris@8
|
37 if ( x2[i] <= x1[0] ) {
|
Chris@8
|
38 y2[i] = y1[0];
|
Chris@8
|
39 } else if ( x2[i] >= x1[N1-1] ) {
|
Chris@8
|
40 y2[i] = y1[N1-1];
|
Chris@8
|
41 } else {
|
Chris@8
|
42 /*Search every value of x2 in x1*/
|
Chris@8
|
43 size_t j = 1;
|
Chris@8
|
44 size_t salir = 0;
|
Chris@8
|
45 while ((j<N1)&&(!salir)) {
|
Chris@8
|
46 if ( x2[i] <= x1[j] ) {
|
Chris@8
|
47 //y2[i] = ( x2[i]*( y1[j] - y1[j-1] ) + x1[j]*y1[j-1] - x1[j-1]*y1[j] ) / ( x1[j] - x1[j-1] );
|
Chris@8
|
48 y2[i] = y1[j-1] + ( ( y1[j] - y1[j-1] )*(x2[i] - x1[j-1] ) )/ ( x1[j] - x1[j-1] );
|
Chris@8
|
49 salir = 1;
|
Chris@8
|
50 } // if
|
Chris@8
|
51 j++;
|
Chris@8
|
52 } // for
|
Chris@8
|
53 } // else
|
Chris@8
|
54 } // for
|
Chris@0
|
55
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 void interp1q(const double *y1, const size_t *x2_int, const double *x2_frac, double *y2, size_t N2){
|
Chris@0
|
59
|
Chris@8
|
60 for(size_t i=0;i<N2;i++){
|
Chris@8
|
61 y2[i] = y1[x2_int[i]]*(1.0-x2_frac[i])+y1[x2_int[i]+1]*x2_frac[i];
|
Chris@8
|
62 } // for
|
Chris@0
|
63
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 void hanning_window(double *p_window, size_t n, bool normalize) {
|
Chris@0
|
67
|
Chris@8
|
68 double accum=0;
|
Chris@0
|
69 for (size_t i = 0; i < n; i++) {
|
Chris@0
|
70 p_window[i] = 0.5*(1.0-cos(2*M_PI*(double)(i+1)/((double)n+1.0)));
|
Chris@0
|
71 accum += p_window[i];
|
Chris@0
|
72 }
|
Chris@8
|
73 if (normalize) {
|
Chris@0
|
74 for (size_t i = 0; i < n; i++) { //window normalization
|
Chris@8
|
75 p_window[i] = p_window[i]/accum;
|
Chris@0
|
76 }
|
Chris@8
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 }
|