Function:
function [lamda_win, flux_win] = show_window(lamda_cent, w_win, lamda, flux, flag_plot)
%Date:Dec 27,2011
%Author:shizhixin
%Email:szhixin@gmail.com
%Blog:http://blog.csdn.net/shizhixin
%Function get the window data(lamda,flux, true) from a center point and the window width.
%note the lamda is a sorted array and w_win should be a postive number.
%the postion of lamda_cent should be in [w_win+1,dim_lamda-w_win]
%EX Input:
% lamda = [1:20];
% flux = [50*sin((lamda*pi)/2)];
% subplot(1,2,1);
% plot(lamda, flux);
% lamda_cent = 10;
% w_win = 2;
% subplot(1,2,2);
% [lamda_win, flux_win] = show_window(lamda_cent, w_win, lamda, flux, true);
%Output:
%lamda_win = 8 9 10 11 12
%flux_win = 1.0e-014 *
% -0.0490 0.0551 -0.0612 0.2450 -0.0735
%test the dim of lamda and flux
dim_lamda = length(lamda);
dim_flux = length(flux);
if dim_lamda ~= dim_flux
disp 'the dim of lamda and flux is inconsistent!'
return
end
%get the position of lamda_cent
pos_cent = 1;
for i=1:dim_lamda
if lamda_cent >= lamda(i)
pos_cent = i;
end
end
%test the window boundary
if pos_cent <= w_win or pos_cent > dim_lamda-w_win
disp 'the window is out of boundary!'
return;
end
lamda_win = [lamda((pos_cent-w_win):(pos_cent+w_win))];
flux_win = [flux((pos_cent-w_win):(pos_cent+w_win))];
if flag_plot
%plot center line
min_flux = min(flux_win);
max_flux = max(flux_win);
plot([lamda_cent:lamda_cent],[min_flux:5:max_flux],'r--');
hold on
plot(lamda_win, flux_win);
title_name = sprintf('%s%d%s%d','lamda\_center:',lamda_cent, ', win\_width:', w_win);
title(title_name);
end
end
Test:
>> lamda = [1:20];
flux = [50*sin((lamda*pi)/2)];
subplot(1,2,1);
plot(lamda, flux);
lamda_cent = 10;
w_win = 2;
subplot(1,2,2);
[lamda_win, flux_win] = show_window(lamda_cent, w_win, lamda, flux, true);
Figure: