File: vmmath.h

package info (click to toggle)
cpl-plugin-vimos 4.1.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 28,228 kB
  • sloc: ansic: 169,271; cpp: 16,177; sh: 4,344; python: 3,678; makefile: 1,138; perl: 10
file content (575 lines) | stat: -rw-r--r-- 15,372 bytes parent folder | download | duplicates (4)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/* $Id: vmmath.h,v 1.2 2013-03-25 11:43:04 cgarcia Exp $
 *
 * This file is part of the VIMOS Pipeline
 * Copyright (C) 2002-2004 European Southern Observatory
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/*
 * $Author: cgarcia $
 * $Date: 2013-03-25 11:43:04 $
 * $Revision: 1.2 $
 * $Name: not supported by cvs2svn $
 */

#ifndef VM_MATH_H
#define VM_MATH_H

#include <limits.h>

#include <pilmacros.h>

#include <vmmatrix.h>


PIL_BEGIN_DECLS

/* Cell size for surface fit */
#define BOXSIZE  100

/* defines for resampling code */
/* Number of tabulations in kernel  */
#define TABSPERPIX      (1000)
#define KERNEL_WIDTH    (2.0)
#define KERNEL_SAMPLES  (1+(int)(TABSPERPIX * KERNEL_WIDTH))

#define TANH_STEEPNESS	(5.0)
#define MIN_DIVISOR     ((double)1e-10)

#define MEANDEV_TO_SIGMA (1.25)

#ifndef PI_NUMB
#define PI_NUMB     (3.1415926535897932384626433832795)
#endif

/* Arithmetic operations */

typedef enum _VIMOS_OPER_TYPE_
{
  VM_OPER_ADD,
  VM_OPER_SUB,
  VM_OPER_MUL,
  VM_OPER_DIV
} VimosOperator;

typedef enum _NORM_METHOD
{
  MEAN,
  MEDIAN,
  MODE
} Method;

/*---------------------------------------------------------------------------
   Function	:	ipow()
   In 		:	double, int
   Out 		:	double
   Job		:	same as pow(x,y) but for integer values of y
   Notice	:	much faster than the math function due to the integer.
   				On some compilers, this optimization is already done in
				the pow() function, we do not rely on that fact.
 ---------------------------------------------------------------------------*/


double ipow(double x, int i);



/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function :   kth_smallest()
In       :   array of elements, # of elements in the array, rank k
Out      :   one element
Job      :   find the kth smallest element in the array
Notice   :   use the medianWirth() macro to get the median. 

Reference:

Author: Wirth, Niklaus 
Title: Algorithms + data structures = programs 
Publisher: Englewood Cliffs: Prentice-Hall, 1976 
Physical description: 366 p. 
Series: Prentice-Hall Series in Automatic Computation 
   
--------------------------------------------------------------------------------
*/


float kthSmallest(float a[], int n, int k);
#define medianWirth(a,n) kthSmallest(a,n,(((n)&1)?((n)/2):(((n)/2)-1)))

double kthSmallestDouble(double a[], int n, int k) ;
#define medianDouble(a,n) \
kthSmallestDouble(a,n,(((n)&1)?((n)/2):(((n)/2)-1)))



VimosBool setupInterpolation(double **kernel, VimosLong32 **leaps, 
                             int imageXlen);


/*----------------------------------------------------------------------------
   Function :   opt_med3()
   In       :   pointer to array of 3 pixel values
   Out      :   a pixelvalue
   Job      :   optimized search of the median of 3 pixel values
   Notice   :   found on sci.image.processing
                cannot go faster unless assumptions are made
                on the nature of the input signal.
				MODIFIES THE INPUT ARRAY
 ---------------------------------------------------------------------------*/

float opt_med3(float  *p) ;
float opt_med5(float  *p) ;
float opt_med7(float  *p) ;
float opt_med9(float  *p) ;

/*---------------------------------------------------------------------------
   Function	:	medianPixelvalue() (extracted from ECLIPSE)
   In 		:	allocated array of pixelvalues, # of pixels in the array
   Out 		:	1 pixel value
   Job		:	compute the median pixel value out of an array
   Notice	:	calls the fastest method depending on the number of
			elements in input.
				MODIFIES THE INPUT ARRAY
 ---------------------------------------------------------------------------*/



float medianPixelvalue(float * a, int n);

double medianPixelvalueDouble(double * a, int n);

/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  void sort(int arrayLen, float *array)

  Description:
  Sorts an array of floats.
  I took this code from Numerical Recipes. NR has 1-based arrays and for
  Virmos we use 0-based. I modified the code for this brute force (i.e. not
  elegantly...). I tested the routine, seems to work...

  Input:
  int arrayLen
  Length of the input array
  
  Input/Output:
  float *array
  Array of floats to sort. Sorting is inplace.

  Return Value:
  void

  Updates:
  18 Apr 99: Created (TAO)

--------------------------------------------------------------------------------
*/
void sort(int n, float *ra);

/*
 * Calculate average of n values (avoiding overflows)
 */

double computeAverageInt(int a[], int n);
double computeAverageFloat(float a[], int n);
double computeAverageDouble(double a[], int n);

/*
 * Calculate variance avoiding trends in image
 */

double computeVarianceFloat2D(float a[], int nx, int ny);

/*
 * Calculate variance avoiding trends in image
 */

double computeVarianceDouble2D(double a[], int nx, int ny);

/*
 * This is for extracting a submatrix from a matrix of sizex X sizey
 * elements, starting from element x,y till element x+nx-1,y+ny-1
 */

float *extractFloatImage
         (float a[], int sizex, int sizey, int x, int y, int nx, int ny);

/*
 * This is for inserting a submatrix into a matrix of sizex
 * times sizey elements, starting from element x,y till element
 * x+nx-1,y+ny-1
 */

VimosBool insertFloatImage
(float a[], int sizex, int sizey, int x, int y, int nx, int ny, float region[]);

/*
 * Compute histogram of a double array: input the array, the array dimension, 
 * and the number of histogram bins
 */

VimosDpoint *darrayHistogram(double *darray, unsigned int arDim, 
			     unsigned int nbins);
/*
 * Compute histogram of double array giving the value of the starting and
 * of the ending bins, and the bin size
 */

VimosDpoint *darrayHistoStartEnd(double *darray, int arrDim, double start,
				 double end, double bin_size);

 /* find the position of the peak and the FWHM of an histogram. 
  * The peak position
  * depends on the  bin size (i.e on the number of bins "npoints")
  * of the histogram. 
  */

double histogramPeak(VimosDpoint *histogram,double *fwhm, unsigned int nbins);
/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  int  waterShed(float *profile, int numPoints, int numLevels, 
                 float fluxLimit, int width, int *mask);

  Description:
  Performs watershed detection on *profile. Idea taken from SExtractor. 

  Input:
  float *profile
  Pointer to float array to process
  
  int numPoints
  length of pointer[] and of mask[]

  int numLevels
  Number of levels to use in watershed thresholding

  float fluxLimit
  Flux limit to use in detection. Only objects with flux larger than
  fluxFraction * total flux will be considered as objects.
  
  int width
  Width of boxcar smoothing applied to profile[]

  Input/Output:
  int *mask
  Pointer to array that will contain the mask. The 

  Return Value (succes):
  number of objects detected

  Return Value (error):
  -1
  
  Updates:
  15 Jun 00: Return -1 on error (Maura)
  21 Apr 99: Created (TAO)

--------------------------------------------------------------------------------
*/
int  waterShed(float *profile, int numPoints, int numLevels, 
               float fluxLimit, int width, int *mask);

VimosBool findPeak1D(float *, int size, float *position, int minPoints);
VimosBool findPeak2D(float *, int sizex, int sizey, float *posx, float *posy, 
                     int minPoints);
VimosBool findDip1D(float *, int size, float *position, int minPoints);
VimosBool findUpJump(float *, int size, float *position, int minPoints);
VimosBool findJump(float *, int size, float *position, int minPoints);
VimosBool findDownJump(float *, int size, float *position, int minPoints);

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  These are functions taken from the Rostat program written by T.Beers
  to compute robust statistics on a data set. See Beers, Flynn & Gebhardt
  1990, AJ 100, 32
-------------------------------------------------------------------------*/
float median(float x[],int n);

void xbiwt(float xdata[],int n,float *xlbiwt,float *xsbiwt,float *xlbiwt1,
	   float *xsbiwt1);

float xmad(float xdata[],int n,float xmed);

float *vector(long nl, long nh);
void free_vector(float *v, long nl, long nh);




/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  These are functions for integrating & c. (NR- like)

------------------------------------------------------------------------------
*/

/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  float trapezeInt(float (*theFunc)(float, float[]), float coeff[], 
                   float a, float b, int n)

  Description:
  Trapeze integration (modified trapzd from NR, addded coeff[]). 
  When called with n = 1 the function returns the crudest estimate of the 
  integral in[a,b] of f(x)dx. Subsequent calls with n=2,3,... (in that 
  sequential order) will improve the accuracy by adding  2^(n-2)  additional 
  interior points.

  Input:
  float (*theFunc)(float, float[])] 
  Function that gives, for each x, an y value computed by means of some 
  coefficients (see e.g. evalYFit)to be integrated

  float coeff[]
  Coefficients to be used to define the function to be integrated (e.g. linear 
  alone, or linear + gaussian, see  evalLineFlux)

  float a
  Lower limit of the integration

  float b
  Upper limit of the integration

  int n
  Stage of refinement of an extended trapezoidal rule.

  Updates:
  03 Jul 00: Created (AZ)

------------------------------------------------------------------------------
*/

float trapezeInt(float (*theFunc)(float, float[]), float coeff[], 
                 float a, float b, int n);


/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  void polint(float xa[], float ya[], int n, float x, float *y, float *dy)

  Description:
  Polinomial interpolation (polint from NR). Used by rombergInt

  Input:
  float xa[]
  Input x array

  float ya[]
  Input y array

  int n
  Dimension of arrays x and y

  float x
  The x at which one wants to interpolate

  float *y
  the returned interpolated y corresponding to x

  float *dy
  Error estimate

  Updates:
  03 Jul 00: Created (AZ)

------------------------------------------------------------------------------
*/

void polint(float xa[], float ya[], int n, float x, float *y, float *dy);


/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  float rombergInt(float (*theFunc)(float, float[]), float coeff[], 
                   float a, float b)

  Description:
  Romberg integration method (modified qromb from NR, added coeffs[]).

  Input:
  float (*theFunc)(float, float[])
  Function that gives, for each x, an y value computed by means of some 
  coefficients (see e.g. evalYFit)

  float coeff[]
  Coefficients to be used to define the function to be integrated (e.g. linear 
  alone, or linear + gaussian, see evalLineFlux)

  float a
  Lower limit for integration

  float b
  Upper limit for integration

  Updates:
  03 Jul 00: Created (AZ)

------------------------------------------------------------------------------
*/

float rombergInt(float (*theFunc)(float, float[]), float coeff[], 
                 float a, float b);



/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  float integrateSpectrum(VimosFloatArray *tmpSpec, float wlenStep)

  Description:
  This function performs a VERY simple integration, by summing all data
  using the formula:
  integrFlux += (tmpSpec->data[i] * wlenStep)
  To be improved!

  Input:
  VimosFloatArray *tmpSpec
  Array of input data to be integrated

  float wlenStep
  Integration step. Usually: wavelength increment in spectra taken from 
  descriptors

  Updates:
  25 Jul 00: Created (AZ)

------------------------------------------------------------------------------
*/

float integrateSpectrum(VimosFloatArray *tmpSpec, float wlenStep);



/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  End of functions for integrating & c. (NR- like)

------------------------------------------------------------------------------
*/



/* 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   void Indexx(int n, float *arrin, int *indx)

   Description:
   Construct an index table to sort an array. Taken from Num. Rec. , changing 
   arrays from 1-based to 0-based.
   Needed for IFU: when sorting in fiber flux, to avoid losing memory of 
   which  fiber corresponds to which flux. Gives an array of subscripts to 
   sort the fiber numbers too.

   Input: 
   int n
   Length of input array

   float arrin
   array for which you want the index table
   
   Output:
   int indx
   Array containing the subscripts to sort arrin in ascendin order

   Return Value:
   void
   
   Updates:
   09 Feb 00: Created (AZ)
------------------------------------------------------------------------------
*/

void Indexx(int n, float *arrin, int *indx);


/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  float computeRMS(float *theData, int n)

  Description:
  Computes root mean square of theData. NB: variance is computed as the
  corrected, not-distorted one, i.e. divide by (n-1)

  Input:
  float *theData
  Array of data for which to compute RMS

  int n
  Number of points in theData

  Updates:
  25 Jul 00: Created (AZ)

------------------------------------------------------------------------------
*/

float computeRMS(float *theData, int n);


/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  int  computeHistogram(VimosFloatArray *theData, int theXaxLen,
                        VimosFloatArray *theHisto, float mini, 
			float maxi, float step)

  Description:
  Computes the histogram of values in a given input array, between a minimum 
  and maximum and with a given bin size.

  Input:
  VimosFloatArray *theData
  Array of input values.

  int theXaxLen
  Length of Array of x axis (computed between mini and maxi, with step) .

  VimosFloatArray *theHisto
  Array of histogram values (defined outside, filled here)

  float mini
  Minimum value for computing the histogram

  float maxi
  Maximum value for computing the histogram

  float step
  Histogram bin size

  Updates:
  25 Jul 00: Created (AZ)

------------------------------------------------------------------------------
*/


int computeHistogram(VimosFloatArray *theData, int theXaxLen,
		     VimosFloatArray *theHisto, float mini, 
		     float maxi, float step);

PIL_END_DECLS

#endif /* VM_MATH_H */