Monday, June 12, 2017

XRF Spectrum in MATLAB

The goal is to use MATLAB to recreate the XRF spectrum, find the peak that is the highest counts and the corresponding energy that represents the K-Alpha value of the primary element.
Furthermore, we can find the counts acquired to the actual value of K-Alpha value of the element/metal.

The 'uiimport' function can be simply used in a script to prompt the user to import the data that we prepared in the previous step. The data will be already selected and ready to be placed in variables that are automatically created. 

The 'pause(time in seconds)' function is also used to give a user enough time to select data before executing the rest of the script.

Select the correct excel data file that was prepared and the data will be automatically selected. MATLAB will prompt the following dialog;



Four new variables will be created which will be seen using 4 blue underlined names. The Data Import dialog can be closed after the data was successfully imported and the rest of the code that depends on the data that was imported will be executed. 

The script that I used to plot the spectrum, find the peak and the other necessary values is shown below;


------------------------------------

prompt = 'How many Samples? ';
N = input(prompt);
Kerror = zeros(N,1);
K = zeros(N,1);
Atomic = zeros(N,1);

for i=0:N-1;
    
close all
clearvars -EXCEPT N Kerror K Atomic i

prompt = 'What is the K alpha of the primary element? ';
realalpha = input(prompt);
prompt = 'What is the Atomic Number of the primary element? ';
Z = input(prompt);

uiimport

pause(20)

LnLE=log(LESubstrateRawSpectrumCounts);
LnME=log(MESubstrateRawSpectrumCounts);
LnHE=log(HESubstrateRawSpectrumCounts);
figure(1)
plot(EnergykeV,LnLE)
title('LE')
xlabel('Energy (keV)')
ylabel('ln (Counts)')
figure(2)
plot(EnergykeV,LnME)
title('ME')
xlabel('Energy (keV)')
ylabel('ln (Counts)')
figure(3)
plot(EnergykeV,LnHE)
title('HE')
xlabel('Energy (keV)')
ylabel('ln (Counts)')

[value, location] = max(LnLE(:));
PeakCount=value
Kalpha = EnergykeV(location)

loc = find(EnergykeV>=realalpha);
LiteratureCount = LnLE(loc(1,1))
% loc2 = find(EnergykeV<=realalpha);
% LiteratureCount2 = LnLE(loc(1,1))
% AvgLiteratureCount= (LiteratureCount+LiteratureCount2)/2
Diff = abs(Kalpha-realalpha);

ind = logical(i);

Kerror(ind+1) = Diff;
Atomic(ind+1) = Z;
K(ind+1) = Kalpha;

end

figure(4)
[m,c] = polyfit(Atomic,K,1);
y = polyval(m,Atomic,c);
plot(Atomic,K,'r.'); hold on;
errorbar(Atomic, y, Kerror);
title('Mosleys Plot')
xlabel('Atomic Number')
ylabel('Energy (keV)')

------------------------------------
The script also produces the Mosley's Plot from the Kalpha and atomic numbers of all the samples that are inserted.

No comments:

Post a Comment