20#include "hdrl_cat_statistics.h"
22#include "hdrl_cat_utils_sort.h"
51cpl_error_code
hdrl_median(
double xbuf[], cpl_size npt, cpl_size nfilt)
53 if ((nfilt / 2) * 2 == nfilt) {
58 return CPL_ERROR_INCOMPATIBLE_INPUT;
61 cpl_size nfo2p1 = nfilt / 2;
64 cpl_size nelem = npt + nfilt;
65 double *ybuf = (
double * )cpl_malloc(nelem *
sizeof(
double ));
66 double *array = (
double * )cpl_malloc(nfilt *
sizeof(
double ));
67 cpl_size *point = (cpl_size *)cpl_malloc(nfilt *
sizeof(cpl_size));
70 cpl_size il = nfilt / 2;
71 cpl_size ilow = (CPL_MAX(3, nfilt / 4) / 2) * 2 + 1;
75 for (cpl_size i = 0; i < ilow; i++) {
78 sort_array_index(array, ilow, point, HDRL_SORT_CPL_SIZE, CPL_SORT_ASCENDING);
79 double xmns = array[ilow / 2];
83 for (cpl_size i = 0; i < ilow; i++) {
84 array[i] = xbuf[npt - 1 - i];
86 sort_array_index(array, ilow, point, HDRL_SORT_CPL_SIZE, CPL_SORT_ASCENDING);
87 double xmnf = array[ilow / 2];
91 for (cpl_size i = 0; i < il; i++) {
92 ybuf[i] = 2. * xmns - xbuf[il - i - 1 + ilow];
93 ybuf[npt + i + il] = 2. * xmnf - xbuf[npt - i - 1 - ilow];
96 for (cpl_size i = 0; i < npt; i++) {
97 ybuf[i + il] = xbuf[i];
101 for (cpl_size i = 0; i < nfilt; i++) {
106 sort_array_index(array, nfilt, point, HDRL_SORT_CPL_SIZE, CPL_SORT_ASCENDING);
107 xbuf[0] = array[nfo2p1];
110 cpl_size jh = nfilt + npt - 1;
113 for (cpl_size j = jl; j < jh; j++) {
115 for (cpl_size i = 0; i < nfilt; i++) {
126 for (cpl_size i = 0; i < nfilt; i++) {
127 if (i != l && array[l] <= array[i]) {
135 double temp = array[l];
136 cpl_size it = point[l];
140 for (cpl_size i = 0; i < l - jj; i++) {
141 cpl_size ii = l - i - 1;
142 array[ii + 1] = array[ii];
143 point[ii + 1] = point[ii];
150 for (cpl_size i = 0; i < jj - l; i++) {
151 cpl_size ii = l + i + 1;
152 array[ii - 1] = array[ii];
153 point[ii - 1] = point[ii];
162 xbuf[j - jl + 1] = array[nfo2p1];
166 cpl_free((
void *) point);
167 cpl_free((
void *) array);
168 cpl_free((
void *) ybuf);
170 return CPL_ERROR_NONE;
cpl_error_code hdrl_median(double xbuf[], cpl_size npt, cpl_size nfilt)
compute median
cpl_error_code sort_array_index(double *a, cpl_size nE, void *b, hdrl_sort_type type, cpl_sort_direction dir)
sort_array_index hdrl function for sort two arrays The alghorithm sort 'a' and in the same way sort t...