%
%  acoustic_weighting.m   ver 1.0  October 19, 2012
%
function[pf,pspl,oaspl,scale_label]=acoustic_weighting(pf,pspl,imax,ref)
%
[fw,aw,bw,cw]=SPL_weights(); 
%
disp(' ');
disp(' Select Scale ');
disp(' 1=A-weight   2=B-weight   3=C-weight \n');
%
iscale=input(' '); 
%
if(iscale==1)
    ww=aw;
    scale_label=sprintf('A');
end
if(iscale==2)
    ww=bw;
    scale_label=sprintf('B');    
end
if(iscale==3)
    ww=cw;
    scale_label=sprintf('C');    
end
%
wmax=length(fw);
%
js=1;
%
k=1;
%
ms=0;
%
for i=1:imax
    for j=js:wmax
%        
        dff=abs(log(pf(i)/fw(j))/log(2));
%
        if( dff < 0.1 )
            ft(k)=pf(i);
            pt(k)=pspl(i)+ww(j);
%
            bs=ref*10^(pt(k)/20);
            ms=ms+bs^2;
%
            k=k+1;
            js=j;
            break;
        end
    end
end
%
ms=sqrt(ms);
%
oaspl = 20*log10(ms/ref);
%
clear pf;
clear pspl;
%
pf=ft;
pspl=pt;