clear;
clc;

% % FY
directoryPath = ".\similarityData";

% Get a list of all files in the directory
files = dir(fullfile(directoryPath, '*.mat')); % You can adjust the file extension as needed

% Extract file names
fileNames = {files.name};

for n=1:length(fileNames)
    % fit the curve of 43075 , 8 inch
    filePath = fullfile(directoryPath, fileNames(n));
    load(filePath);
    [fitresult, gof] = createFit(SAA{8},FYY{8});
    % Evaluate the fitted curves
    for tire=1:length(SAA)
        y_fit = fitresult(SAA{tire});
        % Calculate residuals
        residuals{tire}{n} = abs(FYY{tire} - fitresult(SAA{tire}));
    end
end

for tire=1:length(SAA)
    residuals_one_tire = [];
    for n=1:length(fileNames)
        residuals_one_tire = [residuals_one_tire; residuals{tire}{n}];
    end
    residuals_tire{tire} = residuals_one_tire;
end
% mean
for tire=1:length(SAA)
    num = 0;
    % each tire under 60 measurement conditions
    for i=1:length(residuals_tire{tire})
        if not(isnan(residuals_tire{tire}(i)))
            num = num + 1;
        else
            residuals_tire{tire}(i) = 0;
        end
    end
    residuals_mean{tire} = sum(residuals_tire{tire})/num;
end
% median
for tire=1:length(SAA)
    residuals_median{tire} = median(residuals_tire{tire});
end
% mode
for tire=1:length(SAA)
    residuals_mode{tire} = mode(residuals_tire{tire});
end

%% plots
idx = linspace(1,length(SAA),length(SAA));

figure()
subplot(311)
y = cell2mat(residuals_mean);
plot(idx, y, "-*")
title("Average Absolute Residuials")
xlabel("Order of the tires(8 is our traget tire)")
ylabel("in 1")
xticks(idx)
grid on


subplot(312)
y = cell2mat(residuals_median);
plot(idx, y, "-o")
title("Median of the Absolute Residuials")
xlabel("Order of the tires(8 is our traget tire)")
ylabel("in 1")
xticks(idx)
grid on


subplot(313)
y = cell2mat(residuals_mode);
plot(idx, y, "-o")
title("Mode of the Absolute Residuials")
xlabel("Order of the tires(8 is our traget tire)")
ylabel("in 1")
xticks(idx)
grid on
sgtitle("FY")

%%  FX
directoryPath = ".\similarityDataFX";
% Get a list of all files in the directory
files = dir(fullfile(directoryPath, '*.mat')); % You can adjust the file extension as needed

% Extract file names
fileNames = {files.name};

for n=1:length(fileNames)
    % fit the curve of 43075 , 8 inch
    filePath = fullfile(directoryPath, fileNames(n));
    load(filePath);
    [fitresult, gof] = createFit(SAA{8},FXX{8});
    % Evaluate the fitted curves
    for tire=1:length(SAA)
        y_fit = fitresult(SAA{tire});
        % Calculate residuals
        residuals{tire}{n} = abs(FXX{tire} - fitresult(SAA{tire}));
    end
end

for tire=1:length(SAA)
    residuals_one_tire = [];
    for n=1:length(fileNames)
        residuals_one_tire = [residuals_one_tire; residuals{tire}{n}];
    end
    residuals_tire{tire} = residuals_one_tire;
end
% mean
for tire=1:length(SAA)
    num = 0;
    % each tire under 60 measurement conditions
    for i=1:length(residuals_tire{tire})
        if not(isnan(residuals_tire{tire}(i)))
            num = num + 1;
        else
            residuals_tire{tire}(i) = 0;
        end
    end
    residuals_mean{tire} = sum(residuals_tire{tire})/num;
end
% median
for tire=1:length(SAA)
    residuals_median{tire} = median(residuals_tire{tire});
end
% mode
for tire=1:length(SAA)
    residuals_mode{tire} = mode(residuals_tire{tire});
end

% plots
idx = linspace(1,length(SAA),length(SAA));

figure()
subplot(311)
y = cell2mat(residuals_mean);
plot(idx, y, "-*")
title("Average Absolute Residuials")
xlabel("Order of the tires(8 is our traget tire)")
ylabel("in 1")
xticks(idx)
grid on


subplot(312)
y = cell2mat(residuals_median);
plot(idx, y, "-o")
title("Median of the Absolute Residuials")
xlabel("Order of the tires(8 is our traget tire)")
ylabel("in 1")
xticks(idx)
grid on


subplot(313)
y = cell2mat(residuals_mode);
plot(idx, y, "-o")
title("Mode of the Absolute Residuials")
xlabel("Order of the tires(8 is our traget tire)")
ylabel("in 1")
xticks(idx)
grid on
sgtitle("FX")


%% Mx
directoryPath = ".\similarityDataMX";
% Get a list of all files in the directory
files = dir(fullfile(directoryPath, '*.mat')); % You can adjust the file extension as needed

% Extract file names
fileNames = {files.name};

for n=1:length(fileNames)
    % fit the curve of 43075 , 8 inch
    filePath = fullfile(directoryPath, fileNames(n));
    load(filePath);
    [fitresult, gof] = createFit(SAA{8},MXX{8});
    % Evaluate the fitted curves
    for tire=1:length(SAA)
        y_fit = fitresult(SAA{tire});
        % Calculate residuals
        residuals{tire}{n} = abs(MXX{tire} - fitresult(SAA{tire}));
    end
end

for tire=1:length(SAA)
    residuals_one_tire = [];
    for n=1:length(fileNames)
        residuals_one_tire = [residuals_one_tire; residuals{tire}{n}];
    end
    residuals_tire{tire} = residuals_one_tire;
end
% mean
for tire=1:length(SAA)
    num = 0;
    % each tire under 60 measurement conditions
    for i=1:length(residuals_tire{tire})
        if not(isnan(residuals_tire{tire}(i)))
            num = num + 1;
        else
            residuals_tire{tire}(i) = 0;
        end
    end
    residuals_mean{tire} = sum(residuals_tire{tire})/num;
end
% median
for tire=1:length(SAA)
    residuals_median{tire} = median(residuals_tire{tire});
end
% mode
for tire=1:length(SAA)
    residuals_mode{tire} = mode(residuals_tire{tire});
end

% plots
idx = linspace(1,length(SAA),length(SAA));

figure()
subplot(311)
y = cell2mat(residuals_mean);
plot(idx, y, "-*")
title("Average Absolute Residuials")
xlabel("Order of the tires(8 is our traget tire)")
ylabel("in 1")
xticks(idx)
grid on


subplot(312)
y = cell2mat(residuals_median);
plot(idx, y, "-o")
title("Median of the Absolute Residuials")
xlabel("Order of the tires(8 is our traget tire)")
ylabel("in 1")
xticks(idx)
grid on


subplot(313)
y = cell2mat(residuals_mode);
plot(idx, y, "-o")
title("Mode of the Absolute Residuials")
xlabel("Order of the tires(8 is our traget tire)")
ylabel("in 1")
xticks(idx)
grid on
sgtitle("MX")

%% Mz
directoryPath = ".\similarityDataMZ";
% Get a list of all files in the directory
files = dir(fullfile(directoryPath, '*.mat')); % You can adjust the file extension as needed

% Extract file names
fileNames = {files.name};

for n=1:length(fileNames)
    % fit the curve of 43075 , 8 inch
    filePath = fullfile(directoryPath, fileNames(n));
    load(filePath);
    [fitresult, gof] = createFit(SAA{8},MZZ{8});
    % Evaluate the fitted curves
    for tire=1:length(SAA)
        y_fit = fitresult(SAA{tire});
        % Calculate residuals
        residuals{tire}{n} = abs(MZZ{tire} - fitresult(SAA{tire}));
    end
end

for tire=1:length(SAA)
    residuals_one_tire = [];
    for n=1:length(fileNames)
        residuals_one_tire = [residuals_one_tire; residuals{tire}{n}];
    end
    residuals_tire{tire} = residuals_one_tire;
end
% mean
for tire=1:length(SAA)
    num = 0;
    % each tire under 60 measurement conditions
    for i=1:length(residuals_tire{tire})
        if not(isnan(residuals_tire{tire}(i)))
            num = num + 1;
        else
            residuals_tire{tire}(i) = 0;
        end
    end
    residuals_mean{tire} = sum(residuals_tire{tire})/num;
end
% median
for tire=1:length(SAA)
    residuals_median{tire} = median(residuals_tire{tire});
end
% mode
for tire=1:length(SAA)
    residuals_mode{tire} = mode(residuals_tire{tire});
end

% plots
idx = linspace(1,length(SAA),length(SAA));

figure()
subplot(311)
y = cell2mat(residuals_mean);
plot(idx, y, "-*")
title("Average Absolute Residuials")
xlabel("Order of the tires(8 is our traget tire)")
ylabel("in 1")
grid on
xticks(idx)


subplot(312)
y = cell2mat(residuals_median);
plot(idx, y, "-o")
title("Median of the Absolute Residuials")
xlabel("Order of the tires(8 is our traget tire)")
ylabel("in 1")
xticks(idx)
grid on


subplot(313)
y = cell2mat(residuals_mode);
plot(idx, y, "-o")
title("Mode of the Absolute Residuials")
xlabel("Order of the tires(8 is our traget tire)")
ylabel("in 1")
xticks(idx)
grid on
sgtitle("MZ")






