%
%   convert_spl_to_psd.m  ver 1.0 December 31, 2013
%
function[fc,psd,applied_pressure_psd,tstring,fig_num,iu]=...
                                      convert_spl_to_psd(THM,delta,fig_num)
%
disp(' ');
disp(' Select units:   1=English  2=metric ');
iu=input(' ');
%
if(iu==1)
    reference = 2.9e-09;
else
    reference = 20.e-06;
end
%
fc=THM(:,1);
spl=THM(:,2);
%
rms=0.;
%   
num=length(fc);
psd=zeros(num,1);
%
%
for i=1:num    
%   
    if( spl(i) >= 1.0e-50)
%       
        pressure_rms=reference*(10.^(spl(i)/20.) );
%
        df=fc(i)*delta;
%
        if( df > 0. )   
            psd(i)=(pressure_rms^2.)/df;
            rms=rms+psd(i)*df;
        end
    end
end
%
applied_pressure_psd=[fc psd];
%
rms=sqrt(rms)
disp(' ');
disp(' The applied pressure PSD is: applied_pressure_psd');
disp(' ');
%
if(iu==1)
    disp(' Dimensions are: freq(Hz) & PSD(psi^2/Hz) ');
    ylab='Pressure (psi^2/Hz)';
    tstring=sprintf('Power Spectral Density  %8.4g psi rms overall',rms);
else
    disp(' Dimensions are: freq(Hz) & PSD(Pascal^2/Hz) ');
    ylab='Pressure (Pascal^2/Hz)'; 
    tstring=sprintf('Power Spectral Density  %8.4g Pa rms overall',rms);    
end
%
figure(fig_num);
fig_num=fig_num+1;
plot(fc,psd);
xlabel('Frequency (Hz)');
ylabel(ylab);
title(tstring);
grid on;
set(gca,'MinorGridLineStyle',':','GridLineStyle',':','XScale','log','YScale','log')
%
