00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <stdint.h>
00039 #include <stdlib.h>
00040 #ifdef TEST_DGLITCH
00041 #include <stdio.h>
00042 #endif
00043 #include <string.h>
00044 #include <gsl/gsl_sort.h>
00045 #include <gsl/gsl_statistics.h>
00046
00047 #include "dglitch.h"
00048
00049 #define DG_STRIDE (size_t)1
00050
00051 #define DEBUG_DEGLITCH(a)
00052
00055 typedef struct ts_dglitch {
00057 double *stack;
00059 double *sorted_stack;
00061 uint32_t window_size;
00063 te_dglitch_type dglitch_algor;
00064 } ts_dglitch;
00065
00071 static void dg_push(p_dglitch dg, double num);
00072
00074 static void dg_sort(double *vector, uint32_t size);
00076 static int32_t dg_compare(const void *val1, const void *val2);
00077
00078 p_dglitch dglitch_create(te_dglitch_type type, uint32_t window_size)
00079 {
00080 p_dglitch dg;
00081
00082 if( (dg = malloc(sizeof(struct ts_dglitch))) != NULL) {
00083 dg->window_size = window_size;
00084 if(
00085 ((dg->stack = malloc((window_size * sizeof(double)))) != NULL)
00086 && \
00087 ((dg->sorted_stack = malloc((window_size * sizeof(double)))) != NULL)
00088 )
00089 {
00090 }
00091 else {
00092 free(dg->sorted_stack);
00093 free(dg->stack);
00094 free(dg);
00095 dg = NULL;
00096 }
00097 }
00098 return dg;
00099 }
00100
00101 void dglitch_apply(p_dglitch dg, double *data, uint32_t size)
00102 {
00103 uint32_t i;
00104
00105 if(dg != NULL) {
00106 for(i = 0; i < size; i++) {
00107 dg_push(dg, data[i]);
00108 (void)memcpy((void*)dg->sorted_stack, (void*)dg->stack, (size_t)(dg->window_size * sizeof(double)));
00110 dg_sort((void*)dg->sorted_stack, dg->window_size);
00111 #if 0
00112 gsl_sort(dg->sorted_stack, DG_STRIDE, dg->window_size);
00113 #endif
00114 DEBUG_DEGLITCH( ("%12.3f,", data[i]) );
00115 data[i] = gsl_stats_median_from_sorted_data(dg->sorted_stack, DG_STRIDE, dg->window_size);
00116 DEBUG_DEGLITCH( ("%12.3f\n", data[i]) );
00117 }
00118 }
00119 }
00120
00121 static void dg_sort(double *vector, uint32_t size)
00122 {
00123 qsort((void*)vector, size, sizeof(double), dg_compare);
00124 }
00125
00126 static int32_t dg_compare(const void *val1, const void *val2)
00127 {
00128 int32_t ret;
00129
00130 double a = *((double*)val1);
00131 double b = *((double*)val2);
00132 if(a < b) {
00133 ret = -1;
00134 }
00135 else if(a == b) {
00136 ret = 0;
00137 }
00138 else {
00139 ret = 1;
00140 }
00141 return ret;
00142 }
00143
00144 void dglitch_destroy(p_dglitch *dg)
00145 {
00146 if(*dg != NULL) {
00147 free((*dg)->stack);
00148 free((*dg)->sorted_stack);
00149 free(*dg);
00150 *dg = NULL;
00151 }
00152 }
00153
00154 static void dg_push(p_dglitch dg, double num)
00155 {
00156 int32_t i;
00157
00158 for(i = (dg->window_size - 1); i > 0; i--) {
00159 dg->stack[i] = dg->stack[i - 1];
00160 }
00161 dg->stack[0] = num;
00162 }
00163
00164 #ifdef TEST_DGLITCH
00165
00166 static double test[] = {
00167 1.0,
00168 1.0,
00169 1.0,
00170 1.0,
00171 1.0,
00172 10.0,
00173 10.0,
00174 10.0,
00175 10.0,
00176 10.0,
00177 15.0,
00178 15.0,
00179 15.0,
00180 15.0,
00181 15.0,
00182 100.0,
00183 100.0,
00184 100.0,
00185 100.0,
00186 100.0,
00187 100.0
00188 };
00189 #define TEST_SIZE (sizeof(test) / sizeof(test[0]))
00190
00191 int32_t main(int32_t argc, char *arg[])
00192 {
00193 int32_t i;
00194
00195 p_dglitch dg;
00196
00197 dg = dglitch_create(DG_MEDIAN, 5);
00198 dglitch_apply(dg, test, TEST_SIZE);
00199 dglitch_destroy(&dg);
00200
00201 for(i = 0; i < TEST_SIZE; i++) {
00202 printf("%f\n", test[i]);
00203 }
00204 return 0;
00205 }
00206 #endif