How do I combined all peak to become one spectral line?

3 views (last 30 days)
Hi guys, from the code I have I managed to produce the graph:
%get list of element, prompt user to select one, and get content of element file
filelist = dir('*.txt');
[~, elementlist] = arrayfun(@(f) fileparts(f.name), filelist, 'UniformOutput', false);
[elementindex, ok] = listdlg('ListString', elementlist, 'PromptString', 'Select an element', 'SelectionMode', 'single');
if ~ok, return; end %terminate if user click cancel
elementdata = importdata(filelist(elementindex).name);
assert(isnumeric(elementdata), 'Element does not have data'); %abort if the file does not contain a matrix
%build wavelength array
step = 0.02;
ss = 100;
steparray = -step*ss : step : step*ss;
new_wavelength = (elementdata(:, 1) + steparray); %no loop needed. Requires R2016b or latter
%get row selection
[selectedrow, ok] = listdlg('ListString', compose('%f', elementdata(:, 1)), 'PromptString', 'Select a wavelength', 'SelectionMode', 'multiple');
if ~ok, return; end %terminate if user click cancel
%build Peak vector for selected row
Delta_lambda = 0.2;
int_fact = elementdata(selectedrow, 2);
num = new_wavelength(selectedrow,ss+1)-new_wavelength(selectedrow,:);
denom = Delta_lambda/2;
Peak = (int_fact.*(1./(1+(num/denom).^2)));
%combined peak vector for selected row
plot(new_wavelength(selectedrow, :).', Peak.')
How do I combined all the peak that I chose to become as the graph below bcs I have try by using sum(peaks) but i doesnt work so im not sure how should I do it. One of the expert(Mathworks society) told me to resample each row to the same wavelength before i can sum it but im not sure how should i do it.
I have attached the element file as well. Thanks in advanced.
  3 Comments
Mohamad Khairul Ikhwan Zulkarnain
I got more than one peak at the same time bcs when i select the number of row for wavelength, i select more than one row which eventually will create more than one peak at a time. I dont think my peak share the same x-vector since each peak has its own row but as you see in the first picture, there are some part of the peak is overlapping. Cant we just sum it up? If not, what should I do to sum it up? Can you show it to me?

Sign in to comment.

Accepted Answer

jonas
jonas on 14 Sep 2018
Edited: jonas on 14 Sep 2018
They key is to resample the curves along a common x-vector. You can do this by interp1. Just add these new lines of code:
% ... old code
Peak = (int_fact.*(1./(1+(num/denom).^2)));
% ...New code begins here
x=new_wavelength(selectedrow,:);
y=Peak;
% Remove rows with NaNs
x(isnan(y(:,1)),:)=[];
y(isnan(y(:,1)),:)=[];
% Number of peaks
np=size(y,1);
% New xvec
xh=min(x(:)):1e-3:max(x(:)); %new x vector
% Preallocate new peak vector
yh=nan(size(x,1),size(xh,2));
%Interpolate one peak at a time
for i=1:np
yh(i,:)=interp1(x(i,:),y(i,:),xh);
end
% Sum column-wise, exclude nans
ysum=nansum(yh,1);
% Plot
plot(xh,ysum)
First three wavelengths of Ferum:
  5 Comments
jonas
jonas on 14 Sep 2018
Edited: jonas on 14 Sep 2018
No problem! Should work now.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Preprocessing in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!