@@ -14,90 +14,95 @@ continuum_removal <- function(mat_init, spectral_bands, p = NULL) {
1414
1515 # Filter and prepare data prior to continuum removal
1616 cr_data <- filter_prior_cr(mat_init , spectral_bands )
17- mat_init <- cr_data $ mat_init
18- nb_bands <- dim(mat_init )[2 ]
19- cr_data $ mat_init <- c()
20- spectral_bands <- cr_data $ spectral_bands
17+ nb_bands <- dim(cr_data $ mat_init )[2 ]
2118 nb_samples <- cr_data $ nb_samples
19+ # mat_init <- cr_data$mat_init
20+ # cr_data$mat_init <- c()
21+ # spectral_bands <- cr_data$spectral_bands
2222 nb_samples_update <- length(cr_data $ samples_to_keep )
23+
2324 # if samples to be considered
2425 if (nb_samples > 0 ) {
25- # initialization:
26- # spectral band corresponding to each element of the data matrix
27- lambda <- repmat(matrix (spectral_bands , nrow = 1 ), nb_samples_update , 1 )
28- # prepare matrices used to check evolution of the CR process
29- # - elements still not processed through continuum removal: initialization to 1
30- still_need_cr <- matrix (1 , nrow = nb_samples_update , ncol = nb_bands )
31- # - value of the convex hull: initially set to 0
32- convex_hull <- matrix (0 , nrow = nb_samples_update , ncol = nb_bands )
33- # - reflectance value for latest interception with convex hull:
34- # initialization to value of the first reflectance measurement
35- intercept_hull <- repmat(matrix (mat_init [, 1 ], ncol = 1 ), 1 , nb_bands )
36- # - spectral band of latest interception
37- latest_intercept <- repmat(X = matrix (spectral_bands [1 ], ncol = 1 ),
38- m = nb_samples_update , n = nb_bands )
39- # number of spectral bands found as intercept
40- nb_intercept <- 0
41- # continues until arbitrary stopping criterion:
42- # stops when reach last spectral band (all values before last = 0)
43- # while (max(still_need_cr[, seq_len(nb_bands - 2)]) == 1 & (nb_intercept <= (nb_bands / 2))) {
44- while (max(still_need_cr [, seq_len((nb_bands - 2 ))]) == 1 ) {
45- nb_intercept <- nb_intercept + 1
46- # identify samples still needing continuum removal
47- sel <- which(still_need_cr [,(nb_bands - 2 )]== 1 )
48- # update variables to process samples needing CR only
49- nb_samples_update_tmp <- length(sel )
50- lambda_tmp <- lambda [sel ,]
51- mat_init_tmp <- mat_init [sel ,]
52- latest_intercept_tmp <- latest_intercept [sel ,]
53- still_need_cr_tmp <- still_need_cr [sel ,]
54- convex_hull_tmp <- convex_hull [sel ,]
55- intercept_hull_tmp <- intercept_hull [sel ,]
56- # Mstep give the position of the values to be updated
57- update_data <- matrix (1 , nrow = nb_samples_update_tmp , ncol = nb_bands )
58- update_data [, nb_bands ] <- 0
59- # initial step: first column set to 0; following steps: all bands below
60- # max of the convex hull are set to 0
61- update_data [which((lambda_tmp - latest_intercept_tmp ) < 0 )] <- 0
62- # compute slope for each coordinate
63- slope <- as.matrix((mat_init_tmp - intercept_hull_tmp ) / (lambda_tmp - latest_intercept_tmp ) * still_need_cr_tmp )
64- # set current spectral band and previous bands to -9999
65- if (! length(which(still_need_cr_tmp == 0 )) == 0 ) {
66- slope [which(still_need_cr_tmp == 0 )] <- - 9999
67- }
68- if (! length(which(is.na(slope ))) == 0 ) {
69- slope [which(is.na(slope ))] <- - 9999
70- }
71- # get max index for each row and convert into linear index
72- index_max_slope <- RowToLinear(max.col(slope , ties.method = " last" ),
73- nb_samples_update_tmp , nb_bands )
74- # !!!! OPTIM: replace repmat with column operation
75- # update coordinates of latest intercept
76- latest_intercept_tmp <- repmat(matrix (lambda_tmp [index_max_slope ],
77- ncol = 1 ), 1 , nb_bands )
78- # update latest intercept
79- intercept_hull_tmp <- repmat(matrix (as.matrix(mat_init_tmp )[index_max_slope ],
80- ncol = 1 ), 1 , nb_bands )
81- # values corresponding to the domain between the two continuum maxima
82- update_data [which((lambda_tmp - latest_intercept_tmp ) > = 0 |
83- latest_intercept_tmp == spectral_bands [nb_bands ])] <- 0
84- # values to eliminate for the next analysis: all spectral bands before latest intercept
85- still_need_cr_tmp [which((lambda_tmp - latest_intercept_tmp ) < 0 )] <- 0
86- # the max slope is known, as well as the coordinates of the beginning and ending
87- # a matrix now has to be built
88- convex_hull_tmp <- convex_hull_tmp +
89- update_data * (intercept_hull_tmp + sweep((lambda_tmp - latest_intercept_tmp ),
90- 1 , slope [index_max_slope ], " *" ))
91- # update variables
92- convex_hull [sel ,] <- convex_hull_tmp
93- still_need_cr [sel ,] <- still_need_cr_tmp
94- lambda [sel ,] <- lambda_tmp
95- latest_intercept [sel ,] <- latest_intercept_tmp
96- intercept_hull [sel ,] <- intercept_hull_tmp
97- }
98- cr_results0 <- mat_init [, 2 : (nb_bands - 2 )] / convex_hull [, 2 : (nb_bands - 2 )]
99- cr_results <- matrix (0 , ncol = (nb_bands - 3 ), nrow = nb_samples )
100- cr_results [cr_data $ samples_to_keep , ] <- as.matrix(cr_results0 )
26+ cr_results <- matrix (0 , ncol = (nb_bands - 2 ), nrow = nb_samples )
27+ cr_results0 <- continuumRemoval(X = cr_data $ mat_init ,
28+ wav = cr_data $ spectral_bands )
29+ cr_results [cr_data $ samples_to_keep , ] <- as.matrix(cr_results0 [,2 : (nb_bands - 1 )])
30+ # # initialization:
31+ # # spectral band corresponding to each element of the data matrix
32+ # lambda <- repmat(matrix(spectral_bands, nrow = 1), nb_samples_update, 1)
33+ # # prepare matrices used to check evolution of the CR process
34+ # # - elements still not processed through continuum removal: initialization to 1
35+ # still_need_cr <- matrix(1, nrow = nb_samples_update, ncol = nb_bands)
36+ # # - value of the convex hull: initially set to 0
37+ # convex_hull <- matrix(0, nrow = nb_samples_update, ncol = nb_bands)
38+ # # - reflectance value for latest interception with convex hull:
39+ # # initialization to value of the first reflectance measurement
40+ # intercept_hull <- repmat(matrix(mat_init[, 1], ncol = 1), 1, nb_bands)
41+ # # - spectral band of latest interception
42+ # latest_intercept <- repmat(X = matrix(spectral_bands[1], ncol = 1),
43+ # m = nb_samples_update, n = nb_bands)
44+ # # number of spectral bands found as intercept
45+ # nb_intercept <- 0
46+ # # continues until arbitrary stopping criterion:
47+ # # stops when reach last spectral band (all values before last = 0)
48+ # # while (max(still_need_cr[, seq_len(nb_bands - 2)]) == 1 & (nb_intercept <= (nb_bands / 2))) {
49+ # while (max(still_need_cr[, seq_len((nb_bands - 2))]) == 1) {
50+ # nb_intercept <- nb_intercept + 1
51+ # # identify samples still needing continuum removal
52+ # sel <- which(still_need_cr[,(nb_bands-2)]==1)
53+ # # update variables to process samples needing CR only
54+ # nb_samples_update_tmp <- length(sel)
55+ # lambda_tmp <- lambda[sel,]
56+ # mat_init_tmp <- mat_init[sel,]
57+ # latest_intercept_tmp <- latest_intercept[sel,]
58+ # still_need_cr_tmp <- still_need_cr[sel,]
59+ # convex_hull_tmp <- convex_hull[sel,]
60+ # intercept_hull_tmp <- intercept_hull[sel,]
61+ # # Mstep give the position of the values to be updated
62+ # update_data <- matrix(1, nrow = nb_samples_update_tmp, ncol = nb_bands)
63+ # update_data[, nb_bands] <- 0
64+ # # initial step: first column set to 0; following steps: all bands below
65+ # # max of the convex hull are set to 0
66+ # update_data[which((lambda_tmp - latest_intercept_tmp) < 0)] <- 0
67+ # # compute slope for each coordinate
68+ # slope <- as.matrix((mat_init_tmp - intercept_hull_tmp) / (lambda_tmp - latest_intercept_tmp) * still_need_cr_tmp)
69+ # # set current spectral band and previous bands to -9999
70+ # if (!length(which(still_need_cr_tmp == 0)) == 0) {
71+ # slope[which(still_need_cr_tmp == 0)] <- -9999
72+ # }
73+ # if (!length(which(is.na(slope))) == 0) {
74+ # slope[which(is.na(slope))] <- -9999
75+ # }
76+ # # get max index for each row and convert into linear index
77+ # index_max_slope <- RowToLinear(max.col(slope, ties.method = "last"),
78+ # nb_samples_update_tmp, nb_bands)
79+ # # !!!! OPTIM: replace repmat with column operation
80+ # # update coordinates of latest intercept
81+ # latest_intercept_tmp <- repmat(matrix(lambda_tmp[index_max_slope],
82+ # ncol = 1), 1, nb_bands)
83+ # # update latest intercept
84+ # intercept_hull_tmp <- repmat(matrix(as.matrix(mat_init_tmp)[index_max_slope],
85+ # ncol = 1), 1, nb_bands)
86+ # # values corresponding to the domain between the two continuum maxima
87+ # update_data[which((lambda_tmp - latest_intercept_tmp) >= 0 |
88+ # latest_intercept_tmp == spectral_bands[nb_bands])] <- 0
89+ # # values to eliminate for the next analysis: all spectral bands before latest intercept
90+ # still_need_cr_tmp[which((lambda_tmp - latest_intercept_tmp) < 0)] <- 0
91+ # # the max slope is known, as well as the coordinates of the beginning and ending
92+ # # a matrix now has to be built
93+ # convex_hull_tmp <- convex_hull_tmp +
94+ # update_data * (intercept_hull_tmp + sweep((lambda_tmp - latest_intercept_tmp),
95+ # 1, slope[index_max_slope], "*"))
96+ # # update variables
97+ # convex_hull[sel,] <- convex_hull_tmp
98+ # still_need_cr[sel,] <- still_need_cr_tmp
99+ # lambda[sel,] <- lambda_tmp
100+ # latest_intercept[sel,] <- latest_intercept_tmp
101+ # intercept_hull[sel,] <- intercept_hull_tmp
102+ # }
103+ # cr_results0 <- mat_init[, 2:(nb_bands - 2)] / convex_hull[, 2:(nb_bands-2)]
104+ # cr_results <- matrix(0, ncol = (nb_bands - 3), nrow = nb_samples)
105+ # cr_results[cr_data$samples_to_keep, ] <- as.matrix(cr_results0)
101106 } else {
102107 cr_results <- matrix (0 , ncol = (nb_bands - 3 ), nrow = nb_samples )
103108 }
0 commit comments