-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBasicMath.cpp
1548 lines (1217 loc) · 39.6 KB
/
BasicMath.cpp
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// BasicMath.cpp: implementation of the CBasicMath class.
//
//////////////////////////////////////////////////////////////////////
#undef min
#undef max
#include "stdafx.h"
#include "resource.h"
#include "BasicMath.h"
//#include "MFCTools.h"
//#include "MathHistory.h"
#include "Fit/GaussFunction.h"
#include "Fit/CubicSplineFunction.h"
#include "Fit/StandardMetricFunction.h"
#include "Fit/PolynomialFunction.h"
#include "Fit/DiscreteFunction.h"
#include "Fit/StandardFit.h"
#include "Fit/DOASVector.h"
//#include "windoastools.h"
//#include "DoubleMonitoredArrayData.h"
#include <math.h>
#include <vector>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
using namespace MathFit;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
bool CBasicMath::mDoNotUseMathLimits = false;
CBasicMath::CBasicMath()
{
}
CBasicMath::~CBasicMath()
{
}
double* CBasicMath::LowPassBinomial(double *fData, int iSize, int iNIterations)
{
std::vector<double> fBuffer(iSize);
double *fOut = fData;
double *fIn = fBuffer.data();
const int iLast = iSize - 1;
const int iFirst = 0;
int j, i;
for(j = 0; j < iNIterations; j++)
{
// now swap buffers
double *fTemp = fIn;
fIn = fOut;
fOut = fTemp;
for(i = iFirst; i < iSize; i++)
{
double lMid, lLeft, lRight;
lMid = fIn[i];
if(i == iFirst)
lLeft = fIn[i];
else
lLeft = fIn[i - 1];
if(i == iLast)
lRight = fIn[i];
else
lRight = fIn[i + 1];
fOut[i] = 0.5 * lMid + 0.25 * lLeft + 0.25 * lRight;
}
}
if(fOut != fData) {
memcpy(fData, fOut, sizeof(double) * iSize);
}
return(fData);
}
/*void CBasicMath::LowPassBinomial(ISpectrum& dispData, int iNIterations)
{
CDoubleMonitoredArrayData dmadData(dispData.Data);
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispData, iMeaStart, iMeaEnd);
double* fData = dmadData.GetBuffer();
LowPassBinomial(&fData[iMeaStart], iSize, iNIterations);
dispData.Data = dmadData.GetMonitoredArray();
AddHistory(dispData, LOWPASS);
}*/
double* CBasicMath::HighPassBinomial(double *fData, int iSize, int iNIterations)
{
std::vector<double> fBuffer(iSize);
int i;
// create copy of original data
memcpy(fBuffer.data(), fData, sizeof(double) * iSize);
// create low pass filtered data
LowPassBinomial(fBuffer.data(), iSize, iNIterations);
// remove low pass part from data
for(i = 0; i < iSize; i++)
{
if(fBuffer[i] != 0.0)
fData[i] /= fBuffer[i];
else
fData[i] = 0;
}
return(fData);
}
/*void CBasicMath::HighPassBinomial(ISpectrum& dispData, int iNIterations)
{
CDoubleMonitoredArrayData dmadData(dispData.Data);
double* fData = dmadData.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispData, iMeaStart, iMeaEnd);
HighPassBinomial(&fData[iMeaStart], iSize, iNIterations);
dispData.Data = dmadData.GetMonitoredArray();
AddHistory(dispData, HIGHPASS);
}*/
double* CBasicMath::Log(double *fData, int iSize)
{
int i;
for(i = 0; i < iSize; i++)
fData[i] = fData[i] <= 0 ? 0.0 : log(fData[i]);
return(fData);
}
/*void CBasicMath::Log(ISpectrum& dispData)
{
CDoubleMonitoredArrayData dmadData(dispData.Data);
double* fData = dmadData.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispData, iMeaStart, iMeaEnd);
Log(&fData[iMeaStart], iSize);
dispData.Data = dmadData.GetMonitoredArray();
AddHistory(dispData, LOGSPEC);
}*/
double* CBasicMath::Delog(double *fData, int iSize)
{
int i;
for(i = 0; i < iSize; i++)
fData[i] = exp(fData[i]);
return(fData);
}
/*void CBasicMath::Delog(ISpectrum& dispData)
{
CDoubleMonitoredArrayData dmadData(dispData.Data);
double* fData = dmadData.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispData, iMeaStart, iMeaEnd);
Delog(&fData[iMeaStart], iSize);
dispData.Data = dmadData.GetMonitoredArray();
AddHistory(dispData, EXPSPEC);
}*/
double* CBasicMath::CalcMeasuredSpec(double* fRes, double* fMea, int iMeaScans, double fMeaExpTime, double* fLamp, int iLampScans, double fLampExpTime, double* fBack, int iBackScans, double fBackExpTime, double fOffset, double fOffsetExpTime, int iSize)
{
if(iMeaScans == 0 || iBackScans == 0 || iLampScans == 0)
return(fRes);
int i;
if(fBackExpTime == fOffsetExpTime)
fBackExpTime += 1.0;
double fTau = (fMeaExpTime - fOffsetExpTime) / (fBackExpTime - fOffsetExpTime);
double fMin = 1;
for(i = 0; i < iSize; i++)
{
double fB = fTau * ((fBack[i] / iBackScans) - fOffset);
double fM = (fMea[i] / iMeaScans) - fOffset;
double fL = (fLamp[i] / iLampScans) - fOffset;
fRes[i] = (fM - fB) / (fL != 0 ? fL : 1.0);
if(fRes[i] < fMin)
fMin = fRes[i];
}
// if we have negative values or zero values, move the whole spectrum into the positive space
if(fMin <= 0)
{
// generate positive offset
fMin = -fMin;
// make sure to be non zero
fMin += 1;
for(i = 0; i < iSize; i++)
fRes[i] += fMin;
}
return(fRes);
}
/*void CBasicMath::CalcMeasuredSpec(ISpectrum& dispRes, ISpectrum& dispMea, ISpectrum& dispLamp, ISpectrum& dispBack, double fOffset, double fOffsetExpTime)
{
int iSize = min(dispBack.GetNChannel(), min(dispMea.GetNChannel(), dispLamp.GetNChannel()));
// create the result array
CDoubleMonitoredArrayData dmadMea(dispMea.Data);
CDoubleMonitoredArrayData dmadLamp(dispLamp.Data);
CDoubleMonitoredArrayData dmadBack(dispBack.Data);
CDoubleMonitoredArrayData dmadRes(iSize);
int iMeaStart, iMeaEnd, iNewSize;
// calculate the real size
iNewSize = min(CheckLimits(dispMea, iMeaStart, iMeaEnd), iSize);
double* fRes = dmadRes.GetBuffer();
double* fLamp = dmadLamp.GetBuffer();
double* fBack = dmadBack.GetBuffer();
double* fMea = dmadMea.GetBuffer();
CalcMeasuredSpec(&fRes[iMeaStart], &fMea[iMeaStart], dispMea.GetNumScans(), dispMea.GetExposureTime(), &fLamp[iMeaStart], dispLamp.GetNumScans(), dispLamp.GetExposureTime(), &fBack[iMeaStart], dispBack.GetNumScans(), dispBack.GetExposureTime(), fOffset, fOffsetExpTime, iNewSize);
dispRes.Data = dmadRes.GetMonitoredArray();
AddHistory(dispRes, CALCAIR);
}*/
double CBasicMath::CalcExposureTime(double fSaturation, double fEOffsetExpTime, double fCurrentExpTime, double fCurrentAvg, double fBackExpTime, double fBackAvg, double fIntTime)
{
// if no spec given reduce time
if(fCurrentAvg == 0)
return(fCurrentExpTime * 0.8);
// the currently used exposure time is the exposure time without the electronic background exposure time
double fCurExpTime = std::max(fCurrentExpTime - fEOffsetExpTime, 0.0);
// we need only the average of the real measured signal level without the background
// ensure the difference is at least 1 unit big, so we switch to maximum exposure time
double fDiffAvg = fabs(fCurrentAvg - fBackAvg) + 1;
// ratio of current signal average and desired signal average
double fSignalRatio = fSaturation * 65536.0 / fDiffAvg;
// the new exposure time is the sum of the electronic offset exposure time and
// the old exposure time derived from the ratio calculated
double fRes = std::max((fCurExpTime * fSignalRatio) + fEOffsetExpTime, 0.0);
if(fIntTime > 0)
return(std::min(fRes, fIntTime));
else
return(fRes);
}
/*double CBasicMath::CalcExposureTime(ISpectrum& dispMea, ISpectrum& dispBack, double fSaturation, double fIntTime)
{
return(CalcExposureTime(fSaturation, 0, dispMea.GetExposureTime(), dispMea.GetAverage() / dispMea.GetNumScans(), dispBack.GetExposureTime(), dispBack.GetAverage() / dispBack.GetNumScans(), fIntTime));
}*/
/*
* CheckLimits
*
* Checks for valid math limit settings and returns the array boundaries as zero based
* indices as well as the number of elements contained in the selected array.
*/
/*int CBasicMath::CheckLimits(ISpectrum &dispSpec, int &iLowLimit, int &iHighLimit)
{
int iSize = dispSpec.GetNChannel();
// no limits wanted, so no limts set
if(mDoNotUseMathLimits)
{
iLowLimit = 0;
iHighLimit = iSize - 1;
return(iSize);
}
// check math limits
iLowLimit = (int)min(dispSpec.GetMathLow(), iSize - 1);
if(iLowLimit < 0)
iLowLimit = 0;
iHighLimit = (int)min(dispSpec.GetMathHigh(), iSize - 1);
if(iHighLimit < 0)
iHighLimit = 0;
if(iLowLimit >= iHighLimit)
iHighLimit = iLowLimit + 1;
// calculate the real size
return(min(iHighLimit - iLowLimit + 1, iSize));
}*/
void CBasicMath::BiasAdjust(double *fData, int iSize)
{
double fAverage;
int i;
// get average value
for(fAverage = i = 0; i < iSize; i++)
fAverage += fData[i];
fAverage /= (double)iSize;
// remove average
for(i = 0; i < iSize; i++)
fData[i] -= fAverage;
}
/*void CBasicMath::BiasAdjust(ISpectrum &dispSpec)
{
CDoubleMonitoredArrayData dmadData(dispSpec.Data);
double* fData = dmadData.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispSpec, iMeaStart, iMeaEnd);
BiasAdjust(&fData[iMeaStart], iSize);
dispSpec.Data = dmadData.GetMonitoredArray();
AddHistory(dispSpec, BIASADJUST);
}
*/
void CBasicMath::Zero(double *fData, int iSize, double fZeroLimit)
{
int i;
fZeroLimit = fabs(fZeroLimit);
for(i = 0; i < iSize; i++)
if(fabs(fData[i]) < fZeroLimit)
fData[i] = 0;
}
/*void CBasicMath::Zero(ISpectrum &dispSpec, double fZeroLimit)
{
CDoubleMonitoredArrayData dmadData(dispSpec.Data);
double* fData = dmadData.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispSpec, iMeaStart, iMeaEnd);
Zero(&fData[iMeaStart], iSize, fZeroLimit);
dispSpec.Data = dmadData.GetMonitoredArray();
AddHistory(dispSpec, ZEROSPEC);
}*/
void CBasicMath::NormalizeAmplitude(double *fData, int iSize, double fMaxAmplitude)
{
int i;
double fMax;
// search pivot
fMax = fData[0];
for(i = 1; i < iSize; i++)
fMax = std::max(fMax, fabs(fData[i]));
// nothing to do here
if(fMax == 0)
return;
double fFactor = fMaxAmplitude / fMax;
for(i = 0; i < iSize; i++)
fData[i] *= fFactor;
}
/*void CBasicMath::NormalizeAmplitude(ISpectrum &dispSpec, double fMaxAmplitude)
{
CDoubleMonitoredArrayData dmadData(dispSpec.Data);
double* fData = dmadData.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispSpec, iMeaStart, iMeaEnd);
NormalizeAmplitude(&fData[iMeaStart], iSize, fMaxAmplitude);
dispSpec.Data = dmadData.GetMonitoredArray();
AddHistory(dispSpec, NORMAMP);
}*/
void CBasicMath::NormalizeEnergy(double *fData, int iSize, double fEnergy)
{
int i;
double fCurEnergy;
// get current energy
for(fCurEnergy = i = 0; i < iSize; i++)
fCurEnergy += fabs(fData[i]);
// nothing to divide
if(fCurEnergy == 0)
return;
// normalize energy level
double fFactor = fEnergy / fCurEnergy;
for(i = 0; i < iSize; i++)
fData[i] *= fFactor;
}
/*void CBasicMath::NormalizeEnergy(ISpectrum &dispSpec, double fEnergy)
{
CDoubleMonitoredArrayData dmadData(dispSpec.Data);
double* fData = dmadData.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispSpec, iMeaStart, iMeaEnd);
NormalizeEnergy(&fData[iMeaStart], iSize, fEnergy);
dispSpec.Data = dmadData.GetMonitoredArray();
AddHistory(dispSpec, NORMENERGY);
}
*/
void CBasicMath::Invert(double *fData, int iSize)
{
int i;
for(i = 0; i < iSize; i++)
fData[i] *= -1;
}
/*void CBasicMath::Invert(ISpectrum &dispSpec)
{
CDoubleMonitoredArrayData dmadData(dispSpec.Data);
double* fData = dmadData.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispSpec, iMeaStart, iMeaEnd);
Invert(&fData[iMeaStart], iSize);
dispSpec.Data = dmadData.GetMonitoredArray();
AddHistory(dispSpec, INVERT);
}*/
void CBasicMath::Reciprocal(double *fData, int iSize)
{
int i;
for(i = 0; i < iSize; i++)
fData[i] = 1 / fData[i];
}
/*void CBasicMath::Reciprocal(ISpectrum &dispSpec)
{
CDoubleMonitoredArrayData dmadData(dispSpec.Data);
double* fData = dmadData.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispSpec, iMeaStart, iMeaEnd);
Reciprocal(&fData[iMeaStart], iSize);
dispSpec.Data = dmadData.GetMonitoredArray();
AddHistory(dispSpec, RECIPROC);
}*/
void CBasicMath::Convolute(double *fFirst, int iSize, double *fCore, int iCoreSize)
{
int iCoreMid = iCoreSize / 2;
int i, j;
// backup the original data
std::vector<double> fBuffer(iSize);
memcpy(fBuffer.data(), fFirst, iSize * sizeof(double));
// process every pixel
for(i = 0; i < iSize; i++)
{
fFirst[i] = 0;
int iIndex = -iCoreMid;
// convolute with core, boundaries will be constanstly extended
for(j = 0; j < iCoreSize; j++, iIndex++)
{
// the index to the base data is the core index plus the base index
int iRealIndex = iIndex + i;
if(iRealIndex < 0)
iRealIndex = 0;
else if(iRealIndex >= iSize)
iRealIndex = iSize - 1;
fFirst[i] += fBuffer[iRealIndex] * fCore[j];
}
}
}
/*void CBasicMath::Convolute(ISpectrum &dispFirst, ISpectrum &dispCore)
{
CDoubleMonitoredArrayData dmadData(dispFirst.Data);
double* fData = dmadData.GetBuffer();
CDoubleMonitoredArrayData dmadCore(dispCore.Data);
double* fCore = dmadCore.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispFirst, iMeaStart, iMeaEnd);
// get core limits
int iCoreStart, iCoreEnd, iCoreSize;
iCoreSize = CheckLimits(dispCore, iCoreStart, iCoreEnd);
Convolute(&fData[iMeaStart], iSize, &fCore[iCoreStart], iCoreSize);
dispFirst.Data = dmadData.GetMonitoredArray();
AddHistory(dispFirst, CONVOLUTION);
}
*/
void CBasicMath::Reverse(double *fData, int iSize)
{
std::vector<double> fBuffer(iSize);
memcpy(fBuffer.data(), fData, iSize * sizeof(double));
int i;
for(i = 0; i < iSize; i++) {
fData[i] = fBuffer[iSize - i - 1];
}
}
/*void CBasicMath::Reverse(ISpectrum &dispData)
{
CDoubleMonitoredArrayData dmadData(dispData.Data);
double* fData = dmadData.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispData, iMeaStart, iMeaEnd);
Reverse(&fData[iMeaStart], iSize);
dispData.Data = dmadData.GetMonitoredArray();
AddHistory(dispData, REVERSE);
}*/
void CBasicMath::Add(double *fFirst, double *fSec, int iSize, double fFactor)
{
if(fFactor != 0)
{
int i;
for(i = 0; i < iSize; i++)
fFirst[i] += fFactor * fSec[i];
}
else
{
int i;
for(i = 0; i < iSize; i++)
fFirst[i] += fSec[i];
}
}
/*void CBasicMath::Add(ISpectrum &dispFirst, ISpectrum &dispSec, int iMode)
{
CDoubleMonitoredArrayData dmadData(dispFirst.Data);
double* fData = dmadData.GetBuffer();
CDoubleMonitoredArrayData dmadSec(dispSec.Data);
double* fSec = dmadSec.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispFirst, iMeaStart, iMeaEnd);
if(dispSec.GetNChannel() < iMeaEnd)
return;
double fCorrectFactor = GetCorrectFactor(dispFirst, dispSec, iMode);
Add(&fData[iMeaStart], &fSec[iMeaStart], iSize, fCorrectFactor);
dispFirst.Data = dmadData.GetMonitoredArray();
AddHistory(dispFirst, ADDSPEC);
}*/
void CBasicMath::Add(double *fFirst, int iSize, double fConst)
{
int i;
for(i = 0; i < iSize; i++)
fFirst[i] += fConst;
}
/*void CBasicMath::Add(ISpectrum &dispFirst, double fConst)
{
CDoubleMonitoredArrayData dmadData(dispFirst.Data);
double* fData = dmadData.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispFirst, iMeaStart, iMeaEnd);
Add(&fData[iMeaStart], iSize, fConst);
dispFirst.Data = dmadData.GetMonitoredArray();
AddHistory(dispFirst, ADDSCALAR);
}*/
void CBasicMath::Sub(double *fFirst, double *fSec, int iSize, double fFactor)
{
if(fFactor != 0)
{
int i;
for(i = 0; i < iSize; i++)
fFirst[i] -= fFactor * fSec[i];
}
else
{
int i;
for(i = 0; i < iSize; i++)
fFirst[i] -= fSec[i];
}
}
/*void CBasicMath::Sub(ISpectrum &dispFirst, ISpectrum &dispSec, int iMode)
{
CDoubleMonitoredArrayData dmadData(dispFirst.Data);
double* fData = dmadData.GetBuffer();
CDoubleMonitoredArrayData dmadSec(dispSec.Data);
double* fSec = dmadSec.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispFirst, iMeaStart, iMeaEnd);
if(dispSec.GetNChannel() < iMeaEnd)
return;
double fCorrectFactor = GetCorrectFactor(dispFirst, dispSec, iMode);
Sub(&fData[iMeaStart], &fSec[iMeaStart], iSize, fCorrectFactor);
dispFirst.Data = dmadData.GetMonitoredArray();
AddHistory(dispFirst, SUBSPEC);
}
*/
void CBasicMath::Sub(double *fFirst, int iSize, double fConst)
{
int i;
for(i = 0; i < iSize; i++)
fFirst[i] -= fConst;
}
/*void CBasicMath::Sub(ISpectrum &dispFirst, double fConst)
{
CDoubleMonitoredArrayData dmadData(dispFirst.Data);
double* fData = dmadData.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispFirst, iMeaStart, iMeaEnd);
Sub(&fData[iMeaStart], iSize, fConst);
dispFirst.Data = dmadData.GetMonitoredArray();
AddHistory(dispFirst, SUBSCALAR);
}
*/
void CBasicMath::Mul(double *fFirst, double *fSec, int iSize, double fFactor)
{
if(fFactor != 0)
{
int i;
for(i = 0; i < iSize; i++)
fFirst[i] *= fFactor * fSec[i];
}
else
{
int i;
for(i = 0; i < iSize; i++)
fFirst[i] *= fSec[i];
}
}
/*void CBasicMath::Mul(ISpectrum &dispFirst, ISpectrum &dispSec, int iMode)
{
CDoubleMonitoredArrayData dmadData(dispFirst.Data);
double* fData = dmadData.GetBuffer();
CDoubleMonitoredArrayData dmadSec(dispSec.Data);
double* fSec = dmadSec.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispFirst, iMeaStart, iMeaEnd);
if(dispSec.GetNChannel() < iMeaEnd)
return;
double fCorrectFactor = GetCorrectFactor(dispFirst, dispSec, iMode);
Mul(&fData[iMeaStart], &fSec[iMeaStart], iSize, fCorrectFactor);
dispFirst.Data = dmadData.GetMonitoredArray();
AddHistory(dispFirst, MULSPEC);
}*/
void CBasicMath::Mul(double *fFirst, int iSize, double fConst)
{
int i;
for(i = 0; i < iSize; i++)
fFirst[i] *= fConst;
}
/*void CBasicMath::Mul(ISpectrum &dispFirst, double fConst)
{
CDoubleMonitoredArrayData dmadData(dispFirst.Data);
double* fData = dmadData.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispFirst, iMeaStart, iMeaEnd);
Mul(&fData[iMeaStart], iSize, fConst);
dispFirst.Data = dmadData.GetMonitoredArray();
AddHistory(dispFirst, MULSCALAR);
}*/
void CBasicMath::Div(double *fFirst, double *fSec, int iSize, double fFactor)
{
if(fFactor != 0)
{
int i;
for(i = 0; i < iSize; i++)
if(fSec[i] != 0)
fFirst[i] /= fFactor * fSec[i];
else
fFirst[i] = 0;
}
else
{
int i;
for(i = 0; i < iSize; i++)
if(fSec[i] != 0)
fFirst[i] /= fSec[i];
else
fFirst[i] = 0;
}
}
/*void CBasicMath::Div(ISpectrum &dispFirst, ISpectrum &dispSec, int iMode)
{
CDoubleMonitoredArrayData dmadData(dispFirst.Data);
double* fData = dmadData.GetBuffer();
CDoubleMonitoredArrayData dmadSec(dispSec.Data);
double* fSec = dmadSec.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispFirst, iMeaStart, iMeaEnd);
if(dispSec.GetNChannel() < iMeaEnd)
return;
double fCorrectFactor = GetCorrectFactor(dispFirst, dispSec, iMode);
Div(&fData[iMeaStart], &fSec[iMeaStart], iSize, fCorrectFactor);
dispFirst.Data = dmadData.GetMonitoredArray();
AddHistory(dispFirst, DIVSPEC);
}*/
void CBasicMath::Div(double *fFirst, int iSize, double fConst)
{
if(fConst == 0)
return;
int i;
for(i = 0; i < iSize; i++)
fFirst[i] /= fConst;
}
/*void CBasicMath::Div(ISpectrum &dispFirst, double fConst)
{
CDoubleMonitoredArrayData dmadData(dispFirst.Data);
double* fData = dmadData.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispFirst, iMeaStart, iMeaEnd);
Div(&fData[iMeaStart], iSize, fConst);
dispFirst.Data = dmadData.GetMonitoredArray();
AddHistory(dispFirst, DIVSCALAR);
}*/
bool CBasicMath::ShiftAndSqueeze(CVector& vXData, CVector& vYData, double fOrigin, double fShift, double fSqueeze)
{
MATHFIT_ASSERT(vXData.GetSize() == vYData.GetSize());
bool bResult = false;
try
{
CCubicSplineFunction cbfSpec(vXData, vYData);
int i;
for(i = 0; i < vXData.GetSize(); i++)
{
const double fXValue = (vXData.GetAt(i) - fOrigin) * fSqueeze + fShift + fOrigin;
vYData.SetAt(i, cbfSpec.GetValue(fXValue));
}
bResult = true;
}
catch(CAssertFailedException e)
{
e.ReportError();
}
catch(CFitException e)
{
}
return bResult;
}
/*bool CBasicMath::ShiftAndSqueeze(ISpectrum &dispSpec, double fShift, double fSqueeze)
{
CDoubleMonitoredArrayData dmadData(dispSpec.Data);
double* fData = dmadData.GetBuffer();
int iMeaStart, iMeaEnd, iSize;
// calculate the real size
iSize = CheckLimits(dispSpec, iMeaStart, iMeaEnd);
CVector vXData(dispSpec.GetNChannel());
vXData.Wedge(0, 1);
CVector vYData(fData, dispSpec.GetNChannel(), 1, false);
bool bRet = ShiftAndSqueeze(vXData, vYData, vXData.GetAt(iMeaStart), fShift, fSqueeze);
dispSpec.Data = dmadData.GetMonitoredArray();
AddHistory(dispSpec, SHIFTSQUEEZE);
return(bRet);
}*/
/**
* FitRes2ppb
*
* Converts the result of the non linear dfit given in molecules/cm^2 into
* ppb using the following formular:
*
* The gas formular gives us the number or molecules in the volume:
* n = p * V / (Rm * T)
* where Rm is the universal gas constant which is given by:
* Rm = pn * Vmn / Tn
* pn = 101.325 kPa
* Vmn = 22.413e3 cm^3/mol
* Tn = 273.15 K
*
* The number of molecules of the fit result is given by:
* nfit = fit result * 1 cm^2 = fit result
*
* The number of molecules in the gas cylinder is given by:
* ngas = n * avogadro
* where avogadro is 6.022137e23
*
* So to get ppb (particles per billion) we now just build the releation between the
* two amounts of molecules:
*
* ppb = nfit * 1e9 / ngas
*
* @param fFitResult The concentration in molecules/cm^2
* @param fLightPathLength The length of the light path in cm
* @param fTemperature The temperature in Celcius degrees
* @param fPreasure The preasure in hPa
*
* @return the concentration in ppb
*/
double CBasicMath::FitRes2ppb(double fFitResult, double fLightPathLength, double fTemperature, double fPreasure)
{
const double fRm = 101.325 * 22.413e3 / 273.15; // universal gas constant
const double fT = fTemperature * 273.15; // convert from Celcuis to Kelvin
const double fP = fPreasure / 10; // convert from hPa to kPa!
const double fV = 1 * 1 * fLightPathLength; // normalized volume of cm^2 * light path length
// get the mol number of the current gas cyclinder
double fMol = fP * fV / (fRm * fT);
// determine the number of molcules from the fit divided by the
// whole number of molecules in the volume
double fPPB = fFitResult / (fMol * 6.022137e14);
return(fPPB);
}
/**
* FitRes2MicroGrammPerCubicMeter
*
* Converts the result of the non linear dfit given in molecules/cm^2 into
* µg/m^3 using the following formular:
*
* c = fit * mol.-weight * 1e6 / (light path length * avogadro * 1e-6)
* where avogadro is 6.022137e23
*
* @param fFitResult The concentration in molecules/cm^2
* @param fLightPathLength The length of the light path in cm
* @param fMolecularWeight The molecular weight in g/mol
*
* @return the concentration in µg/cm^3
*/
double CBasicMath::FitRes2MicroGrammPerCubicMeter(double fFitResult, double fLightPathLength, double fMolecularWeight)
{
return(fFitResult * fMolecularWeight / (fLightPathLength * 6.022137e11));
}
/*void CBasicMath::PolyFill(ISpectrum &dispSpec, int iStartChannel, int iNumChannels, double *fPolyCoeff, int iPolyDegree)
{
int iMaxChannel = dispSpec.GetNChannel();
CDoubleMonitoredArrayData dmadData(iMaxChannel);
double* fData = dmadData.GetBuffer();
int i;
for(i = 0; i < iNumChannels; i++)
{
int iChannel = iStartChannel + i - 1;
if(iChannel < iMaxChannel)
fData[iChannel] = CalcPoly(fPolyCoeff, iPolyDegree, iChannel);
else
break;
}
// write the new data set back to the spectrum
dispSpec.Data = dmadData.GetMonitoredArray();
AddHistory(dispSpec, POLYFILL);
}*/
/*double CBasicMath::GetCorrectFactor(ISpectrum &dispFirst, ISpectrum &dispSec, int iMode)
{
switch(iMode)
{
case SCANWEIGHT:
return((double)dispFirst.GetNumScans() / (double)dispSec.GetNumScans());
case TIMEWEIGHT: