comparison src/delta.c @ 239:b015187746fa

Add normalisation capability to xtract_lnorm()
author Jamie Bullock <jamie@jamiebullock.com>
date Thu, 05 Jun 2014 20:26:06 +0100
parents 71870680f7c1
children d383a8c66b5d
comparison
equal deleted inserted replaced
238:7ebeac9b0d92 239:b015187746fa
22 */ 22 */
23 23
24 /* xtract_delta.c: defines functions that extract a feature as a single value from more than one input vector */ 24 /* xtract_delta.c: defines functions that extract a feature as a single value from more than one input vector */
25 25
26 #include <math.h> 26 #include <math.h>
27
28 #include "../xtract/libxtract.h" 27 #include "../xtract/libxtract.h"
29 28
30 int xtract_flux(const double *data, const int N, const void *argv , double *result) 29 int xtract_flux(const double *data, const int N, const void *argv , double *result)
31 { 30 {
32 31
37 36
38 int xtract_lnorm(const double *data, const int N, const void *argv , double *result) 37 int xtract_lnorm(const double *data, const int N, const void *argv , double *result)
39 { 38 {
40 39
41 int n, 40 int n,
42 type; 41 type,
42 normalise,
43 k = 0;
43 44
44 double order; 45 double order;
45 46
46 order = *(double *)argv; 47 order = *(double *)argv;
47 type = *((double *)argv+1); 48 type = *((double *)argv+1);
49 normalise = (int)*((double *)argv+2);
48 50
49 order = order > 0 ? order : 2.0; 51 order = order > 0 ? order : 2.0;
50 52
51 *result = 0.0; 53 *result = 0.0;
52 54
53 switch(type) 55 switch(type)
54 { 56 {
55 57
56 case XTRACT_POSITIVE_SLOPE: 58 case XTRACT_POSITIVE_SLOPE:
57 for(n = 0; n < N; n++) 59 for(n = 0; n < N; n++)
58 { 60 {
59 if(data[n] > 0) 61 if(data[n] > 0)
62 {
60 *result += pow(data[n], order); 63 *result += pow(data[n], order);
64 ++k;
65 }
61 } 66 }
62 break; 67 break;
63 default: 68 default:
64 for(n = 0; n < N; n++) 69 for(n = 0; n < N; n++)
65 *result += pow(data[n], order); 70 {
71 *result += pow(fabs(data[n]), order);
72 ++k;
73 }
66 break; 74 break;
67 75
68 } 76 }
69 77
70 *result = pow(*result, 1.0 / order); 78 *result = pow(*result, 1.0 / order);
79
80 if (k == 0)
81 {
82 return XTRACT_NO_RESULT;
83 }
84
85 if (normalise == 1)
86 {
87 *result = log(1 + *result);
88 }
71 89
72 return XTRACT_SUCCESS; 90 return XTRACT_SUCCESS;
73 91
74 } 92 }
75 93