%Introduction: This program is designed to fit the measured N2 unresolved
%rovibronic band with theoretical curve and hence gives the rotational
%temperature and vibrational temperature.
%Author: Bo Bai and Brett Cruden.
% SSE = fit_n2_rig(params,wavel,spectrum,vibmodes,AtomicWavelengths);
% Calculates a fit spectrum versus the spectrum desrcibed by spectrum and wavel. Params are fit parameters as follows:
% params(1) = T; The gas temperature for modelling CH spectrum
% params(2) = I_0; The peak intensity of the C2 (0,0) line
% params(3) = Tvib, vibrational temp
% params(4) = Wavelength shift, representing slight inaccuracies in calibration
% Params(5) = Baseline Associated with the experiments.
% Params(6) = Wavelength between two pixels, can not be totally
% params(7:n) = I_Atomic; in order of atomic line wavelength parameter
% wavel is a vector of wavelengths over which fit is performed
% spectrum is the corresponding spectrum to be fit
% vibmodes is a 3 component vector,
% vibmode(1) = nu', upper state vibr. quantum #
% vibmode(2) = nu", lower state vibr. quantum #
% vibmode(3) = nbands, number of higher order bands to include in fit
% newpars = fminsearch('fit_n2_rig',params,[],wavel,spectrum,1);
%function [SSE, new_spectrum] = fit_n2_rig(params,wavel,data,vibmode,atomic);
%Part I. Read data and show it in the figure.
file='2202.txt'; % The file name of the fitted curve.
Directory='I:\area-4-test\';
OutputDirectory='I:\area-4-test\nw\';
outputsuffix='dVN1.fig';
specfile=[Directory,file]; % The reading file with full directory path.
outputfile=[OutputDirectory,file,outputsuffix]; % The output file with full directory path.
expspec=load(specfile,'-ascii'); % This matrix will be a single row matrix of line intensity.
plot(expspec);
%stop
params=[1300,7,5000,7.5,0.18,0.67736]; % As described in the introduction, the sequence is rotational tempeature, peak intensity, vibrational temperature and wavelength shift.
vibmode=[1,0,4]; % [1,0,4] is corresponding to 1'-0", 2'-1", 3'-2", till 4'-3" bands. First element is upper and second is lower and third is total numbers.
newpars = fminsearch('fit_n2_rig',params,[],expspec,vibmode)
global FinalWav FinalExpCurv FinalTheCurv;
%%% Plot out the results now.
LabelYmax=4/4.5*max(FinalTheCurv);
stepforlabel=max(FinalTheCurv)/4*0.2;
plot(FinalWav,FinalExpCurv,'b',FinalWav,FinalTheCurv,'r');
title('Theoretical and experimental rovibronic band for 1-0 of SPS of N2 ');
xlabel('wavelength(angstrom)');ylabel('intensity(a.u.)');
h=legend('Experimental','Theoretical',2);
%text(min(FinalWav)+1,LabelYmax,['File:',num2str(file)],'FontSize',10);
%text(Wave1)+10,LabelYmax-stepforlabel,['Baseline=',num2str(baseline)]);
%text(min(FinalWav)+1,LabelYmax-stepforlabel*1,['Wavelength Adjustment=',num2str(newpars(4)),'A']);
%text(Wave1)+10,LabelYmax-stepforlabel*3,['Spectral Width=',num2str(w),'A']);
%text(Wave1)+10,LabelYmax-stepforlabel*4,['Residue of Fitting=',num2str(SSE)]);
text(min(FinalWav)+1,LabelYmaxstepforlabel*2,['T_{rot}=',num2str(newpars(1)),'^{o}K']);
text(min(FinalWav)+1,LabelYmax-stepforlabel*3,['T_{vib}=',num2str(newpars(3)),'^{o}K']);
saveas(gcf,outputfile)