22#include <lal/LALStdlib.h>
23#include <lal/LALConstants.h>
24#include <lal/LALMalloc.h>
25#include <lal/SeqFactories.h>
26#include <lal/PrintVector.h>
27#include <lal/LALRunningMedian.h>
64#define LALRUNNINGMEDIANTESTC_ENOM 0
65#define LALRUNNINGMEDIANTESTC_EARG 1
66#define LALRUNNINGMEDIANTESTC_ESUB 2
67#define LALRUNNINGMEDIANTESTC_EALOC 3
68#define LALRUNNINGMEDIANTESTC_EFALSE 4
69#define LALRUNNINGMEDIANTESTC_EERR 5
73#define LALRUNNINGMEDIANTESTC_MSGENOM "Nominal exit"
74#define LALRUNNINGMEDIANTESTC_MSGEARG "Error parsing command-line arguments"
75#define LALRUNNINGMEDIANTESTC_MSGESUB "Subroutine returned error"
76#define LALRUNNINGMEDIANTESTC_MSGEALOC "Could not allocate data space"
77#define LALRUNNINGMEDIANTESTC_MSGEFALSE "Medians mismatch"
78#define LALRUNNINGMEDIANTESTC_MSGEERR "Subroutine returned wrong or no error"
87#define EXIT( code, program, message ) \
89 if (( lalDebugLevel & LALERROR ) && (code)) \
90 LALPrintError( "Error[0] %d: program %s, file %s, line %d, %s\n"\
91 " %s\n", (code), (program), __FILE__, \
92 __LINE__, "$Id$", (message) ); \
93 else if ( lalDebugLevel & LALINFO ) \
94 LALPrintError( "Info[0]: program %s, file %s, line %d, %s\n" \
95 " %s\n", (program), __FILE__, __LINE__, \
96 "$Id$", (message) ); \
101#define TOLERANCE ( 10.0 * LAL_REAL8_EPS )
102#define compare_double( x, y ) \
103( ( y == 0 ? ( x == 0 ? 0 : fabs((x-y)/x) ) : fabs((x-y)/y) ) > TOLERANCE )
109int compare_double(
double x,
double y );
110int compare_single(
float x,
float y );
111static int rngmed_sortindex(
const void *elem1,
const void *elem2);
118struct rngmed_val_index {
124int compare_double(
double x,
double y )
139 diff = fabs(
x -
y );
148int compare_single(
float x,
float y )
163 diff = fabs(
x -
y );
171static int rngmed_sortindex(
const void *elem1,
const void *elem2){
174 const struct rngmed_val_index *A = elem1;
175 const struct rngmed_val_index *B = elem2;
182 else if (data1==data2)
196 struct rngmed_val_index *index_block;
202 printf(
"ERROR: LALDCreateVector returned status %d\n",stat->
statusCode);
212 printf(
"ERROR: LALDRunningMedian returned status %d\n",stat->
statusCode);
223 index_block = (
struct rngmed_val_index *)
LALCalloc(param.
blocksize,
sizeof(
struct rngmed_val_index));
229 for(i=0;i<length-param.
blocksize+1;i++) {
233 index_block[k].data=input->
data[k+i];
234 index_block[k].index=k;
238 qsort(index_block, param.
blocksize,
sizeof(
struct rngmed_val_index),rngmed_sortindex);
242 median = index_block[(param.
blocksize-1)/2].data;
244 median = (index_block[param.
blocksize/2-1].data+index_block[param.
blocksize/2].data)/2;
247 if(compare_double(median,medians->
data[i])) {
248 printf(
"ERROR: index:%d median:% 22.15e running median:% 22.15e mismatch:% 22.15e\n",
249 i, median, medians->
data[i], median - medians->
data[i]);
260 printf(
"ERROR: LALDestroyVector returned status %d\n",stat->
statusCode);
276 struct rngmed_val_index *index_block;
282 printf(
"ERROR: LALDCreateVector returned status %d\n",stat->
statusCode);
292 printf(
"ERROR: LALRunningMedian returned status %d\n",stat->
statusCode);
303 index_block = (
struct rngmed_val_index *)
LALCalloc(param.
blocksize,
sizeof(
struct rngmed_val_index));
309 for(i=0;i<length-param.
blocksize+1;i++) {
313 index_block[k].data=input->
data[k+i];
314 index_block[k].index=k;
318 qsort(index_block, param.
blocksize,
sizeof(
struct rngmed_val_index),rngmed_sortindex);
322 median = index_block[(param.
blocksize-1)/2].data;
324 median = (index_block[param.
blocksize/2-1].data+index_block[param.
blocksize/2].data)/2;
327 if(compare_single(median,medians->
data[i])) {
328 printf(
"ERROR: index:%d median:%f running median:%f mismatch\n", i, median, medians->
data[i]);
339 printf(
"ERROR: LALDestroyVector returned status %d\n",stat->
statusCode);
354int main(
int argc,
char **argv )
357 UINT4 blocksize = 512, length = 1024;
372 memset(¶m, 0,
sizeof(param));
377 length = atoi(argv[1]);
378 blocksize = atoi(argv[2]);
384 fprintf(stderr,
"blocksize must be >3\n");
387 if (blocksize > length){
388 fprintf(stderr,
"blocksize must be <= length\n");
401 for(i=0;i<length;i++)
402 input4->
data[i] = (input8->
data[i] = (
double)rand()/(
double)RAND_MAX);
426 printf(
" PASS: LALSRunningMedian blocksize =0 results in error\n");
434 printf(
" PASS: LALDRunningMedian blocksize =0 results in error\n");
447 printf(
" PASS: LALSRunningMedian blocksize =2 results in error\n");
455 printf(
" PASS: LALDRunningMedian blocksize =2 results in error\n");
468 printf(
" PASS: LALSRunningMedian blocksize > input length results in error\n");
476 printf(
" PASS: LALDRunningMedian blocksize > input length results in error\n");
492 printf(
" PASS: LALSRunningMedian NULL input results in error\n");
500 printf(
" PASS: LALDRunningMedian NULL input results in error\n");
509 printf(
" PASS: LALSRunningMedian NULL medians results in error\n");
517 printf(
" PASS: LALDRunningMedian NULL medians results in error\n");
545 printf(
" PASS: LALSRunningMedian with too small median array results in error\n");
563 printf(
" PASS: LALDRunningMedian with too small median array results in error\n");
584 printf(
" PASS: LALSRunningMedian with too large median array results in error\n");
602 printf(
" PASS: LALDRunningMedian with too large median array results in error\n");
619 if(testDRunningMedian(&stat,input8,length,param,
verbose,0)) {
622 printf(
" PASS: LALDRunningMedian(%d,%d)\n",length,param.
blocksize);
625 if(testSRunningMedian(&stat,input4,length,param,
verbose,0)) {
628 printf(
" PASS: LALSRunningMedian(%d,%d)\n",length,param.
blocksize);
634 if(testDRunningMedian(&stat,input8,length,param,
verbose,0)) {
637 printf(
" PASS: LALDRunningMedian(%d,%d)\n",length,param.
blocksize);
640 if(testSRunningMedian(&stat,input4,length,param,
verbose,0)) {
643 printf(
" PASS: LALSRunningMedian(%d,%d)\n",length,param.
blocksize);
646 if(testDRunningMedian(&stat,input8,length,param,
verbose,1)) {
649 printf(
" PASS: LALDRunningMedian2(%d,%d)\n",length,param.
blocksize);
652 if(testSRunningMedian(&stat,input4,length,param,
verbose,1)) {
655 printf(
" PASS: LALSRunningMedian2(%d,%d)\n",length,param.
blocksize);
661 if(testDRunningMedian(&stat,input8,length,param,
verbose,1)) {
664 printf(
" PASS: LALDRunningMedian2(%d,%d)\n",length,param.
blocksize);
667 if(testSRunningMedian(&stat,input4,length,param,
verbose,1)) {
670 printf(
" PASS: LALSRunningMedian2(%d,%d)\n",length,param.
blocksize);
void REPORTSTATUS(LALStatus *status)
void LALCheckMemoryLeaks(void)
int main(int argc, char *argv[])
#define LAL_REAL8_EPS
Difference between 1 and the next resolvable REAL8 2^-52.
#define LAL_REAL4_MIN
Smallest normalized REAL4 number 2^-126.
#define LAL_REAL8_MIN
Smallest normalized REAL8 number 2^-1022.
#define LAL_REAL4_EPS
Difference between 1 and the next resolvable REAL4 2^-23.
unsigned char BOOLEAN
Boolean logical type, see Headers LAL(Atomic)Datatypes.h for more details.
uint64_t UINT8
Eight-byte unsigned integer; on some platforms this is equivalent to unsigned long int instead.
double REAL8
Double precision real floating-point number (8 bytes).
uint32_t UINT4
Four-byte unsigned integer.
float REAL4
Single precision real floating-point number (4 bytes).
void LALDPrintVector(REAL8Vector *vector)
void LALSPrintVector(REAL4Vector *vector)
void LALDCreateVector(LALStatus *, REAL8Vector **, UINT4)
void LALDDestroyVector(LALStatus *, REAL8Vector **)
void LALSDestroyVector(LALStatus *, REAL4Vector **)
void LALSCreateVector(LALStatus *, REAL4Vector **, UINT4)
LAL status structure, see The LALStatus structure for more details.
INT4 statusCode
A numerical code identifying the type of error, or 0 for nominal status; Negative values are reserved...
Vector of type REAL4, see DATATYPE-Vector types for more details.
REAL4 * data
Pointer to the data array.
UINT4 length
Number of elements in array.
Vector of type REAL8, see DATATYPE-Vector types for more details.
REAL8 * data
Pointer to the data array.
UINT4 length
Number of elements in array.