% This code belongs to research group of Professor Iakovos Tzanakis at Oxford Brookes University 
% Written by Dr. Mohammad Khavari
% NOTE: variables are defined at the end of the code
% t is in s, f is in Hz, V is in V and P is in kPa
% This program uses the function 'settingsdlg' written by Rody P.S. Oldenhuis input panels. 
clc;clear;close all;tic
Input_Method        = 1;                      % 1: Input data via UI
                                              % 2: Input data in the Code
Data_Method         = 1;                      % 1: Data Acquisition by PicosCope
                                              % 2: Data Acquisition by PCI Board
Multi_Method        = 1;                      % 1: Single case (enter directory of the FOLDER containing csv files)
                                              % 2: Multi cases (enter directory of the FOLDER containing folders of all cases)
Noise_Method        = 1;                      % 1: Noise subtraction in time domain
                                              % 2: Noise subtraction in frequency domain
Filter_Method       = 1;                      % 1: With Applying filter
                                              % 2: Without Applying filter                                                                  
Save_Plots_Method   = 1;                      % 1: Save plots as JPEG using export_fig
                                              % 2: Do not save plots 
Save_Data_Method    = 1;                      % 1: Save data as MATLAB dat file
                                              % 2: Do not save data 
Save_Excel_Method   = 1;                      % 1: Save the results in an Excel file 
                                              % 2: Do not save the results in an Excel file 
                                         
alpha = 0;                                    % Percentage of ignored data at beginning and end of P(t) for calculating Pmax (avoid counting peaks at t~0)
switch Input_Method
    case 2
switch Data_Method
    case 1
        route1 = 'D:\Experiments\Sample Data';
        Noise_up   = 10;                                % Noise upper value in mV (only needed if Noise_Method = 1)
        Noise_down = 10;                                % Noise lower value in mV (only needed if Noise_Method = 1)
        fs   = 500e6;                                   % Sampling rate in samples per seconds
        NWF1 = 60;                                      % Number of wave forms (if you don't want to analyze all)
        NoiseFileName = 'Noise_Silver_x1_y1_01';        % Noise file name as csv  (only needed if Noise_Method = 2)
    case 2
        BoardFileName = 'signal';                       % CSV file name of the PCI board output from PFV software
        Noise_up   = 0;                                 % Noise upper value in mV (only needed if Noise_Method = 1)
        Noise_down = 0;                                 % Noise lower value in mV (only needed if Noise_Method = 1)
        NWF1 = 1;                                       % For PCI board, it must be 1  
        NoiseFileName = 'Noise';                        % Noise file name as csv  (only needed if Noise_Method = 2)
        route1 = cd;                                    % cd is current directory: to be set where the code is
        BoardFileName = [BoardFileName '.csv'];
        fs = csvread(BoardFileName,17,2,[17,2,17,2]);   % Read the sampling rate from csv file
end
% Probe_Class = 'Cavitometer';
Probe_Class = 'Fibre-Optic Hydrophone';
% Probe_Class = 'Needle Hydrophone';

% ProbeType = 'Probe2';                 
% ProbeType = 'Probe3_Shunt';
% ProbeType = 'Probe3_Attenuator';       
% ProbeType = 'Probe4';                  

% Cavitometer_position = 'Side';        
% Cavitometer_position = 'Under';

% PreAmpType  = 'A';                    % Only for Probe 2 & Probe 3_Shunt
% PreAmpType  = 'B';                    % Only for Probe 2 & Probe 3_Shunt

% ProbeType = 'Needle Hydrophone1';
% ProbeType = 'Needle Hydrophone2';
% ProbeType = 'Needle Hydrophone3';
% ProbeType = 'Needle Hydrophone4';

% ProbeType = 'Silver';                   
% ProbeType = 'Gold';                   
% ProbeType = '10T';                   
ProbeType = '11T';                   
% ProbeType = '12T';                   
% ProbeType = '14T';                   
% ProbeType = '15T';                   
% ProbeType = '16T';                   
% ProbeType = '17T';                   

Loading_Effect = 1;                     % The loading effect of the PicoScope
MATLAB_Version = 'R2018b';
% MATLAB_Version = 'Older Versions';
if (Save_Plots_Method == 1)
    Save_Plots = 'Yes';
else
    Save_Plots = 'No';
end
if (Save_Data_Method == 1)
    Save_Data = 'Yes';
else
    Save_Data = 'No';       
end
if (Save_Excel_Method == 1)
    Save_Excel = 'Yes';
else
    Save_Excel = 'No';       
end
if (Data_Method == 2)                   % For PCI board case, only Single Method is used
    Multi_Method = 1;
end
if (Noise_Method == 2)                  % Noise csv file is needed only for noise subtraction in frequency domain 
    NoiseFileName = [NoiseFileName '.csv'];
end
    case 1
    [Input, Button] = settingsdlg(...
    'Description'        , 'Data Acquisition Device',...
    'title'              , 'Choose the device',...
    'Device'              , {'PicosCope','PCI Board'});
    Device_Status   = Input.Device;  
    if strcmpi(Button, 'Cancel')  
        disp('Error! Please enter the required parameters')
        return
    end
    if strcmpi(Device_Status, 'PicosCope')  
       Data_Method = 1;
    else 
       Data_Method = 2;
    end

    switch Data_Method
        case 1  % Data Acquisition by PicosCope
    [Input, Button] = settingsdlg(...
    'Description'        , 'Input Data',...
    'title'              , 'Choose the desired cases',...
    'Cases'              , {'Single','Multiple'},...
    'Probe'              , {'Cavitometer','Fibre-Optic Hydrophone','Needle Hydrophone'},...
    'Noise Subtraction'  , {'Time Domain','Frequency Domain'},...
    'Apply Filter'       , {'Yes','No'},...
    'Save Plots'         , {'Yes','No'},...
    'Save Data'          , {'Yes','No'},...
    'Save in Excel'      , {'Yes','No'},...
    'Data Source'        , 'D:\...');
    Case_Status   = Input.Cases;
    route1        = Input.DataSource;
    Probe_Class   = Input.Probe;
    NoiseMethod1  = Input.NoiseSubtraction;
    FilterMethod1 = Input.ApplyFilter;
    Save_Plots    = Input.SavePlots;
    Save_Data     = Input.SaveData;
    Save_Excel    = Input.SaveInExcel;
    if strcmpi(Button, 'Cancel')  
        disp('Error! Please enter the required parameters')
        return
    end
    if strcmpi(Case_Status, 'Single')  
       Multi_Method = 1;
    else
       Multi_Method = 2;       
    end          
    if strcmpi(NoiseMethod1, 'Time Domain')  
       Noise_Method = 1;
    else
        Noise_Method = 2;       
    end
    if strcmpi(FilterMethod1, 'Yes')  
       Filter_Method = 1;
    else
        Filter_Method = 2;       
    end
    switch Noise_Method
        case 1
    if strcmpi(Probe_Class, 'Cavitometer')
        [Input, Button] = settingsdlg(...
        'Description',                      'Input all the required parameters/variables',...
        'title'      ,                      'CAVITOMETER',...
        'Cavitometer Position',             {'Under','Side'},...
        'Probe' ,                           {'Probe2','Probe3_Shunt','Probe3_Attenuator','Probe4'},...               
        'Pre Amplifier Type' ,              {'B','A'},...               
        'MATLAB Version' ,                  {'R2018b','Older Versions'},...               
        'Sampling Rate Mega Sample per sec',500,...
        'Upper threshold of noise in mV',   0.5,...
        'Lower threshold of noise in mV',   0.5,...
        'Loading Effect of Picoscope',      1,...
        {'Use all the waveforms?'; 'Check'}, [true, true],...
        'Number of Waveforms',              1);
        if strcmpi(Button, 'Cancel')  
            disp('Error! Please enter the required parameters')
            return
        end
        Cavitometer_position = Input.CavitometerPosition;
        ProbeType = Input.Probe;
        PreAmpType = Input.PreAmplifierType; 
        Loading_Effect = Input.LoadingEffectOfPicoscope; 
        fs = (Input.SamplingRateMegaSamplePerSec)*1e6;
        Noise_up        = Input.UpperThresholdOfNoiseInMV*1e-3;
        Noise_down      = Input.LowerThresholdOfNoiseInMV*1e-3;
        MATLAB_Version = Input.MATLABVersion;       
    elseif strcmpi(Probe_Class, 'Fibre-Optic Hydrophone') 
        [Input, Button] = settingsdlg(...
        'Description',                       'Input all the required parameters/variables',...
        'title'      ,                       'FIBRE-OPTIC HYDROPHONE',...
        'Probe'      ,                       {'Silver','Gold','10T','11T','12T','14T','15T','16T','17T',},...                            
        'Sampling Rate Mega Sample per sec', 500,...
        'Upper threshold of noise in mV',    10,...
        'Lower threshold of noise in mV',    10,...
        'Loading Effect of Picoscope',       1,...
        'MATLAB Version' ,                   {'R2018b','Older Versions'},...               
        {'Use all the waveforms?'; 'Check'}, [true, true],...
        'Number of Waveforms',              1);
        if strcmpi(Button, 'Cancel')  
            disp('Error! Please enter the required parameters')
            return
        end
        Loading_Effect  = Input.LoadingEffectOfPicoscope; 
        fs              = (Input.SamplingRateMegaSamplePerSec)*1e6;
        Noise_up        = Input.UpperThresholdOfNoiseInMV*1e-3;
        Noise_down      = Input.LowerThresholdOfNoiseInMV*1e-3;
        MATLAB_Version  = Input.MATLABVersion;
        ProbeType       = Input.Probe;
    elseif strcmpi(Probe_Class, 'Needle Hydrophone') 
        [Input, Button] = settingsdlg(...
        'Description',                       'Input all the required parameters/variables',...
        'title'      ,                       'NEEDLE HYDROPHONE',...
        'Probe'      ,                       {'Needle Hydrophone1','Needle Hydrophone2','Needle Hydrophone3','Needle Hydrophone4'},...                            
        'Sampling Rate Mega Sample per sec', 500,...
        'Upper threshold of noise in mV',    10,...
        'Lower threshold of noise in mV',    10,...
        'Loading Effect of Picoscope',       1,...
        'MATLAB Version' ,                   {'R2018b','Older Versions'},...               
        {'Use all the waveforms?'; 'Check'}, [true, true],...
        'Number of Waveforms',              1);
        if strcmpi(Button, 'Cancel')  
            disp('Error! Please enter the required parameters')
            return
        end
        Loading_Effect  = Input.LoadingEffectOfPicoscope; 
        fs              = (Input.SamplingRateMegaSamplePerSec)*1e6;
        Noise_up        = Input.UpperThresholdOfNoiseInMV*1e-3;
        Noise_down      = Input.LowerThresholdOfNoiseInMV*1e-3;
        MATLAB_Version  = Input.MATLABVersion;
        ProbeType       = Input.Probe;
    end
        case 2
    if strcmpi(Probe_Class, 'Cavitometer')
        [Input, ~] = settingsdlg(...
        'Description',                      'Input all the required parameters/variables',...
        'title'      ,                      'CAVITOMETER',...
        'Cavitometer Position',             {'Under','Side'},...
        'Probe' ,                           {'Probe2','Probe3_Shunt','Probe3_Attenuator','Probe4'},...               
        'Pre Amplifier Type' ,              {'B','A'},...               
        'MATLAB Version' ,                  {'R2018b','Older Versions'},...               
        'Sampling Rate Mega Sample per sec',500,...
        'Noise File Name in csv',           'Noise',...
        'Loading Effect of Picoscope',      1,...
        {'Use all the waveforms?'; 'Check'}, [true, true],...
        'Number of Waveforms',              1);
        if strcmpi(Button, 'Cancel')  
            disp('Error! Please enter the required parameters')
            return
        end
        Cavitometer_position = Input.CavitometerPosition;
        ProbeType = Input.Probe;
        PreAmpType = Input.PreAmplifierType; 
        Loading_Effect = Input.LoadingEffectOfPicoscope; 
        fs = (Input.SamplingRateMegaSamplePerSec)*1e6;
        NoiseFileName = [Input.NoiseFileNameInCsv  '.csv'];
        MATLAB_Version = Input.MATLABVersion;       
    elseif strcmpi(Probe_Class, 'Fibre-Optic Hydrophone') 
        [Input, Button] = settingsdlg(...
        'Description',                       'Input all the required parameters/variables',...
        'title'      ,                       'FIBRE-OPTIC HYDROPHONE',...
        'Probe'      ,                       {'Silver','Gold','10T','11T','12T','14T','15T','16T','17T',},...                            
        'Sampling Rate Mega Sample per sec', 500,...
        'Noise File Name in csv',           'Noise',...
        'Loading Effect of Picoscope',       1,...
        'MATLAB Version' ,                   {'R2018b','Older Versions'},...               
        {'Use all the waveforms?'; 'Check'}, [true, true],...
        'Number of Waveforms',              1);
        if strcmpi(Button, 'Cancel')  
            disp('Error! Please enter the required parameters')
            return
        end
        Loading_Effect  = Input.LoadingEffectOfPicoscope; 
        fs              = (Input.SamplingRateMegaSamplePerSec)*1e6;
        NoiseFileName = [Input.NoiseFileNameInCsv  '.csv'];
        MATLAB_Version  = Input.MATLABVersion;
        ProbeType       = Input.Probe;
    elseif strcmpi(Probe_Class, 'Needle Hydrophone') 
        [Input, Button] = settingsdlg(...
        'Description',                       'Input all the required parameters/variables',...
        'title'      ,                       'NEEDLE HYDROPHONE',...
        'Probe'      ,                       {'Needle Hydrophone1','Needle Hydrophone2','Needle Hydrophone3','Needle Hydrophone4'},...                            
        'Sampling Rate Mega Sample per sec', 500,...
        'Noise File Name in csv',           'Noise',...
        'Loading Effect of Picoscope',       1,...
        'MATLAB Version' ,                   {'R2018b','Older Versions'},...               
        {'Use all the waveforms?'; 'Check'}, [true, true],...
        'Number of Waveforms',              1);
        if strcmpi(Button, 'Cancel')  
            disp('Error! Please enter the required parameters')
            return
        end
        Loading_Effect  = Input.LoadingEffectOfPicoscope; 
        fs              = (Input.SamplingRateMegaSamplePerSec)*1e6;
        NoiseFileName = [Input.NoiseFileNameInCsv  '.csv'];
        MATLAB_Version  = Input.MATLABVersion;
        ProbeType       = Input.Probe;
    end
    end
    
        case 2  % Data Acquisition by PCI Board  
    [Input, Button] = settingsdlg(...
    'Description'        , 'Input Data',...
    'title'              , 'Choose the desired cases',...
    'Probe'              , {'Cavitometer','Fibre-Optic Hydrophone','Needle Hydrophone'},...
    'Noise Subtraction'  , {'Time Domain','Frequency Domain'},...
    'Apply Filter'       , {'Yes','No'},...
    'Save Plots'         , {'Yes','No'},...
    'Save Data'          , {'Yes','No'},...
    'Save in Excel'      , {'Yes','No'});
    route1        = cd;
    Probe_Class   = Input.Probe;
    NoiseMethod1  = Input.NoiseSubtraction;
    FilterMethod1 = Input.ApplyFilter;
    Save_Plots    = Input.SavePlots;
    Save_Data     = Input.SaveData;
    Save_Excel    = Input.SaveInExcel;
    NWF1 = 1;                                      % For PCI board, it must be 1  
    if strcmpi(Button, 'Cancel')  
        disp('Error! Please enter the required parameters')
        return
    end
   Multi_Method = 1;                               % For PCI board, only Single Method is used
    if strcmpi(NoiseMethod1, 'Time Domain')  
       Noise_Method = 1;
    else
        Noise_Method = 2;       
    end
    if strcmpi(FilterMethod1, 'Yes')  
       Filter_Method = 1;
    else
        Filter_Method = 2;       
    end
    BoardFileName = uigetfile('*.csv');             % Open the csv file of PCI board via UI    
    switch Noise_Method
        case 1
    if strcmpi(Probe_Class, 'Cavitometer')
        [Input, Button] = settingsdlg(...
        'Description',                      'Input all the required parameters/variables',...
        'title'      ,                      'CAVITOMETER',...
        'Cavitometer Position',             {'Under','Side'},...
        'Probe' ,                           {'Probe2','Probe3_Shunt','Probe3_Attenuator','Probe4'},...               
        'Pre Amplifier Type' ,              {'B','A'},...               
        'Upper threshold of noise in mV',   0.5,...
        'Lower threshold of noise in mV',   0.5,...
        'MATLAB Version' ,                  {'R2018b','Older Versions'});               
        if strcmpi(Button, 'Cancel')  
            disp('Error! Please enter the required parameters')
            return
        end
        Cavitometer_position = Input.CavitometerPosition;
        ProbeType = Input.Probe;
        PreAmpType = Input.PreAmplifierType; 
        Loading_Effect = 1; 
        fs = csvread(BoardFileName,17,2,[17,2,17,2]);                   % Read the sampling rate from csv file
        Noise_up        = Input.UpperThresholdOfNoiseInMV*1e-3;
        Noise_down      = Input.LowerThresholdOfNoiseInMV*1e-3;
        MATLAB_Version = Input.MATLABVersion;       
    elseif strcmpi(Probe_Class, 'Fibre-Optic Hydrophone') 
        [Input, Button] = settingsdlg(...
        'Description',                       'Input all the required parameters/variables',...
        'title'      ,                       'FIBRE-OPTIC HYDROPHONE',...
        'Probe'      ,                       {'Silver','Gold','10T','11T','12T','14T','15T','16T','17T',},...                            
        'Upper threshold of noise in mV',    10,...
        'Lower threshold of noise in mV',    10,...
        'MATLAB Version' ,                   {'R2018b','Older Versions'});               
        if strcmpi(Button, 'Cancel')  
            disp('Error! Please enter the required parameters')
            return
        end
        Loading_Effect  = 1; 
        fs = csvread(BoardFileName,17,2,[17,2,17,2]);                   % Read the sampling rate from csv file
        Noise_up        = Input.UpperThresholdOfNoiseInMV*1e-3;
        Noise_down      = Input.LowerThresholdOfNoiseInMV*1e-3;
        MATLAB_Version  = Input.MATLABVersion;
        ProbeType       = Input.Probe;
    elseif strcmpi(Probe_Class, 'Needle Hydrophone') 
        [Input, Button] = settingsdlg(...
        'Description',                       'Input all the required parameters/variables',...
        'title'      ,                       'NEEDLE HYDROPHONE',...
        'Probe'      ,                       {'Needle Hydrophone1','Needle Hydrophone2','Needle Hydrophone3','Needle Hydrophone4'},...                            
        'Upper threshold of noise in mV',    10,...
        'Lower threshold of noise in mV',    10,...
        'MATLAB Version' ,                   {'R2018b','Older Versions'});               
        if strcmpi(Button, 'Cancel')  
            disp('Error! Please enter the required parameters')
            return
        end
        Loading_Effect  = 1; 
        fs = csvread(BoardFileName,17,2,[17,2,17,2]);                   % Read the sampling rate from csv file
        Noise_up        = Input.UpperThresholdOfNoiseInMV*1e-3;
        Noise_down      = Input.LowerThresholdOfNoiseInMV*1e-3;
        MATLAB_Version  = Input.MATLABVersion;
        ProbeType       = Input.Probe;
    end
        case 2
    if strcmpi(Probe_Class, 'Cavitometer')
        [Input, ~] = settingsdlg(...
        'Description',                      'Input all the required parameters/variables',...
        'title'      ,                      'CAVITOMETER',...
        'Cavitometer Position',             {'Under','Side'},...
        'Probe' ,                           {'Probe2','Probe3_Shunt','Probe3_Attenuator','Probe4'},...               
        'Pre Amplifier Type' ,              {'B','A'},...               
        'Noise File Name in csv',           'Noise',...
        'MATLAB Version' ,                  {'R2018b','Older Versions'});               
        if strcmpi(Button, 'Cancel')  
            disp('Error! Please enter the required parameters')
            return
        end
        Cavitometer_position = Input.CavitometerPosition;
        ProbeType = Input.Probe;
        PreAmpType = Input.PreAmplifierType; 
        Loading_Effect = 1; 
        fs = csvread(BoardFileName,17,2,[17,2,17,2]);                   % Read the sampling rate from csv file
        NoiseFileName = [Input.NoiseFileNameInCsv  '.csv'];
        MATLAB_Version = Input.MATLABVersion;       
    elseif strcmpi(Probe_Class, 'Fibre-Optic Hydrophone') 
        [Input, Button] = settingsdlg(...
        'Description',                       'Input all the required parameters/variables',...
        'title'      ,                       'FIBRE-OPTIC HYDROPHONE',...
        'Probe'      ,                       {'Silver','Gold','10T','11T','12T','14T','15T','16T','17T',},...                            
        'Noise File Name in csv',            'Noise',...
        'MATLAB Version' ,                   {'R2018b','Older Versions'});               
        if strcmpi(Button, 'Cancel')  
            disp('Error! Please enter the required parameters')
            return
        end
        Loading_Effect  = 1; 
        fs = csvread(BoardFileName,17,2,[17,2,17,2]);                   % Read the sampling rate from csv file
        NoiseFileName = [Input.NoiseFileNameInCsv  '.csv'];
        MATLAB_Version  = Input.MATLABVersion;
        ProbeType       = Input.Probe;
    elseif strcmpi(Probe_Class, 'Needle Hydrophone') 
        [Input, Button] = settingsdlg(...
        'Description',                       'Input all the required parameters/variables',...
        'title'      ,                       'NEEDLE HYDROPHONE',...
        'Probe'      ,                       {'Needle Hydrophone1','Needle Hydrophone2','Needle Hydrophone3','Needle Hydrophone4'},...                            
        'Noise File Name in csv',           'Noise',...
        'MATLAB Version' ,                   {'R2018b','Older Versions'});              
        if strcmpi(Button, 'Cancel')  
            disp('Error! Please enter the required parameters')
            return
        end
        Loading_Effect  = 1; 
        fs = csvread(BoardFileName,17,2,[17,2,17,2]);                   % Read the sampling rate from csv file
        NoiseFileName = [Input.NoiseFileNameInCsv  '.csv'];
        MATLAB_Version  = Input.MATLABVersion;
        ProbeType       = Input.Probe;
    end
    end
    end
    
end
switch Multi_Method
    case 1          % Single Case
      NFiles = 1;   % Number of cases
    case 2          % Multi Case
      main_route = route1; 
      File_Pattern1 = fullfile(main_route);All_Files1 = dir(File_Pattern1);
      NFiles = length(All_Files1)-2;   
end
warning off
marker_font = 24;font_size = 20;font_size2 = 24;box_thickness = 3;Line_thickness = 1; 
save Input_Data
for u = 1:NFiles    
load Input_Data
switch Multi_Method
    case 1            % Single Case
        route = route1;
    case 2            % Multi Cases
        disp(NFiles-u)
        route = [File_Pattern1 '\' All_Files1(u+2).name]
end
%% Find the calibration range for each probe class and type
switch Probe_Class
    case 'Cavitometer'
    switch ProbeType 
        case 'Probe2'
            fmin = 15e3;                % in Hz           
            fmax = 50e3;                % in Hz           
        case 'Probe3_Shunt'
            fmin = 15e3;                % in Hz           
            fmax = 50e3;                % in Hz           
        case 'Probe3_Attenuator'
            fmin = 8e3;                 % in Hz           
            fmax = 400e3;               % in Hz           
        case 'Probe4'
            fmin = 8e3;                 % in Hz           
            fmax = 400e3;               % in Hz           
    end
    case 'Fibre-Optic Hydrophone'
    switch Data_Method  
        case 1   % Data Acquisition by PicosCope
            switch ProbeType 
                case 'Gold'                             
                    fmin = 1e6;                 % in Hz           
                    fmax = 30e6;                % in Hz           
                case 'Silver' 
                    fmin = 1e6;                 % in Hz           
                    fmax = 30e6;                % in Hz           
                case '10T' 
                    fmin = 1e6;                 % in Hz           
                    fmax = 30e6;                % in Hz           
                case '11T' 
                    fmin = 1e6;                 % in Hz           
                    fmax = 30e6;                % in Hz           
                case '12T' 
                    fmin = 1e6;                 % in Hz           
                    fmax = 30e6;                % in Hz           
                case '14T' 
                    fmin = 1e6;                 % in Hz           
                    fmax = 30e6;                % in Hz           
                case '15T' 
                    fmin = 1e6;                 % in Hz           
                    fmax = 30e6;                % in Hz           
                case '16T' 
                    fmin = 1e6;                 % in Hz           
                    fmax = 30e6;                % in Hz           
                case '17T' 
                    fmin = 1e6;                 % in Hz           
                    fmax = 30e6;                % in Hz 
            end
            if      (floor(fs/2)<fmax)
                    fmax = floor(fs/2);         % in Hz                          
            end                               
        case 2  % Data Acquisition by PCI Board
            switch ProbeType 
                case 'Gold'                             
                    fmin = 1e6;                 % in Hz           
                    fmax = floor(fs/2);         % in Hz           
                case 'Silver' 
                    fmin = 1e6;                 % in Hz           
                    fmax = floor(fs/2);         % in Hz           
                case '10T' 
                    fmin = 1e6;                 % in Hz           
                    fmax = floor(fs/2);         % in Hz           
                case '11T' 
                    fmin = 1e6;                 % in Hz           
                    fmax = floor(fs/2);         % in Hz           
                case '12T' 
                    fmin = 1e6;                 % in Hz           
                    fmax = floor(fs/2);         % in Hz           
                case '14T' 
                    fmin = 1e6;                 % in Hz           
                    fmax = floor(fs/2);         % in Hz           
                case '15T' 
                    fmin = 1e6;                 % in Hz           
                    fmax = floor(fs/2);         % in Hz           
                case '16T' 
                    fmin = 1e6;                 % in Hz           
                    fmax = floor(fs/2);         % in Hz           
                case '17T' 
                    fmin = 1e6;                 % in Hz           
                    fmax = floor(fs/2);         % in Hz           
            end
    end
    case 'Needle Hydrophone'
    switch ProbeType  
    case 'Needle Hydrophone1'
            fmin = 8e3;                 % in Hz           
            fmax = 400e3;               % in Hz           
    case 'Needle Hydrophone2'
            fmin = 8e3;                 % in Hz           
            fmax = 400e3;               % in Hz           
    case 'Needle Hydrophone3'
            fmin = 8e3;                 % in Hz           
            fmax = 400e3;               % in Hz           
    case 'Needle Hydrophone4'
            fmin = 8e3;                 % in Hz           
            fmax = 400e3;               % in Hz           
    end
end
%% Filter Design
% Each probe with different calibration needs a different filter. 
% If the filter becomes unstable, play with values of Wdiff (and so Ws) and Rs
switch Filter_Method
    case 1
        Fn = fs/2;
        switch Probe_Class
            case 'Cavitometer'
            switch ProbeType 
                case 'Probe2'
                    Wp = fmax/Fn;               % Normalised Passband Frequency (in Hz)
                    Wdiff = 50e3;               % Length of transition region of the filter (in Hz)
                    Ws = (Wdiff+fmax)/Fn;       % Normalised Stopband 
                    Rp =  1;                    % Passband Ripple
                    Rs = 20;                    % Stopband Ripple (Rs>Rp)
                case 'Probe3_Shunt'
                    Wp = fmax/Fn;               % Normalised Passband Frequency (in Hz)
                    Wdiff = 50e3;               % Length of transition region of the filter (in Hz)
                    Ws = (Wdiff+fmax)/Fn;       % Normalised Stopband 
                    Rp =  1;                    % Passband Ripple
                    Rs = 20;                    % Stopband Ripple (Rs>Rp)
                case 'Probe3_Attenuator'
                switch Data_Method
                    case 1              % Data Acquisition by PicosCope
                    Wp = fmax/Fn;               % Normalised Passband Frequency (in Hz)
                    Wdiff = 100e3;              % Length of transition region of the filter (in Hz)
                    Ws = (Wdiff+fmax)/Fn;       % Normalised Stopband 
                    Rp =  1;                    % Passband Ripple
                    Rs = 20;                    % Stopband Ripple (Rs>Rp)
                    case 2              % Data Acquisition by PCI Board
                    Wp = fmax/Fn;               % Normalised Passband Frequency (in Hz)
                    Wdiff = 50e3;               % Length of transition region of the filter (in Hz)
                    Ws = (Wdiff+fmax)/Fn;       % Normalised Stopband 
                    Rp =  1;                    % Passband Ripple
                    Rs = 20;                    % Stopband Ripple (Rs>Rp)
                end
                case 'Probe4'
                switch Data_Method
                    case 1              % Data Acquisition by PicosCope
                    Wp = fmax/Fn;               % Normalised Passband Frequency (in Hz)
                    Wdiff = 100e3;              % Length of transition region of the filter (in Hz)
                    Ws = (Wdiff+fmax)/Fn;       % Normalised Stopband 
                    Rp =  1;                    % Passband Ripple
                    Rs = 20;                    % Stopband Ripple (Rs>Rp)
                    case 2              % Data Acquisition by PCI Board
                    Wp = fmax/Fn;               % Normalised Passband Frequency (in Hz)
                    Wdiff = 50e3;               % Length of transition region of the filter (in Hz)
                    Ws = (Wdiff+fmax)/Fn;       % Normalised Stopband 
                    Rp =  1;                    % Passband Ripple
                    Rs = 20;                    % Stopband Ripple (Rs>Rp)
                end
            end
            case 'Needle Hydrophone'
                switch Data_Method
                    case 1              % Data Acquisition by PicosCope
                    Wp = fmax/Fn;               % Normalised Passband Frequency 
                    Wdiff = 100e3;              % Length of transition region of the filter (in Hz)
                    Ws = (Wdiff+fmax)/Fn;       % Normalised Stopband 
                    Rp =  1;                    % Passband Ripple
                    Rs = 20;                    % Stopband Ripple (Rs>Rp)
                    case 2              % Data Acquisition by PCI Board
                    Wp = fmax/Fn;               % Normalised Passband Frequency 
                    Wdiff = 50e3;               % Length of transition region of the filter (in Hz)
                    Ws = (Wdiff+fmax)/Fn;       % Normalised Stopband 
                    Rp =  1;                    % Passband Ripple
                    Rs = 20;                    % Stopband Ripple (Rs>Rp)
                end                        
        end       
end
%% Read Noise 
switch Noise_Method
    case 2
        switch Data_Method
            case 1   % Read data from PicosCope
            M1 = csvread(NoiseFileName,3,0);
            fileID = fopen(NoiseFileName);
            C1 = textscan(fileID,'%s %s','Delimiter',',');fclose(fileID);
            if (strcmpi(C1{2}{2}, '(V)'))                         
                disp(['Unit of Noise is ' C1{2}{2}]); 
                Noise = M1(:,2);                               % Noise in V  
            elseif(strcmpi(C1{2}{2}, '(mV)')) 
                Noise = 0.001.*M1(:,2);                        % Noise in V  
            end
            case 2  % Read data from PCI Board
            M1 = csvread(NoiseFileName,24,0);                  % Noise should be in volt. t from Noise file is not read
            Noise = M1(:,4);
        end
    clear M1
    switch Filter_Method
        case 1                                         
        if (strcmpi(Probe_Class, 'Fibre-Optic Hydrophone'))    % use bandpass filter for FOH and low pass for other probes   
            Noise2      = bandpass(Noise,[fmin fmax],fs);      % Noise after filtering (band pass for FOH)
        else
            Noise2      = filtfilt(sos, g, Noise);             % Noise after filtering (low pass for other probes) 
        end           
        FT_Noise        = fft(Noise2);                         % The next few lines will be used for Noise sibtraction in freq domain
        SS_FT_Noise     = DS2SS(FT_Noise);
        FT_NF_Noise     = fft(Noise);                          % Even with filter, we calculate fft of noise for without filter case
        SS_FT_NF_Noise  = DS2SS(FT_NF_Noise);           
        case 2                                                 % Without Applying Filter 
        FT_NF_Noise     = fft(Noise);
        SS_FT_NF_Noise  = DS2SS(FT_NF_Noise);           
    end
end
disp('Reading Signals')
%% Read V-t data from csv
switch Data_Method    
case 1              % Data Acquisition by PicosCope
format long
dt=1/fs; 
start_counter = 1;
switch MATLAB_Version
    case 'R2018b'                                       % If using MATLAB R2018b or newer versions (5x faster) 
    File_Pattern = fullfile(route,'*.csv');
    All_Files = dir(File_Pattern);FileRouteName = fullfile(route, All_Files(start_counter).name);
    fileID = fopen(FileRouteName);
    C = textscan(fileID,'%s %s','Delimiter',',');
    fclose(fileID);
    if(isempty(C{1}{3}))                                % Check if the empty (third) row in first csv file is ignored or not    
        L0 = length(C{1})-1;                            % Size of original data (if the empty row is not automatically ignored)
    else
        L0 = length(C{1});                              % Size of original data (if the empty row is automatically ignored)
    end
    switch Input_Method
        case 1        
        if(Input.Check == 1)                            % Check whether to use all the number of waveeforms or the ones specified by user
            NWF = length(All_Files);
        elseif (Input.Check ~= 1)
            NWF = Input.NumberOfWaveforms; 
        end
        case 2        
            NWF = min ([length(All_Files) NWF1]);
    end
    if (length(All_Files)<10)                           % check if there is less than 10 waveforms (to keep the original name intact)
        format1 = All_Files(start_counter).name(1:end-6);
    else
        format1 = All_Files(start_counter).name(1:end-7);
    end
    if (length(All_Files)==1)                           % check if there is less than 10 waveforms (to keep the original name intact)
        format1 = All_Files(start_counter).name(1:end-4);
    end
    L1 =  L0-2;                                         % Size of rows containing data  (without two first non-numeric rows)
    tV  = zeros(1,L0);                                  % Preallocation to L0        
    M   = zeros(1,L0);                                  % Preallocation to L0       
    Vt1 = zeros(L1,NWF);                                % Preallocation to L1 (because first two rows are already removed in M)
    for i = start_counter:NWF
        disp(i)
        FileRouteName = fullfile(route, All_Files(i).name);
        opts = delimitedTextImportOptions("NumVariables", 2);    
        opts.VariableTypes(:) = {'double'};opts.Delimiter = ",";opts.ExtraColumnsRule = "ignore";
        opts.EmptyLineRule = "read";D = readtable(FileRouteName, opts);
        D = D{:,:};     
        fileID = fopen(FileRouteName);
        C = textscan(fileID,'%s %s','Delimiter',',');
        fclose(fileID); 
        if (strcmpi(C{2}{2}, '(V)'))                    
            disp(['Unit is ' C{2}{2}]); 
            M = D(:,2);                                 % no need to convert to V     
            M(isnan(M))=0;                              % convert NaN values to zero
            M(1:3) = [];                                % remove first three rows (no data)
        elseif (strcmpi(C{2}{2}, '(mV)'))                     
            M = 0.001.*D(:,2);                          % convert to V      
            M(isnan(M))=0;                              % convert NaN values to zero
            M(1:3) = [];                                % remove first three rows (no data)
        end
        Vt1(:,i) = M;                                   % Assign to main Voltage Matrix
        if (i==1)
            tV = 0.001.*D(:,1);                         % READ TIME data (only once)
        end    
            clear D M
        M   = zeros(1,L0);                              % Preallocation to L0       
    end
    tV(1:3) = [];                                       % remove first three rows (no data)
    case 'Older Versions'                               % If using MATLAB versions older than R2018b (5x slower)
        File_Pattern = fullfile(route,'*.csv');
        All_Files = dir(File_Pattern);FileRouteName = fullfile(route, All_Files(start_counter).name);
        fileID = fopen(FileRouteName);
        C = textscan(fileID,'%s %s','Delimiter',',');
        fclose(fileID);
        if(isempty(C{1}{3}))                            % Check if the empty (third) row in first csv file is ignored or not    
            L0 = length(C{1})-1;                        % Size of original data (if the empty row is not automatically ignored)
        else
            L0 = length(C{1});                          % Size of original data (if the empty row is automatically ignored)
        end                                          
        switch Input_Method
            case 1        
            if(Input.Check == 1)                        % Check whether to use all number of waveeforms or the ones specified by user
                NWF = length(All_Files);
            elseif (Input.Check ~= 1)
                NWF = Input.NumberOfWaveforms; 
            end
            case 2        
                NWF = length(All_Files);
        end
        if (length(All_Files)<10)                       % check if there is less than 10 waveforms (to keep the original name intact)
            format1 = All_Files(start_counter).name(1:end-6);
        else
            format1 = All_Files(start_counter).name(1:end-7);
        end    
        if (length(All_Files)==1)                       % check if there is less than 10 waveforms (to keep the original name intact)
            format1 = All_Files(start_counter).name(1:end-4);
        end
        L1 =  L0-2;                                     % Size of rows containing data  (without two first non-numeric rows)
        tV  = zeros(1,L0);                              % Preallocation to L0        
        M   = zeros(1,L0);                              % Preallocation to L0       
        Vt1 = zeros(L1,NWF);                            % Preallocation to L1 (because first two rows are already removed in M)
        %%% READ TIME
        for j = 3:L0
            tV(j) = 0.001.*sscanf(C{1}{j},'%f');        % convert to seconds 
        end
        tV(1:2) = [];                                   % remove first two rows (no data)
        %%% READ VOLTAGE 
        for i = start_counter:NWF
            disp(i)
            FileRouteName = fullfile(route, All_Files(i).name);
            fileID = fopen(FileRouteName);
            C = textscan(fileID,'%s %s','Delimiter',',');
            fclose(fileID); 
            if (strcmpi(C{2}{2}, '(V)'))                % if length is 3 it means original voltage is in V, if it is 4 it means voltage is in mV
                disp(['Unit is ' C{2}{2}]); 
                for j = 1:length(C{1})
                    so  = sscanf(C{2}{j},'%f'); 
                    if ~isempty(so)
                       M(j) = so;
                    end
                end
                M(isnan(M))=0;                          % convert NaN values to Zero
                M(1:2) = [];                            % remove first two rows (no data)
            elseif (strcmpi(C{2}{2}, '(mV)'))                     
                for j = 1:length(C{1})        
                    so  = 0.001.*sscanf(C{2}{j},'%f'); 
                    if ~isempty(so)
                       M(j) = so;
                    end
                end
                M(isnan(M))=0;                          % convert NaN values to Zero
                M(1:2) = [];                            % remove first two rows (no data)
            end
            Vt1(:,i) = M;
            clear M
            M   = zeros(1,L0);                          % Preallocation to L0       
        end
end
% Check if the number of original data points is odd, if yes, ignore the last data for tV and Vt
if (mod(L1,2)~=0)
    L1        = L1-1;                                   % if number of data point is odd
    tV(end)   = [];                                     % ignore the last data point
    y1_temp = zeros(L1,NWF);
    for i=1:NWF
        y1 = Vt1(:,i);
        y1(end) = [];
        y1_temp(:,i) = y1;
        clear y1
    end
    clear Vt1
    Vt1 = y1_temp;
end
clear y1_temp
L2 = L1/2+1;                                            % Size of single sided spectrum
bin_vals = (0 : L1-1);
fV = bin_vals*(fs/L1);                                  % in Hz
ind_min = find(fV>fmin,1,'first')-1;                    % First index of calibration range 
ind_max = find(fV<fmax,1, 'last');                      % Last index of calibration range

case 2         % Data Acquisition by PCI Board                     
format1 = BoardFileName(1:end-4);
M2 = csvread(BoardFileName,24,0);
fs = csvread(BoardFileName,17,2,[17,2,17,2]);
FR = csvread(BoardFileName,8,2,[8,2,8,2]);
tV  = M2(:,1)-M2(1);
dt = 1/fs;
tV(1) = 0;
for i = 2:size(M2(:,1))
    tV(i) = tV(i-1)+dt;
end
Vt1 = M2(:,4);
if mod(length(Vt1),2)~=0
    Vt1 = Vt1(1:end-1);
    tV = tV(1:end-1);
end
NWF = NWF1;
L1 = length(M2);
L2 = L1/2+1;                                            % Size of single sided spectrum
bin_vals = (0 : L1-1);
fV = bin_vals*(fs/L1);                                  % in Hz
f1 = fmin:1e6:fmax;         % in Hz
ind_min = find(fV>fmin,1,'first')-1;                      % First index of calibration range 
ind_max = find(fV<fmax,1, 'last');                      % Last index of calibration range
end
%% Sensitivity Interpolation 
switch Probe_Class
    case 'Cavitometer'
    switch ProbeType 
        
        case 'Probe2'
        [f1,S_Original,S_1MHz] = Sensitivity_CAV(Cavitometer_position,ProbeType);                 % Sensitivity values from Sensitivity function
        [AF_Original,AF_1MHz]  = AF_CAV(Cavitometer_position,ProbeType,PreAmpType);               % Amplification values from AF function (only for probe 2)
        S1 = zeros(L2,1);                             % create the sensitivity vector
        AF = zeros(L2,1);                             % create the AF vector
        S1(1:ind_min-1)   = S_Original(1);            % assign all values before fmin to the mean value 
        S1(ind_max+1:end) = S_Original(end);          % assign all values after fmax to the mean value 
        AF(1:ind_min-1)   = AF_Original(1);           % assign all values before fmin to the mean
        AF(ind_max+1:end) = AF_Original(end);         % assign all values after fmax to the mean value 
        for i = ind_min:1:ind_max   
            S1(i)  = interp1(f1,S_Original,fV(i),'PCHIP');
            AF(i)  = interp1(f1,AF_Original,fV(i),'PCHIP');
        end
        S = S1./(AF.*Loading_Effect);
        S(find(fV>1e3,1,'first')) = S_1MHz/(AF_1MHz*Loading_Effect);

        case 'Probe3_Shunt'
        [f1,S_Original,S_1MHz] = Sensitivity_CAV(Cavitometer_position,ProbeType);     
        [AF_Original,AF_1MHz]  = AF_CAV(Cavitometer_position,ProbeType,PreAmpType);   
        S1 = zeros(L2,1);                             % create the sensitivity vector
        AF = zeros(L2,1);                             % create the AF vector
        S1(1:ind_min-1)   = S_Original(1);            % assign all values before fmin to the mean value 
        S1(ind_max+1:end) = S_Original(end);          % assign all values after fmax to the mean value 
        AF(1:ind_min-1)   = AF_Original(1);           % assign all values before fmin to the mean
        AF(ind_max+1:end) = AF_Original(end);         % assign all values after fmax to the mean value 
        for i = ind_min:1:ind_max   
            S1(i)  = interp1(f1,S_Original,fV(i),'PCHIP');
            AF(i)  = interp1(f1,AF_Original,fV(i),'PCHIP');
        end
        S = S1./(AF.*Loading_Effect);
        S(find(fV>1e3,1,'first')) = S_1MHz/(AF_1MHz*Loading_Effect);
 
        case 'Probe3_Attenuator'
        [f1,S_Original] = Sensitivity_CAV(Cavitometer_position,ProbeType);                         
        S1 = zeros(L2,1);                             % create the sensitivity vector
        S1(1:ind_min-1)   = S_Original(1);            % assign all values before fmin to the mean value 
        S1(ind_max+1:end) = S_Original(end);          % assign all values after fmax to the mean value 
        for i = ind_min:1:ind_max   
            S1(i)  = interp1(f1,S_Original,fV(i),'PCHIP');
        end
        S = S1./Loading_Effect;
 
        case 'Probe4'
        [f1,S_Original] = Sensitivity_CAV(Cavitometer_position,ProbeType);    
        S1 = zeros(L2,1);                           % create the sensitivity vector
        S1(1:ind_min-1)   = S_Original(1);          % assign all values before fmin to the mean value 
        S1(ind_max+1:end) = S_Original(end);        % assign all values after fmax to the mean value 
        for i = ind_min:1:ind_max   
            S1(i)  = interp1(f1,S_Original,fV(i),'PCHIP');
        end
        S = S1./Loading_Effect;
    end
    
    case 'Fibre-Optic Hydrophone'
    switch ProbeType 
    
        case 'Gold'                             
            f1 = fmin:1e6:fmax;                                 % in Hz
            S_Original = 1e-9.*[88.6865	116.769	149.379	188.043	235.177	267.947	295.265	340.59	420.472	381.795	352.388	447.799	405.218	219.771	261.289	332.772	401.501	186.622	230.063	216.796	226.13	285.709	332.434	353.535	325.738	300.518	299.879	278.411	264.042	319.394];

        case 'Silver'                           
            f1 = fmin:1e6:fmax;                                 % in Hz
            S_Original = 1e-9.*[154.488	183.76	235.792	283.117	321.603	345.314	330.164	335.531	562.665	486.585	420.343	624.699	550.421	539.225	554.477	349.248	265.096	408.709	208.393	349.235	387.905	568.605	566.198	424.067	299.041	419.107	367.134	588.476	460.728	268.767];

        case '10T'                           
            f1 = fmin:1e6:fmax;                                 % in Hz
            S_Original = 1e-9.*[256.307	282.425	309.992	353.795	398.284	428.797	464.433	499.977	558.636	649.384	597.184	623.976	690.781	711.75	780.066	778.891	686.172	487.769	563.25	1120.55	1010.68	756.805	640.464	666.229	683.299	699.514	735.52	733.858	558.253	572.352];

        case '11T'                           
            f1 = fmin:1e6:fmax;                                 % in Hz
            S_Original = 1e-9.*[318.028	349.322	398.582	448.177	486.613	545.975	594.643	638.928	696.453	736.341	774.401	805.312	809.178	815.294	839.521	849.01	841.159	795.828	483.747	1140.2	1133.31	983.453	838.554	815.584	857.379	851.787	867.713	826.492	784.866	757.892];
   
        case '12T'                           
            f1 = fmin:1e6:fmax;                                 % in Hz
            S_Original = 1e-9.*[388.047	433.923	495.97	565.526	624.555	700.826	760.779	810.706	876.91	925.975	964.536	986.92	1017.12	1022.18	1038.78	1037.86	1032.38	975.344	547.097	1070.04	1202.76	1175.71	1065.58	1025.95	1002.17	998.858	1045.53	1052.51	962.867	933.43];
   
        case '14T'                           
            f1 = fmin:1e6:fmax;                                 % in Hz
            S_Original = 1e-9.*[371.064	413.311	471.457	538.406	570.939	669.458	722.184	788.069	778.725	809.759	898.869	1001.42	956.697	923.695	887.33	829.618	876.142	943.934	436.67	896.22	1159.32	1153.97	1103.51	782.874	1015.42	966.9	1037.92	1064.62	994.881	942.889];
   
        case '15T'                           
            f1 = fmin:1e6:fmax;                                 % in Hz
            S_Original = 1e-9.*[346.39	383.916	451.416	515.427	591.687	657.23	715.513	781.914	809.747	870.973	862.04	910.573	906.476	890.088	886.189	881.81	879.117	859.586	258.374	773.845	960.207	982.795	874.376	869.717	881.538	916.421	920.115	891.504	868.854	918.966];
   
        case '16T'                           
            f1 = fmin:1e6:fmax;                                 % in Hz
            S_Original = 1e-9.*[362.299	397.101	454.389	501.665	550.503	617.588	678.569	739.049	814.933	879.285	923.197	998.696	1023.5	1136.69	1055.64	810.919	834.416	882.407	1043.52	1051.08	972.467	863.25	1100.15	1065.57	817.508	882.842	1000.95	962.898	1063.46	1067.25];
   
        case '17T'                           
            f1 = fmin:1e6:fmax;                                 % in Hz
            S_Original = 1e-9.*[302.038	334.443	382.696	425.03	475.705	511.62	556.954	592.71	640.956	678.286	708.15	741.682	750.574	752.637	755.832	765.194	756.918	692.961	510.526	1041.98	1020.12	893.444	781.777	783.685	772.32	754.523	775.191	827.821	782.321	732.581];
            
    end
    if length(f1)<length(S_Original)                                    % if sampling rate is lower than calibration range (30MHz), smaller range of calibration function should be used
            S_Original = S_Original(1:length(f1));
   end    
    switch Data_Method
        case 1    % Data Acquisition by PicosCope  
            
            S1 = zeros(L2,1);                                           % Preallocate the sensitivity vector
            S1(1:ind_min-1)   = S_Original(1);                          % assign all values before fmin to the mean value 
            S1(ind_max+1:end) = S_Original(end);                        % assign all values after fmax to the mean value 
            for i = ind_min:1:ind_max   
                S1(i)  = interp1(f1,S_Original,fV(i),'PCHIP');
            end
            S = S1./Loading_Effect;
        
        case 2    % Data Acquisition by PCI Board
            
            S1 = zeros(L2,1);                                           % Preallocate the sensitivity vector
            S_Original_Selected = S_Original(1:fmax/1e6);               % Since the sampling rate is less than the calibration range, we use a selected range of S upto fs/2
            S1(1:ind_min-1)   = S_Original_Selected(1);                 % assign all values before fmin to the mean value 
            S1(ind_max+1:end) = S_Original_Selected(end);               % assign all values after fmax to the mean value 
            for i = ind_min:1:ind_max   
                S1(i)  = interp1(f1,S_Original_Selected,fV(i),'PCHIP');
            end
            S = S1./Loading_Effect;
    end

    case 'Needle Hydrophone'
    switch ProbeType
        
        case 'Needle Hydrophone1'       
        f1 = 1000.*[8	8.5	9	9.5	10	12	14	16	18	20	22	24	26	28	30	32	34	36	38	40	45	50	55	60	65	70	75	80	85	90	95	100	105	110	115	120	125	130	135	140	145	150	155	160	165	170	175	180	185	190	195	200	205	210	215	220	225	230	235	240	245	250	255	260	265	270	275	280	285	290	295	300	305	310	315	320	325	330	335	340	345	350	360	370	380	390	400];
        SdB_NeedleHyd = [-244.9616	-248.9885	-249.4009	-250.082	-252.0194	-253.9786	-254.5181	-257.8421	-260.3204	-253.8374	-256.2626	-253.3638	-251.6654	-249.5458	-247.6	-246.2	-245.3	-245.6	-245.2	-246.2	-246.2	-245.9	-245.7	-243.0	-240.8	-240.4	-240.4	-239.5	-238.7	-238.0	-237.2	-236.8	-237.3	-236.1	-234.4	-234.3	-234.8	-235.3	-234.9	-234.2	-233.5	-232.5	-232.2	-232.3	-232.4	-232.8	-233.7	-234.1	-233.9	-232.9	-232.1	-232.1	-232.4	-232.7	-232.7	-232.2	-231.7	-231.4	-231.2	-231.2	-231.1	-231.1	-230.9	-230.5	-230.1	-229.8	-229.5	-229.3	-229.0	-228.8	-228.9	-229.0	-229.1	-229.0	-229.0	-229.0	-228.9	-228.8	-228.7	-228.7	-228.7	-228.6	-228.6	-228.7	-228.9	-228.8	-229.2];
        S_Original    = 10.^ (SdB_NeedleHyd./20).*1e6;          % result is in V/Pa             
        S1 = zeros(L2,1);                                       % Preallocate the sensitivity vector
        S1(1:ind_min-1)   = S_Original(1);                      % assign all values before fmin to the mean value 
        S1(ind_max+1:end) = S_Original(end);                    % assign all values after fmax to the mean value 
        for i = ind_min:1:ind_max   
            S1(i)  = interp1(f1,S_Original,fV(i),'PCHIP');
        end
        S = S1./Loading_Effect;

        case 'Needle Hydrophone2'
        f1 = 1000.*[8	8.5	9	9.5	10	12	14	16	18	20	22	24	26	28	30	32	34	36	38	40	45	50	55	60	65	70	75	80	85	90	95	100	105	110	115	120	125	130	135	140	145	150	155	160	165	170	175	180	185	190	195	200	205	210	215	220	225	230	235	240	245	250	255	260	265	270	275	280	285	290	295	300	305	310	315	320	325	330	335	340	345	350	360	370	380	390	400];
        SdB_NeedleHyd = [-239.6072	-251.076	-252.1315	-247.2539	-249.0159	-257.9611	-256.0215	-257.0168	-256.0701	-257.9802	-258.1948	-253.8865	-251.7839	-248.2705	-247.6	-246.2	-245.3	-245.6	-245.2	-246.2	-246.2	-245.9	-245.7	-243.0	-240.8	-240.4	-240.4	-239.5	-238.7	-238.0	-237.2	-236.8	-237.3	-236.1	-234.4	-234.3	-234.8	-235.3	-234.9	-234.2	-233.5	-232.5	-232.2	-232.3	-232.4	-232.8	-233.7	-234.1	-233.9	-232.9	-232.1	-232.1	-232.4	-232.7	-232.7	-232.2	-231.7	-231.4	-231.2	-231.2	-231.1	-231.1	-230.9	-230.5	-230.1	-229.8	-229.5	-229.3	-229.0	-228.8	-228.9	-229.0	-229.1	-229.0	-229.0	-229.0	-228.9	-228.8	-228.7	-228.7	-228.7	-228.6	-228.6	-228.7	-228.9	-228.8	-229.2];
        S_Original    = 10.^ (SdB_NeedleHyd./20).*1e6;          % result is in V/Pa             
        S1 = zeros(L2,1);                                       % Preallocate the sensitivity vector
        S1(1:ind_min-1)   = S_Original(1);                      % assign all values before fmin to the mean value 
        S1(ind_max+1:end) = S_Original(end);                    % assign all values after fmax to the mean value 
        for i = ind_min:1:ind_max   
            S1(i)  = interp1(f1,S_Original,fV(i),'PCHIP');
        end
        S = S1./Loading_Effect;

        case 'Needle Hydrophone3'
        f1 = 1000.*[8	8.5	9	9.5	10	12	14	16	18	20	22	24	26	28	30	32	34	36	38	40	45	50	55	60	65	70	75	80	85	90	95	100	105	110	115	120	125	130	135	140	145	150	155	160	165	170	175	180	185	190	195	200	205	210	215	220	225	230	235	240	245	250	255	260	265	270	275	280	285	290	295	300	305	310	315	320	325	330	335	340	345	350	360	370	380	390	400];
        SdB_NeedleHyd = [-247.6893	-246.9877	-245.4765	-250.673	-250.5176	-253.6891	-254.0109	-253.1985	-252.5673	-257.0336	-258.4688	-256.8742	-252.2575	-249.7645	-247.6	-246.2	-245.3	-245.6	-245.2	-246.2	-246.2	-245.9	-245.7	-243.0	-240.8	-240.4	-240.4	-239.5	-238.7	-238.0	-237.2	-236.8	-237.3	-236.1	-234.4	-234.3	-234.8	-235.3	-234.9	-234.2	-233.5	-232.5	-232.2	-232.3	-232.4	-232.8	-233.7	-234.1	-233.9	-232.9	-232.1	-232.1	-232.4	-232.7	-232.7	-232.2	-231.7	-231.4	-231.2	-231.2	-231.1	-231.1	-230.9	-230.5	-230.1	-229.8	-229.5	-229.3	-229.0	-228.8	-228.9	-229.0	-229.1	-229.0	-229.0	-229.0	-228.9	-228.8	-228.7	-228.7	-228.7	-228.6	-228.6	-228.7	-228.9	-228.8	-229.2];
        S_Original    = 10.^ (SdB_NeedleHyd./20).*1e6;          % result is in V/Pa             
        S1 = zeros(L2,1);                                       % Preallocate the sensitivity vector
        S1(1:ind_min-1)   = S_Original(1);                      % assign all values before fmin to the mean value 
        S1(ind_max+1:end) = S_Original(end);                    % assign all values after fmax to the mean value 
        for i = ind_min:1:ind_max   
            S1(i)  = interp1(f1,S_Original,fV(i),'PCHIP');
        end
        S = S1./Loading_Effect;

        case 'Needle Hydrophone4'
        f1 = 1000.*[8	8.5	9	9.5	10	12	14	16	18	20	22	24	26	28	30	32	34	36	38	40	45	50	55	60	65	70	75	80	85	90	95	100	105	110	115	120	125	130	135	140	145	150	155	160	165	170	175	180	185	190	195	200	205	210	215	220	225	230	235	240	245	250	255	260	265	270	275	280	285	290	295	300	305	310	315	320	325	330	335	340	345	350	360	370	380	390	400];
        SdB_NeedleHyd = [-252.3765	-254.9498	-250.2872	-256.2297	-257.3037	-257.5169	-258.9914	-252.6751	-254.8878	-257.6208	-262.187	-257.702	-253.9848	-249.5395	-247.6	-246.2	-245.3	-245.6	-245.2	-246.2	-246.2	-245.9	-245.7	-243.0	-240.8	-240.4	-240.4	-239.5	-238.7	-238.0	-237.2	-236.8	-237.3	-236.1	-234.4	-234.3	-234.8	-235.3	-234.9	-234.2	-233.5	-232.5	-232.2	-232.3	-232.4	-232.8	-233.7	-234.1	-233.9	-232.9	-232.1	-232.1	-232.4	-232.7	-232.7	-232.2	-231.7	-231.4	-231.2	-231.2	-231.1	-231.1	-230.9	-230.5	-230.1	-229.8	-229.5	-229.3	-229.0	-228.8	-228.9	-229.0	-229.1	-229.0	-229.0	-229.0	-228.9	-228.8	-228.7	-228.7	-228.7	-228.6	-228.6	-228.7	-228.9	-228.8	-229.2];
        S_Original    = 10.^ (SdB_NeedleHyd./20).*1e6;          % result is in V/Pa             
        S1 = zeros(L2,1);                                       % Preallocate the sensitivity vector
        S1(1:ind_min-1)   = S_Original(1);                      % assign all values before fmin to the mean value 
        S1(ind_max+1:end) = S_Original(end);                    % assign all values after fmax to the mean value 
        for i = ind_min:1:ind_max   
            S1(i)  = interp1(f1,S_Original,fV(i),'PCHIP');
        end
        S = S1./Loading_Effect;
    end
end
%% Main Calculations 
% Preallocation of matrices and vectors (NF is for Non-filtered values)
Vt2      = zeros(L1,NWF);                                
Vf1      = zeros(L1,NWF);                                
Vf2_0    = zeros(L2,NWF);                                
Vf2NF_0  = zeros(L2,NWF);                                
Vf2      = zeros(L2,NWF);                                
P2       = zeros(L1,NWF);                                
P3       = zeros(L1,NWF);                                
Pt       = zeros(L1,NWF);                                
Pmax0    = zeros(1,NWF);                                
Prms0    = zeros(1,NWF);                                
Pmax1    = zeros(1,NWF);                                
Prms1    = zeros(1,NWF);                                
P_mean   = zeros(1,L1);                                
P_std    = zeros(1,L1);                                
Vt2NF    = zeros(L1,NWF);                                                                         
Vf1NF    = zeros(L1,NWF);                                
Vf2NF    = zeros(L2,NWF);                                
P2NF     = zeros(L1,NWF);                                
P3NF     = zeros(L1,NWF);                                
PtNF     = zeros(L1,NWF);                                
P_meanNF = zeros(1,L1);                                
P_stdNF  = zeros(1,L1);                                
Pmax1NF  = zeros(1,NWF);                                
Prms1NF  = zeros(1,NWF);                                
switch Data_Method
    case 1    % Data Acquisition by PicosCope
        tP = linspace(0,round(tV(end)*1000,1),length(tV));            % t vector (in ms) for plotting P(t), based on t of original data
    case 2    % Data Acquisition by PCI Board
        tP = linspace(0,round(tV(end)*1e6,1)/1000,length(tV));        % t vector (in ms) for plotting P(t), based on t of original data
end
alpha_t   = 0.01*alpha*tP(end);                           % time (in ms) for ignoring P(t) values for calculating Pmax
alpha_ind = find(tP>alpha_t,1,'first');                   % index of time for ignoring P(t) values for calculating Pmax
switch Filter_Method
    case 1                      % Applying Filter  
        switch Noise_Method
            case 1              % Noise subtraction in time domain
        for i=1:NWF
            disp(i)    
            Vt2(:,i)     = Noise_Reduction(Vt1(:,i),Noise_up*0.001,Noise_down*0.001);   % Implementing Noise in time domain  
            Vt2NF(:,i)   = Vt2(:,i);                                         % NO Filter 
            x            = Vt2(:,i);
            if (strcmpi(Probe_Class, 'Fibre-Optic Hydrophone'))              % use bandpass filter for FOH and low pass for other probes   
            x_filtered = bandpass(x, [fmin fmax], fs);
%             x_lp = lowpass(x, fmax, fs);                                   % use these two lines in case bandpass filter becomes unstable
%             x_filtered = highpass(x_lp, fmin, fs);
            else             
            x_filtered   = filtfilt(sos, g, x);           
            end
            Vt2(:,i)     = x_filtered;                                       % WITH Filter          
            
            % With Applying Filter
            K1           = Vt2(:,i);                                         % Temporary array
            Vf1(:,i)     = fft(K1);                                    
            Vf2(:,i)     = DS2SS(Vf1(:,i));                            
            P2(:,i)      = 0.001.*SS2DS(Vf2(:,i)./S);                        % in kPa (P values in freq domain)  
            P3(:,i)      = 0.001.*SS2DS2((abs(Vf2(:,i))./S));                % in kPa [positive values for averaging (P-f)]
            K2           = P2(:,i);                                          % Temporary array
            Pt(:,i)      = ifft(K2);                                         % in kPa (P values in time domain) 
            if (max(abs(Pt(:,i))) > max(abs(Pt(alpha_ind:end-alpha_ind,i))))   
                Pmax1(i) = max(abs(Pt(alpha_ind:end-alpha_ind,i)));          % Calculate rms and maximum values (ignore alpha percent at beginning and end)
                Prms1(i) = rms(abs(Pt(alpha_ind:end-alpha_ind,i)));
            else
                Pmax1(i) = max(abs(Pt(:,i)));                                % Calculate rms and maximum values among all values
                Prms1(i) = rms(abs(Pt(:,i)));              
            end          
            Pmax0(i)     = max(abs(Pt(:,i)));                                % Calculate rms and maximum values among all values
            Prms0(i)     = rms(abs(Pt(:,i)));

            % Without Applying Filter   
            K3           = Vt2NF(:,i);                                       % Temporary array
            Vf1NF(:,i)   = fft(K3);                                    
            Vf2NF(:,i)   = DS2SS(Vf1NF(:,i));                    
            P2NF(:,i)    = 0.001.*SS2DS(Vf2NF(:,i)./S);                      % in kPa (P values in freq domain)  
            P3NF(:,i)    = 0.001.*SS2DS2(abs(Vf2NF(:,i))./S); 
            K4           = P2NF(:,i);
            PtNF(:,i)    = ifft(K4);                                         % in kPa (P values in time domain) 
            if (max(abs(PtNF(:,i))) > max(abs(PtNF(alpha_ind:end-alpha_ind,i))))   
                Pmax1NF(i) = max(abs(PtNF(alpha_ind:end-alpha_ind,i)));      % Calculate rms and maximum values (ignore alpha percent at beginning and end)
                Prms1NF(i) = rms(abs(PtNF(alpha_ind:end-alpha_ind,i)));
            else
                Pmax1NF(i) = max(abs(PtNF(:,i)));                            % Calculate rms and maximum values among all values
                Prms1NF(i) = rms(abs(PtNF(:,i)));              
            end          
            clear K1 K2 K3 K4 x x_filtered
        end
            case 2             % Noise subtraction in frequency domain
        for i=1:NWF
            disp(i)    
            Vt2(:,i)     = Vt1(:,i);                                         % No Noise subtraction in time domain 
            Vt2NF(:,i)   = Vt2(:,i);                                         % NO filter 
            x            = Vt2(:,i);                             
            if (strcmpi(Probe_Class, 'Fibre-Optic Hydrophone'))              % use bandpass filter for FOH and low pass for other probes   
            x_filtered = bandpass(x, [fmin fmax], fs);
%             x_lp = lowpass(x, fmax, fs);                                   % use these two lines in case bandpass filter becomes unstable
%             x_filtered = highpass(x_lp, fmin, fs);
            else             
            x_filtered   = filtfilt(sos, g, x);           
            end
            Vt2(:,i)     = x_filtered;                                       % WITH filter

            % With Applying Filter 
            K1           = Vt2(:,i);                                         % Temporary array
            Vf1(:,i)     = fft(K1);                                    
            Vf2_0(:,i)   = DS2SS(Vf1(:,i));                            
            for jj = 1:length(Vf2_0)                                         % Noise subtraction in freq domain 
                if (real(Vf2_0(jj,i))>=0) 
                    real_temp = real(Vf2_0(jj,i))- abs(real(SS_FT_Noise(jj)));           % Reminder: S_FT_Noise = DS2SS(FT_Noise); 
                    if (imag(Vf2_0(jj,i))>=0) 
                        imag_temp = imag(Vf2_0(jj,i))- abs(imag(SS_FT_Noise(jj)));           
                    else 
                        imag_temp = imag(Vf2_0(jj,i))+ abs(imag(SS_FT_Noise(jj)));           
                    end
                    Vf2(jj,i) = complex (real_temp, imag_temp);    
                else
                    real_temp = real(Vf2_0(jj,i))+ abs(real(SS_FT_Noise(jj)));
                    if (imag(Vf2_0(jj,i))>=0) 
                        imag_temp = imag(Vf2_0(jj,i))- abs(imag(SS_FT_Noise(jj)));           
                    else 
                        imag_temp = imag(Vf2_0(jj,i))+ abs(imag(SS_FT_Noise(jj)));           
                    end 
                    Vf2(jj,i) = complex (real_temp, imag_temp);    
                end
            end
            P2(:,i)      = 0.001.*SS2DS(Vf2(:,i)./S);                        % in kPa (P values in freq domain)  
            P3(:,i)      = 0.001.*SS2DS2(abs(Vf2(:,i))./S);                  % in kPa [positive values for averaging (P-f)]
            K2           = P2(:,i);                                          % Temporary array
            Pt(:,i)      = ifft(K2);                                         % in kPa (P values in time domain) 
            if (max(abs(Pt(:,i))) > max(abs(Pt(alpha_ind:end-alpha_ind,i))))   
                Pmax1(i) = max(abs(Pt(alpha_ind:end-alpha_ind,i)));          % Calculate rms and maximum values (ignore alpha percent at beginning and end)
                Prms1(i) = rms(abs(Pt(alpha_ind:end-alpha_ind,i)));
            else
                Pmax1(i) = max(abs(Pt(:,i)));                                % Calculate rms and maximum values among all values
                Prms1(i) = rms(abs(Pt(:,i)));              
            end          
            Pmax0(i)     = max(abs(Pt(:,i)));                                % Calculate rms and maximum values among all values
            Prms0(i)     = rms(abs(Pt(:,i)));

            % Without Applying Filter 
            K3           = Vt2NF(:,i);                                       % Temporary array
            Vf1NF(:,i)   = fft(K3);                              
            Vf2NF_0(:,i) = DS2SS(Vf1NF(:,i));              
            for jj = 1:length(Vf2NF_0)                                       % Noise subtraction in freq domain 
                if (real(Vf2NF_0(jj,i))>=0) 
                    real_temp = real(Vf2NF_0(jj,i))- abs(real(SS_FT_NF_Noise(jj)));           
                    if (imag(Vf2NF_0(jj,i))>=0) 
                        imag_temp = imag(Vf2NF_0(jj,i))- abs(imag(SS_FT_NF_Noise(jj)));           
                    else 
                        imag_temp = imag(Vf2NF_0(jj,i))+ abs(imag(SS_FT_NF_Noise(jj)));           
                    end
                    Vf2NF(jj,i) = complex (real_temp, imag_temp);    
                else
                    real_temp = real(Vf2NF_0(jj,i))+ abs(real(SS_FT_NF_Noise(jj)));
                    if (imag(Vf2NF_0(jj,i))>=0) 
                        imag_temp = imag(Vf2NF_0(jj,i))- abs(imag(SS_FT_NF_Noise(jj)));           
                    else 
                        imag_temp = imag(Vf2NF_0(jj,i))+ abs(imag(SS_FT_NF_Noise(jj)));           
                    end 
                    Vf2NF(jj,i) = complex (real_temp, imag_temp);    
                end
            end  
            P2NF(:,i)    = 0.001.*SS2DS(Vf2NF(:,i)./S);                      % in kPa (P values in freq domain)  
            P3NF(:,i)    = 0.001.*SS2DS2(abs(Vf2NF(:,i))./S);                % in kPa [positive values for averaging (P-f)]
            K4           = P2NF(:,i);
            PtNF(:,i)    = ifft(K4);                                         % in kPa (P values in time domain) 
            if (max(abs(PtNF(:,i))) > max(abs(PtNF(alpha_ind:end-alpha_ind,i))))   
                Pmax1NF(i) = max(abs(PtNF(alpha_ind:end-alpha_ind,i)));      % Calculate rms and maximum values (ignore alpha percent at beginning and end)
                Prms1NF(i) = rms(abs(PtNF(alpha_ind:end-alpha_ind,i)));
            else
                Pmax1NF(i) = max(abs(PtNF(:,i)));                            % Calculate rms and maximum values among all values
                Prms1NF(i) = rms(abs(PtNF(:,i)));              
            end          
            clear K1 K2 K3 K4 x x_filtered
        end
        end
    case 2                      % Without Applying Filter
        switch Noise_Method
            case 1              % Noise subtraction in time domain
        for i=1:NWF
            disp(i)    
            Vt2(:,i)     = Noise_Reduction(Vt1(:,i),Noise_up*0.001,Noise_down*0.001); % Implementing Noise in time domain  
            K1           = Vt2(:,i);                                         % Temporary array
            Vf1(:,i)     = fft(K1);                              
            Vf2(:,i)     = DS2SS(Vf1(:,i));                
            P2(:,i)      = 0.001.*SS2DS(Vf2(:,i)./S);                        % in kPa (P values in freq domain)  
            P3(:,i)      = 0.001.*SS2DS2(abs(Vf2(:,i))./S);                  % in kPa [positive values for averaging (P-f)]
            K2           = P2(:,i);                                          % Temporary array
            Pt(:,i)      = ifft(K2);                                         % in kPa (P values in time domain) 
            if (max(abs(Pt(:,i))) > max(abs(Pt(alpha_ind:end-alpha_ind,i))))   
                Pmax1(i) = max(abs(Pt(alpha_ind:end-alpha_ind,i)));          % Calculate rms and maximum values (ignore alpha percent at beginning and end)
                Prms1(i) = rms(abs(Pt(alpha_ind:end-alpha_ind,i)));
            else
                Pmax1(i) = max(abs(Pt(:,i)));                                % Calculate rms and maximum values among all values
                Prms1(i) = rms(abs(Pt(:,i)));              
            end          
            Pmax0(i)     = max(abs(Pt(:,i)));                                % Calculate rms and maximum values among all values
            Prms0(i)     = rms(abs(Pt(:,i)));
            clear K1 K2 
        end
            case 2               % Noise subtraction in frequency domain
        for i=1:NWF
            disp(i)    
            Vt2(:,i)     = Vt1(:,i);                                         % No Noise subtraction in time domain 
            K1           = Vt2(:,i);                                         % Temporary array
            Vf1(:,i)     = fft(K1);                                   
            Vf2_0(:,i)   = DS2SS(Vf1(:,i));                
            for jj = 1:length(Vf2_0)                                         % Noise subtraction in freq domain 
                if (real(Vf2_0(jj,i))>=0) 
                    real_temp = real(Vf2_0(jj,i))- abs(real(SS_FT_NF_Noise(jj)));           % Reminder: S_FT_Noise = DS2SS(FT_Noise); 
                    if (imag(Vf2_0(jj,i))>=0) 
                        imag_temp = imag(Vf2_0(jj,i))- abs(imag(SS_FT_NF_Noise(jj)));           
                    else 
                        imag_temp = imag(Vf2_0(jj,i))+ abs(imag(SS_FT_NF_Noise(jj)));           
                    end
                    Vf2(jj,i) = complex (real_temp, imag_temp);    
                else
                    real_temp = real(Vf2_0(jj,i))+ abs(real(SS_FT_NF_Noise(jj)));
                    if (imag(Vf2_0(jj,i))>=0) 
                        imag_temp = imag(Vf2_0(jj,i))- abs(imag(SS_FT_NF_Noise(jj)));           
                    else 
                        imag_temp = imag(Vf2_0(jj,i))+ abs(imag(SS_FT_NF_Noise(jj)));           
                    end 
                    Vf2(jj,i) = complex (real_temp, imag_temp);    
                end
            end
            P2(:,i)      = 0.001.*SS2DS(Vf2(:,i)./S);                        % in kPa (P values in freq domain)  
            P3(:,i)      = 0.001.*SS2DS2(abs(Vf2(:,i))./S);                  % in kPa [positive values for averaging (P-f)]
            K2           = P2(:,i);                                          % Temporary array
            Pt(:,i)      = ifft(K2);                                         % in kPa (P values in time domain) 
            if (max(abs(Pt(:,i))) > max(abs(Pt(alpha_ind:end-alpha_ind,i))))   
                Pmax1(i) = max(abs(Pt(alpha_ind:end-alpha_ind,i)));          % Calculate rms and maximum values (ignore alpha percent at beginning and end)
                Prms1(i) = rms(abs(Pt(alpha_ind:end-alpha_ind,i)));
            else
                Pmax1(i) = max(abs(Pt(:,i)));                                % Calculate rms and maximum values among all values
                Prms1(i) = rms(abs(Pt(:,i)));              
            end          
            Pmax0(i)     = max(abs(Pt(:,i)));                                % Calculate rms and maximum values among all values
            Prms0(i)     = rms(abs(Pt(:,i)));
            clear K1 K2 
        end
        end        
end
%% Display the input information
switch Probe_Class
    case 'Cavitometer'
    switch ProbeType 
        case 'Probe2'
            VarNames = {'Probe_Class','Probe_Type','Probe_Position','Pre_Amp'};
            DispData1 = {Probe_Class, ProbeType, Cavitometer_position, PreAmpType};
            T = cell2table(DispData1,'VariableNames',VarNames);disp(T)
        case 'Probe3_Shunt'
            VarNames = {'Probe_Class','Probe_Type','Probe_Position','Pre_Amp'};
            DispData1 = {Probe_Class, ProbeType, Cavitometer_position, PreAmpType};
            T = cell2table(DispData1,'VariableNames',VarNames);disp(T)
        case 'Probe3_Attenuator'
            VarNames = {'Probe_Class','Probe_Type','Probe_Position'};
            DispData1 = {Probe_Class, ProbeType, Cavitometer_position};
            T = cell2table(DispData1,'VariableNames',VarNames);disp(T)
        case 'Probe4'
            VarNames = {'Probe_Class','Probe_Type','Probe_Position'};
            DispData1 = {Probe_Class, ProbeType, Cavitometer_position};
            T = cell2table(DispData1,'VariableNames',VarNames);disp(T)
    end
    case 'Fibre-Optic Hydrophone'
            VarNames = {'Probe_Class','Probe_Type'};
            DispData1 = {Probe_Class, ProbeType};
            T = cell2table(DispData1,'VariableNames',VarNames);disp(T)
    case 'Needle Hydrophone'
            VarNames = {'Probe_Class','Probe_Type'};
            DispData1 = {Probe_Class, ProbeType};
            T = cell2table(DispData1,'VariableNames',VarNames);disp(T)
end
            clear VarNames DispData1 T
%% Finding the Max and RMS Pressure 
switch Filter_Method
    case 1   % With Applying Filter
        P2(1,:)    = P2(2,:);
        P3(1,:)    = P3(2,:);
        format short g
        % P_max and RMS values with applying filter
        Pmax          = mean (Pmax1);                               % P_max  
        Pmax_std      = std  (Pmax1);                               % std of P_max   
        Prms          = mean (Prms1);                               % RMS
        Prms_std      = std  (Prms1);                               % std of RMS
        PmaxNS        = mean (Pmax0);                               % P_max        (without ignoring alpha percent at beginning and end) 
        PmaxNS_std    = std  (Pmax0);                               % std of P_max (without ignoring alpha percent at beginning and end)   
        PrmsNS        = mean (Prms0);                               % RMS          (without ignoring alpha percent at beginning and end)
        PrmsNS_std    = std  (Prms0);                               % std of RMS   (without ignoring alpha percent at beginning and end)
        % P_max and RMS values without applying filter
        PmaxNF        = mean (Pmax1NF);                             % P_max without filter
        PmaxNF_std    = std  (Pmax1NF);                             
        PrmsNF        = mean (Prms1NF);                             % RMS without filter   
        PrmsNF_std    = std  (Prms1NF);
        disp('Units are in kPa');
        fprintf('\n')
        if(alpha~=0)
            disp('Comparing the results: with and without skipping the peaks at two ends');
            fprintf('\n')
            VarNames = {'Pmax','Pmax_NoSkip','RMS','RMS_NoSkip','Std_Max','Std_Max_NoSkip','Std_RMS','Std_RMS_NoSkip'};
            DispData1 = [Pmax PmaxNS Prms PrmsNS Pmax_std PmaxNS_std Prms_std PrmsNS_std];
            T = table(DispData1(1),DispData1(2),DispData1(3),DispData1(4),DispData1(5),DispData1(6),DispData1(7),DispData1(8),'VariableNames',VarNames);disp(T)
            clear VarNames DispData1 T
        end
        if(Filter_Method == 1)
            disp('Comparing the results: with and without filtering');
            fprintf('\n')
            VarNames = {'Pmax','Pmax_NoFilter','RMS','RMS_NoFilter','Std_Max','Std_RMS'};
            DispData1 = [Pmax PmaxNF Prms PrmsNF Pmax_std Prms_std];
            T = table(DispData1(1),DispData1(2),DispData1(3),DispData1(4),DispData1(5),DispData1(6),'VariableNames',VarNames);disp(T)
            clear VarNames DispData1 T
        end
        disp('Final Results');
        fprintf('\n')
        VarNames = {'Pmax','RMS','Std_Max','Std_RMS'};
        DispData1 = [Pmax Prms Pmax_std Prms_std];
        T = table(DispData1(1),DispData1(2),DispData1(3),DispData1(4),'VariableNames',VarNames);disp(T)
    case 2   % Without Applying Filter
        format short g
        % P_max and RMS values with applying filter
        Pmax        = mean (Pmax1);                               % P_max  
        Pmax_std    = std  (Pmax1);                               % std of P_max   
        Prms        = mean (Prms1);                               % RMS
        Prms_std    = std  (Prms1);                               % std of RMS
        PmaxNS        = mean (Pmax0);                             % P_max        (without ignoring alpha percent at beginning and end) 
        PmaxNS_std    = std  (Pmax0);                             % std of P_max (without ignoring alpha percent at beginning and end)   
        PrmsNS        = mean (Prms0);                             % RMS          (without ignoring alpha percent at beginning and end)
        PrmsNS_std    = std  (Prms0);                             % std of RMS   (without ignoring alpha percent at beginning and end)
        disp('Units are in kPa');
        if(alpha~=0)
            disp('Comparing with and without skipping the peaks at two ends');
            fprintf('\n')
            VarNames = {'Pmax','Pmax_NoSkip','RMS','RMS_NoSkip','Std_Max','Std_Max_NoSkip','Std_RMS','Std_RMS_NoSkip'};
            DispData1 = [Pmax PmaxNS Prms PrmsNS Pmax_std PmaxNS_std Prms_std PrmsNS_std];
            T = table(DispData1(1),DispData1(2),DispData1(3),DispData1(4),DispData1(5),DispData1(6),DispData1(7),DispData1(8),'VariableNames',VarNames);disp(T)
        end 
        clear VarNames DispData1 T
        disp('Final Results');
        fprintf('\n')
        clear VarNames DispData1 T
        VarNames = {'Pmax','RMS','Std_Max','Std_RMS'};
        DispData1 = [Pmax Prms Pmax_std Prms_std];
        T = table(DispData1(1),DispData1(2),DispData1(3),DispData1(4),'VariableNames',VarNames);disp(T)
end
%% Calculate mean of Voltage and Pressure to plot in Freq domain (P-f)
switch Filter_Method
    case 1   % With Applying Filter
        for i=1:L1
            % With Applying Filter 
            P_mean(i)    = mean ((P3(i,:)))./L1;
            P_std(i)     = std  ((P3(i,:)))./L1;
            % Without Applying Filter     
            P_meanNF(i)  = mean ((P3NF(i,:)))./L1;
            P_stdNF(i)   = std  ((P3NF(i,:)))./L1;
        end
    case 2   % Without Applying Filter
        for i=1:L1
            P_mean(i)    = mean ((P3(i,:)))./L1;
            P_std(i)     = std  ((P3(i,:)))./L1;
        end
end
fV_kHz    = 0.001.*fV;                                           % in kHz (whole range of freq)
f_kHz     = fV_kHz(ind_min:ind_max)';                            % in kHz (only calibrated range)
P_kPa     = P_mean(ind_min:ind_max)';                            % Averaged P (kPa) values (in f domain) only in calibrated range 
%% Save data 
Pt1 = Pt(:,1);                                                   % Save the P(t) of the first waveform by default
if strcmpi(Save_Data, 'Yes')  
    switch Filter_Method 
        case 1
            save (['Result_' format1 ],'-regexp','^(?!(Vf1|Vf2_0|Vf2NF_0|Vf2|P2|P3|Vt2NF|Vf1NF|Vf2NF|P2NF|P3NF|Pt|PtNF|Vt1|Vt2|tV|M|)$).');        
        case 2
            save (['Result_' format1 ],'-regexp','^(?!(Vf1|Vf2_0|Vf2|P2|P3|Vt1|Vt2|tV|M|)$).');        
    end
end
%% %%%%%%%%%%%%%%%%%%%%% Plots: All plots on different tabs on ONE window without saving %%%%%%%%%%%%%%%%%%%%%%%%
%%% Plots: Sensitivity
set(0, 'DefaultFigureWindowStyle', 'docked');
if strcmpi(Probe_Class, 'Fibre-Optic Hydrophone')  
   switch Data_Method
        case 1    % Data Acquisition by PicosCope
            figure('Name','S','NumberTitle','off'),plot(1e-6.*fV(ind_min:ind_max),S(ind_min:ind_max),'-b','linewidth',Line_thickness);box on;set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);set(gcf,'units','normalized','outerposition',[0 0 1 1]);xlabel('$ f\ \rm (MHz)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ S\ (V/\rm Pa) $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title(format1,'interpreter','none','fontsize',font_size,'fontweight','bold','Color',[0 0 0]);
            hold on; plot(1e-6.*f1,S_Original,'sr','linewidth',Line_thickness,'MarkerSize',16,'MarkerEdgeColor','red','MarkerFaceColor','red');hold off
        case 2    % Data Acquisition by PCI Board
            figure('Name','S','NumberTitle','off'),plot(1e-6.*fV(ind_min:ind_max),S(ind_min:ind_max),'-b','linewidth',Line_thickness);box on;set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);set(gcf,'units','normalized','outerposition',[0 0 1 1]);xlabel('$ f\ \rm (MHz)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ S\ (V/\rm Pa) $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title(format1,'interpreter','none','fontsize',font_size,'fontweight','bold','Color',[0 0 0]);
            hold on; plot(1e-6.*f1,S_Original_Selected,'sr','linewidth',Line_thickness,'MarkerSize',16,'MarkerEdgeColor','red','MarkerFaceColor','red');hold off
   end
else
   figure('Name','S','NumberTitle','off'),plot(1e-3.*fV(ind_min:ind_max),S(ind_min:ind_max),'-b','linewidth',Line_thickness);box on;set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);set(gcf,'units','normalized','outerposition',[0 0 1 1]);xlabel('$ f\ \rm (kHz)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ S\ (V/\rm Pa) $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title(format1,'interpreter','none','fontsize',font_size,'fontweight','bold','Color',[0 0 0]);
   hold on; plot(1e-3.*f1,S_Original,'sr','linewidth',Line_thickness,'MarkerSize',16,'MarkerEdgeColor','red','MarkerFaceColor','red');hold off
end
WF_Number = 1; 
switch Filter_Method
    case 1                      % Applying Filter  
        figure('Name','V-t','NumberTitle','off'),
        subplot(2, 1, 1)
        plot(1000.*tV, 1000.*Vt1(:,WF_Number),'-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ V\ {\rm(mV)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title('Original Signal','fontsize',font_size,'fontweight','bold','Color',[0 0 0]);
        xlim([0 tV(end)*1000]);
        subplot(2, 1, 2)
        plot(1000.*tV, 1000.*Vt2NF(:,WF_Number),'-b','linewidth',Line_thickness); box on
        set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ V\ {\rm(mV)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title('Signal with Noise Subtraction','fontsize',font_size,'fontweight','bold','Color',[0 0 0]);
        xlim([0 tV(end)*1000]);
        figure('Name','V-t2','NumberTitle','off'),plot(1000.*tV, 1000.*Vt2NF(:,WF_Number),'-b','linewidth',Line_thickness); box on; hold on
        plot(1000.*tV, 1000.*Vt2(:,WF_Number)  ,'-r','linewidth',Line_thickness); 
        set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ V\ {\rm(mV)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
        xlim([0 tV(end)*1000]);
        legend ('No Filter','With Filter')
    case 2                       % No Filter
        figure('Name','V-t','NumberTitle','off'),
        subplot(2, 1, 1)
        plot(1000.*tV, 1000.*Vt1(:,WF_Number),'-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ V\ {\rm(mV)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title('Original Signal','fontsize',font_size,'fontweight','bold','Color',[0 0 0]);
        xlim([0 tV(end)*1000]);
        subplot(2, 1, 2)
        plot(1000.*tV, 1000.*Vt2(:,WF_Number),'-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ V\ {\rm(mV)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title('Signal with Noise Subtraction','fontsize',font_size,'fontweight','bold','Color',[0 0 0]);
        xlim([0 tV(end)*1000]);
end
% Plots: Pmax-N, RMS-N
if (Data_Method == 1)
    figure('Name','Pmax','NumberTitle','off'),plot(1:NWF,Pmax1,'-sb','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ \rm Waveform $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$P_{\rm max} \ (\rm kPa)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title(format1, 'Interpreter', 'none' ,'Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    if(alpha~=0)
        figure('Name','Pmax2','NumberTitle','off'),plot(1:NWF,Pmax1,'-sb','linewidth',Line_thickness); box on; hold on
        plot(1:NWF,Pmax0,'-sr','linewidth',Line_thickness);set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ \rm Waveform $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$P_{\rm max} \ (\rm kPa)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title(format1, 'Interpreter', 'none' ,'Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
        legend ('With Skipping','No Skipping')
    end

    figure('Name','RMS','NumberTitle','off'),plot(1:NWF,Prms1,'-sb','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ \rm Waveform $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$P_{\rm RMS} \ (\rm kPa)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title(format1, 'Interpreter', 'none' ,'Color',[0 0 0],'fontsize',font_size,'fontweight','bold')

    if(alpha~=0)
        figure('Name','RMS2','NumberTitle','off'),plot(1:NWF,Prms1,'-sb','linewidth',Line_thickness); box on; hold on
        plot(1:NWF,Prms0,'-sr','linewidth',Line_thickness);set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ \rm Waveform $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$P_{\rm RMS} \ (\rm kPa)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title(format1, 'Interpreter', 'none' ,'Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
        legend ('With Skipping','No Skipping')
    end
end
% Plots: P-f
if strcmpi(Probe_Class, 'Fibre-Optic Hydrophone')  
    figure('Name','P-f','NumberTitle','off'),plot(fV_kHz/1000, P_mean,'-b','linewidth',Line_thickness);box on;set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);set(gcf,'units','normalized','outerposition',[0 0 1 1]);xlabel('$ f\ \rm (MHz)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$P_{\rm mean} \ (\rm kPa)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title(format1, 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    xlim([fmin/1e6 fmax/1e6]);
else 
    figure('Name','P-f','NumberTitle','off'),plot(fV_kHz, P_mean,'-b','linewidth',Line_thickness);box on;set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);set(gcf,'units','normalized','outerposition',[0 0 1 1]);xlabel('$ f\ \rm (kHz)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$P_{\rm mean} \ (\rm kPa)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title(format1, 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    xlim([fmin/1000 fmax/1000]);
end
% Compare P-f of filtered and non-filtered plots (without saving and with wider x range)
switch Filter_Method
    case 1
        if strcmpi(Probe_Class, 'Fibre-Optic Hydrophone')
            figure('Name','P-f2','NumberTitle','off'),plot(fV_kHz/1000, P_mean,'-b','linewidth',Line_thickness); box on; hold on
            plot(fV_kHz/1000, P_meanNF,'-r','linewidth',Line_thickness);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);set(gcf,'units','normalized','outerposition',[0 0 1 1]);xlabel('$ f\ \rm (MHz)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$P_{\rm mean} \ (\rm kPa)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title(format1, 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
            xlim([-10 2*fmax/1e6]);legend ('With Filter','No Filter')
        else 
            figure('Name','P-f2','NumberTitle','off'),plot(fV_kHz, P_mean,'-b','linewidth',Line_thickness); box on; hold on
            plot(fV_kHz, P_meanNF,'-r','linewidth',Line_thickness);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);set(gcf,'units','normalized','outerposition',[0 0 1 1]);xlabel('$ f\ \rm (kHz)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$P_{\rm mean} \ (\rm kPa)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title(format1, 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
            xlim([-10 2*fmax/1000]);legend ('With Filter','No Filter')
        end
end
if (NWF >= 6)
    figure('Name','P-f3','NumberTitle','off'),hold on
    for i=1:6
        if strcmpi(Probe_Class, 'Fibre-Optic Hydrophone')  
            subplot(3, 2, i)
            WF_Number= i;plot(fV_kHz./1000, P3(:,WF_Number)./L1,'-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ f\ \rm (MHz)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$P \ (\rm kPa)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
            xlim([fmin/1e6 fmax/1e6]);
        else        
            subplot(3, 2, i)
            WF_Number= i;plot(fV_kHz, P3(:,WF_Number)./L1,'-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ f\ \rm (kHz)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$P \ (\rm kPa)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
            xlim([fmin/1000 fmax/1000]);
        end
    end
end
% Plots: V-f
figure('Name','V-f','NumberTitle','off'),
WF_Number = 1; 
subplot(2, 1, 1)
plot(tV.*1000, 1000.*Vt2(:,WF_Number),'-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ V\ {\rm(mV)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
xlim([0 tP(end)]);
subplot(2, 1, 2)
plot(fV_kHz, abs(Vf1(:,WF_Number)).*1000,'-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ f\ \rm (kHz)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ FFT (V(t))$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);
xlim([fmin/1000 fmax/1000]);
% Plots: P-t
figure('Name','P-t','NumberTitle','off'),plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
% Compare P-t of filtered and non-filtered plots 
switch Filter_Method
    case 1
        figure('Name','P-t2','NumberTitle','off'),plot(tP,PtNF(:,WF_Number)','-b','linewidth',Line_thickness); box on; hold on;
        plot(tP,Pt(:,WF_Number)','-r','linewidth',Line_thickness);set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold');
        legend ('No Filter','With Filter')
end
% save P-t of all waveforms in 5 subplots (4x3) 
if (Data_Method == 1)
font_size = 10;font_size2 = 13;box_thickness = 3;Line_thickness = 1; 
if (NWF <= 12)
    figure('Name','P-t3','NumberTitle','off'),
    for i = 1:NWF
        subplot(4, 3, i)
        WF_Number= i;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness); box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end   
elseif (NWF > 12 && NWF <= 24)
    figure('Name','P-t3','NumberTitle','off'),
    for i = 1:12
        subplot(4, 3, i)
        WF_Number= i;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness); box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end
    figure('Name','P-t4','NumberTitle','off'),
    for i = 1:NWF-12
        subplot(4, 3, i)
        WF_Number= i+12;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness); box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end  
elseif (NWF > 24 && NWF <= 36)
    figure('Name','P-t3','NumberTitle','off'),
    for i = 1:12
        subplot(4, 3, i)
        WF_Number= i;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness); box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end
    figure('Name','P-t4','NumberTitle','off'),
    for i = 1:12
        subplot(4, 3, i)
        WF_Number= i+12;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness); box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end 
    figure('Name','P-t5','NumberTitle','off'),
    for i = 1:NWF-24
        subplot(4, 3, i)
        WF_Number= i+24;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end   
elseif (NWF > 36 && NWF <= 48)
    figure('Name','P-t3','NumberTitle','off'),
    for i = 1:12
        subplot(4, 3, i)
        WF_Number= i;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end
    figure('Name','P-t4','NumberTitle','off'),
    for i = 1:12
        subplot(4, 3, i)
        WF_Number= i+12;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end 
    figure('Name','P-t5','NumberTitle','off'),
    for i = 1:12
        subplot(4, 3, i)
        WF_Number= i+24;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end    
    figure('Name','P-t6','NumberTitle','off'),
    for i = 1:NWF-36
        subplot(4, 3, i)
        WF_Number= i+36;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end   
elseif (NWF > 48 && NWF <= 60)
    figure('Name','P-t3','NumberTitle','off'),
    for i = 1:12
        subplot(4, 3, i)
        WF_Number= i;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end
    figure('Name','P-t4','NumberTitle','off'),
    for i = 1:12
        subplot(4, 3, i)
        WF_Number= i+12;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end 
    figure('Name','P-t5','NumberTitle','off'),
    for i = 1:12
        subplot(4, 3, i)
        WF_Number= i+24;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end    
    figure('Name','P-t6','NumberTitle','off'),
    for i = 1:12
        subplot(4, 3, i)
        WF_Number= i+36;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end      
    figure('Name','P-t7','NumberTitle','off'),
    for i = 1:NWF-48
        subplot(4, 3, i)
        WF_Number= i+48;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end
end
end
marker_font = 24;font_size = 20;font_size2 = 24;box_thickness = 3;Line_thickness = 1;
%% %%%%%%%%%%%%%%%%%  Plots: All plots on separate windows FOR saving %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set(0, 'DefaultFigureWindowStyle', 'normal');
if strcmpi(Save_Plots, 'Yes')  
WF_Number = 1; 
% Plot V-t
switch Filter_Method
    case 1                      % Applying Filter  
        figure,
        plot(1000.*tV, 1000.*Vt2NF(:,WF_Number),'-b','linewidth',Line_thickness);box on; hold on
        plot(1000.*tV, 1000.*Vt2(:,WF_Number)  ,'-r','linewidth',Line_thickness);set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ V\ {\rm(mV)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold');legend ('No Filter','With Filter');
        xlim([0 tV(end)*1000]);
    case 2                      % Without Applying Filter  
        figure,
end
% Plots: Pmax-N, RMS-N
hold off
if (Data_Method == 1)
    plot(1:NWF,Pmax1,'-sb','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ \rm Waveform $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$P_{\rm max} \ (\rm kPa)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title(format1, 'Interpreter', 'none' ,'Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    export_fig(sprintf(['Pmax-N_',format1]),'-jpg','-transparent','-m3')      
    hold off,
    if(alpha~=0)
        plot(1:NWF,Pmax1,'-sb','linewidth',Line_thickness); box on; hold on
        plot(1:NWF,Pmax0,'-sr','linewidth',Line_thickness);set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ \rm Waveform $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$P_{\rm max} \ (\rm kPa)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title(format1, 'Interpreter', 'none' ,'Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
        legend ('With Skipping','No Skipping')
        export_fig(sprintf(['Pmax-N_',format1,'_2']),'-jpg','-transparent','-m3')     
        hold off,
    end
    plot(1:NWF,Prms1,'-sb','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ \rm Waveform $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$P_{\rm RMS} \ (\rm kPa)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title(format1, 'Interpreter', 'none' ,'Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    export_fig(sprintf(['Prms-N_',format1]),'-jpg','-transparent','-m3')     
    hold off,
    if(alpha~=0)
        plot(1:NWF,Prms1,'-sb','linewidth',Line_thickness); box on; hold on
        plot(1:NWF,Prms0,'-sr','linewidth',Line_thickness);set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ \rm Waveform $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$P_{\rm RMS} \ (\rm kPa)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title(format1, 'Interpreter', 'none' ,'Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
        legend ('With Skipping','No Skipping')
        export_fig(sprintf(['Prms-N_',format1,'_2']),'-jpg','-transparent','-m3')
        hold off,
    end
end
% Plots: P-f
if strcmpi(Probe_Class, 'Fibre-Optic Hydrophone')  
    hold off,
    plot(fV_kHz/1000, P_mean,'-b','linewidth',Line_thickness);box on;set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);set(gcf,'units','normalized','outerposition',[0 0 1 1]);xlabel('$ f\ \rm (MHz)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$P_{\rm mean} \ (\rm kPa)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title(format1, 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    xlim([fmin/1e6 fmax/1e6]);
    export_fig(sprintf(['P-f_',format1]),'-jpg','-transparent','-m3') 
else 
    hold off,
    plot(fV_kHz, P_mean,'-b','linewidth',Line_thickness);box on;set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);set(gcf,'units','normalized','outerposition',[0 0 1 1]);xlabel('$ f\ \rm (kHz)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$P_{\rm mean} \ (\rm kPa)$','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title(format1, 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    xlim([fmin/1000 fmax/1000]);
    export_fig(sprintf(['P-f_',format1]),'-jpg','-transparent','-m3')     
end
% Plots: P-t
WF_Number = 1; 
% figure,
plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness); box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
export_fig(sprintf(['P-t_',format1]),'-jpg','-transparent','-m3') 

% Compare P-t of filtered and non-filtered plots 
switch Filter_Method
case 1
hold off,
plot(tP,PtNF(:,WF_Number)','-b','linewidth',Line_thickness);box on; hold on;
plot(tP,Pt(:,WF_Number)','-r','linewidth',Line_thickness);set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
legend ('No Filter','With Filter')
export_fig(sprintf(['P-t_',format1,'_0']),'-jpg','-transparent','-m3') 
end
% save P-t of all waveforms in 5 subplots (4x3) 
if (Data_Method == 1)
font_size = 10;font_size2 = 13;box_thickness = 3;Line_thickness = 1; 
if (NWF <= 12)
    hold off,
    for i = 1:NWF
        subplot(4,3,i)
        WF_Number= i;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end
    export_fig(sprintf(['P-t_',format1,'_1']),'-jpg','-transparent','-m3');close          
elseif (NWF > 12 && NWF <= 24)
    hold off,
    for i = 1:12
        subplot(4,3,i)
        WF_Number= i;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end
    export_fig(sprintf(['P-t_',format1,'_1']),'-jpg','-transparent','-m3');close      
    figure,
    for i = 1:NWF-12
        subplot(4,3,i)
        WF_Number= i+12;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end
    export_fig(sprintf(['P-t_',format1,'_2']),'-jpg','-transparent','-m3');close      
elseif (NWF > 24 && NWF <= 36)
    hold off,
    for i = 1:12
        subplot(4,3,i)
        WF_Number= i;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end
    export_fig(sprintf(['P-t_',format1,'_1']),'-jpg','-transparent','-m3')      
    hold off,
    for i = 1:12
        subplot(4,3,i)
        WF_Number= i+12;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end 
    export_fig(sprintf(['P-t_',format1,'_2']),'-jpg','-transparent','-m3');close     
    figure,
    for i = 1:NWF-24
        subplot(4,3,i)
        WF_Number= i+24;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end
    export_fig(sprintf(['P-t_',format1,'_3']),'-jpg','-transparent','-m3');close     
elseif (NWF > 36 && NWF <= 48)
    hold off,
    for i = 1:12
        subplot(4,3,i)
        WF_Number= i;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end
    export_fig(sprintf(['P-t_',format1,'_1']),'-jpg','-transparent','-m3')      
    hold off,
    for i = 1:12
        subplot(4,3,i)
        WF_Number= i+12;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end 
    export_fig(sprintf(['P-t_',format1,'_2']),'-jpg','-transparent','-m3')     
    hold off,
    for i = 1:12
        subplot(4,3,i)
        WF_Number= i+24;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end
    export_fig(sprintf(['P-t_',format1,'_3']),'-jpg','-transparent','-m3');close    
    figure,
    for i = 1:NWF-36
        subplot(4,3,i)
        WF_Number= i+36;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end
    export_fig(sprintf(['P-t_',format1,'_4']),'-jpg','-transparent','-m3');close      
elseif (NWF > 48 && NWF <= 60)
    hold off,
    for i = 1:12
        subplot(4,3,i)
        WF_Number= i;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end
    export_fig(sprintf(['P-t_',format1,'_1']),'-jpg','-transparent','-m3')      
    hold off,
    for i = 1:12
        subplot(4,3,i)
        WF_Number= i+12;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end 
    export_fig(sprintf(['P-t_',format1,'_2']),'-jpg','-transparent','-m3')     
    hold off,
    for i = 1:12
        subplot(4,3,i)
        WF_Number= i+24;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end
    export_fig(sprintf(['P-t_',format1,'_3']),'-jpg','-transparent','-m3')    
    hold off,
    for i = 1:12
        subplot(4,3,i)
        WF_Number= i+36;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end
    export_fig(sprintf(['P-t_',format1,'_4']),'-jpg','-transparent','-m3');close        
    figure,
    for i = 1:NWF-48
        subplot(4,3,i)
        WF_Number= i+48;plot(tP,Pt(:,WF_Number)','-b','linewidth',Line_thickness);box on;set(gcf,'units','normalized','outerposition',[0 0 1 1]);set(gca,'fontweight','bold','fontsize',font_size,'LineWidth',box_thickness);xlabel('$ t\ {\rm(ms)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]),ylabel('$ P\ {\rm(kPa)} $','interpreter','latex','fontsize',font_size2,'fontweight','bold','Color',[0 0 1]);title([format1,' (WF ' num2str(WF_Number),')'], 'Interpreter', 'none','Color',[0 0 0],'fontsize',font_size,'fontweight','bold')
    end
    export_fig(sprintf(['P-t_',format1,'_5']),'-jpg','-transparent','-m3');close    
end
end
end
marker_font = 24;font_size = 20;font_size2 = 24;box_thickness = 3;Line_thickness = 1; 
set(0, 'DefaultFigureWindowStyle', 'normal');
%% WRITING to EXCEL: P and V data in freq domain from fmin to fmax and their average
switch Filter_Method
    case 1
        switch Save_Excel
            case 'Yes' 
            ExcelFile= ['Output_' format1 '.xlsx'];
            for i = 1:NWF 
                temp = ['WaveForm' num2str(i)];
                WF_Vector{i} = temp;
            end
            WaveForms = WF_Vector';                                                       % Vector for number of Waveforms 
            Maximum_P = Pmax1';RMS_P = Prms1';
            warning('off','MATLAB:xlswrite:AddSheet');
            writetable(table(WaveForms,Maximum_P,RMS_P),ExcelFile,'Sheet',1)
%             writetable(table(f_kHz,P_kPa),              ExcelFile,'Sheet',4)
            xlswrite(ExcelFile,cellstr('Mean(kPa)'),'Sheet1',['A' num2str(NWF+3)]);       % Write Headline for Mean 
            xlswrite(ExcelFile,cellstr('Std (kPa)'),'Sheet1',['A' num2str(NWF+4)]);       % Write Headline for Std
            xlswrite(ExcelFile,Pmax,'Sheet1',['B' num2str(NWF+3)]);                       % Write Mean of P_max
            xlswrite(ExcelFile,Prms,'Sheet1',['C' num2str(NWF+3)]);                       % Write Mean of P_RMS
            xlswrite(ExcelFile,Pmax_std,'Sheet1',['B' num2str(NWF+4)]);                   % Write Std of P_max
            xlswrite(ExcelFile,Prms_std,'Sheet1',['C' num2str(NWF+4)]);                   % Write Std of P_RMS
            xlswrite(ExcelFile,cellstr('Max'),'Sheet1',['B' num2str(NWF+5)]);             % Write Headline for Max 
            xlswrite(ExcelFile,cellstr('RMS'),'Sheet1',['C' num2str(NWF+5)]);             % Write Headline for RMS

            % Write Pmax and RMS of non-filtered values 
            Maximum_P = Pmax1NF';RMS_P = Prms1NF';
            writetable(table(WaveForms,Maximum_P,RMS_P),ExcelFile,'Sheet',2)
            xlswrite(ExcelFile,cellstr('Mean(kPa)'),'Sheet2',['A' num2str(NWF+3)]);       % Write Headline for Mean 
            xlswrite(ExcelFile,cellstr('Std (kPa)'),'Sheet2',['A' num2str(NWF+4)]);       % Write Headline for Std
            xlswrite(ExcelFile,PmaxNF,'Sheet2',['B' num2str(NWF+3)]);                     % Write Mean of P_max
            xlswrite(ExcelFile,PrmsNF,'Sheet2',['C' num2str(NWF+3)]);                     % Write Mean of P_RMS
            xlswrite(ExcelFile,PmaxNF_std,'Sheet2',['B' num2str(NWF+4)]);                 % Write Std of P_max
            xlswrite(ExcelFile,PrmsNF_std,'Sheet2',['C' num2str(NWF+4)]);                 % Write Std of P_RMS
            xlswrite(ExcelFile,cellstr('Max (No Filter)'),'Sheet2',['B' num2str(NWF+5)]); % Write Headline for Max 
            xlswrite(ExcelFile,cellstr('RMS (No Filter)'),'Sheet2',['C' num2str(NWF+5)]); % Write Headline for RMS           
            
            % Write Pmax and RMS of No skipping values (original Pmax and RMS without skipping the two ends)
            Maximum_P = Pmax0';RMS_P = Prms0';
            writetable(table(WaveForms,Maximum_P,RMS_P),ExcelFile,'Sheet',3)
            xlswrite(ExcelFile,cellstr('Mean(kPa)'),'Sheet3',['A' num2str(NWF+3)]);       % Write Headline for Mean 
            xlswrite(ExcelFile,cellstr('Std (kPa)'),'Sheet3',['A' num2str(NWF+4)]);       % Write Headline for Std
            xlswrite(ExcelFile,PmaxNS,'Sheet3',['B' num2str(NWF+3)]);                     % Write Mean of P_max
            xlswrite(ExcelFile,PrmsNS,'Sheet3',['C' num2str(NWF+3)]);                     % Write Mean of P_RMS
            xlswrite(ExcelFile,PmaxNS_std,'Sheet3',['B' num2str(NWF+4)]);                 % Write Std of P_max
            xlswrite(ExcelFile,PrmsNS_std,'Sheet3',['C' num2str(NWF+4)]);                 % Write Std of P_RMS
            xlswrite(ExcelFile,cellstr('Max (No Skipping)'),'Sheet3',['B' num2str(NWF+5)]);  % Write Headline for Max 
            xlswrite(ExcelFile,cellstr('RMS (No Skipping)'),'Sheet3',['C' num2str(NWF+5)]);  % Write Headline for RMS      
       end
    case 2
        switch Save_Excel
            case 'Yes' 
            ExcelFile= ['Output_' format1 '.xlsx'];
            for i = 1:NWF 
                temp = ['WaveForm' num2str(i)];
                WF_Vector{i} = temp;
            end
            WaveForms = WF_Vector';                                                        % Vector for number of Waveforms 
            Maximum_P = Pmax1';RMS_P = Prms1';
            warning('off','MATLAB:xlswrite:AddSheet');
            writetable(table(WaveForms,Maximum_P,RMS_P),ExcelFile,'Sheet',1)
            writetable(table(f_kHz,P_kPa),              ExcelFile,'Sheet',3)
            xlswrite(ExcelFile,cellstr('Mean(kPa)'),'Sheet1',['A' num2str(NWF+3)]);        % Write Headline for Mean 
            xlswrite(ExcelFile,cellstr('Std (kPa)'),'Sheet1',['A' num2str(NWF+4)]);        % Write Headline for Std
            xlswrite(ExcelFile,Pmax,'Sheet1',['B' num2str(NWF+3)]);                        % Write Mean of P_max
            xlswrite(ExcelFile,Prms,'Sheet1',['C' num2str(NWF+3)]);                        % Write Mean of P_RMS
            xlswrite(ExcelFile,Pmax_std,'Sheet1',['B' num2str(NWF+4)]);                    % Write Std of P_max
            xlswrite(ExcelFile,Prms_std,'Sheet1',['C' num2str(NWF+4)]);                    % Write Std of P_RMS
            xlswrite(ExcelFile,cellstr('Max'),'Sheet1',['B' num2str(NWF+5)]);              % Write Headline for Max 
            xlswrite(ExcelFile,cellstr('RMS'),'Sheet1',['C' num2str(NWF+5)]);              % Write Headline for RMS

            % Write Pmax and RMS of No skipping values (original Pmax and RMS without skipping the two ends)
            Maximum_P = Pmax0';RMS_P = Prms0';
            writetable(table(WaveForms,Maximum_P,RMS_P),ExcelFile,'Sheet',2)
            xlswrite(ExcelFile,cellstr('Mean(kPa)'),'Sheet2',['A' num2str(NWF+3)]);       % Write Headline for Mean 
            xlswrite(ExcelFile,cellstr('Std (kPa)'),'Sheet2',['A' num2str(NWF+4)]);       % Write Headline for Std
            xlswrite(ExcelFile,Pmax,'Sheet2',['B' num2str(NWF+3)]);                       % Write Mean of P_max
            xlswrite(ExcelFile,Prms,'Sheet2',['C' num2str(NWF+3)]);                       % Write Mean of P_RMS
            xlswrite(ExcelFile,Pmax_std,'Sheet2',['B' num2str(NWF+4)]);                   % Write Std of P_max
            xlswrite(ExcelFile,Prms_std,'Sheet2',['C' num2str(NWF+4)]);                   % Write Std of P_RMS
            xlswrite(ExcelFile,cellstr('Max (No Skipping)'),'Sheet2',['B' num2str(NWF+5)]);             % Write Headline for Max 
            xlswrite(ExcelFile,cellstr('RMS (No Skipping)'),'Sheet2',['C' num2str(NWF+5)]);             % Write Headline for RMS      
        end
end
switch Multi_Method
    case 2
        if (u ~= NFiles)
            close all;clear 
        end
end
end
toc
%% Internal Functions (DO NOT CHANGE)  
% Function to subtract the noise from a signal
function [y1] = Noise_Reduction (y1,upper_limit,lower_limit)
    for i = 1:length(y1)
    if (y1(i) >= upper_limit)
        y1(i) = y1(i) - upper_limit;
    elseif (y1(i) <= -lower_limit)
        y1(i) = y1(i) + lower_limit;
    else
        y1(i) = y1(i);
    end
    end
end
%% Function to convert from double-sided spectrum to single-sided spectrum
function [SS] = DS2SS (DS)
    N1 = length(DS);
    N2 = N1/2+1;
    SS = zeros(N2,1);
    SS (1) = DS(1);
    for i=2:N2-1
        SS(i) = 2.*DS(i);
    end
    SS (N2) = DS(N2);
end
%% Function to convert from single-sided spectrum to double-sided spectrum
function [DS] = SS2DS (SS)
    N1 = length(SS);
    N2 = 2*(N1-1);
    DS = zeros(N2,1);
    DS (1) = SS(1);
    for i=2:N2/2
        DS(i) = 0.5.*SS(i);
    end
    DS (N2/2+1) = SS(N2/2+1);
    for i=1:N2/2-1
        DS_real       =  0.5.*real(SS(N2/2+1-i));            % real part (*1/2)  
        DS_imag       = -0.5.*imag(SS(N2/2+1-i));            % imag part (*(-1/2)) to create conjugate part 
        DS(N2/2+1+i)  = complex (DS_real, DS_imag);          % create the complex number
    end
end
function [DS] = SS2DS2 (SS)
    N1 = length(SS);
    N2 = 2*(N1-1);
    DS = zeros(N2,1);
    DS (1) = SS(1);
    for i=2:N2/2
        DS(i) = 0.5.*SS(i);
    end
    DS (N2/2+1) = SS(N2/2+1);
    for i=1:N2/2-1
        DS(N2/2+1+i) = 0.5.*SS(N2/2+1-i);                   % Original form (for abs or real values)
    end
end
%% Function for computing the sensitivity of Cavitometers 
function [f1,S_Original,S_1MHz] = Sensitivity_CAV(Cavitometer_position,ProbeType)
%%%% Probe 2 uses the old calibration from 15-50 kHz 
%%%% Probe 3_Shunt used with shunt uses the old calibration from 15-50 kHz 
%%%% Probe 3_Attenuator and Probe 4 use the new calibration from 8-400 kHz
SdB_Side_Probe2  = [-263.6	-263.3	-263.8	-264.8	-264.8	-264.9	-266.5	-266.4	-267.4	-270.1	-277.3	-268.3	-263.2	-261.6	-262.7	-265.7	-273.2	-271.3	-270.5	-269.4	-265.7	-262.8	-262.3	-263.5	-268.8	-283.3	-274.2	-271	-278.6	-275.1	-267.6	-265.3	-267.2	-274.5	-277	-267.3	-263.8	-262.3	-262.1	-262.8	-265	-268.7	-273.5	-278.6	-276.5	-271.7	-267.9	-265.2	-263.7	-263.2	-264.1	-266.5	-272.2	-281.5	-275.1	-270.8	-269.1	-268.2	-267.8	-267.8	-269.6	-273.5	-277.9	-279	-275.1	-271.9	-267.8	-264.7	-263.6	-264.1	-266.7];
SdB_Under_Probe2 = [-266.5	-266.2	-266.7	-267.7	-267.8	-267.8	-269.4	-269.3	-270.3	-273	-280.3	-271.2	-266.1	-264.5	-265.6	-268.6	-276.1	-274.2	-273.5	-272.3	-268.6	-265.7	-265.2	-266.5	-271.7	-286.2	-277.2	-273.9	-281.5	-278	-270.5	-268.2	-270.1	-277.4	-279.9	-270.3	-266.7	-265.3	-265	-265.7	-268	-271.6	-276.5	-281.5	-279.4	-274.6	-270.8	-268.1	-266.6	-266.1	-267	-269.4	-275.1	-284.4	-278	-273.7	-272	-271.1	-270.7	-270.7	-272.6	-276.5	-280.8	-281.9	-278	-274.8	-270.7	-267.6	-266.5	-267	-269.6];
SdB_Side_Probe3  = [-271	-271	-268	-271	-271	-274	-276	-277	-283	-284	-280	-279	-285	-282	-283	-282	-280	-283	-286	-287	-290	-288	-290	-288	-286	-281	-280	-278	-277	-275	-276	-276	-276	-276	-277	-277	-276	-277	-279	-281	-282	-284	-283	-283	-286	-282	-290	-289	-287	-286	-285	-286	-285	-286	-289	-287	-291	-284	-286	-285	-283	-285	-287	-281	-285	-287	-287	-284	-285	-286	-284	-285	-282	-283	-281	-284	-283	-288	-282	-284	-283	-283	-286	-281	-279	-283	-278	-278	-277	-282	-282	-284	-283	-285	-288	-290	-283	-285	-283	-285	-286	-285	-283	-281	-278	-272	-272	-266	-264	-263	-262	-260	-249	-248	-249	-250	-251	-252	-253	-254	-254	-254	-253	-252	-251	-250	-249	-248	-248	-248	-248	-248	-249	-250	-251	-252	-253	-254	-253	-252	-251	-249	-248	-247	-246	-246	-246	-245	-246	-246	-245	-245	-245	-245	-245	-245	-245	-245	-245	-245	-246	-246	-246	-246	-246	-246	-245	-245	-245	-245	-244	-244	-244	-244	-244	-244	-244	-245	-245	-245	-246	-246	-246	-247	-247	-248	-249	-251	-252	-254	-256	-257	-258	-257	-256	-255	-253	-252	-251	-250	-250	-249	-249	-249	-250	-250	-251	-252	-252	-252	-252	-252	-251	-250	-249	-248	-247	-247	-246	-246	-247	-247	-248	-248	-249	-249	-249	-248	-248	-247	-246	-246	-245	-245	-245	-246	-246	-247	-248	-250	-252	-254	-256	-257	-257	-256	-254	-252	-251	-250	-249	-248	-247	-247	-246	-245	-245	-244	-244	-244	-243	-243	-243	-243	-243	-243	-244	-245	-246	-247	-250	-252	-255	-255	-251	-248	-246	-244	-242	-241	-240	-239	-239	-238	-238	-237	-237	-237	-237	-237	-238	-238	-239	-240	-241	-243	-246	-251	-259	-256	-249	-245	-243	-241	-240	-238	-238	-237	-236	-236	-236	-236	-236	-237	-237	-238	-239	-240	-241	-243	-245	-247	-251	-255	-258	-255	-252	-250	-248	-247	-246	-246	-245	-245	-245	-245	-245	-245	-246	-246	-247	-247	-248	-249	-250	-251	-252	-254	-255	-256	-256	-255	-254	-254	-253	-252	-252	-251	-251	-251	-251	-251	-251	-251	-251	-251	-252	-252	-253	-253	-254	-254	-254	-255	-255	-255	-255	-255	-255	-255	-255	-255	-255	-255	-255	-256	-258	-260	-261	-262	-261	-259	-257	-256	-255	-254	-254	-253	-253	-252	-251	-250	-248	-247	-246	-245	-244	-243	-243	-242	-242	-242	-242	-242	-242	-242	-243	-243	-244	-245	-245	-246	-246	-247	-247	-247	-248	-248	-248	-248	-249	-249	-250	-250	-251	-252	-252	-252	-252	-253	-253	-254	-255	-257	-259	-262	-267	-266	-262	-259	-257	-255	-254	-254	-253	-253	-254	-254	-255	-255	-256	-256	-256	-257	-257	-257	-257	-258	-258	-257	-257	-257	-257	-257	-258	-259	-259	-258	-257	-256	-255	-253	-252	-251	-249	-248	-246	-245	-244	-243	-243	-243	-242	-242	-242	-242	-243	-243	-243	-244	-244	-245	-246	-247	-248	-248	-249	-249	-249	-249	-249	-249	-249	-249	-249	-249	-249	-250	-250	-251	-253	-254	-256	-257	-259	-260	-261	-261	-260	-260	-259	-258	-256	-253	-251	-248	-246	-244	-243	-242	-241	-241	-241	-241	-242	-243	-244	-246	-248	-250	-253	-255	-257	-258	-258	-258	-259	-259	-256	-253	-249	-247	-244	-243	-241	-240	-239	-239	-238	-238	-238	-239	-239	-240	-241	-242	-244	-246	-249	-253	-261	-259	-253	-249	-247	-245	-244	-244	-243	-243	-243	-244	-244	-245	-245	-246	-246	-246	-246	-245	-245	-244	-244	-243	-243	-242	-242	-242	-242	-242	-242	-243	-243	-244	-244	-245	-247	-248	-250	-251	-253	-254	-254	-254	-253	-251	-250	-250	-249	-249	-249	-249	-248	-247	-246	-244	-243	-242	-242	-241	-241	-241	-241	-240	-239	-239	-238	-238	-238	-239	-240	-243	-247	-257	-254	-246	-242	-240	-239	-238	-238	-238	-238	-238	-239	-240	-241	-243	-245	-250	-258	-254];
SdB_Under_Probe3 = [-272	-270	-270	-273	-275	-273	-280	-281	-281	-281	-280	-281	-285	-282	-288	-285	-292	-289	-288	-287	-287	-287	-291	-293	-295	-295	-292	-286	-287	-289	-285	-284	-286	-284	-286	-290	-285	-286	-286	-287	-288	-287	-285	-283	-286	-287	-288	-285	-284	-283	-281	-281	-285	-286	-287	-293	-285	-280	-286	-283	-289	-283	-284	-279	-284	-283	-289	-283	-283	-282	-282	-281	-281	-280	-285	-282	-289	-282	-282	-279	-279	-281	-278	-284	-284	-285	-285	-280	-280	-280	-283	-286	-281	-284	-284	-285	-285	-284	-286	-284	-288	-282	-281	-273	-274	-272	-270	-269	-266	-263	-261	-260	-245	-246	-248	-250	-253	-256	-258	-263	-273	-264	-256	-252	-249	-248	-247	-246	-246	-246	-247	-248	-249	-250	-251	-252	-253	-254	-256	-259	-263	-272	-271	-262	-258	-255	-253	-251	-251	-250	-249	-249	-248	-248	-247	-248	-248	-249	-249	-250	-251	-251	-251	-250	-250	-249	-248	-248	-248	-247	-246	-246	-245	-245	-245	-245	-245	-246	-246	-247	-247	-248	-249	-249	-249	-250	-250	-250	-250	-249	-249	-248	-247	-246	-245	-245	-244	-243	-243	-242	-242	-242	-241	-241	-241	-241	-241	-240	-240	-240	-240	-240	-240	-240	-240	-240	-240	-241	-242	-243	-244	-245	-246	-247	-247	-246	-245	-244	-242	-241	-241	-240	-240	-240	-240	-240	-240	-241	-241	-242	-243	-244	-245	-247	-249	-251	-254	-257	-260	-258	-255	-252	-250	-249	-247	-247	-246	-246	-245	-245	-245	-246	-246	-247	-249	-251	-254	-258	-267	-265	-257	-253	-251	-249	-247	-246	-245	-245	-244	-244	-244	-244	-244	-245	-245	-245	-246	-246	-246	-246	-246	-246	-246	-245	-245	-244	-243	-243	-242	-242	-241	-240	-240	-239	-239	-238	-238	-237	-237	-237	-237	-237	-237	-238	-238	-239	-239	-240	-240	-240	-240	-241	-241	-241	-241	-241	-241	-241	-242	-242	-243	-243	-244	-244	-244	-245	-245	-246	-247	-248	-249	-250	-252	-252	-252	-252	-251	-250	-249	-249	-248	-248	-248	-248	-249	-250	-250	-252	-253	-254	-254	-255	-254	-254	-253	-252	-252	-251	-251	-251	-251	-251	-251	-251	-251	-251	-251	-251	-251	-251	-252	-253	-253	-254	-254	-255	-254	-254	-253	-252	-252	-251	-251	-250	-250	-250	-250	-250	-250	-250	-250	-250	-250	-250	-250	-251	-251	-250	-250	-249	-248	-248	-247	-246	-246	-246	-246	-246	-247	-247	-248	-249	-250	-251	-253	-254	-255	-256	-257	-258	-259	-259	-259	-258	-258	-257	-256	-256	-255	-255	-255	-255	-255	-255	-255	-256	-257	-258	-258	-258	-257	-256	-255	-254	-254	-253	-253	-253	-254	-255	-255	-257	-258	-260	-262	-265	-269	-271	-267	-264	-262	-260	-258	-257	-255	-253	-252	-250	-249	-247	-246	-245	-244	-243	-243	-242	-241	-241	-241	-241	-241	-242	-242	-243	-245	-246	-248	-251	-253	-255	-256	-255	-253	-251	-249	-248	-247	-246	-246	-245	-245	-244	-244	-243	-243	-243	-243	-243	-243	-244	-244	-245	-246	-246	-247	-249	-250	-252	-255	-258	-263	-270	-275	-265	-259	-255	-252	-249	-247	-245	-244	-244	-243	-243	-243	-243	-243	-243	-243	-244	-244	-245	-245	-245	-246	-246	-246	-246	-246	-247	-247	-248	-249	-249	-249	-249	-249	-248	-247	-246	-246	-245	-245	-245	-245	-246	-246	-247	-248	-250	-251	-252	-254	-255	-256	-256	-257	-256	-256	-256	-255	-255	-255	-254	-254	-254	-253	-252	-251	-250	-249	-248	-247	-246	-245	-245	-245	-245	-246	-247	-248	-250	-251	-252	-251	-250	-249	-248	-248	-247	-247	-247	-247	-247	-247	-248	-248	-249	-250	-251	-253	-253	-252	-251	-249	-247	-246	-244	-243	-242	-241	-240	-238	-237	-236	-236	-235	-235	-235	-236	-237	-238	-240	-243	-245	-247	-247	-246	-245	-244	-243	-243	-243	-243	-242	-242	-242	-241	-240	-239	-238	-237];
SdB_Side_Probe4  = [-273	-276	-277	-275	-270	-274	-271	-271	-274	-271	-270	-270	-269	-268	-267	-268	-268	-270	-271	-271	-270	-269	-267	-268	-268	-269	-270	-272	-274	-275	-278	-281	-284	-286	-289	-289	-285	-288	-290	-288	-290	-290	-292	-289	-285	-279	-276	-272	-271	-269	-269	-270	-272	-279	-283	-285	-282	-278	-280	-277	-277	-274	-270	-267	-265	-264	-263	-262	-261	-260	-259	-259	-259	-259	-260	-261	-259	-257	-255	-255	-255	-255	-254	-252	-251	-250	-249	-249	-249	-249	-249	-248	-247	-247	-246	-246	-247	-248	-249	-251	-253	-254	-255	-256	-258	-259	-261	-262	-263	-262	-261	-260	-248	-249	-249	-249	-248	-248	-247	-247	-247	-247	-248	-249	-251	-251	-252	-252	-251	-251	-250	-250	-250	-250	-250	-251	-252	-254	-255	-257	-258	-259	-259	-256	-253	-251	-248	-247	-246	-245	-245	-244	-244	-243	-243	-242	-242	-243	-243	-243	-244	-245	-245	-246	-246	-246	-246	-245	-245	-245	-244	-244	-244	-243	-243	-243	-242	-242	-242	-241	-241	-241	-241	-241	-242	-242	-243	-244	-245	-246	-247	-248	-250	-251	-254	-257	-260	-265	-274	-284	-272	-269	-267	-266	-264	-263	-261	-259	-257	-256	-255	-255	-254	-254	-255	-256	-256	-258	-259	-262	-267	-278	-268	-261	-258	-255	-254	-253	-252	-252	-251	-251	-251	-251	-252	-252	-253	-254	-256	-258	-261	-265	-273	-277	-265	-260	-257	-254	-253	-251	-250	-249	-249	-248	-248	-247	-247	-248	-248	-248	-249	-250	-251	-252	-254	-256	-259	-262	-264	-262	-260	-258	-257	-256	-255	-254	-254	-254	-255	-255	-256	-256	-256	-256	-255	-254	-254	-253	-251	-250	-249	-247	-246	-245	-244	-243	-243	-243	-243	-243	-243	-244	-245	-247	-250	-252	-251	-247	-244	-242	-240	-238	-237	-236	-235	-234	-234	-233	-233	-233	-234	-234	-235	-236	-238	-239	-241	-241	-240	-239	-237	-235	-233	-231	-229	-228	-227	-225	-225	-224	-223	-223	-223	-223	-223	-223	-224	-225	-225	-226	-227	-227	-228	-228	-228	-229	-229	-229	-230	-231	-233	-235	-238	-242	-242	-238	-235	-232	-230	-229	-228	-228	-228	-228	-228	-228	-229	-230	-231	-232	-234	-235	-236	-236	-235	-234	-234	-233	-233	-233	-233	-234	-235	-236	-238	-239	-239	-239	-239	-238	-238	-237	-237	-237	-237	-237	-237	-238	-239	-239	-240	-242	-243	-245	-247	-249	-251	-252	-253	-252	-251	-250	-249	-248	-247	-247	-246	-246	-245	-245	-245	-245	-245	-245	-245	-245	-245	-245	-244	-244	-243	-243	-242	-242	-241	-241	-240	-240	-240	-241	-241	-241	-242	-243	-244	-245	-247	-247	-247	-247	-246	-245	-244	-243	-242	-242	-242	-241	-241	-241	-242	-242	-242	-241	-241	-240	-240	-239	-239	-238	-238	-238	-238	-238	-238	-239	-239	-240	-241	-242	-243	-244	-245	-246	-247	-248	-248	-248	-247	-245	-244	-242	-242	-241	-241	-241	-242	-242	-244	-245	-246	-247	-247	-247	-247	-246	-245	-244	-243	-242	-242	-242	-242	-243	-244	-246	-248	-251	-255	-263	-272	-262	-258	-256	-255	-254	-253	-251	-249	-246	-245	-243	-242	-240	-240	-239	-239	-238	-238	-238	-239	-239	-240	-240	-241	-242	-243	-245	-247	-249	-251	-253	-256	-258	-259	-259	-259	-259	-259	-260	-261	-263	-265	-266	-265	-263	-262	-260	-257	-255	-253	-251	-250	-249	-248	-247	-247	-246	-247	-247	-247	-248	-248	-249	-249	-250	-250	-251	-252	-253	-254	-255	-255	-256	-255	-255	-254	-253	-253	-252	-251	-251	-250	-250	-249	-248	-248	-247	-247	-247	-246	-246	-246	-247	-247	-248	-248	-249	-250	-251	-253	-254	-256	-257	-259	-260	-262	-263	-263	-264	-265	-265	-265	-264	-262	-260	-258	-256	-255	-253	-252	-251	-251	-250	-250	-250	-250	-250	-250	-250	-250	-250	-250	-250	-251	-253	-255	-259	-268	-266	-259	-255];
SdB_Under_Probe4 = [-270	-271	-273	-273	-272	-273	-276	-279	-279	-279	-282	-276	-279	-280	-282	-280	-278	-280	-280	-278	-280	-284	-288	-285	-283	-281	-279	-279	-279	-278	-280	-284	-288	-295	-295	-291	-290	-294	-291	-291	-293	-287	-296	-289	-282	-281	-277	-274	-272	-271	-271	-272	-272	-276	-276	-281	-278	-275	-274	-271	-273	-275	-276	-275	-269	-267	-265	-266	-267	-267	-267	-266	-265	-266	-268	-270	-270	-265	-263	-261	-260	-259	-260	-260	-259	-258	-257	-258	-261	-269	-260	-253	-250	-247	-246	-246	-247	-249	-254	-263	-258	-254	-253	-254	-260	-272	-262	-257	-256	-257	-256	-254	-252	-251	-249	-247	-246	-245	-243	-242	-240	-240	-239	-239	-238	-239	-239	-239	-239	-239	-239	-239	-239	-239	-239	-240	-240	-241	-241	-242	-243	-244	-245	-246	-248	-250	-251	-250	-248	-246	-245	-244	-244	-243	-243	-243	-243	-242	-242	-242	-242	-242	-241	-241	-241	-241	-241	-241	-241	-241	-242	-242	-242	-243	-244	-246	-247	-249	-252	-254	-257	-259	-261	-260	-260	-260	-261	-262	-263	-261	-258	-255	-253	-252	-251	-250	-249	-248	-248	-247	-247	-246	-246	-246	-245	-245	-244	-243	-243	-242	-241	-241	-240	-240	-239	-239	-239	-238	-238	-238	-238	-238	-239	-239	-239	-240	-241	-241	-242	-243	-244	-245	-246	-247	-248	-249	-249	-250	-251	-252	-252	-253	-252	-252	-251	-250	-249	-248	-247	-246	-245	-244	-243	-242	-241	-241	-240	-240	-240	-240	-239	-239	-240	-240	-240	-241	-241	-242	-243	-244	-245	-247	-249	-251	-253	-256	-258	-259	-258	-257	-256	-256	-255	-255	-255	-255	-254	-254	-253	-252	-251	-251	-250	-250	-250	-250	-251	-252	-254	-256	-256	-253	-250	-247	-245	-243	-241	-240	-239	-238	-237	-236	-236	-235	-235	-235	-236	-236	-236	-236	-235	-234	-233	-231	-230	-229	-228	-227	-227	-227	-227	-228	-228	-229	-229	-228	-228	-227	-226	-225	-225	-225	-225	-225	-225	-226	-227	-228	-229	-230	-232	-235	-238	-246	-250	-239	-234	-231	-228	-227	-226	-225	-224	-224	-224	-225	-225	-226	-226	-227	-228	-229	-230	-231	-232	-233	-235	-236	-236	-237	-237	-236	-235	-233	-232	-230	-229	-228	-227	-226	-226	-226	-226	-226	-226	-227	-228	-228	-230	-231	-232	-234	-236	-238	-239	-240	-240	-240	-240	-240	-242	-244	-248	-263	-250	-243	-238	-235	-233	-231	-229	-228	-228	-227	-227	-227	-227	-228	-229	-230	-231	-232	-233	-235	-235	-235	-235	-235	-235	-235	-235	-235	-235	-234	-234	-235	-235	-235	-236	-237	-238	-240	-241	-243	-243	-243	-241	-239	-237	-235	-234	-233	-232	-231	-230	-229	-229	-228	-228	-228	-227	-227	-227	-227	-226	-226	-226	-226	-226	-226	-226	-226	-226	-226	-227	-227	-228	-228	-229	-230	-231	-231	-232	-233	-234	-235	-236	-236	-237	-237	-237	-238	-238	-238	-239	-239	-239	-239	-239	-239	-238	-238	-237	-237	-237	-236	-237	-237	-237	-238	-238	-239	-239	-240	-240	-241	-241	-242	-242	-242	-243	-243	-244	-246	-247	-249	-251	-252	-251	-250	-249	-249	-249	-248	-248	-247	-247	-246	-245	-245	-245	-245	-245	-246	-247	-249	-251	-253	-255	-256	-256	-255	-253	-252	-250	-249	-249	-248	-248	-248	-248	-249	-249	-250	-251	-252	-253	-254	-256	-259	-261	-264	-264	-261	-259	-258	-256	-255	-254	-252	-251	-250	-249	-248	-247	-246	-245	-244	-244	-243	-243	-244	-244	-245	-246	-247	-248	-249	-250	-250	-250	-250	-250	-249	-248	-246	-245	-244	-244	-243	-243	-243	-244	-244	-245	-246	-247	-248	-249	-250	-250	-251	-251	-252	-252	-253	-254	-255	-256	-256	-256	-255	-253	-252	-251	-250	-249	-249	-249	-250	-251	-252	-254	-255	-256	-256	-255	-254	-253	-252	-252	-251	-251	-251	-251	-250	-250	-249	-249	-249	-249	-249];
SdB_Side_Probe3_Old  = [-282.4	-272	-266.9	-263.1	-261.3	-261.3	-263.3	-265.7	-272.5	-281.3	-266.6	-260.8	-258.2	-257.6	-258.4	-260.4	-264.4	-269.5	-274.8	-275.2	-270.6	-268.7	-268.8	-269.7	-273.9	-283.1	-279.8	-272.1	-269.1	-268.5	-269.8	-273.5	-283.7	-286.9	-279.9	-278.2	-280.1	-288.5	-291.8	-282.1	-278.6	-280.1	-281	-283.6	-287.8	-287.5	-283.1	-278.2	-273.3	-271.5	-272.6	-275.4	-274.4	-274.7	-277	-278.7	-285.1	-282.8	-277.1	-273.9	-273.7	-274.3	-286.3	-280.7	-278.4	-271.2	-267.7	-266.1	-266.9	-268.5	-269.7];
SdB_Under_Probe3_Old = [-285.3	-274.9	-269.8	-266	-264.2	-264.2	-266.3	-268.6	-275.4	-284.2	-269.5	-263.8	-261.1	-260.5	-261.3	-263.3	-267.4	-272.4	-277.7	-278.2	-273.5	-271.7	-271.7	-272.7	-276.9	-286	-282.7	-275.1	-272	-271.5	-272.7	-276.4	-286.6	-289.9	-282.8	-281.1	-283.1	-291.4	-294.7	-285	-281.5	-283	-283.9	-286.6	-290.7	-290.4	-286	-281.1	-276.2	-274.4	-275.6	-278.3	-277.3	-277.6	-279.9	-281.6	-288	-285.7	-280	-276.9	-276.7	-277.2	-289.3	-283.6	-281.4	-274.1	-270.6	-269	-269.8	-271.4	-272.6];
switch Cavitometer_position
    case 'Side'
%%% f1 is in Hz and 
    switch ProbeType  
        case 'Probe2'
            f1 = 1000.*[15	15.5	16	16.5	17	17.5	18	18.5	19	19.5	20	20.5	21	21.5	22	22.5	23	23.5	24	24.5	25	25.5	26	26.5	27	27.5	28	28.5	29	29.5	30	30.5	31	31.5	32	32.5	33	33.5	34	34.5	35	35.5	36	36.5	37	37.5	38	38.5	39	39.5	40	40.5	41	41.5	42	42.5	43	43.5	44	44.5	45	45.5	46	46.5	47	47.5	48	48.5	49	49.5	50]; 
            SdB_Side = SdB_Side_Probe2;
            S_1MHz = 35e-6;    % V/Pa (for Probe 2 Side)
        case 'Probe3_Shunt'
            f1 = 1000.*[15	15.5	16	16.5	17	17.5	18	18.5	19	19.5	20	20.5	21	21.5	22	22.5	23	23.5	24	24.5	25	25.5	26	26.5	27	27.5	28	28.5	29	29.5	30	30.5	31	31.5	32	32.5	33	33.5	34	34.5	35	35.5	36	36.5	37	37.5	38	38.5	39	39.5	40	40.5	41	41.5	42	42.5	43	43.5	44	44.5	45	45.5	46	46.5	47	47.5	48	48.5	49	49.5	50]; 
            SdB_Side = SdB_Side_Probe3_Old;
            S_1MHz = 25e-6;    % V/Pa (for Probe 3 Side)
        case 'Probe3_Attenuator'
            f1 = [8000	9000	10000	11000	12000	13000	14000	15000	16000	17000	18000	19000	20000	21000	22000	23000	24000	25000	26000	27000	28000	29000	30000	31000	32000	33000	34000	35000	36000	37000	38000	39000	40000	41000	42000	43000	44000	45000	46000	47000	48000	49000	50000	51000	52000	53000	54000	55000	56000	57000	58000	59000	60000	61000	62000	63000	64000	65000	66000	67000	68000	69000	70000	71000	72000	73000	74000	75000	76000	77000	78000	79000	80000	81000	82000	83000	84000	85000	86000	87000	88000	89000	90000	91000	92000	93000	94000	95000	96000	97000	98000	99000	100000	101000	102000	103000	104000	105000	106000	107000	108000	109000	110000	111000	112000	113000	114000	115000	116000	117000	118000	119000	120000.0034	120500.0034	121000.0034	121500.0034	122000.0035	122500.0035	123000.0035	123500.0035	124000.0035	124500.0035	125000.0035	125500.0035	126000.0036	126500.0036	127000.0036	127500.0036	128000.0036	128500.0036	129000.0036	129500.0037	130000.0037	130500.0037	131000.0037	131500.0037	132000.0037	132500.0037	133000.0038	133500.0038	134000.0038	134500.0038	135000.0038	135500.0038	136000.0038	136500.0039	137000.0039	137500.0039	138000.0039	138500.0039	139000.0039	139500.0039	140000.004	140500.004	141000.004	141500.004	142000.004	142500.004	143000.004	143500.0041	144000.0041	144500.0041	145000.0041	145500.0041	146000.0041	146500.0041	147000.0042	147500.0042	148000.0042	148500.0042	149000.0042	149500.0042	150000.0042	150500.0043	151000.0043	151500.0043	152000.0043	152500.0043	153000.0043	153500.0043	154000.0044	154500.0044	155000.0044	155500.0044	156000.0044	156500.0044	157000.0044	157500.0045	158000.0045	158500.0045	159000.0045	159500.0045	160000.0045	160500.0045	161000.0046	161500.0046	162000.0046	162500.0046	163000.0046	163500.0046	164000.0046	164500.0047	165000.0047	165500.0047	166000.0047	166500.0047	167000.0047	167500.0047	168000.0048	168500.0048	169000.0048	169500.0048	170000.0048	170500.0048	171000.0048	171500.0049	172000.0049	172500.0049	173000.0049	173500.0049	174000.0049	174500.0049	175000.0049	175500.005	176000.005	176500.005	177000.005	177500.005	178000.005	178500.005	179000.0051	179500.0051	180000.0051	180500.0051	181000.0051	181500.0051	182000.0051	182500.0052	183000.0052	183500.0052	184000.0052	184500.0052	185000.0052	185500.0052	186000.0053	186500.0053	187000.0053	187500.0053	188000.0053	188500.0053	189000.0053	189500.0054	190000.0054	190500.0054	191000.0054	191500.0054	192000.0054	192500.0054	193000.0055	193500.0055	194000.0055	194500.0055	195000.0055	195500.0055	196000.0055	196500.0056	197000.0056	197500.0056	198000.0056	198500.0056	199000.0056	199500.0056	200000.0057	200500.0057	201000.0057	201500.0057	202000.0057	202500.0057	203000.0057	203500.0058	204000.0058	204500.0058	205000.0058	205500.0058	206000.0058	206500.0058	207000.0059	207500.0059	208000.0059	208500.0059	209000.0059	209500.0059	210000.0059	210500.006	211000.006	211500.006	212000.006	212500.006	213000.006	213500.006	214000.0061	214500.0061	215000.0061	215500.0061	216000.0061	216500.0061	217000.0061	217500.0062	218000.0062	218500.0062	219000.0062	219500.0062	220000.0062	220500.0062	221000.0063	221500.0063	222000.0063	222500.0063	223000.0063	223500.0063	224000.0063	224500.0063	225000.0064	225500.0064	226000.0064	226500.0064	227000.0064	227500.0064	228000.0064	228500.0065	229000.0065	229500.0065	230000.0065	230500.0065	231000.0065	231500.0065	232000.0066	232500.0066	233000.0066	233500.0066	234000.0066	234500.0066	235000.0066	235500.0067	236000.0067	236500.0067	237000.0067	237500.0067	238000.0067	238500.0067	239000.0068	239500.0068	240000.0068	240500.0068	241000.0068	241500.0068	242000.0068	242500.0069	243000.0069	243500.0069	244000.0069	244500.0069	245000.0069	245500.0069	246000.007	246500.007	247000.007	247500.007	248000.007	248500.007	249000.007	249500.0071	250000.0071	250500.0071	251000.0071	251500.0071	252000.0071	252500.0071	253000.0072	253500.0072	254000.0072	254500.0072	255000.0072	255500.0072	256000.0072	256500.0073	257000.0073	257500.0073	258000.0073	258500.0073	259000.0073	259500.0073	260000.0074	260500.0074	261000.0074	261500.0074	262000.0074	262500.0074	263000.0074	263500.0075	264000.0075	264500.0075	265000.0075	265500.0075	266000.0075	266500.0075	267000.0076	267500.0076	268000.0076	268500.0076	269000.0076	269500.0076	270000.0076	270500.0077	271000.0077	271500.0077	272000.0077	272500.0077	273000.0077	273500.0077	274000.0077	274500.0078	275000.0078	275500.0078	276000.0078	276500.0078	277000.0078	277500.0078	278000.0079	278500.0079	279000.0079	279500.0079	280000.0079	280500.0079	281000.0079	281500.008	282000.008	282500.008	283000.008	283500.008	284000.008	284500.008	285000.0081	285500.0081	286000.0081	286500.0081	287000.0081	287500.0081	288000.0081	288500.0082	289000.0082	289500.0082	290000.0082	290500.0082	291000.0082	291500.0082	292000.0083	292500.0083	293000.0083	293500.0083	294000.0083	294500.0083	295000.0083	295500.0084	296000.0084	296500.0084	297000.0084	297500.0084	298000.0084	298500.0084	299000.0085	299500.0085	300000.0085	300500.0085	301000.0085	301500.0085	302000.0085	302500.0086	303000.0086	303500.0086	304000.0086	304500.0086	305000.0086	305500.0086	306000.0087	306500.0087	307000.0087	307500.0087	308000.0087	308500.0087	309000.0087	309500.0088	310000.0088	310500.0088	311000.0088	311500.0088	312000.0088	312500.0088	313000.0089	313500.0089	314000.0089	314500.0089	315000.0089	315500.0089	316000.0089	316500.009	317000.009	317500.009	318000.009	318500.009	319000.009	319500.009	320000.0091	320500.0091	321000.0091	321500.0091	322000.0091	322500.0091	323000.0091	323500.0091	324000.0092	324500.0092	325000.0092	325500.0092	326000.0092	326500.0092	327000.0092	327500.0093	328000.0093	328500.0093	329000.0093	329500.0093	330000.0093	330500.0093	331000.0094	331500.0094	332000.0094	332500.0094	333000.0094	333500.0094	334000.0094	334500.0095	335000.0095	335500.0095	336000.0095	336500.0095	337000.0095	337500.0095	338000.0096	338500.0096	339000.0096	339500.0096	340000.0096	340500.0096	341000.0096	341500.0097	342000.0097	342500.0097	343000.0097	343500.0097	344000.0097	344500.0097	345000.0098	345500.0098	346000.0098	346500.0098	347000.0098	347500.0098	348000.0098	348500.0099	349000.0099	349500.0099	350000.0099	350500.0099	351000.0099	351500.0099	352000.01	352500.01	353000.01	353500.01	354000.01	354500.01	355000.01	355500.0101	356000.0101	356500.0101	357000.0101	357500.0101	358000.0101	358500.0101	359000.0102	359500.0102	360000.0102	360500.0102	361000.0102	361500.0102	362000.0102	362500.0103	363000.0103	363500.0103	364000.0103	364500.0103	365000.0103	365500.0103	366000.0104	366500.0104	367000.0104	367500.0104	368000.0104	368500.0104	369000.0104	369500.0105	370000.0105	370500.0105	371000.0105	371500.0105	372000.0105	372500.0105	373000.0105	373500.0106	374000.0106	374500.0106	375000.0106	375500.0106	376000.0106	376500.0106	377000.0107	377500.0107	378000.0107	378500.0107	379000.0107	379500.0107	380000.0107	380500.0108	381000.0108	381500.0108	382000.0108	382500.0108	383000.0108	383500.0108	384000.0109	384500.0109	385000.0109	385500.0109	386000.0109	386500.0109	387000.0109	387500.011	388000.011	388500.011	389000.011	389500.011	390000.011	390500.011	391000.0111	391500.0111	392000.0111	392500.0111	393000.0111	393500.0111	394000.0111	394500.0112	395000.0112	395500.0112	396000.0112	396500.0112	397000.0112	397500.0112	398000.0113	398500.0113	399000.0113	399500.0113];
            SdB_Side = SdB_Side_Probe3; 
        case 'Probe4'
            f1 = [8000	9000	10000	11000	12000	13000	14000	15000	16000	17000	18000	19000	20000	21000	22000	23000	24000	25000	26000	27000	28000	29000	30000	31000	32000	33000	34000	35000	36000	37000	38000	39000	40000	41000	42000	43000	44000	45000	46000	47000	48000	49000	50000	51000	52000	53000	54000	55000	56000	57000	58000	59000	60000	61000	62000	63000	64000	65000	66000	67000	68000	69000	70000	71000	72000	73000	74000	75000	76000	77000	78000	79000	80000	81000	82000	83000	84000	85000	86000	87000	88000	89000	90000	91000	92000	93000	94000	95000	96000	97000	98000	99000	100000	101000	102000	103000	104000	105000	106000	107000	108000	109000	110000	111000	112000	113000	114000	115000	116000	117000	118000	119000	120000.0034	120500.0034	121000.0034	121500.0034	122000.0035	122500.0035	123000.0035	123500.0035	124000.0035	124500.0035	125000.0035	125500.0035	126000.0036	126500.0036	127000.0036	127500.0036	128000.0036	128500.0036	129000.0036	129500.0037	130000.0037	130500.0037	131000.0037	131500.0037	132000.0037	132500.0037	133000.0038	133500.0038	134000.0038	134500.0038	135000.0038	135500.0038	136000.0038	136500.0039	137000.0039	137500.0039	138000.0039	138500.0039	139000.0039	139500.0039	140000.004	140500.004	141000.004	141500.004	142000.004	142500.004	143000.004	143500.0041	144000.0041	144500.0041	145000.0041	145500.0041	146000.0041	146500.0041	147000.0042	147500.0042	148000.0042	148500.0042	149000.0042	149500.0042	150000.0042	150500.0043	151000.0043	151500.0043	152000.0043	152500.0043	153000.0043	153500.0043	154000.0044	154500.0044	155000.0044	155500.0044	156000.0044	156500.0044	157000.0044	157500.0045	158000.0045	158500.0045	159000.0045	159500.0045	160000.0045	160500.0045	161000.0046	161500.0046	162000.0046	162500.0046	163000.0046	163500.0046	164000.0046	164500.0047	165000.0047	165500.0047	166000.0047	166500.0047	167000.0047	167500.0047	168000.0048	168500.0048	169000.0048	169500.0048	170000.0048	170500.0048	171000.0048	171500.0049	172000.0049	172500.0049	173000.0049	173500.0049	174000.0049	174500.0049	175000.0049	175500.005	176000.005	176500.005	177000.005	177500.005	178000.005	178500.005	179000.0051	179500.0051	180000.0051	180500.0051	181000.0051	181500.0051	182000.0051	182500.0052	183000.0052	183500.0052	184000.0052	184500.0052	185000.0052	185500.0052	186000.0053	186500.0053	187000.0053	187500.0053	188000.0053	188500.0053	189000.0053	189500.0054	190000.0054	190500.0054	191000.0054	191500.0054	192000.0054	192500.0054	193000.0055	193500.0055	194000.0055	194500.0055	195000.0055	195500.0055	196000.0055	196500.0056	197000.0056	197500.0056	198000.0056	198500.0056	199000.0056	199500.0056	200000.0057	200500.0057	201000.0057	201500.0057	202000.0057	202500.0057	203000.0057	203500.0058	204000.0058	204500.0058	205000.0058	205500.0058	206000.0058	206500.0058	207000.0059	207500.0059	208000.0059	208500.0059	209000.0059	209500.0059	210000.0059	210500.006	211000.006	211500.006	212000.006	212500.006	213000.006	213500.006	214000.0061	214500.0061	215000.0061	215500.0061	216000.0061	216500.0061	217000.0061	217500.0062	218000.0062	218500.0062	219000.0062	219500.0062	220000.0062	220500.0062	221000.0063	221500.0063	222000.0063	222500.0063	223000.0063	223500.0063	224000.0063	224500.0063	225000.0064	225500.0064	226000.0064	226500.0064	227000.0064	227500.0064	228000.0064	228500.0065	229000.0065	229500.0065	230000.0065	230500.0065	231000.0065	231500.0065	232000.0066	232500.0066	233000.0066	233500.0066	234000.0066	234500.0066	235000.0066	235500.0067	236000.0067	236500.0067	237000.0067	237500.0067	238000.0067	238500.0067	239000.0068	239500.0068	240000.0068	240500.0068	241000.0068	241500.0068	242000.0068	242500.0069	243000.0069	243500.0069	244000.0069	244500.0069	245000.0069	245500.0069	246000.007	246500.007	247000.007	247500.007	248000.007	248500.007	249000.007	249500.0071	250000.0071	250500.0071	251000.0071	251500.0071	252000.0071	252500.0071	253000.0072	253500.0072	254000.0072	254500.0072	255000.0072	255500.0072	256000.0072	256500.0073	257000.0073	257500.0073	258000.0073	258500.0073	259000.0073	259500.0073	260000.0074	260500.0074	261000.0074	261500.0074	262000.0074	262500.0074	263000.0074	263500.0075	264000.0075	264500.0075	265000.0075	265500.0075	266000.0075	266500.0075	267000.0076	267500.0076	268000.0076	268500.0076	269000.0076	269500.0076	270000.0076	270500.0077	271000.0077	271500.0077	272000.0077	272500.0077	273000.0077	273500.0077	274000.0077	274500.0078	275000.0078	275500.0078	276000.0078	276500.0078	277000.0078	277500.0078	278000.0079	278500.0079	279000.0079	279500.0079	280000.0079	280500.0079	281000.0079	281500.008	282000.008	282500.008	283000.008	283500.008	284000.008	284500.008	285000.0081	285500.0081	286000.0081	286500.0081	287000.0081	287500.0081	288000.0081	288500.0082	289000.0082	289500.0082	290000.0082	290500.0082	291000.0082	291500.0082	292000.0083	292500.0083	293000.0083	293500.0083	294000.0083	294500.0083	295000.0083	295500.0084	296000.0084	296500.0084	297000.0084	297500.0084	298000.0084	298500.0084	299000.0085	299500.0085	300000.0085	300500.0085	301000.0085	301500.0085	302000.0085	302500.0086	303000.0086	303500.0086	304000.0086	304500.0086	305000.0086	305500.0086	306000.0087	306500.0087	307000.0087	307500.0087	308000.0087	308500.0087	309000.0087	309500.0088	310000.0088	310500.0088	311000.0088	311500.0088	312000.0088	312500.0088	313000.0089	313500.0089	314000.0089	314500.0089	315000.0089	315500.0089	316000.0089	316500.009	317000.009	317500.009	318000.009	318500.009	319000.009	319500.009	320000.0091	320500.0091	321000.0091	321500.0091	322000.0091	322500.0091	323000.0091	323500.0091	324000.0092	324500.0092	325000.0092	325500.0092	326000.0092	326500.0092	327000.0092	327500.0093	328000.0093	328500.0093	329000.0093	329500.0093	330000.0093	330500.0093	331000.0094	331500.0094	332000.0094	332500.0094	333000.0094	333500.0094	334000.0094	334500.0095	335000.0095	335500.0095	336000.0095	336500.0095	337000.0095	337500.0095	338000.0096	338500.0096	339000.0096	339500.0096	340000.0096	340500.0096	341000.0096	341500.0097	342000.0097	342500.0097	343000.0097	343500.0097	344000.0097	344500.0097	345000.0098	345500.0098	346000.0098	346500.0098	347000.0098	347500.0098	348000.0098	348500.0099	349000.0099	349500.0099	350000.0099	350500.0099	351000.0099	351500.0099	352000.01	352500.01	353000.01	353500.01	354000.01	354500.01	355000.01	355500.0101	356000.0101	356500.0101	357000.0101	357500.0101	358000.0101	358500.0101	359000.0102	359500.0102	360000.0102	360500.0102	361000.0102	361500.0102	362000.0102	362500.0103	363000.0103	363500.0103	364000.0103	364500.0103	365000.0103	365500.0103	366000.0104	366500.0104	367000.0104	367500.0104	368000.0104	368500.0104	369000.0104	369500.0105	370000.0105	370500.0105	371000.0105	371500.0105	372000.0105	372500.0105	373000.0105	373500.0106	374000.0106	374500.0106	375000.0106	375500.0106	376000.0106	376500.0106	377000.0107	377500.0107	378000.0107	378500.0107	379000.0107	379500.0107	380000.0107	380500.0108	381000.0108	381500.0108	382000.0108	382500.0108	383000.0108	383500.0108	384000.0109	384500.0109	385000.0109	385500.0109	386000.0109	386500.0109	387000.0109	387500.011	388000.011	388500.011	389000.011	389500.011	390000.011	390500.011	391000.0111	391500.0111	392000.0111	392500.0111	393000.0111	393500.0111	394000.0111	394500.0112	395000.0112	395500.0112	396000.0112	396500.0112	397000.0112	397500.0112	398000.0113	398500.0113	399000.0113	399500.0113];
            SdB_Side = SdB_Side_Probe4; 
    end  
    TF_Side  = 10.^ (SdB_Side./20).*1e6;        % result is in V/Pa     % work with Standard SI units
    S_Original =  TF_Side;
    case 'Under'
    switch ProbeType
        case 'Probe2'
            f1 = 1000.*[15	15.5	16	16.5	17	17.5	18	18.5	19	19.5	20	20.5	21	21.5	22	22.5	23	23.5	24	24.5	25	25.5	26	26.5	27	27.5	28	28.5	29	29.5	30	30.5	31	31.5	32	32.5	33	33.5	34	34.5	35	35.5	36	36.5	37	37.5	38	38.5	39	39.5	40	40.5	41	41.5	42	42.5	43	43.5	44	44.5	45	45.5	46	46.5	47	47.5	48	48.5	49	49.5	50]; 
            SdB_Under = SdB_Under_Probe2;
            S_1MHz = 120e-6;   % V/Pa (for Probe 2 Under)
        case 'Probe3_Shunt'
            f1 = 1000.*[15	15.5	16	16.5	17	17.5	18	18.5	19	19.5	20	20.5	21	21.5	22	22.5	23	23.5	24	24.5	25	25.5	26	26.5	27	27.5	28	28.5	29	29.5	30	30.5	31	31.5	32	32.5	33	33.5	34	34.5	35	35.5	36	36.5	37	37.5	38	38.5	39	39.5	40	40.5	41	41.5	42	42.5	43	43.5	44	44.5	45	45.5	46	46.5	47	47.5	48	48.5	49	49.5	50]; 
            SdB_Under = SdB_Under_Probe3_Old;
            S_1MHz = 200e-6;   % V/Pa (for Probe 3 Under)
        case 'Probe3_Attenuator'
            f1 = [8000	9000	10000	11000	12000	13000	14000	15000	16000	17000	18000	19000	20000	21000	22000	23000	24000	25000	26000	27000	28000	29000	30000	31000	32000	33000	34000	35000	36000	37000	38000	39000	40000	41000	42000	43000	44000	45000	46000	47000	48000	49000	50000	51000	52000	53000	54000	55000	56000	57000	58000	59000	60000	61000	62000	63000	64000	65000	66000	67000	68000	69000	70000	71000	72000	73000	74000	75000	76000	77000	78000	79000	80000	81000	82000	83000	84000	85000	86000	87000	88000	89000	90000	91000	92000	93000	94000	95000	96000	97000	98000	99000	100000	101000	102000	103000	104000	105000	106000	107000	108000	109000	110000	111000	112000	113000	114000	115000	116000	117000	118000	119000	120000.0034	120500.0034	121000.0034	121500.0034	122000.0035	122500.0035	123000.0035	123500.0035	124000.0035	124500.0035	125000.0035	125500.0035	126000.0036	126500.0036	127000.0036	127500.0036	128000.0036	128500.0036	129000.0036	129500.0037	130000.0037	130500.0037	131000.0037	131500.0037	132000.0037	132500.0037	133000.0038	133500.0038	134000.0038	134500.0038	135000.0038	135500.0038	136000.0038	136500.0039	137000.0039	137500.0039	138000.0039	138500.0039	139000.0039	139500.0039	140000.004	140500.004	141000.004	141500.004	142000.004	142500.004	143000.004	143500.0041	144000.0041	144500.0041	145000.0041	145500.0041	146000.0041	146500.0041	147000.0042	147500.0042	148000.0042	148500.0042	149000.0042	149500.0042	150000.0042	150500.0043	151000.0043	151500.0043	152000.0043	152500.0043	153000.0043	153500.0043	154000.0044	154500.0044	155000.0044	155500.0044	156000.0044	156500.0044	157000.0044	157500.0045	158000.0045	158500.0045	159000.0045	159500.0045	160000.0045	160500.0045	161000.0046	161500.0046	162000.0046	162500.0046	163000.0046	163500.0046	164000.0046	164500.0047	165000.0047	165500.0047	166000.0047	166500.0047	167000.0047	167500.0047	168000.0048	168500.0048	169000.0048	169500.0048	170000.0048	170500.0048	171000.0048	171500.0049	172000.0049	172500.0049	173000.0049	173500.0049	174000.0049	174500.0049	175000.0049	175500.005	176000.005	176500.005	177000.005	177500.005	178000.005	178500.005	179000.0051	179500.0051	180000.0051	180500.0051	181000.0051	181500.0051	182000.0051	182500.0052	183000.0052	183500.0052	184000.0052	184500.0052	185000.0052	185500.0052	186000.0053	186500.0053	187000.0053	187500.0053	188000.0053	188500.0053	189000.0053	189500.0054	190000.0054	190500.0054	191000.0054	191500.0054	192000.0054	192500.0054	193000.0055	193500.0055	194000.0055	194500.0055	195000.0055	195500.0055	196000.0055	196500.0056	197000.0056	197500.0056	198000.0056	198500.0056	199000.0056	199500.0056	200000.0057	200500.0057	201000.0057	201500.0057	202000.0057	202500.0057	203000.0057	203500.0058	204000.0058	204500.0058	205000.0058	205500.0058	206000.0058	206500.0058	207000.0059	207500.0059	208000.0059	208500.0059	209000.0059	209500.0059	210000.0059	210500.006	211000.006	211500.006	212000.006	212500.006	213000.006	213500.006	214000.0061	214500.0061	215000.0061	215500.0061	216000.0061	216500.0061	217000.0061	217500.0062	218000.0062	218500.0062	219000.0062	219500.0062	220000.0062	220500.0062	221000.0063	221500.0063	222000.0063	222500.0063	223000.0063	223500.0063	224000.0063	224500.0063	225000.0064	225500.0064	226000.0064	226500.0064	227000.0064	227500.0064	228000.0064	228500.0065	229000.0065	229500.0065	230000.0065	230500.0065	231000.0065	231500.0065	232000.0066	232500.0066	233000.0066	233500.0066	234000.0066	234500.0066	235000.0066	235500.0067	236000.0067	236500.0067	237000.0067	237500.0067	238000.0067	238500.0067	239000.0068	239500.0068	240000.0068	240500.0068	241000.0068	241500.0068	242000.0068	242500.0069	243000.0069	243500.0069	244000.0069	244500.0069	245000.0069	245500.0069	246000.007	246500.007	247000.007	247500.007	248000.007	248500.007	249000.007	249500.0071	250000.0071	250500.0071	251000.0071	251500.0071	252000.0071	252500.0071	253000.0072	253500.0072	254000.0072	254500.0072	255000.0072	255500.0072	256000.0072	256500.0073	257000.0073	257500.0073	258000.0073	258500.0073	259000.0073	259500.0073	260000.0074	260500.0074	261000.0074	261500.0074	262000.0074	262500.0074	263000.0074	263500.0075	264000.0075	264500.0075	265000.0075	265500.0075	266000.0075	266500.0075	267000.0076	267500.0076	268000.0076	268500.0076	269000.0076	269500.0076	270000.0076	270500.0077	271000.0077	271500.0077	272000.0077	272500.0077	273000.0077	273500.0077	274000.0077	274500.0078	275000.0078	275500.0078	276000.0078	276500.0078	277000.0078	277500.0078	278000.0079	278500.0079	279000.0079	279500.0079	280000.0079	280500.0079	281000.0079	281500.008	282000.008	282500.008	283000.008	283500.008	284000.008	284500.008	285000.0081	285500.0081	286000.0081	286500.0081	287000.0081	287500.0081	288000.0081	288500.0082	289000.0082	289500.0082	290000.0082	290500.0082	291000.0082	291500.0082	292000.0083	292500.0083	293000.0083	293500.0083	294000.0083	294500.0083	295000.0083	295500.0084	296000.0084	296500.0084	297000.0084	297500.0084	298000.0084	298500.0084	299000.0085	299500.0085	300000.0085	300500.0085	301000.0085	301500.0085	302000.0085	302500.0086	303000.0086	303500.0086	304000.0086	304500.0086	305000.0086	305500.0086	306000.0087	306500.0087	307000.0087	307500.0087	308000.0087	308500.0087	309000.0087	309500.0088	310000.0088	310500.0088	311000.0088	311500.0088	312000.0088	312500.0088	313000.0089	313500.0089	314000.0089	314500.0089	315000.0089	315500.0089	316000.0089	316500.009	317000.009	317500.009	318000.009	318500.009	319000.009	319500.009	320000.0091	320500.0091	321000.0091	321500.0091	322000.0091	322500.0091	323000.0091	323500.0091	324000.0092	324500.0092	325000.0092	325500.0092	326000.0092	326500.0092	327000.0092	327500.0093	328000.0093	328500.0093	329000.0093	329500.0093	330000.0093	330500.0093	331000.0094	331500.0094	332000.0094	332500.0094	333000.0094	333500.0094	334000.0094	334500.0095	335000.0095	335500.0095	336000.0095	336500.0095	337000.0095	337500.0095	338000.0096	338500.0096	339000.0096	339500.0096	340000.0096	340500.0096	341000.0096	341500.0097	342000.0097	342500.0097	343000.0097	343500.0097	344000.0097	344500.0097	345000.0098	345500.0098	346000.0098	346500.0098	347000.0098	347500.0098	348000.0098	348500.0099	349000.0099	349500.0099	350000.0099	350500.0099	351000.0099	351500.0099	352000.01	352500.01	353000.01	353500.01	354000.01	354500.01	355000.01	355500.0101	356000.0101	356500.0101	357000.0101	357500.0101	358000.0101	358500.0101	359000.0102	359500.0102	360000.0102	360500.0102	361000.0102	361500.0102	362000.0102	362500.0103	363000.0103	363500.0103	364000.0103	364500.0103	365000.0103	365500.0103	366000.0104	366500.0104	367000.0104	367500.0104	368000.0104	368500.0104	369000.0104	369500.0105	370000.0105	370500.0105	371000.0105	371500.0105	372000.0105	372500.0105	373000.0105	373500.0106	374000.0106	374500.0106	375000.0106	375500.0106	376000.0106	376500.0106	377000.0107	377500.0107	378000.0107	378500.0107	379000.0107	379500.0107	380000.0107	380500.0108	381000.0108	381500.0108	382000.0108	382500.0108	383000.0108	383500.0108	384000.0109	384500.0109	385000.0109	385500.0109	386000.0109	386500.0109	387000.0109	387500.011	388000.011	388500.011	389000.011	389500.011	390000.011	390500.011	391000.0111	391500.0111	392000.0111	392500.0111	393000.0111	393500.0111	394000.0111	394500.0112	395000.0112	395500.0112	396000.0112	396500.0112	397000.0112	397500.0112	398000.0113	398500.0113	399000.0113	399500.0113];
            SdB_Under = SdB_Under_Probe3; 
        case 'Probe4'
            f1 = [8000	9000	10000	11000	12000	13000	14000	15000	16000	17000	18000	19000	20000	21000	22000	23000	24000	25000	26000	27000	28000	29000	30000	31000	32000	33000	34000	35000	36000	37000	38000	39000	40000	41000	42000	43000	44000	45000	46000	47000	48000	49000	50000	51000	52000	53000	54000	55000	56000	57000	58000	59000	60000	61000	62000	63000	64000	65000	66000	67000	68000	69000	70000	71000	72000	73000	74000	75000	76000	77000	78000	79000	80000	81000	82000	83000	84000	85000	86000	87000	88000	89000	90000	91000	92000	93000	94000	95000	96000	97000	98000	99000	100000	101000	102000	103000	104000	105000	106000	107000	108000	109000	110000	111000	112000	113000	114000	115000	116000	117000	118000	119000	120000.0034	120500.0034	121000.0034	121500.0034	122000.0035	122500.0035	123000.0035	123500.0035	124000.0035	124500.0035	125000.0035	125500.0035	126000.0036	126500.0036	127000.0036	127500.0036	128000.0036	128500.0036	129000.0036	129500.0037	130000.0037	130500.0037	131000.0037	131500.0037	132000.0037	132500.0037	133000.0038	133500.0038	134000.0038	134500.0038	135000.0038	135500.0038	136000.0038	136500.0039	137000.0039	137500.0039	138000.0039	138500.0039	139000.0039	139500.0039	140000.004	140500.004	141000.004	141500.004	142000.004	142500.004	143000.004	143500.0041	144000.0041	144500.0041	145000.0041	145500.0041	146000.0041	146500.0041	147000.0042	147500.0042	148000.0042	148500.0042	149000.0042	149500.0042	150000.0042	150500.0043	151000.0043	151500.0043	152000.0043	152500.0043	153000.0043	153500.0043	154000.0044	154500.0044	155000.0044	155500.0044	156000.0044	156500.0044	157000.0044	157500.0045	158000.0045	158500.0045	159000.0045	159500.0045	160000.0045	160500.0045	161000.0046	161500.0046	162000.0046	162500.0046	163000.0046	163500.0046	164000.0046	164500.0047	165000.0047	165500.0047	166000.0047	166500.0047	167000.0047	167500.0047	168000.0048	168500.0048	169000.0048	169500.0048	170000.0048	170500.0048	171000.0048	171500.0049	172000.0049	172500.0049	173000.0049	173500.0049	174000.0049	174500.0049	175000.0049	175500.005	176000.005	176500.005	177000.005	177500.005	178000.005	178500.005	179000.0051	179500.0051	180000.0051	180500.0051	181000.0051	181500.0051	182000.0051	182500.0052	183000.0052	183500.0052	184000.0052	184500.0052	185000.0052	185500.0052	186000.0053	186500.0053	187000.0053	187500.0053	188000.0053	188500.0053	189000.0053	189500.0054	190000.0054	190500.0054	191000.0054	191500.0054	192000.0054	192500.0054	193000.0055	193500.0055	194000.0055	194500.0055	195000.0055	195500.0055	196000.0055	196500.0056	197000.0056	197500.0056	198000.0056	198500.0056	199000.0056	199500.0056	200000.0057	200500.0057	201000.0057	201500.0057	202000.0057	202500.0057	203000.0057	203500.0058	204000.0058	204500.0058	205000.0058	205500.0058	206000.0058	206500.0058	207000.0059	207500.0059	208000.0059	208500.0059	209000.0059	209500.0059	210000.0059	210500.006	211000.006	211500.006	212000.006	212500.006	213000.006	213500.006	214000.0061	214500.0061	215000.0061	215500.0061	216000.0061	216500.0061	217000.0061	217500.0062	218000.0062	218500.0062	219000.0062	219500.0062	220000.0062	220500.0062	221000.0063	221500.0063	222000.0063	222500.0063	223000.0063	223500.0063	224000.0063	224500.0063	225000.0064	225500.0064	226000.0064	226500.0064	227000.0064	227500.0064	228000.0064	228500.0065	229000.0065	229500.0065	230000.0065	230500.0065	231000.0065	231500.0065	232000.0066	232500.0066	233000.0066	233500.0066	234000.0066	234500.0066	235000.0066	235500.0067	236000.0067	236500.0067	237000.0067	237500.0067	238000.0067	238500.0067	239000.0068	239500.0068	240000.0068	240500.0068	241000.0068	241500.0068	242000.0068	242500.0069	243000.0069	243500.0069	244000.0069	244500.0069	245000.0069	245500.0069	246000.007	246500.007	247000.007	247500.007	248000.007	248500.007	249000.007	249500.0071	250000.0071	250500.0071	251000.0071	251500.0071	252000.0071	252500.0071	253000.0072	253500.0072	254000.0072	254500.0072	255000.0072	255500.0072	256000.0072	256500.0073	257000.0073	257500.0073	258000.0073	258500.0073	259000.0073	259500.0073	260000.0074	260500.0074	261000.0074	261500.0074	262000.0074	262500.0074	263000.0074	263500.0075	264000.0075	264500.0075	265000.0075	265500.0075	266000.0075	266500.0075	267000.0076	267500.0076	268000.0076	268500.0076	269000.0076	269500.0076	270000.0076	270500.0077	271000.0077	271500.0077	272000.0077	272500.0077	273000.0077	273500.0077	274000.0077	274500.0078	275000.0078	275500.0078	276000.0078	276500.0078	277000.0078	277500.0078	278000.0079	278500.0079	279000.0079	279500.0079	280000.0079	280500.0079	281000.0079	281500.008	282000.008	282500.008	283000.008	283500.008	284000.008	284500.008	285000.0081	285500.0081	286000.0081	286500.0081	287000.0081	287500.0081	288000.0081	288500.0082	289000.0082	289500.0082	290000.0082	290500.0082	291000.0082	291500.0082	292000.0083	292500.0083	293000.0083	293500.0083	294000.0083	294500.0083	295000.0083	295500.0084	296000.0084	296500.0084	297000.0084	297500.0084	298000.0084	298500.0084	299000.0085	299500.0085	300000.0085	300500.0085	301000.0085	301500.0085	302000.0085	302500.0086	303000.0086	303500.0086	304000.0086	304500.0086	305000.0086	305500.0086	306000.0087	306500.0087	307000.0087	307500.0087	308000.0087	308500.0087	309000.0087	309500.0088	310000.0088	310500.0088	311000.0088	311500.0088	312000.0088	312500.0088	313000.0089	313500.0089	314000.0089	314500.0089	315000.0089	315500.0089	316000.0089	316500.009	317000.009	317500.009	318000.009	318500.009	319000.009	319500.009	320000.0091	320500.0091	321000.0091	321500.0091	322000.0091	322500.0091	323000.0091	323500.0091	324000.0092	324500.0092	325000.0092	325500.0092	326000.0092	326500.0092	327000.0092	327500.0093	328000.0093	328500.0093	329000.0093	329500.0093	330000.0093	330500.0093	331000.0094	331500.0094	332000.0094	332500.0094	333000.0094	333500.0094	334000.0094	334500.0095	335000.0095	335500.0095	336000.0095	336500.0095	337000.0095	337500.0095	338000.0096	338500.0096	339000.0096	339500.0096	340000.0096	340500.0096	341000.0096	341500.0097	342000.0097	342500.0097	343000.0097	343500.0097	344000.0097	344500.0097	345000.0098	345500.0098	346000.0098	346500.0098	347000.0098	347500.0098	348000.0098	348500.0099	349000.0099	349500.0099	350000.0099	350500.0099	351000.0099	351500.0099	352000.01	352500.01	353000.01	353500.01	354000.01	354500.01	355000.01	355500.0101	356000.0101	356500.0101	357000.0101	357500.0101	358000.0101	358500.0101	359000.0102	359500.0102	360000.0102	360500.0102	361000.0102	361500.0102	362000.0102	362500.0103	363000.0103	363500.0103	364000.0103	364500.0103	365000.0103	365500.0103	366000.0104	366500.0104	367000.0104	367500.0104	368000.0104	368500.0104	369000.0104	369500.0105	370000.0105	370500.0105	371000.0105	371500.0105	372000.0105	372500.0105	373000.0105	373500.0106	374000.0106	374500.0106	375000.0106	375500.0106	376000.0106	376500.0106	377000.0107	377500.0107	378000.0107	378500.0107	379000.0107	379500.0107	380000.0107	380500.0108	381000.0108	381500.0108	382000.0108	382500.0108	383000.0108	383500.0108	384000.0109	384500.0109	385000.0109	385500.0109	386000.0109	386500.0109	387000.0109	387500.011	388000.011	388500.011	389000.011	389500.011	390000.011	390500.011	391000.0111	391500.0111	392000.0111	392500.0111	393000.0111	393500.0111	394000.0111	394500.0112	395000.0112	395500.0112	396000.0112	396500.0112	397000.0112	397500.0112	398000.0113	398500.0113	399000.0113	399500.0113];
            SdB_Under = SdB_Under_Probe4; 
    end
    TF_Under = 10.^ (SdB_Under./20).*1e6;       % result is in V/Pa
    S_Original =  TF_Under;
end
end
%% Function for computing the Pre-Amplication factor for Cavitometers 2 and 3_Shunt
function [AF_Original,AF_1MHz] = AF_CAV(Cavitometer_position,ProbeType,PreAmpType) 
%%% This function now only works for Probe 2 and Probe3_shunt, as Probe 3_Attenuator (and Probe 4) new calibration doesn't need AF
AF_Probe2_PAmp_A  = [0.1745	0.1755	0.1761	0.177	0.1779	0.1785	0.1795	0.1801	0.1808	0.1817	0.1824	0.1831	0.1837	0.1844	0.1851	0.1857	0.1864	0.1871	0.1878	0.1885	0.1888	0.1895	0.1902	0.1906	0.1913	0.1917	0.1924	0.1928	0.1935	0.1939	0.1943	0.1947	0.1954	0.1958	0.1962	0.1966	0.197	0.1973	0.1977	0.1981	0.1985	0.1985	0.1989	0.1993	0.1997	0.1997	0.2001	0.2001	0.2005	0.2009	0.2009	0.2013	0.2013	0.2013	0.2017	0.2017	0.2017	0.2022	0.2022	0.2022	0.2022	0.2022	0.2026	0.2026	0.2026	0.2026	0.2026	0.2026	0.2026	0.2026	0.2026];
AF_Probe3_PAmp_A  = [0.1746	0.1755	0.1761	0.177	0.178	0.1786	0.1795	0.1802	0.1808	0.1818	0.1824	0.1831	0.1837	0.1844	0.185	0.1857	0.1864	0.1871	0.1878	0.1885	0.1888	0.1895	0.1902	0.1906	0.1913	0.1916	0.1924	0.1927	0.1935	0.1938	0.1943	0.1946	0.1954	0.1958	0.1961	0.1965	0.1969	0.1973	0.1977	0.198	0.1984	0.1984	0.1988	0.1992	0.1996	0.1996	0.2     0.2	    0.2004	0.2008	0.2008	0.2012	0.2012	0.2012	0.2016	0.2016	0.2016	0.202	0.2021	0.2021	0.2021	0.2021	0.2025	0.2025	0.2025	0.2025	0.2025	0.2025	0.2025	0.2025	0.2025];
AF_Probe2_PAmp_B  = [0.1664	0.167	0.1675	0.1681	0.1686	0.1692	0.1698	0.1703	0.1709	0.1715	0.1721	0.1727	0.1733	0.1736	0.1742	0.1748	0.1754	0.176	0.1763	0.1769	0.1775	0.1778	0.1785	0.1791	0.1794	0.1801	0.1804	0.181	0.1813	0.182	0.1823	0.183	0.1833	0.1837	0.1843	0.1847	0.185	0.1854	0.186	0.1864	0.1867	0.1871	0.1874	0.1878	0.1881	0.1885	0.1888	0.1888	0.1892	0.1895	0.1899	0.1899	0.1903	0.1903	0.1906	0.1906	0.191	0.191	0.191	0.1913	0.1913	0.1913	0.1913	0.1913	0.1913	0.1913	0.1913	0.1913	0.1913	0.1913	0.191];
AF_Probe3_PAmp_B  = [0.1665	0.167	0.1676	0.1681	0.1687	0.1692	0.1698	0.1704	0.1709	0.1715	0.1721	0.1727	0.1733	0.1736	0.1741	0.1747	0.1753	0.176	0.1763	0.1769	0.1775	0.1778	0.1784	0.1791	0.1794	0.18	0.1803	0.181	0.1813	0.1819	0.1823	0.1829	0.1833	0.1836	0.1843	0.1846	0.1849	0.1853	0.186	0.1863	0.1867	0.187	0.1873	0.1877	0.188	0.1884	0.1887	0.1887	0.1891	0.1895	0.1898	0.1898	0.1902	0.1902	0.1905	0.1905	0.1909	0.1909	0.1909	0.1913	0.1913	0.1913	0.1912	0.1912	0.1913	0.1913	0.1913	0.1913	0.1913	0.1913	0.1909];
switch Cavitometer_position
    case 'Side'
    switch ProbeType  
        case 'Probe2'
            switch PreAmpType
                case 'A'
                    AF_Original = AF_Probe2_PAmp_A;
                    AF_1MHz = 0.2152;   % AF for 1 MHz (for Probe A either Probe 2 or 3)
                case 'B'
                    AF_Original = AF_Probe2_PAmp_B;       
                    AF_1MHz = 0.1963;   % AF for 1 MHz (for Probe B either Probe 2 or 3)
            end
        case 'Probe3_Shunt'
            switch PreAmpType
                case 'A'
                    AF_Original = AF_Probe3_PAmp_A;
                    AF_1MHz = 0.2152;   % AF for 1 MHz (for Probe A either Probe 2 or 3)
               case 'B'
                    AF_Original = AF_Probe3_PAmp_B;       
                    AF_1MHz = 0.1963;   % AF for 1 MHz (for Probe B either Probe 2 or 3)
            end
    end  
    case 'Under'
    switch ProbeType
        case 'Probe2'
            switch PreAmpType
                case 'A'
                    AF_Original = AF_Probe2_PAmp_A;
                    AF_1MHz = 0.2152;   % AF for 1 MHz (for Probe A either Probe 2 or 3)
                case 'B'
                    AF_Original = AF_Probe2_PAmp_B;       
                    AF_1MHz = 0.1963;   % AF for 1 MHz (for Probe B either Probe 2 or 3)
            end
        case 'Probe3_Shunt'
            switch PreAmpType
                case 'A'
                    AF_Original = AF_Probe3_PAmp_A;
                    AF_1MHz = 0.2152;   % AF for 1 MHz (for Probe A either Probe 2 or 3)
                case 'B'
                    AF_Original = AF_Probe3_PAmp_B;       
                    AF_1MHz = 0.1963;   % AF for 1 MHz (for Probe B either Probe 2 or 3)
            end
    end
end
end
%% Function to create the dialog boxes for input values 
function [settings, button] = settingsdlg(varargin)
%%% This function was written by: 
% Name   : Rody P.S. Oldenhuis
% E-mail : oldenhuis@gmail.com
% Licence: 2-clause BSD (See Licence.txt)
% If you find this work useful, please consider a donation:
% https://www.paypal.me/RodyO/3.5
%% Initialize

    % errortraps
    narg = nargin;
    if verLessThan('MATLAB', '8.6')
        error(nargchk(1, inf, narg, 'struct')); %#ok<NCHKN>
    else
        narginchk(1, inf);
    end

    % parse input (+errortrap)
    have_settings = 0;
    if isstruct(varargin{1})
        settings = varargin{1}; have_settings = 1; end
    if (narg == 1)
        if isstruct(varargin{1})
            parameters = fieldnames(settings);
            values = cellfun(@(x)settings.(x), parameters, 'UniformOutput', false);
        else
            error('settingsdlg:incorrect_input',...
                'When pasing a single argument, that argument must be a structure.')
        end
    else
        parameters = varargin(1+have_settings : 2 : end);
        values     = varargin(2+have_settings : 2 : end);
    end

    % Initialize data
    button = [];
    fields = cell(numel(parameters),1);
    tags   = fields;

    % Fill [settings] with default values & collect data
    for ii = 1:numel(parameters)

        % Extract fields & tags
        if iscell(parameters{ii})
            tags{ii}   = parameters{ii}{1};
            fields{ii} = parameters{ii}{2};
        else
            % More errortraps
            if ~ischar(parameters{ii})
                error('settingsdlg:nonstring_parameter',...
                'Arguments should be given as [''parameter'', value,...] pairs.')
            end
            tags{ii}   = parameters{ii};
            fields{ii} = parameters{ii};
        end

        % More errortraps
        if ~ischar(fields{ii})
            error('settingsdlg:fieldname_not_char',...
                'Fieldname should be a string.')
        end
        if ~ischar(tags{ii})
            error('settingsdlg:tag_not_char',...
                'Display name should be a string.')
        end

        % NOTE: 'Separator' is now in 'fields' even though
        % it will not be used as a fieldname

        % Make sure all fieldnames are properly formatted
        % (alternating capitals, no whitespace)
        if ~strcmpi(fields{ii}, {'Separator';'Title';'Description'})
            whitespace = isspace(fields{ii});
            capitalize = circshift(whitespace,[0,1]);
            fields{ii}(capitalize) = upper(fields{ii}(capitalize));
            fields{ii} = fields{ii}(~whitespace);
            % insert associated value in output
            if iscell(values{ii})
                settings.(fields{ii}) = values{ii}{1};
            elseif (length(values{ii}) > 1)
                settings.(fields{ii}) = values{ii}(1);
            else
                settings.(fields{ii}) = values{ii};
            end
        end
    end

    % Avoid (some) confusion
    clear parameters

    % Use default colorscheme from the OS
    bgcolor = get(0, 'defaultUicontrolBackgroundColor');
    % Default fontsize
    fontsize = get(0, 'defaultuicontrolfontsize');
    % Edit-bgcolor is platform-dependent.
    % MS/Windows: white.
    % UNIX: same as figure bgcolor
%     if ispc, edit_bgcolor = 'White';
%     else     edit_bgcolor = bgcolor;
%     end

% TODO: not really applicable since defaultUicontrolBackgroundColor
% doesn't really seem to work on Unix...
edit_bgcolor = 'White';

    % Get basic window properties
    title         = getValue('Adjust settings', 'Title');
    description   = getValue( [], 'Description');
    total_width   = getValue(325, 'WindowWidth');
    control_width = getValue(100, 'ControlWidth');

    % Window positioning:
    % Put the window in the center of the screen by default.
    % This will usually work fine, except on some  multi-monitor setups.
    scz  = get(0, 'ScreenSize');
    scxy = round(scz(3:4)/2-control_width/2);
    scx  = min(scz(3),max(1,scxy(1)));
    scy  = min(scz(4),max(1,scxy(2)));

    % String to pass on to movegui
    window_position = getValue('center', 'WindowPosition');


    % Calculate best height for all uicontrol()
    control_height = max(18, (fontsize+6));

    % Calculate figure height (will be adjusted later according to description)
    total_height = numel(fields)*1.25*control_height + ... % to fit all controls
                     1.5*control_height + 20; % to fit "OK" and "Cancel" buttons

    % Total number of separators
    num_separators = nnz(strcmpi(fields,'Separator'));

    % Draw figure in background
    fighandle = figure(...
         'integerhandle'   , 'off',...         % use non-integers for the handle (prevents accidental plots from going to the dialog)
         'Handlevisibility', 'off',...         % only visible from within this function
         'position'        , [scx, scy, total_width, total_height],...% figure position
         'visible'         , 'off',...         % hide the dialog while it is being constructed
         'backingstore'    , 'off',...         % DON'T save a copy in the background
         'resize'          , 'off', ...        % but just keep it resizable
         'renderer'        , 'zbuffer', ...    % best choice for speed vs. compatibility
         'WindowStyle'     ,'modal',...        % window is modal
         'units'           , 'pixels',...      % better for drawing
         'DockControls'    , 'off',...         % force it to be non-dockable
         'name'            , title,...         % dialog title
         'menubar'         ,'none', ...        % no menubar of course
         'toolbar'         ,'none', ...        % no toolbar
         'NumberTitle'     , 'off',...         % "Figure 1.4728...:" just looks corny
         'color'           , bgcolor);         % use default colorscheme

    %% Draw all required uicontrols(), and unhide window

    % Define X-offsets (different when separators are used)
    separator_offset_X = 2;
    if num_separators > 0
        text_offset_X = 20;
        text_width = (total_width-control_width-text_offset_X);
    else
        text_offset_X = separator_offset_X;
        text_width = (total_width-control_width);
    end

    % Handle description
    description_offset = 0;
    if ~isempty(description)

        % create textfield (negligible height initially)
        description_panel = uicontrol(...
            'parent'  , fighandle,...
            'style'   , 'text',...
            'Horizontalalignment', 'left',...
            'position', [separator_offset_X,...
                         total_height,total_width,1]);

        % wrap the description
        description = textwrap(description_panel, {description});

        % adjust the height of the figure
        textheight = size(description,1)*(fontsize+6);
        description_offset = textheight + 20;
        total_height = total_height + description_offset;
        set(fighandle,...
            'position', [scx, scy, total_width, total_height])

        % adjust the position of the textfield and insert the description
        set(description_panel, ...
            'string'  , description,...
            'position', [separator_offset_X, total_height-textheight, ...
                         total_width, textheight]);
    end

    % Define Y-offsets (different when descriptions are used)
    control_offset_Y = total_height-control_height-description_offset;

    % initialize loop
    controls = zeros(numel(tags)-num_separators,1);
    ii = 1;             sep_ind = 1;
    enable = 'on';      separators = zeros(num_separators,1);

    % loop through the controls
    if numel(tags) > 0
        while true

            % Should we draw a separator?
            if strcmpi(tags{ii}, 'Separator')

                % Print separator
                uicontrol(...
                    'style'   , 'text',...
                    'parent'  , fighandle,...
                    'string'  , values{ii},...
                    'horizontalalignment', 'left',...
                    'fontweight', 'bold',...
                    'position', [separator_offset_X,control_offset_Y-4, ...
                    total_width, control_height]);

                % remove separator, but save its position
                fields(ii) = [];
                tags(ii)   = [];  separators(sep_ind) = ii;
                values(ii) = [];  sep_ind = sep_ind + 1;

                % reset enable (when neccessary)
                if strcmpi(enable, 'off')
                    enable = 'on'; end

                % NOTE: DON'T increase loop index

            % ... or a setting?
            else

                % logicals: use checkbox
                if islogical(values{ii})

                    % First draw control
                    controls(ii) = uicontrol(...
                        'style'   , 'checkbox',...
                        'parent'  , fighandle,...
                        'enable'  , enable,...
                        'string'  , tags{ii},...
                        'value'   , values{ii}(1),...
                        'position', [text_offset_X,control_offset_Y-4, ...
                        total_width, control_height]);

                    % Should everything below here be OFF?
                    if (length(values{ii})>1)
                        % turn next controls off when asked for
                        if values{ii}(2)
                            enable = 'off'; end
                        % Turn on callback function
                        set(controls(ii),...
                            'Callback', @(varargin) EnableDisable(ii,varargin{:}));
                    end

                % doubles      : use edit box
                % cells        : use popup
                % cell-of-cells: use table
                else
                    % First print parameter
                    uicontrol(...
                        'style'   , 'text',...
                        'parent'  , fighandle,...
                        'string'  , [tags{ii}, ':'],...
                        'horizontalalignment', 'left',...
                        'position', [text_offset_X,control_offset_Y-4, ...
                        text_width, control_height]);

                    % Popup, edit box or table?
                    style = 'edit';
                    draw_table = false;
                    if iscell(values{ii})
                        style = 'popup';
                        if all(cellfun('isclass', values{ii}, 'cell'))
                            draw_table = true; end
                    end

                    % Draw appropriate control
                    if ~draw_table
                        controls(ii) = uicontrol(...
                            'enable'  , enable,...
                            'style'   , style,...
                            'Background', edit_bgcolor,...
                            'parent'  , fighandle,...
                            'string'  , values{ii},...
                            'position', [text_width,control_offset_Y,...
                            control_width, control_height]);
                    else
                        % TODO
                        % ...table? ...radio buttons? How to do this?
                        warning(...
                            'settingsdlg:not_yet_implemented',...
                            'Treatment of cells is not yet implemented.');

                    end
                end

                % increase loop index
                ii = ii + 1;
            end

            % end loop?
            if ii > numel(tags)
                break, end

            % Decrease offset
            control_offset_Y = control_offset_Y - 1.25*control_height;
        end
    end

    % Draw cancel button
    uicontrol(...
        'style'   , 'pushbutton',...
        'parent'  , fighandle,...
        'string'  , 'Cancel',...
        'position', [separator_offset_X,2, total_width/2.5,control_height*1.5],...
        'Callback', @Cancel)

    % Draw OK button
    uicontrol(...
        'style'   , 'pushbutton',...
        'parent'  , fighandle,...
        'string'  , 'OK',...
        'position', [total_width*(1-1/2.5)-separator_offset_X,2, ...
                     total_width/2.5,control_height*1.5],...
        'Callback', @OK)

    % move to center of screen and make visible
    movegui(fighandle, window_position);
    set(fighandle, 'Visible', 'on');

    % WAIT until OK/Cancel is pressed
    uiwait(fighandle);



    %% Helper funcitons

    % Get a value from the values array:
    % - if it does not exist, return the default value
    % - if it exists, assign it and delete the appropriate entries from the
    %   data arrays
    function val = getValue(default, tag)
        index = strcmpi(fields, tag);
        if any(index)
            val = values{index};
            values(index) = [];
            fields(index) = [];
            tags(index)   = [];
        else
            val = default;
        end
    end

    %% callback functions

    % Enable/disable controls associated with (some) checkboxes
    function EnableDisable(which, varargin) %#ok<VANUS>

        % find proper range of controls to switch
        if (num_separators > 1)
             range = (which+1):(separators(separators > which)-1);
        else range = (which+1):numel(controls);
        end

        % enable/disable these controls
        if strcmpi(get(controls(range(1)), 'enable'), 'off')
            set(controls(range), 'enable', 'on')
        else
            set(controls(range), 'enable', 'off')
        end
    end

    % OK button:
    % - update fields in [settings]
    % - assign [button] output argument ('ok')
    % - kill window
    function OK(varargin) %#ok<VANUS>

        % button pressed
        button = 'OK';

        % fill settings
        for i = 1:numel(controls)

            % extract current control's string, value & type
            str   = get(controls(i), 'string');
            val   = get(controls(i), 'value');
            style = get(controls(i), 'style');

            % popups/edits
            if ~strcmpi(style, 'checkbox')
                % extract correct string (popups only)
                if strcmpi(style, 'popupmenu'), str = str{val}; end
                % try to convert string to double
                val = str2double(str);
                % insert this double in [settings]. If it was not a
                % double, insert string instead
                if ~isnan(val), settings.(fields{i}) = val;
                else            settings.(fields{i}) = str;
                end

            % checkboxes
            else
                % we can insert value immediately
                settings.(fields{i}) = val;
            end
        end

        %  kill window
        delete(fighandle);
    end

    % Cancel button:
    % - assign [button] output argument ('cancel')
    % - delete figure (so: return default settings)
    function Cancel(varargin) %#ok<VANUS>
        button = 'cancel';
        delete(fighandle);
    end

end

%% Variables
% S:   Sensitivity vector (contains the interpolation)
% tV:  t from CVS file (in s)
% fV:  freq (from fs or tV)
% Vt1: V matrix (Voltage) from CSV (in V)
% Vt2: new V matrix (after noise subtraction)
% Vf1: fft(Vt2) -->   Fast Fourier Transform of Vt1
% Vf2: Single-sided form of Vf1 (Vf1 is double-sided)
% P1:  real(Vf2)/S                                                                                  
% P2:  Double-sided form of P1 (P1 is single-sided)
% Pt:  P in time domain = ifft(P2) --> Inverse Fast Fourier Transform of P2
% tP:  time vector of P (in s)
% P4:  Double-sided form of abs(Vf2)/S --> for averaging the pressure for all waveforms (for averaging we use positive values) 
