SDDSlib
Loading...
Searching...
No Matches
smooth.c File Reference

Functions for smoothing data and removing spikes from data arrays. More...

#include "mdb.h"

Go to the source code of this file.

Functions

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.
 

Detailed Description

Functions for smoothing data and removing spikes from data arrays.

This file provides two main functions:

  • smoothData(): Smooths a data set using a simple moving average over a defined number of points and passes.
  • despikeData(): Attempts to remove spike values from a data set by comparing each point to its neighbors and replacing it if it exceeds a defined threshold.
License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, R. Soliday, H. Shang

Definition in file smooth.c.

Function Documentation

◆ despikeData()

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.

This function identifies and replaces "spikes" in a data array. A spike is defined as a value that differs significantly from its neighboring values. The function compares each point to its neighbors over multiple passes and replaces values exceeding a given threshold with an average of their neighbors. The process can be halted if too many spikes are found (based on countLimit).

Parameters
dataPointer to the data array.
rowsThe number of data points.
neighborsThe number of neighboring points to consider around each point.
passesThe maximum number of passes to attempt when removing spikes.
averageOfThe number of points to average when replacing a spiked value.
thresholdThe threshold for determining if a value is a spike.
countLimitThe maximum number of spikes allowed before the process stops.
Returns
The number of spikes removed on the final pass.

Definition at line 86 of file smooth.c.

87 {
88 long i0, i1, i2, i, j, i1a, i2a;
89 int64_t imin, imax;
90 double *deltaSum, sum, *tempdata;
91 long despikeCount;
92
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);
98 despikeCount = 0;
99 while (passes-- > 0) {
100 despikeCount = 0;
101 for (i0 = 0; i0 < rows; i0 += neighbors / 2) {
102 i1 = i0 - neighbors / 2;
103 i2 = i0 + neighbors / 2;
104 if (i1 < 0)
105 i1 = 0;
106 if (i2 >= rows)
107 i2 = rows - 1;
108 if (i2 - i1 == 0)
109 continue;
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]);
114 }
115 if (index_min_max(&imin, &imax, deltaSum, i2 - i1 + 1)) {
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);
120 exit(1);
121 }
122 if (threshold == 0 || threshold * neighbors < deltaSum[imax - i1]) {
123 if ((i1a = imax - averageOf / 2) < 0)
124 i1a = 0;
125 if ((i2a = imax + averageOf / 2) >= rows)
126 i2a = rows - 1;
127 for (sum = 0, i = i1a; i <= i2a; i++) {
128 if (i != imax)
129 sum += tempdata[i];
130 }
131 despikeCount++;
132 tempdata[imax] = sum / (i2a - i1a);
133 }
134 }
135 }
136 if (!despikeCount || (countLimit > 0 && despikeCount > countLimit))
137 break;
138 }
139 if (countLimit <= 0 || despikeCount < countLimit)
140 for (i = 0; i < rows; i++)
141 data[i] = tempdata[i];
142 else
143 despikeCount = 0;
144 free(tempdata);
145 free(deltaSum);
146 return despikeCount;
147}
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
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.
Definition findMinMax.c:116

◆ smoothData()

void smoothData ( double * data,
long rows,
long smoothPoints,
long smoothPasses )

Smooth a data array using a moving average.

This function applies a moving average smoothing operation to an array of data points a specified number of times. It uses a window defined by smoothPoints, averaging over this number of points and performing the operation for smoothPasses iterations.

Parameters
dataPointer to the array of data to be smoothed.
rowsThe number of data points.
smoothPointsThe number of points to include in the smoothing window.
smoothPassesThe number of smoothing passes to perform.

Definition at line 35 of file smooth.c.

35 {
36 long lower, upper, row, pass, smoothPoints2, terms;
37 double sum;
38 static double *smoothedData = NULL;
39
40 smoothedData = trealloc(smoothedData, rows * sizeof(*smoothedData));
41
42 smoothPoints2 = smoothPoints / 2;
43
44 for (pass = 0; pass < smoothPasses; pass++) {
45 for (row = sum = 0; row < smoothPoints2; row++)
46 sum += data[row];
47
48 terms = row;
49 lower = -smoothPoints2;
50 upper = smoothPoints2;
51 for (row = 0; row < rows; row++, lower++, upper++) {
52 if (upper < rows) {
53 sum += data[upper];
54 terms += 1;
55 }
56 smoothedData[row] = sum / terms;
57 if (lower >= 0) {
58 sum -= data[lower];
59 terms -= 1;
60 }
61 }
62
63 for (row = 0; row < rows; row++)
64 data[row] = smoothedData[row];
65 }
66}
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
Definition array.c:181