Title: | Linear Combination Fitting |
---|---|
Description: | Baseline correction, normalization and linear combination fitting (LCF) of X-ray absorption near edge structure (XANES) spectra. The package includes data loading of .xmu files exported from 'ATHENA' (Ravel and Newville, 2005) <doi:10.1107/S0909049505012719>. Loaded spectra can be background corrected and all standards can be fitted at once. Two linear combination fitting functions can be used: (1) fit_athena(): Simply fitting combinations of standards as in ATHENA, (2) fit_float(): Fitting all standards with changing baseline correction and edge-step normalization parameters. |
Authors: | Florian Werner [aut, cre] |
Maintainer: | Florian Werner <[email protected]> |
License: | GPL-3 |
Version: | 1.7.0 |
Built: | 2024-12-09 06:31:33 UTC |
Source: | CRAN |
This function allows you to base-line correct and edge-step normalize XANES spectra (background correction). Raw spectra are loaded, then base-line corrected and edge-step normalized. The spectrum is flattened after E0. The function returns the corrected, normalized and flattened spectrum
bkg_corr(raw.spec, corr.norm)
bkg_corr(raw.spec, corr.norm)
raw.spec |
Raw spectrum |
corr.norm |
Vector of the base-line correction and edge-step normalization values (vector of length 4) |
data(stdmix) corr.spec.samples <- initial_load(specdat[5:8], corr.norm = c(-36, -15, 37, 58)) corr.spec <- bkg_corr(raw.spec = corr.spec.samples[[1]], corr.norm = c(-36, -15, 37, 58)) print(corr.spec)
data(stdmix) corr.spec.samples <- initial_load(specdat[5:8], corr.norm = c(-36, -15, 37, 58)) corr.spec <- bkg_corr(raw.spec = corr.spec.samples[[1]], corr.norm = c(-36, -15, 37, 58)) print(corr.spec)
The function can be used to check which combinations of standards produce a good fit and if output from 'ATHENA' is similar.
fit_athena(all.samples, all.standards, LC.vals, amoSTD, ex.smaller = NULL, file.output = NULL, best.fits = NULL)
fit_athena(all.samples, all.standards, LC.vals, amoSTD, ex.smaller = NULL, file.output = NULL, best.fits = NULL)
all.samples |
List of all samples |
all.standards |
List of all standards |
LC.vals |
The fitting range values for the linear combination fitting |
amoSTD |
Use at most X standards |
ex.smaller |
Exclude portions smaller than a given value (decimal form), default to NULL |
file.output |
Possibility to have a file output, default to NULL |
best.fits |
Possibility to output more than the best fit (e.g. the first 10 best fits), default to 1 |
data(stdmix) corr.spec.standards <- initial_load(specdat[1:4], corr.norm = c(-36, -15, 37, 58)) corr.spec.samples <- initial_load(specdat[5:8], corr.norm = c(-36, -15, 37, 58)) athena.fit <- fit_athena(all.samples = corr.spec.samples, all.standards = corr.spec.standards, LC.vals = c(-14, 46), amoSTD = 4) ## exclude portions smaller 5% = 0.05 athena.fit.exlcude <- fit_athena(all.samples = corr.spec.samples, all.standards = corr.spec.standards, LC.vals = c(-14, 46), amoSTD = 4, ex.smaller = 0.05)
data(stdmix) corr.spec.standards <- initial_load(specdat[1:4], corr.norm = c(-36, -15, 37, 58)) corr.spec.samples <- initial_load(specdat[5:8], corr.norm = c(-36, -15, 37, 58)) athena.fit <- fit_athena(all.samples = corr.spec.samples, all.standards = corr.spec.standards, LC.vals = c(-14, 46), amoSTD = 4) ## exclude portions smaller 5% = 0.05 athena.fit.exlcude <- fit_athena(all.samples = corr.spec.samples, all.standards = corr.spec.standards, LC.vals = c(-14, 46), amoSTD = 4, ex.smaller = 0.05)
This function allows to process all samples, especially written for the float environment.
fit_float(all.samples, all.standards, LC.vals, float, ex.smaller = NULL, file.output = NULL, best.fits = NULL)
fit_float(all.samples, all.standards, LC.vals, float, ex.smaller = NULL, file.output = NULL, best.fits = NULL)
all.samples |
List of all samples |
all.standards |
List of all standards |
LC.vals |
The fitting range values for the linear combination fitting |
float |
Let vary the energy range paramerters |
ex.smaller |
Exclude portions smaller than a given value (decimal form), default to NULL |
file.output |
Possibility to have a file output, default to NULL |
best.fits |
Possibility to output more than the best fit (e.g. the first 10 best fits), default to 1 |
data(stdmix) corr.spec.standards <- initial_load(specdat[1:4], corr.norm = c(-36, -15, 37, 58)) corr.spec.samples <- initial_load(specdat[5:8], corr.norm = c(-36, -15, 37, 58)) ## Select parameters for baseline correction and edge-step normalization param.float <- expand.grid(pre.adj.1 = seq(-42,-30,6), pre.adj.2 = seq(-19,-9,5), post.adj.1 = seq(35,40,5), post.adj.2 = seq(50,65,5)) length(param.float[,1]) float.fit <- fit_float(all.samples = corr.spec.samples, all.standards = corr.spec.standards, LC.vals = c(-14, 46), float = param.float, best.fits = 20) print(float.fit) ###### Using next configuration can be very time consuming param.float.2 <- expand.grid(pre.adj.1 = seq(-43,-30,1), pre.adj.2 = seq(-19,-9,.5), post.adj.1 = seq(34,40,.5), post.adj.2 = seq(50,65,1))
data(stdmix) corr.spec.standards <- initial_load(specdat[1:4], corr.norm = c(-36, -15, 37, 58)) corr.spec.samples <- initial_load(specdat[5:8], corr.norm = c(-36, -15, 37, 58)) ## Select parameters for baseline correction and edge-step normalization param.float <- expand.grid(pre.adj.1 = seq(-42,-30,6), pre.adj.2 = seq(-19,-9,5), post.adj.1 = seq(35,40,5), post.adj.2 = seq(50,65,5)) length(param.float[,1]) float.fit <- fit_float(all.samples = corr.spec.samples, all.standards = corr.spec.standards, LC.vals = c(-14, 46), float = param.float, best.fits = 20) print(float.fit) ###### Using next configuration can be very time consuming param.float.2 <- expand.grid(pre.adj.1 = seq(-43,-30,1), pre.adj.2 = seq(-19,-9,.5), post.adj.1 = seq(34,40,.5), post.adj.2 = seq(50,65,1))
This function loads and initially normalizes the raw spectra. Output is a list with the raw and initially corrected spectra.
initial_load(raw.spec = NULL, file = NULL, corr.norm, use.eshift = NULL)
initial_load(raw.spec = NULL, file = NULL, corr.norm, use.eshift = NULL)
raw.spec |
List of files already loaded with read_raw_spec() |
file |
Vector with file names |
corr.norm |
Initial correction and normalization parameters |
use.eshift |
Set TRUE, if using energy shift value, defaults to NULL |
data(stdmix) corr.spec.standards <- initial_load(specdat[1:4], corr.norm = c(-36, -15, 37, 58)) corr.spec.samples <- initial_load(specdat[5:8], corr.norm = c(-36, -15, 37, 58))
data(stdmix) corr.spec.standards <- initial_load(specdat[1:4], corr.norm = c(-36, -15, 37, 58)) corr.spec.samples <- initial_load(specdat[5:8], corr.norm = c(-36, -15, 37, 58))
This function performs the LC fitting of the input sample/samples. It outputs the fitting results with the R-Factors as fitting statistics.
LC_fit(sample, standards, LC.vals, float = NULL, E.zero = NULL, ex.smaller = NULL)
LC_fit(sample, standards, LC.vals, float = NULL, E.zero = NULL, ex.smaller = NULL)
sample |
The sample spectrum |
standards |
The standards spectra |
LC.vals |
Values for ranges of linear combination fitting, with respect to the edge-step |
float |
Set float parameters, defaults to NULL |
E.zero |
Set E0, defaults to NULL |
ex.smaller |
Set value to exclude small portions (as portion of 1), defaults to NULL |
data(stdmix) corr.spec.standards <- initial_load(specdat[1:4], corr.norm = c(-36, -15, 37, 58)) corr.spec.samples <- initial_load(specdat[5:8], corr.norm = c(-36, -15, 37, 58)) fit.standards <- std_df(sample = corr.spec.samples[[1]], all.standards = corr.spec.standards) fit.result <- LC_fit(sample = corr.spec.samples[[1]], standards = fit.standards, LC.vals = c(-14, 46)) print(fit.result)
data(stdmix) corr.spec.standards <- initial_load(specdat[1:4], corr.norm = c(-36, -15, 37, 58)) corr.spec.samples <- initial_load(specdat[5:8], corr.norm = c(-36, -15, 37, 58)) fit.standards <- std_df(sample = corr.spec.samples[[1]], all.standards = corr.spec.standards) fit.result <- LC_fit(sample = corr.spec.samples[[1]], standards = fit.standards, LC.vals = c(-14, 46)) print(fit.result)
Quadratic programming solution function for linear combination fitting (LCF)
LCF_solve_QP(LCF.stds, LCF.samp)
LCF_solve_QP(LCF.stds, LCF.samp)
LCF.stds |
Standards for LCF |
LCF.samp |
Sample for LCF |
data(stdmix) corr.spec.standards <- initial_load(specdat[1:4], corr.norm = c(-36, -15, 37, 58)) corr.spec.samples <- initial_load(specdat[5:8], corr.norm = c(-36, -15, 37, 58)) fit.standards <- std_df(sample = corr.spec.samples[[1]], all.standards = corr.spec.standards) corr.spec <- bkg_corr(raw.spec = corr.spec.samples[[1]], corr.norm = c(-36, -15, 37, 58)) ## set fitting range parameters relative to E zero E.zero <- corr.spec.samples[[1]]$data$E0 LC.pre <- -14 LC.post <- 46 ## find ranges that have to be fitted abs.pre <- abs(corr.spec[["energy"]]-(E.zero+LC.pre)) abs.post <- abs(corr.spec[["energy"]]-(E.zero+LC.post)) range.pre <- which(abs.pre == min(abs.pre)) range.post <- which(abs.post == min(abs.post)) ## extract standards and sample in given range LC.sample <- corr.spec["cor.absorption"][range.pre:range.post,] LC.standards <- fit.standards[range.pre:range.post,] ## actual fitting fit.result <- LCF_solve_QP(LCF.stds = LC.standards, LCF.samp = LC.sample) print(fit.result)
data(stdmix) corr.spec.standards <- initial_load(specdat[1:4], corr.norm = c(-36, -15, 37, 58)) corr.spec.samples <- initial_load(specdat[5:8], corr.norm = c(-36, -15, 37, 58)) fit.standards <- std_df(sample = corr.spec.samples[[1]], all.standards = corr.spec.standards) corr.spec <- bkg_corr(raw.spec = corr.spec.samples[[1]], corr.norm = c(-36, -15, 37, 58)) ## set fitting range parameters relative to E zero E.zero <- corr.spec.samples[[1]]$data$E0 LC.pre <- -14 LC.post <- 46 ## find ranges that have to be fitted abs.pre <- abs(corr.spec[["energy"]]-(E.zero+LC.pre)) abs.post <- abs(corr.spec[["energy"]]-(E.zero+LC.post)) range.pre <- which(abs.pre == min(abs.pre)) range.post <- which(abs.post == min(abs.post)) ## extract standards and sample in given range LC.sample <- corr.spec["cor.absorption"][range.pre:range.post,] LC.standards <- fit.standards[range.pre:range.post,] ## actual fitting fit.result <- LCF_solve_QP(LCF.stds = LC.standards, LCF.samp = LC.sample) print(fit.result)
This function allows plotting (png or tiff image files) of the corrected sample spectrum, the linear combination fit and the residual.
plot_LCF(all.samples, all.standards, LCF.res, LC.vals, corr.norm, float = NULL, exclude = NULL, use.tiff = NULL, E.zero = NULL, set.plot.ymax = NULL, file.output = NULL)
plot_LCF(all.samples, all.standards, LCF.res, LC.vals, corr.norm, float = NULL, exclude = NULL, use.tiff = NULL, E.zero = NULL, set.plot.ymax = NULL, file.output = NULL)
all.samples |
List of all samples |
all.standards |
List of all standards |
LCF.res |
Results from function fit_float() |
LC.vals |
The fitting range values for the linear combination fitting |
corr.norm |
Vector of the base-line correction and edge-step normalization values (vector of length 4) |
float |
Logical, default to FALSE |
exclude |
Logical, default to FALSE |
use.tiff |
Logical, default to FALSE |
E.zero |
Set E0, defaults to NULL |
set.plot.ymax |
Set maximum of plot y axis, defaults to NULL |
file.output |
Logical, default to FALSE |
data(stdmix) corr.spec.standards <- initial_load(specdat[1:4], corr.norm = c(-36, -15, 37, 58)) corr.spec.samples <- initial_load(specdat[5:8], corr.norm = c(-36, -15, 37, 58)) param.float <- expand.grid(pre.adj.1 = seq(-45,-30,5), pre.adj.2 = seq(-19,-9,5), post.adj.1 = seq(34,40,2), post.adj.2 = seq(50,65,5)) float.fit <- fit_float(all.samples = corr.spec.samples[1], all.standards = corr.spec.standards, LC.vals = c(-14, 46), float = param.float, ex.smaller = 0.05) par(pty="s") plot_LCF(all.samples = corr.spec.samples[1], all.standards = corr.spec.standards, LCF.res = float.fit[1,], LC.vals = c(-14,46), corr.norm = c(-36, -15, 37, 58))
data(stdmix) corr.spec.standards <- initial_load(specdat[1:4], corr.norm = c(-36, -15, 37, 58)) corr.spec.samples <- initial_load(specdat[5:8], corr.norm = c(-36, -15, 37, 58)) param.float <- expand.grid(pre.adj.1 = seq(-45,-30,5), pre.adj.2 = seq(-19,-9,5), post.adj.1 = seq(34,40,2), post.adj.2 = seq(50,65,5)) float.fit <- fit_float(all.samples = corr.spec.samples[1], all.standards = corr.spec.standards, LC.vals = c(-14, 46), float = param.float, ex.smaller = 0.05) par(pty="s") plot_LCF(all.samples = corr.spec.samples[1], all.standards = corr.spec.standards, LCF.res = float.fit[1,], LC.vals = c(-14,46), corr.norm = c(-36, -15, 37, 58))
This function reads the raw .xmu file, extracts E0 and energy shift and returns a list with name, E0, energy shift, and the raw spectrum
read_raw_spec(file, use.eshift = NULL)
read_raw_spec(file, use.eshift = NULL)
file |
The raw .xmu file |
use.eshift |
Set TRUE, if using energy shift value, defaults to NULL |
## any .xmu file as output from ATHENA (>=0.9.25)
## any .xmu file as output from ATHENA (>=0.9.25)
Data from an experiment on the quality of XANES spectrscopy for phosphorus speciation
data(stdmix)
data(stdmix)
List of four standard spectra comounds and four spectra of mixtures of these standard compounds, output objects of function read_raw_spec().
Werner & Prietzel 2015, Environ. Sci. Technol. (49), 10521-10528 (DOI)
data(stdmix) specdat[[1]] specdat[[5]]
data(stdmix) specdat[[1]] specdat[[5]]
The function creates a data frame with all standards in one data frame. The data frame has the same energy values as the sample that is loaded.
std_df(sample, all.standards)
std_df(sample, all.standards)
sample |
A raw sample |
all.standards |
List of all standards |
data(stdmix) corr.spec.standards <- initial_load(specdat[1:4], corr.norm = c(-36, -15, 37, 58)) corr.spec.samples <- initial_load(specdat[5:8], corr.norm = c(-36, -15, 37, 58)) fit.standards <- std_df(sample = corr.spec.samples[[1]], all.standards = corr.spec.standards) print(fit.standards)
data(stdmix) corr.spec.standards <- initial_load(specdat[1:4], corr.norm = c(-36, -15, 37, 58)) corr.spec.samples <- initial_load(specdat[5:8], corr.norm = c(-36, -15, 37, 58)) fit.standards <- std_df(sample = corr.spec.samples[[1]], all.standards = corr.spec.standards) print(fit.standards)