% Create a new folder to save the output files
if ~exist('psd_output_brf\', 'dir')
    mkdir('psd_output_brf\');
end

fx = fxX;
fy = fxY;
fz = fxZ;

% Loop through each direction
directions = {'x', 'y', 'z'};
for d = 1:length(directions)
    % Choose the appropriate frequency and amplitude values based on the direction
    switch directions{d}
        case 'x'
            frequency = fx;
            amplitude = pxxX;
        case 'y'
            frequency = fy;
            amplitude = pxxY;
        case 'z'
            frequency = fz;
            amplitude = pxxZ;
    end
    
    % Open the file for writing
    fileID = fopen(sprintf('psd_output/psd_brf_%s.txt', directions{d}), 'w');
    
    % Loop through each frequency and its corresponding amplitude value
    for i = 1:length(frequency)
        fprintf(fileID,'%d,%f\n', frequency(i), amplitude(i));
    end
    
    % Close the file
    fclose(fileID);
end

% Write the output_psd_all.txt file
fileID = fopen('psd_output/psd_brr_all.txt', 'w');
for i = 1:length(fx)
    fprintf(fileID,'%d,%f,%f,%f\n', fx(i), pxxX(i), pxxY(i), pxxZ(i));
end
fclose(fileID);

%% %% Graph output
% figure;
% hold on;
% plot(fz,pxxX,'r');
% plot(fz,pxxY,'g');
% plot(fz,pxxZ,'b');
% grid on;
% xlabel('Frequency (Hz)');
% ylabel('PSD ((m*s^2)^2/Hz)');
% title('bracket right');
% legend('PSD bracket right x','PSD bracket right y','PSD bracket right z');
% axis([5 120 0 0.75]);
% % Save figure to .jpg file
% saveas(gcf, 'psd_output/psd_brr_all.jpg');
% 
% %% Output X-axis
% 
% figure Name brr_x
% plot(fx,pxxX)
% xlabel('Frequency (Hz)')
% ylabel('PSD ((m*s^2)^2/Hz)')
% axis([5 120 0 0.75])
% % Save figure to .jpg file
% saveas(gcf, 'psd_output/psd_brr_x.jpg');
% 
% figure Name brr_x
% subplot(2,1,1),plot(t,brr_x)        
% xlabel('Time [s]');
% ylabel('Acceleration (g)');
% title('PumpBrac_F_X');
% subplot(2,1,2),plot(fx,pxxX);
% xlabel('Frequency (Hz)')
% ylabel('PSD ((m*s^2)^2/Hz)')
% axis([5 120 0 0.75])
% % Save figure to .jpg file
% saveas(gcf, 'psd_output/psd_time_brr_x_.jpg');
% 
% %% Output Y-axis
% 
% figure Name brr_y
% plot(fy,pxxY)
% xlabel('Frequency (Hz)')
% ylabel('PSD ((m*s^2)^2/Hz)')
% axis([5 120 0 0.75])
% % Save figure to .jpg file
% saveas(gcf, 'psd_output/psd_brr_y.jpg');
% 
% figure Name brr_y
% subplot(2,1,1),plot(t,brr_y)        
% xlabel('Time [s]');
% ylabel('Acceleration (g)');
% title('PumpBrac_F_Y');
% subplot(2,1,2),plot(fy,pxxY);
% xlabel('Frequency (Hz)')
% ylabel('PSD ((m*s^2)^2/Hz)')
% axis([5 120 0 0.75])
% % Save figure to .jpg file
% saveas(gcf, 'psd_output/psd_time_brr_y_.jpg');
% 
% %% Output Z-axis
% 
% figure Name brr_z
% plot(fz,pxxZ)
% xlabel('Frequency (Hz)')
% ylabel('PSD ((m*s^2)^2/Hz)')
% axis([5 120 0 0.75])
% % Save figure to .jpg file
% saveas(gcf, 'psd_output/psd_brr_z.jpg');
% 
% figure Name brr_z
% subplot(2,1,1),plot(t,brr_z)        
% xlabel('Time [s]');
% ylabel('Acceleration (g)');
% title('PumpBrac_F_Z');
% subplot(2,1,2),plot(fz,pxxZ);
% xlabel('Frequency (Hz)')
% ylabel('PSD ((m*s^2)^2/Hz)')
% axis([5 120 0 0.75])
% % Save figure to .jpg file
% saveas(gcf, 'psd_output/psd_time_brr_z_.jpg');
