miwhitatzb1.R 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # Author: A.Lobo 2012 [email protected]
  2. # Date : August 2012
  3. # slightly modified by Matteo Mattiuzzi
  4. miwhitatzb2 <- function(orgTS,w=NULL, l=10, f=2, minval=-3000, maxval=10000, minlength=80, maxiter=3, ver=F)
  5. {
  6. # Modified Whittaker smoother
  7. # according to Atzberger & Eilers 2011 International Journal of Digital Earth 4(5):365-386.
  8. # A.Lobo 2012 [email protected]
  9. # Input from Clement Atzberger <[email protected]>
  10. # see whittaker_smoother.m
  11. # orgTS : the time series to be filtered. It is assumed that the
  12. # observations are equally spaced. Missing values and
  13. # flag values should be either coded as NA or assigned
  14. # a value < minVAL or > maxVAL
  15. #
  16. # l : smoothing parameter. Allowed are (non-integer)
  17. # values > 0.
  18. #
  19. # f : differences for opimization in filter (i.e. f=2)
  20. #
  21. # minval : scalar, indicating which values in the input time
  22. # series are valid (i.e. orgTS >= minval),
  23. # respectively, not valid (i.e. orgTS < minval). The
  24. # value of [minVAL] must be specified in the scaling of
  25. # the input time series [orgTS]. If the smoother
  26. # yields an output < minVAL, this element is set to
  27. # minval
  28. #
  29. # maxval : scalar, indicating which values in the input time
  30. # series are valid (i.e. orgTS <= maxval),
  31. # respectively, not valid (i.e. orgTS > maxval). The
  32. # value of [maxVAL] must be specified in the scaling of
  33. # the input time series [orgTS]. If the smoother
  34. # yields an output > maxVAL, this element is set to
  35. # maxval
  36. #
  37. # minlength : minimum number of valid observations in [orgTS] for
  38. # that a filtering is performed. Not valid observations
  39. # are those (1) value = NA, (2) value < minval, or (3)
  40. # value > maxval. [minlength] should be a scalar
  41. # (usually an integer). If the number of valid
  42. # observations is lower than the specified minimum
  43. # requirement, [filteredTS] = [orgTS] ... that is no
  44. # filtering is performed
  45. #
  46. # maxiter : scalar (integer) indicating the number of iterations
  47. # to be performed. If maxiter = 1, only a least square
  48. # fit is performed. If maxiter > 1, the upper envelope
  49. # of the input time series [orgTS] is fitted
  50. #
  51. ##########################################################################
  52. # Replacement of not valid measurements by NA (.. this should also include
  53. # flag values)
  54. orgTS[orgTS > maxval] <- NA
  55. orgTS[orgTS < minval] <- NA
  56. # prepare weights
  57. if (is.null(w))
  58. {
  59. w <- orgTS * 0 + 1
  60. }
  61. nas <- is.na(orgTS)
  62. w[nas] <- 0
  63. orgTS[nas] <- 0 # whit1() does not accept NAs !!! but value will be ignored because of miw
  64. for(i in 1:maxiter)
  65. {
  66. filteredTS <- whit2(orgTS,w=w,lambda=l)
  67. #The modification according to whittaker_smoother.m:
  68. #"Finds x positions were measured values are lower than the fitted
  69. #values. These positions are than set to the fitted values at these places"
  70. orgTS[orgTS<filteredTS] <- filteredTS[orgTS<filteredTS]
  71. }
  72. # All filtered values lower than minval or larger than maxval are set to
  73. # these limits
  74. filteredTS[filteredTS > maxval & !is.na(filteredTS)] = maxval
  75. filteredTS[filteredTS < minval & !is.na(filteredTS)] = minval
  76. return(filteredTS)
  77. }
  78. miwhitatzb1 <- function(orgTS,w=NULL, l=10, f=2, minval=-3000, maxval=10000, minlength=80, maxiter=3, ver=F)
  79. {
  80. # Modified Whittaker smoother
  81. # according to Atzberger & Eilers 2011 International Journal of Digital Earth 4(5):365-386.
  82. # A.Lobo 2012 [email protected]
  83. # Input from Clement Atzberger <[email protected]>
  84. # see whittaker_smoother.m
  85. ########################################################################
  86. #
  87. # orgTS : the time series to be filtered. It is assumed that the
  88. # observations are equally spaced. Missing values and
  89. # flag values should be either coded as NA or assigned
  90. # a value < minVAL or > maxVAL
  91. #
  92. # l : smoothing parameter. Allowed are (non-integer)
  93. # values > 0.
  94. #
  95. # f : differences for opimization in filter (i.e. f=2)
  96. #
  97. # minval : scalar, indicating which values in the input time
  98. # series are valid (i.e. orgTS >= minval),
  99. # respectively, not valid (i.e. orgTS < minval). The
  100. # value of [minVAL] must be specified in the scaling of
  101. # the input time series [orgTS]. If the smoother
  102. # yields an output < minVAL, this element is set to
  103. # minval
  104. #
  105. # maxval : scalar, indicating which values in the input time
  106. # series are valid (i.e. orgTS <= maxval),
  107. # respectively, not valid (i.e. orgTS > maxval). The
  108. # value of [maxVAL] must be specified in the scaling of
  109. # the input time series [orgTS]. If the smoother
  110. # yields an output > maxVAL, this element is set to
  111. # maxval
  112. #
  113. # minlength : minimum number of valid observations in [orgTS] for
  114. # that a filtering is performed. Not valid observations
  115. # are those (1) value = NA, (2) value < minval, or (3)
  116. # value > maxval. [minlength] should be a scalar
  117. # (usually an integer). If the number of valid
  118. # observations is lower than the specified minimum
  119. # requirement, [filteredTS] = [orgTS] ... that is no
  120. # filtering is performed
  121. #
  122. # maxiter : scalar (integer) indicating the number of iterations
  123. # to be performed. If maxiter = 1, only a least square
  124. # fit is performed. If maxiter > 1, the upper envelope
  125. # of the input time series [orgTS] is fitted
  126. #
  127. ##########################################################################
  128. # Replacement of not valid measurements by NA (.. this should also include
  129. # flag values)
  130. orgTS[orgTS > maxval] <- NA
  131. orgTS[orgTS < minval] <- NA
  132. # prepare weights
  133. if (is.null(w))
  134. {
  135. w <- orgTS * 0 + 1
  136. }
  137. nas <- is.na(orgTS)
  138. w[nas] <- 0
  139. orgTS[nas] <- 0 # whit1() does not accept NAs !!! but value will be ignored because of miw
  140. for(i in 1:maxiter)
  141. {
  142. filteredTS <- whit1(orgTS,w=w,lambda=l)
  143. #The modification according to whittaker_smoother.m:
  144. #"Finds x positions were measured values are lower than the fitted
  145. #values. These positions are than set to the fitted values at these places"
  146. orgTS[orgTS<filteredTS] <- filteredTS[orgTS<filteredTS]
  147. }
  148. # All filtered values lower than minval or larger than maxval are set to
  149. # these limits
  150. filteredTS[filteredTS > maxval & !is.na(filteredTS)] = maxval
  151. filteredTS[filteredTS < minval & !is.na(filteredTS)] = minval
  152. return(filteredTS)
  153. }