forked from scottransom/psrfits2psrfits
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrescale.c
197 lines (156 loc) · 5.37 KB
/
rescale.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/******
* rescale.c SC 20091123
* Receive array of floats in [0..65535] and Nvalues
* Return offset and scale to convert array to [0..15]
* gcc rescale.c testrsc.c -o testrsc -lm
******/
static char rcsid[] = "$Id: rescale.c,v 1.5 2009/11/25 05:40:49 shami Exp $";
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define USIGMA 3.5
#define LSIGMA 2.5
#define VERBOSE 0
int shortintcmp(const void *a, const void *b)
{
/*
short int *da = (const short int *) a;
short int *db = (const short int *) b;
return (*da > *db) - (*da < *db);
*/
if (*((short int*)a) > *((short int*)b))
return 1;
else
return -1;
}
int rescale2(short int *datav, int ndata, int nbits, float *offset, float *scale)
{
short int* datacopy;
float qlow, qhigh;
short int median,s1lo,s1hi;
datacopy = (short int *) malloc(ndata * sizeof(short int));
if (!datacopy) {
/* malloc apparently failed */
printf("Error! malloc failed?\n");
return (-1);
}
memcpy(datacopy, datav, ndata * sizeof(short int));
qsort(datacopy, ndata, sizeof(datav[0]), shortintcmp);
/* Now calculate median and percentiles */
median = datacopy[(int) (0.5 * ndata)]; // Ignore odd-even issue
s1lo = datacopy[(int) (0.1587 * ndata)]; // since we're approximating
s1hi = datacopy[(int) (0.8413 * ndata)]; // percentiles anyway.
qlow = (float)median - LSIGMA * (float)(median - s1lo);
qlow = (qlow < (float)datacopy[0]) ? (float)datacopy[0] : qlow;
qhigh = (float)median + USIGMA * (float)(s1hi - median);
qhigh = (qhigh > (float)datacopy[ndata - 1]) ? (float)datacopy[ndata - 1] : qhigh;
#if(VERBOSE)
fprintf(stderr, "# Median = %.1f, 1 sigma = %.1f %.1f, Clip at %.1f %.1f\n",
median, s1lo, s1hi, qlow, qhigh);
#endif
// X(qlow..qhigh) -> Y(0..15); for 4-bit
// X = scale*Y + offset;
// Y = (X-offset)/scale = (X-qlow)/(qhigh-qlow) * 16.0.
if (nbits == 4)
*scale = (qhigh - qlow) / 16.0;
else // nbits = 8
*scale = (qhigh - qlow) / 256.0;
*offset = qlow;
free(datacopy);
return 0;
}
/******************************/
int floatcmp(const void *a, const void *b)
{
const float *da = (const float *) a;
const float *db = (const float *) b;
return (*da > *db) - (*da < *db);
/*
if (*((float*)a) > *((float*)b))
return 1;
else
return -1;
*/
}
/******************************/
int basicscale(float *datav, int ndata, float *offset, float *scale)
{
/* No-op. Return default offset and scale values */
*scale = 4096.0;
*offset = 2048.0;
return (0);
}
/******************************/
int rescale(float *datav, int ndata, int nbits, float *offset, float *scale)
{
float *datacopy;
float median, s1lo, s1hi, qlow, qhigh;
datacopy = (float *) malloc(ndata * sizeof(float));
if (!datacopy) {
/* malloc apparently failed */
printf("Error! malloc failed?\n");
return (-1);
}
memcpy(datacopy, datav, ndata * sizeof(float));
qsort(datacopy, ndata, sizeof(datav[0]), floatcmp);
/* Now calculate median and percentiles */
median = datacopy[(int) (0.5 * ndata)]; // Ignore odd-even issue
s1lo = datacopy[(int) (0.1587 * ndata)]; // since we're approximating
s1hi = datacopy[(int) (0.8413 * ndata)]; // percentiles anyway.
qlow = median - LSIGMA * (median - s1lo);
qlow = (qlow < datacopy[0]) ? datacopy[0] : qlow;
qhigh = median + USIGMA * (s1hi - median);
qhigh = (qhigh > datacopy[ndata - 1]) ? datacopy[ndata - 1] : qhigh;
#if(VERBOSE)
fprintf(stderr, "# Median = %.1f, 1 sigma = %.1f %.1f, Clip at %.1f %.1f\n",
median, s1lo, s1hi, qlow, qhigh);
#endif
// X(qlow..qhigh) -> Y(0..15); for 4-bit
// X = scale*Y + offset;
// Y = (X-offset)/scale = (X-qlow)/(qhigh-qlow) * 16.0.
if (nbits == 4)
*scale = (qhigh - qlow) / 16.0;
else // nbits = 8
*scale = (qhigh - qlow) / 256.0;
*offset = qlow;
free(datacopy);
return 0;
}
/******************************/
int medianrescale(float *datav, int ndata, float *offset, float *scale)
{
float *datacopy;
float median, s1lo, s1hi, qlow, qhigh;
datacopy = (float *) malloc(ndata * sizeof(float));
if (!datacopy) {
/* malloc apparently failed */
printf("Error! malloc failed?\n");
return (-1);
}
memcpy(datacopy, datav, ndata * sizeof(float));
qsort(datacopy, ndata, sizeof(datav[0]), floatcmp);
/* Now calculate median and percentiles */
median = datacopy[(int) (0.5 * ndata)]; // Ignore odd-even issue
s1lo = datacopy[(int) (0.1587 * ndata)]; // since we're approximating
s1hi = datacopy[(int) (0.8413 * ndata)]; // percentiles anyway.
qlow = median - LSIGMA * (median - s1lo);
if (qlow < 0) {
qlow = 0.0;
}
qhigh = median + USIGMA * (s1hi - median);
if (qhigh > 65536) {
qhigh = 65536.0;
}
#if(VERBOSE)
printf("# Median = %.1f, 1 sigma = %.1f %.1f, Clip at %.1f %.1f\n",
median, s1lo, s1hi, qlow, qhigh);
#endif
// X(0..65536) -> Y(0..15); X = scale*Y + offset.
// scale converts clip range to 16 units
*scale = (qhigh - qlow) / 16.0;
// offset removes median and adds back scale*16/2
*offset = median - (*scale) * 8.0;
free(datacopy);
return (0);
}