How do I combined all peak to become one spectral line?
2 次查看(过去 30 天)
显示 更早的评论

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 个评论
Guillaume
2018-9-14
For info, (a bit late), this is a continuation of why-am-i-getting-an-error-of-subscripted-assignment-dimension-mismatch
采纳的回答
jonas
2018-9-14
编辑:jonas
2018-9-14
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Preprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!