% Demo showing the use of INLSA.m with made-up data.
clc; clear all;
load ('Parameters_INLSA.mat')
randn('state',0);
rand('state',0);

% Choice of the parameters will be used
par = 'ssim';  % ssim, psnr, tse (in this case log(tse)

error_ratio = 1;  % can be used to allocate error between MOS and parameters
norm_set = 3;
% experiment 1 is the reference and will not be adjusted
% can change this to 2 or 3 and see what happens
% use the experiment that you trust most as the reference

% Experiment 1
szExp1      = size(s1,1);
scale_exp1  = [0 100]';
cost_exp1   = ones(szExp1,1);
switch (par)
    case {'ssim'}
        par_exp1 = ssim1;               % create random parameter values
    case 'psnr'
        par_exp1 = psnr1;               % create random parameter values
    case 'tse'
        par_exp1 = tse1;                % create random parameter values
    otherwise
        disp('Unknown method.')
end
MOS_exp1    = s1;                          % create MOS values that are correlated to those random parameter values 
MOS_exp1    = min(100,MOS_exp1);           % limt MOS values to the proper range
MOS_exp1    = max(0,MOS_exp1);


% Experiment 2
szExp2      = size(s2,1);
scale_exp2  = [0 100]';
cost_exp2   = ones(szExp2,1);
switch (par)
    case {'ssim'}
        par_exp2 = ssim2;               % create random parameter values
    case 'psnr'
        par_exp2 = psnr2;               % create random parameter values
    case 'tse'
        par_exp2 = tse2;                % create random parameter values
    otherwise
        disp('Unknown method.')
end
MOS_exp2    = s2;
MOS_exp2    = min(100,MOS_exp2);
MOS_exp2    = max(0,MOS_exp2);

%Experiment 3
szExp3      = size(s3,1);
scale_exp3  = [0 100]';
cost_exp3   = ones(szExp3,1);
switch (par)
    case {'ssim'}
        par_exp3 = ssim3;               % create random parameter values
    case 'psnr'
        par_exp3 = psnr3;               % create random parameter values
    case 'tse'
        par_exp3 = tse3;                % create random parameter values
    otherwise
        disp('Unknown method.')
end
MOS_exp3    = s3;
MOS_exp3    = min(100,MOS_exp3);
MOS_exp3    = max(0,MOS_exp3);

figure
subplot(2,2,1)
plot(par_exp1,MOS_exp1,'ob')
hold on
plot(par_exp2,MOS_exp2,'og')
plot(par_exp3,MOS_exp3,'or')
grid
hold off
xlabel('Parameter Value')
ylabel('Subjective Score')
title(strcat({'MOS vs '}, upper(par), {' Parameter (Before INLSA)'}))
legend('Exp1','Exp2','Exp3','Location','SouthWest');

[rhosq,W,BA,MOS_HAT,MOS_TILDA] = inlsa(error_ratio,norm_set, ...
    scale_exp1,cost_exp1,MOS_exp1,par_exp1,...
    scale_exp2,cost_exp2,MOS_exp2,par_exp2,...
    scale_exp3,cost_exp3,MOS_exp3,par_exp3);

W  % Paremeter gain and shift
BA % MOS gain and shift for each experiment

subplot(2,2,2)
plot(par_exp1,MOS_TILDA(1 : szExp1),'ob')
hold on
plot(par_exp2,MOS_TILDA(szExp1 + 1 : szExp1 + szExp2),'og')
plot(par_exp3,MOS_TILDA(szExp1 + szExp2 + 1 : szExp1 + szExp2 + szExp3),'or')
grid
hold off
xlabel('Parameter Value')
ylabel('Distortion')
title(strcat({'Distortion vs '}, upper(par), {' Parameter (After INLSA)'}))
legend('Exp1','Exp2','Exp3','Location','SouthWest');

subplot(2,2,3)
plot(par_exp1, 100 * (1 - MOS_TILDA(1 : szExp1)),'ob')
hold on
plot(par_exp2, 100 * (1 - MOS_TILDA(szExp1 + 1 : szExp1 + szExp2)),'og')
plot(par_exp3, 100 * (1 - MOS_TILDA(szExp1 + szExp2 + 1 : szExp1 + szExp2 + szExp3)),'or')
grid
hold off
xlabel('Parameter Value')
ylabel('Subjective Score')
title(strcat({'MOS vs '}, upper(par), {' Parameter (After INLSA)'}))
legend('Exp1','Exp2','Exp3','Location','SouthWest');

% Note that the gain and shift in BA must be used in the distortion domain,
% not the MOS domain, this section shows how.  The figure will agree with
% figure 3
subplot(2,2,4)
temp = (100 - MOS_exp1) / 100;      % convert MOS to distortion
temp = BA(1,1) + BA(2,1) * temp;    % apply gain and shift produced by INSLA
temp = 100 * (1 - temp);            % convert distortion to MOS
plot(par_exp1,temp,'ob')
hold on

temp = (100 - MOS_exp2) / 100;      % convert MOS to distortion
temp = BA(1,2) + BA(2,2) * temp;    % apply gain and shift produced by INSLA
temp = 100 * (1 - temp);            % convert distortion to MOS
plot(par_exp2,temp,'og')

temp = (100 - MOS_exp3) / 100;      % convert MOS to distortion
temp = BA(1,3) + BA(2,3) * temp;    % apply gain and shift produced by INSLA
temp = 100 * (1 - temp);            % convert distortion to MOS
plot(par_exp3,temp,'or')

grid
hold off
xlabel('Parameter Value')
ylabel('Subjective Score')
title('Same as Fig 3, but calculated the long way')
legend('Exp1','Exp2','Exp3','Location','SouthWest');