%  Ultrafast Fibre Laser Experiment, University of Toronto.
%  Written by:  Gajanan Kuganesan       Date:  Spring 2004.
%  Programmed for MATLAB 6.1, (available on Faraday via Nortel Labs).
%  
%  The following code imports the Optical Spectrum Analyzer signal and determines
%  the pulse width (in the frequency domain.
%  
%  Imported Data:  Data can be imported from LABVIEW software (OSA)
%  available on the UFL computer.  Data from LABVIEW should be saved as an 
%  Microsoft Excel file (*.xls).  The imported data has the following format:
%  It contains only 2 column in an *.xls file; the first column contains the 
%  wavelength (frequency domain) data and the second column contains amplitude
%  at the corresponding wavelength.

%*****************************************************************************

clear;              %clear previous data
rawdata = importdata('c:\ultra1b\ml1a.xls');

% Arrange raw data into Wavelength and Amplitude vectors
for i = 1:length(rawdata)
   wavelength(i) = rawdata(i,1);
   amplitude(i) = rawdata(i,2);
end;

%   Despike Amplitude with Median Filter
%   (can be used to reduce stray peaks in data)
%subplot(2,1,1); plot(wavelength,amplitude); subplot(2,1,2); 
%filtersize = 10;
%amplitude = medfilt1(amplitude, filtersize);  

plot(wavelength,amplitude);
title('Optical Spectrum Analyzer');
ylabel('Amplitude')
xlabel('Wavelength [nm]');

% Finds FWHM (not optimized for extreme spike)
maxpeak = max(amplitude);
peakindex = find(amplitude == max(amplitude));
initial = find((amplitude >= 0.5*max(amplitude)));
lowval = wavelength(initial(1));
highval = wavelength(initial(length(initial)));
FWHM = highval - lowval;
text(1540,0.6*maxpeak,'Max');
text(1555,0.6*maxpeak, num2str(maxpeak));
text(1540,0.5*maxpeak,'FWHM');
text(1555,0.5*maxpeak,num2str(FWHM)); 