% Clear all variables and close all figures
clear all;
close all;

% Define the subfolder and file names
subfolder = 'psd_output_mod';
output_subfolder = 'psd_output_for_diploma';  % New subfolder for saving figures
file_names = {
    'psd_brl_default.txt',
    'psd_brl_mod_v01_01-n.txt',
    'psd_brl_mod_v01_01-n_maxhold.txt',
    'psd_brl_mod_v02_01-17g.txt',
    'psd_brl_mod_v02_17-250g.txt',
    'psd_z_init_v03.txt'
};

% Create the output subfolder if it doesn't exist
if ~exist(output_subfolder, 'dir')
    mkdir(output_subfolder);
end

% Create figures for each file
for i = 1:length(file_names)
    file_path = fullfile(subfolder, file_names{i});

    % Load data from the text file
    data = load(file_path);

    % Extract frequency and amplitude
    frequency = data(:, 1);
    amplitude = data(:, 2);

    % Create a new figure
    figure('Position', [100, 100, 1280, 720]);  % Set plot dimensions
    plot(frequency, amplitude);
    title(['PSD - ' strrep(file_names{i}, '_', ' ')]);
    xlabel('Frequency (Hz)')
    ylabel('PSD (g^2/Hz)')
    xlim([0, 100]);
    
    % Save the figure as .png in the output subfolder
    saveas(gcf, fullfile(output_subfolder, [strrep(file_names{i}, '_', ' '), '.png']));
end

% Create a last figure with all PSD
figure('Position', [100, 100, 1280, 720]);  % Set plot dimensions
hold on;
for i = 1:length(file_names)
    file_path = fullfile(subfolder, file_names{i});

    % Load data from the text file
    data = load(file_path);

    % Extract frequency and amplitude
    frequency = data(:, 1);
    amplitude = data(:, 2);

    % Plot on the same figure
    plot(frequency, amplitude, 'DisplayName', strrep(file_names{i}, '_', ' '));
end
hold off;

title('Combined PSD from All Files');
xlabel('Frequency (Hz)')
ylabel('PSD (g^2/Hz)')
legend('show');
axis([5 200 0 0.35]);
grid on;

% Save the last figure as .png in the output subfolder
saveas(gcf, fullfile(output_subfolder, 'Combined_PSD.png'));
