clc;

%%
% import raw data and name it as osatest first
[m,n] = size(osatest);

% length of test fibre unit[m]
l = 11.5; 
% wavelength
wavelength = linspace(1078,1082,n);

% step size unit [um]
spacial_step = 15;

% horizontal and vertical steps of spatial scan
max_x_pos = 30;
max_y_pos = 30;
%% Normalized DelayProfile at different scan points

% reset OSA raw data with fW unit
spec_inter = osatest*1e-3;

% Caculating FFT of spectrum at each spatial position
for x = 1:max_x_pos
    for y = 1:max_y_pos
        temp = x + (y-1)*max_x_pos; % points label
        [delay, spec_inter(temp, :)] = modeDelayFFT(wavelength, osatest(temp, :), l);
    end
end

delayProfile = zeros(1,n);
modeProfile = zeros(m,n);
angleProfile = zeros(m,n);

% sum-up delay profile
for x = 1 : m
   delayProfile = delayProfile +  abs(spec_inter(x, :));
end

% reconstructed mode Profile
for y = 1 : n
    modeProfile(:, y) = abs(spec_inter(:, y))/max(abs(spec_inter(:, y)));
    angleProfile(:, y) = angle(spec_inter(:, y));
end

%% Plot delay at different scanning points
figure;

% delay profile plot
semilogy(delay, delayProfile, '-o');
xlabel('group delay [ps/m]');
ylabel('Amplitude');
xlim([0,8]);

% choose the peak number to plot the mode profile

n_delay = 17;

figure;
modePlot(max_x_pos, max_y_pos, spacial_step, n_delay, modeProfile);