-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetrend.c
121 lines (87 loc) · 2.64 KB
/
detrend.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
/*
-------------------------------------------------------------------------
OBJECT NAME: detrend.c
FULL NAME: Detrending Functions
ENTRY POINTS: DetrendNone()
DetrendMean()
DetrendLinear()
STATIC FNS: none
DESCRIPTION: Perform detrending function for spectral routines. All
MISSING_VALUES are converted to zero.
REFERENCES: regret.c (Linear only).
REFERENCED BY: spec.c, cospec.c
COPYRIGHT: University Corporation for Atmospheric Research, 1996-2005
-------------------------------------------------------------------------
*/
#include "define.h"
#include "spec.h"
/* -------------------------------------------------------------------- */
void CleanAndCopyData(DATASET_INFO *set, float out[])
{
size_t i;
float datum;
for (i = 0; i < set->nPoints; ++i)
{
datum = set->data[(set->head + i) % set->nPoints];
if (datum != set->missingValue)
out[i] = datum;
else
{ /* Linear interp missing values. */
size_t e;
float d1 = 0.0;
for (e = i; e < set->nPoints; ++e)
if ((d1 = set->data[(set->head + e) % set->nPoints]) != set->missingValue)
break;
for (; i < e; ++i)
out[i] = out[i-1] + ((d1 - out[i-1]) / (e - i + 1));
if (i > 0) --i;
}
}
} /* END CLEANANDCOPYDATA */
/* -------------------------------------------------------------------- */
void DiffPreFilter(DATASET_INFO *set, float out[])
{
size_t i;
for (i = set->nPoints-1; i > 0; --i)
if (isMissingValue(out[i], set->missingValue) || isMissingValue(out[i-1], set->missingValue))
out[i] = set->missingValue;
else
out[i] = out[i] - out[i-1];
out[0] = 0.0;
} /* END DIFFPREFILTER */
/* -------------------------------------------------------------------- */
void DetrendNone(DATASET_INFO *set, float out[])
{
} /* END DETRENDNONE */
/* -------------------------------------------------------------------- */
void DetrendMean(DATASET_INFO *set, float out[])
{
size_t i;
float datum;
for (i = 0; i < set->nPoints; ++i)
{
datum = out[i];
if (isMissingValue(datum, set->missingValue))
out[i] = out[i-1];
else
out[i] = datum - set->stats.mean;
}
} /* END DETRENDMEAN */
/* -------------------------------------------------------------------- */
void DetrendLinear(DATASET_INFO *set, float out[])
{
size_t i;
float datum, *out1;
out1 = new float[set->nPoints];
LinearLeastSquare(set, out, out1);
for (i = 0; i < set->nPoints; ++i)
{
datum = out[i];
if (isMissingValue(datum, set->missingValue))
out[i] = out[i-1];
else
out[i] = datum - out1[i];
}
delete [] out1;
} /* END DETRENDLINEAR */
/* END DETREND.C */