Message Boards Message Boards

GROUPS:

Optimize the code (Shirley algorithm for XPS data)?

Posted 10 months ago
443 Views
|
0 Replies
|
0 Total Likes
|

Hello dear Community,

Recently I've been working on creating a complex program for XPS analysis and i realized I have trouble making the code for shirley background removal optimized (the code is pasted below). First of all, I thought of using nested list to generate the iterations of the background, which in turn forced me to use slow While loop. Second of all, I thought of compiling the code, since it will be used often, however, I have large block of calculations and I want the compiled function/module to return the final result (background) only. So to sum up, i would like to be able to invoke the code below by providing the data (a list of intensities), number of iterations and two values of energy, which constitute for the boundaries of the calculation.

I tried to be descriptive in the comments in code, however, if something is not clear I will try to clear it up as soon as i can.

energyR = 187; energyL = 196; (* L < E < R - boundaries for which background is generated*)
iterations = 50; (* number of iterations *)
NearestPosition[haystack_, needle_] := Nearest[haystack, needle] /. MapIndexed[Rule, haystack]; 
(*function which searches the data for closest value in the list and returns the index of the number*)
eLi = 
 NearestPosition[data59B[[All, 1]], energyL][[1, 1]]; 
eRi = NearestPosition[data59B[[All, 1]], energyR][[1, 1]];
data = data59B[[eLi ;; eRi, 
    All]]; (*this line takes the data in x-y format and cuts of \
non-analyzed ends of the spectra*)
max = data // Length;
B0 = Table[
  x (-data[[1, 2]] + data[[max, 2]])/max + data[[1, 2]], {x, 1, 
   max}];(* three lines here set up the initial background (which is \
assumed to be linear) and geenrates the first iteration of \
calculations*)
k1 = (data[[1, 2]] - data[[max, 2]])/
 Total[data[[All, 2]] - data[[max, 2]] - B0];
B1 = Table[
   k1 Total[(data[[x ;; max, 2]] - data[[max, 2]] - 
       B0[[x ;; max]])], {x, 1, max}];
Btable = Table[
  B1, {i, 1, 
   iterations}]; (*since i have trouble generating nested list i'm \
making local table to contain the iterative results*)
ktable = 
 Table[k1, {i, 1, iterations}];
j = 2;
While[j <= iterations, 
  ktable[[j]] = (data[[1, 2]] - data[[max, 2]])/
   Total[data[[All, 2]] - data[[max, 2]] - Btable[[j - 1]]]; 
  Btable[[j]] = 
   Table[ktable[[j]] Total[(data[[x ;; max, 2]] - data[[max, 2]] - 
        Btable[[j - 1, x ;; max]])], {x, 1, max}]; j++];
N[ktable[[iterations]] - 
  ktable[[iterations - 
     1]]] (*this returns the parameter with which we can estimate the \
convergence of the calculations*)

background = 
  data[[max, 2]] + 
   Btable[[iterations]]; (*and here we have a final result - a \
background for further analysis*)
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract