39 #include "vircam_stats.h"
40 #include "vircam_utils.h"
44 static float kselect(
float *a,
int n,
int k);
45 static double dkselect(
double *a,
int n,
int k);
46 static float histexam(
int *histo,
int nhist,
int level);
89 extern float vircam_med(
float *data,
unsigned char *bpm,
long npts) {
90 int i,j,is_even,ilevel;
100 buf = cpl_malloc(npts*
sizeof(*buf));
102 is_even = !(npts & 1);
103 memmove((
char *)buf,(
char *)data,npts*
sizeof(
float));
106 value = kselect(buf,npts,ilevel);
108 value = 0.5*(value + kselect(buf,npts,ilevel));
111 value = kselect(buf,npts,ilevel);
118 for (i = 0; i < npts; i++) {
130 value = kselect(buf,j,ilevel);
132 value = 0.5*(value + kselect(buf,j,ilevel));
135 value = kselect(buf,j,ilevel);
170 extern double vircam_dmed(
double *data,
unsigned char *bpm,
long npts) {
171 int i,j,is_even,ilevel;
176 buf = cpl_malloc(npts*
sizeof(*buf));
178 is_even = !(npts & 1);
179 memmove((
char *)buf,(
char *)data,npts*
sizeof(
double));
182 value = dkselect(buf,npts,ilevel);
184 value = 0.5*(value + dkselect(buf,npts,ilevel));
187 value = dkselect(buf,npts,ilevel);
194 for (i = 0; i < npts; i++) {
200 value = CX_MAXDOUBLE;
206 value = dkselect(buf,j,ilevel);
208 value = 0.5*(value + dkselect(buf,j,ilevel));
211 value = dkselect(buf,j,ilevel);
246 extern float vircam_mean(
float *data,
unsigned char *bpm,
long npts) {
255 for (i = 0; i < npts; i++)
259 for (i = 0; i < npts; i++) {
267 value = sum/(float)n;
301 extern double vircam_dmean(
double *data,
unsigned char *bpm,
long npts) {
310 for (i = 0; i < npts; i++)
314 for (i = 0; i < npts; i++) {
322 value = sum/(float)n;
324 value = CX_MAXDOUBLE;
363 float *mean,
float *sig) {
366 const char *fctid =
"vircam_meansig";
374 for (i = 0; i < npts; i++) {
375 d = (double)(data[i]);
381 for (i = 0; i < npts; i++) {
383 d = (double)(data[i]);
397 cpl_msg_warning(fctid,
"All values flagged as bad\n");
406 sum2 = sum2/(double)n - sum*sum;
407 *sig = (float)sqrt(max(1.0e-12,sum2));
452 float lcut,
float hcut,
float *mean,
float *sig) {
455 const char *fctid =
"vircam_meansigcut";
463 for (i = 0; i < npts; i++) {
464 if (data[i] > lcut && data[i] < hcut) {
466 sum2 += data[i]*data[i];
472 for (i = 0; i < npts; i++) {
473 if (bpm[i] == 0 && data[i] > lcut && data[i] < hcut) {
475 sum2 += data[i]*data[i];
487 cpl_msg_warning(fctid,
"All values flagged as bad\n");
496 sum2 = sum2/(double)n - sum*sum;
497 *sig = (float)sqrt(max(1.0e-12,sum2));
543 float thresh,
int niter,
float lowv,
float highv,
544 float *median,
float *sigma) {
545 int *histo,nbins,nhist,ilev,iclip,nhist2,halflev,quartlev;
555 b = cpl_calloc(npts,
sizeof(
unsigned char));
558 nbins = vircam_nint(highv - lowv + 1.0);
559 histo = cpl_calloc(nbins,
sizeof(*histo));
561 for (i = 0; i < npts; i++) {
562 if (b[i] || data[i] < lowv || data[i] > highv)
564 ilev = vircam_nint(data[i] - lowv);
565 ilev = max(0,min(nbins-1,ilev));
576 for (i = 0; i <= niter; i++) {
577 halflev = (nhist2 + 1)/2;
578 quartlev = (nhist2 + 3)/4;
579 mlev = histexam(histo,nbins,halflev);
580 *median = mlev + lowv;
581 qlev = histexam(histo,nbins,quartlev);
582 *sigma = (mlev - qlev)*1.48;
586 jst = vircam_nint(mlev + thresh*(*sigma));
587 for (j = jst; j <= iclip; j++)
625 extern void vircam_medmad(
float *data,
unsigned char *bpm,
long np,
float *med,
637 work = cpl_malloc(np*
sizeof(*work));
638 for (i = 0; i < np; i++)
639 work[i] = (
float)fabs((
double)(data[i] - *med));
685 float lcut,
float hcut,
float *med,
float *mad) {
692 bad = cpl_calloc(np,
sizeof(*bad));
694 for (i = 0; i < np; i++)
695 if (bpm[i] != 0 || data[i] < lcut || data[i] > hcut)
698 for (i = 0; i < np; i++)
699 if (data[i] < lcut || data[i] > hcut)
706 if (*med == CX_MAXFLOAT) {
715 work = cpl_malloc(np*
sizeof(*work));
716 for (i = 0; i < np; i++)
717 work[i] = (
float)fabs((
double)(data[i] - *med));
756 extern void vircam_medsig(
float *data,
unsigned char *bpm,
long np,
float *med,
764 if (*med == CX_MAXFLOAT) {
773 for (i = 0; i < np; i++) {
774 resid = data[i] - *med;
777 *sig = sqrt(sum/(
float)np);
784 for (i = 0; i < np; i++) {
787 resid = data[i] - *med;
792 *sig = sqrt(sum/(
float)n);
826 for (j = 0; j < npts; j++)
833 static float histexam(
int *histo,
int nhist,
int level) {
839 while (ii < level && ilev < nhist-1)
841 value = (float)ilev - (
float)(ii - level)/(
float)histo[ilev] + 0.5;
856 static float kselect(
float *a,
int n,
int k) {
858 int i = 0, j = n - 1;
862 while (a[i] < x) i++;
863 while (a[j] > x) j--;
865 w = a[i]; a[i] = a[j]; a[j] = w;
870 }
while (++i <= --j);
875 a += i; n -= i; k -= i;
882 static double dkselect(
double *a,
int n,
int k) {
884 int i = 0, j = n - 1;
885 double x = a[j/2], w;
888 while (a[i] < x) i++;
889 while (a[j] > x) j--;
891 w = a[i]; a[i] = a[j]; a[j] = w;
896 }
while (++i <= --j);
901 a += i; n -= i; k -= i;