General Functions
Peak shapes
The following functions are useful when generating peaks with various shapes. See the examples for using them during peak fitting for instance.
Spectra.gaussiennes
— Function.gaussiennes(amplitude::Array{Float64},centre::Array{Float64},hwhm::Array{Float64},x::Array{Float64};style::String = "None")
gaussiennes, written in the plural french form there, is a function that allows to build gaussian peaks. The gaussian function used there is:
y = amplitude x exp(-ln(2) x [(x-centre)/hwhm]^2 )
You can enter the amplitude, centre and half-width at half-maximum (hwhm) values as arrays of float 64 (even containing one float value), without specifying style. hwhm is proportional to the standard deviation sigma:
hwhm= sqrt(2xln(2)) x sigma
that is used in a normal distribution (see function normal_dist).
Inputs
amplitude: Array{Float64}
peaks amplitudes
centre: Array{Float64}
peaks centres
hwhm: Array{Float64}
peaks half-width at middle heights (hwhm);
x: Array{Float64}
x axis values;
Options
style: ASCIIString = "None"
see examples below.
Outputs
y_calc: Array{Float64}
calculated y values
y_peaks: Array{Float64}
calculated y values of the different peaks.
Examples
To have four gaussian peaks centered at 800, 900, 1000 and 1100 cm-1 with hwhm of 50 cm-1 on a Raman spectrum, you will enter:
```julia-repl
julia> y_calc, y_peaks = gaussiennes([1.0,1.0,1.0,1.0], [800.0,900.0,1000.0,1100.0], [50.0,50.0,50.0,50.0], x)
```
and ypeaks will contain in 4 columns the 4 different y values of the peaks, and ycalc their sum (the total model). Now, if you want to calculate more complex models, such as for instance contructing how the Raman peaks of water vary with pressure, you might like to parametrize the variations of the peak parameters rather than just fitting each spectrum. This will provide more robust fits of the spectra, as you will fit them together, and will also force you to find the correct underlying mathematical assumption.
The gaussiennes function allows you to do that. If you specify style = "poly", you can enter arrays for the amplitudes, centres and half-widths at half-maximum (hwhm) of the peaks, with in each column the coefficients for the polynomial variations of this parameters. The second column of x will need to contain the second variable for those polynomial functions.
Let's say for instance that we have one peak at 900 cm-1 in a pure material. It's frequency seems to linearly shift with increasing the amount of hydrogen in this material, but it's intensity is non-linearly increasing, following a quadratic variation. It's width seems constant.
How to write that with gaussiennes? Well, first you need to construct a relevant x axis: first column contains the frequency, and the second one contains the chemical variable value. In our case, we want to model the peak between 800 and 1000 cm-1, for 1 wt% H. So we have an x array build like:
```julia-repl
julia> frequency = collect(800:1:1000)
julia> x = ones(length(frequency),2)
julia> x[:,1] = frequency[:]
julia> x[:,2] = 1.0
```
Ok, now lets build our y peaks:
```julia-repl
julia> amplitudes = [1.0 0.1 0.1]
julia> frequencies = [900.0 2.0]
julia> hwhm = 20.0
julia> y_calc, y_peaks = gaussiennes(amplitudes, frequencies, hwhm, x)
```
This should provide you how the shape of the peak is as a function of both the frequency and the chemical composition there. If you want to go further, you might just want to stick gaussiennes in a loop, and play with creating various peaks with changing the chemical parameter in the x[:,2] column!
Spectra.lorentziennes
— Function.lorentziennes(amplitude::Array{Float64},centre::Array{Float64},hwhm::Array{Float64},x::Array{Float64};style::String = "None")
Inputs
amplitude: Array{Float64}
peaks amplitudes
centre: Array{Float64}
peaks centres
hwhm: Array{Float64}
peaks half-width at middle heights (hwhm)
x: Array{Float64}
x axis values
Options
style: ASCIIString = "None", see examples in the gaussiennes documentation.
Outputs
y_calc: Array{Float64}
calculated y values
y_peaks: Array{Float64}
calculated y values of the different peaks.
Spectra.pearson7
— Function.pearson7(a1::Array{Float64},a2::Array{Float64},a3::Array{Float64},a4::Array{Float64},x::Array{Float64};style::String = "None")
a Pearson 7 peak with formula a1 ./ (1 + ((x-a2)./a3).^2 .* (2.0.^(1./a4) - 1.0))
Inputs
a1: Array{Float64}
parameter a1
a2: Array{Float64}
parameter a2
a3: Array{Float64}
parameter a3
a4: Array{Float64}
parameter a4
x: Array{Float64}
x axis values
Options
style: ASCIIString = "None", see examples in the gaussiennes documentation.
Outputs
y_calc: Array{Float64}
calculated y values
y_peaks: Array{Float64}
y values of the different peaks.
Spectra.pseudovoigts
— Function.pseudovoigts(amplitude::Array{Float64},centre::Array{Float64},hwhm::Array{Float64},lorentzian_fraction::Array{Float64},x::Array{Float64};style::String = "None")
A mixture of gaussian and lorentzian peaks.
Inputs
amplitude: Array{Float64}
peaks amplitudes
centre: Array{Float64}
peaks centres
hwhm: Array{Float64}
peaks half-width at middle heights (hwhm)
lorentzian_fraction: Array{Float64}
lorentzian fraction of the pseudovoigt function. Should be comprised between 0 and 1;
x: Array{Float64}
x axis values
Options
style: ASCIIString = "None", see examples in the gaussiennes documentation.
Outputs
y_calc: Array{Float64}
calculated y values
y_peaks: Array{Float64}
y values of the different peaks
Peak measurement
Spectra.peakmeas
— Function.peakmeas(x::Array{Float64,1}, y::Array{Float64,1}; smoothing = "yes", method = "savgol", window_length=5, polyorder=2, ese_y=1., y_smo_out=false)
The peakmeas function allows performing measurements of the position, width, intensity and centroïd of a dominant peak in a provided x-y signal.
It smooths the signal with a Savitzky-Golay filter prior to measuring the peak position, width and intensity. It is advised to check that the M and N values of the Savitzky-Golay filter are adequate for your problem before trusting the results from peakmeas. For that, just use the ysmoout option.
half-width at half-maximum are calculated as the width of the peak at half its maximum intensity. This calculation is not affected by any asumption of peak symmetry (no fitting is done).
Centroïd is calculated as sum(y./sum(y).*x).
Inputs
x: Array{Float64}
x values
y: Array{Float64}
y values
Options
smoothing, String
triggers the smoothing of the spectrum if set to yes (default value);
filter, Symbol
the filter that will be used. See the smooth function documentation;
M=5, Int
M parameter for smoothing y with a Savitzky-Golay filter. See smooth function documentation;
N=2, Int
M parameter for smoothing y with a Savitzky-Golay filter. See smooth function documentation;
y_smo_out=false
Outputs the smoothed signal.
Outputs
intensity: Float64
peak intensity
position: Float64
peak position
hwhm: Float64
peak half-width at half-maximum
centroïd: Float64
peak centroid
Integration
Spectra.jl provides functions that allow one to integrate the area under a region of a spectrum, or to calculate the area under Gaussian, Lorentzian or other bands.
Spectra.trapz
— Function.trapz(x::Vector{Tx}, y::Vector{Ty}) where {Tx <: Number, Ty <: Number}
Trapezoidal integration. This function is particularly helpful to calculate the area under a portion of a spectrum, and can be used for various purposes (normalisation, area comparison, etc.).
Inputs
x: Vector{Float64}
x values
y: Vector{Float64}
y values.
Outputs
area: Vector{Float64}
trapezoidal integration value.
Spectra.bandarea
— Function.bandarea(Amplitude::Array{Float64},HWHM::Array{Float64}; peak_shape = "Gaussian", error_switch = "no", eseAmplitude::Array{Float64} = [0.0], eseHWHM::Array{Float64} = [0.0])
This function allows calculating the area under a specific band, with different shapes. For now, only Gaussian bands are supported, but other band shapes will be added soon.
Inputs
Amplitude: Array{Float64}
peak amplitude
HWHM: Array{Float64}
peak half width at half maximum
Options
peak_shape: String
shape of the peak. Only "Gaussian" is supported for now
error_switch: String
should be "yes" or "no". If "yes", the arrays containing the errors affecting the band amplitude and widhts should be provided in eseAmplitude and eseHWHM (see below);
eseAmplitude: Array{Float64}
array containing the errors affecting Amplitude
eseHWHM: Array{Float64}
array containing the errors affecting HWHM;
Outputs
area: Array{Float64}
array containing peak areas
if error_switch is set to "yes", then a second output is provided:
esearea: Array{Float64}
array that contains the propagated errors affecting the areas calculations.
Polynomial
Spectra.poly
— Function.poly(p::Vector{Float64},x::Array{Float64})
This function just allows to build a polynomial curve.
Inputs
p: Vector{Float64}
polynomial parameters. For a linear curve, p = [1.0,1.0], for a second order polynomial, p = [1.0,1.0,1.0], etc.;
x: Array{Float64}
x values for calculation.
Outputs
y: Array{Float64}, containing the result of calculation.