35void smoothData(
double *data,
long rows,
long smoothPoints,
long smoothPasses) {
36 long lower, upper, row, pass, smoothPoints2, terms;
38 static double *smoothedData = NULL;
40 smoothedData =
trealloc(smoothedData, rows *
sizeof(*smoothedData));
42 smoothPoints2 = smoothPoints / 2;
44 for (pass = 0; pass < smoothPasses; pass++) {
45 for (row = sum = 0; row < smoothPoints2; row++)
49 lower = -smoothPoints2;
50 upper = smoothPoints2;
51 for (row = 0; row < rows; row++, lower++, upper++) {
56 smoothedData[row] = sum / terms;
63 for (row = 0; row < rows; row++)
64 data[row] = smoothedData[row];
86long despikeData(
double *data,
long rows,
long neighbors,
long passes,
long averageOf,
87 double threshold,
long countLimit) {
88 long i0, i1, i2, i, j, i1a, i2a;
90 double *deltaSum, sum, *tempdata;
93 neighbors = 2 * ((neighbors + 1) / 2);
94 if (!(tempdata = (
double *)malloc(
sizeof(*tempdata) * rows)) ||
95 !(deltaSum = (
double *)malloc(
sizeof(*deltaSum) * (neighbors + 1))))
96 bomb(
"despikeData: memory allocation failure", NULL);
97 memcpy(tempdata, data,
sizeof(*data) * rows);
99 while (passes-- > 0) {
101 for (i0 = 0; i0 < rows; i0 += neighbors / 2) {
102 i1 = i0 - neighbors / 2;
103 i2 = i0 + neighbors / 2;
110 for (i = i1; i <= i2; i++) {
111 deltaSum[i - i1] = 0;
112 for (j = i1; j <= i2; j++)
113 deltaSum[i - i1] += fabs(tempdata[i] - tempdata[j]);
116 if ((imax += i1) < 0 || imax > rows) {
117 fprintf(stderr,
"Error: index out of range in despikeData (sddssmooth)\n");
118 fprintf(stderr,
" imax = %" PRId64
", rows=%ld, i1=%ld, i2=%ld, neighbors=%ld\n",
119 imax - 1, rows, i1, i2, neighbors);
122 if (threshold == 0 || threshold * neighbors < deltaSum[imax - i1]) {
123 if ((i1a = imax - averageOf / 2) < 0)
125 if ((i2a = imax + averageOf / 2) >= rows)
127 for (sum = 0, i = i1a; i <= i2a; i++) {
132 tempdata[imax] = sum / (i2a - i1a);
136 if (!despikeCount || (countLimit > 0 && despikeCount > countLimit))
139 if (countLimit <= 0 || despikeCount < countLimit)
140 for (i = 0; i < rows; i++)
141 data[i] = tempdata[i];
int index_min_max(int64_t *imin, int64_t *imax, double *list, int64_t n)
Finds the indices of the minimum and maximum values in a list of doubles.
void smoothData(double *data, long rows, long smoothPoints, long smoothPasses)
Smooth a data array using a moving average.
long despikeData(double *data, long rows, long neighbors, long passes, long averageOf, double threshold, long countLimit)
Remove spikes from a data array by comparing each point to its neighbors.