function [cf_lin,A_up_dn_t,i_itr]=fit_lin_lor(pwr,h_e,fr_e,par_c,h0,h1_int,fr_wb,pw_b,pw_rat,fr_w_d,cf_pw_dev)
cf_lin=[1,1]-1e11;
A_up_dn_t=-1e11;
i_itr=-1;
fr_w=fr_wb(1);
fr_b=fr_wb(2);

fd_w=fr_w_d(1);
fd_b=fr_w_d(2);

h1=h1_int(1);
h2=h1_int(2);
F0=par_c(1);
DF=par_c(2);
k=par_c(3);
alf=par_c(4);
F1=F0+DF*(k*(h1-h0)^alf/(k*(h1-h0)^alf+1));
F2=F0+DF*(k*(h2-h0)^alf/(k*(h2-h0)^alf+1));
f_h1=find(h_e>=h1_int(1));
f_h_11=f_h1(1);
pwr_h1=pwr(:,f_h_11);
pwr_h1_max=max(pwr_h1);
f_h2=find(h_e>=h1_int(2));
f_h_21=f_h2(1);
pwr_h2=pwr(:,f_h_21);

pwr_h1_max=max(pwr_h1);
pwr_h2_max=max(pwr_h2);
key_bndr_ok=0;
if pwr_h1_max>pw_b && pwr_h2_max>pw_b
    key_bndr_ok=1;
elseif pwr_h1_max>pw_b
    
elseif pwr_h2_max>pw_b
else
end
if key_bndr_ok>0
L_fr=length(fr_e);
pwr_h1_c=pwr_h1(2:L_fr-1);
pwr_h1_r=pwr_h1(3:L_fr);
pwr_h1_l=pwr_h1(1:L_fr-2);
f_loc_max_h1=find(pwr_h1_c>pwr_h1_l & pwr_h1_c>pwr_h1_r)+1;
L_loc_max_h1=length(f_loc_max_h1);
if L_loc_max_h1>0
    by_w_ar=zeros(L_loc_max_h1,2);
    for i_lmax=1:L_loc_max_h1
        i_loc_max_h1=f_loc_max_h1(i_lmax);
        pw_lmax=pwr_h1(i_loc_max_h1);
        i_1=1;
        i_2=1;
        key_by_1=1;
        
        while key_by_1
            i1_by=i_loc_max_h1-i_1;
         
            by_1_w=i_loc_max_h1-i1_by+1;
            pw_by_1=pwr_h1(i1_by:i_loc_max_h1);
            pw_by_1_min=min(pw_by_1);
            [pw_by_1_max,i_pw_by_1_max]=max(pw_by_1);
            
            if pw_by_1_min>=pw_by_1_max*pw_rat &&  i_pw_by_1_max==i_1+1 && i1_by>1
                i_1=i_1+1;
                
                
            else
                key_by_1=0;
               
            end
        end
        key_by_2=1;
        while key_by_2
            i2_by=i_loc_max_h1+i_2;
         
            by_2_w=i2_by-i_loc_max_h1;
            pw_by_2=pwr_h1(i_loc_max_h1+1:i2_by);
            pw_by_2_min=min(pw_by_2);
            pw_by_2_max=max(pw_by_2);
            
            if pw_by_2_min>=pw_lmax*pw_rat &&  pw_by_2_max < pw_lmax  && i2_by<L_fr
                i_2=i_2+1;
                
                
            else
                key_by_2=0;
            end
        end
        pw_by_0=[pw_by_1;pw_by_2];
        L_pw_by_0=length(pw_by_0);
        pw_by=pw_by_0(2:L_pw_by_0-1);
    pw_by_av=mean(pw_by);
    L_pw_by=length(pw_by);
    if L_pw_by>1
        by_w_ar(i_lmax,:)=[L_pw_by,pw_by_av];
    end
    end
    
    [by_w_max,ii_by_w_max]=max(by_w_ar(:,2));
    i_by_w_max=f_loc_max_h1(ii_by_w_max)+i_2-1;
    cf_0=fr_e(i_by_w_max);
else
    cf_0=F1;
end
pwr_h2_c=pwr_h2(2:L_fr-1);
pwr_h2_r=pwr_h2(3:L_fr);
pwr_h2_l=pwr_h2(1:L_fr-2);
f_loc_max_h2=find(pwr_h2_c>pwr_h2_l & pwr_h2_c>pwr_h2_r)+1;
L_loc_max_h2=length(f_loc_max_h2);
if L_loc_max_h2>0
    by_w_ar2=zeros(L_loc_max_h2,2);
    for i_lmax=1:L_loc_max_h2
        i_loc_max_h2=f_loc_max_h2(i_lmax);
        pw_lmax=pwr_h2(i_loc_max_h2);
        i_1=1;
        i_2=1;
        key_by_1=1;
        
        while key_by_1
            i1_by=i_loc_max_h2-i_1;
         
            by_1_w=i_loc_max_h2-i1_by+1;
            pw_by_1=pwr_h2(i1_by:i_loc_max_h2);
            pw_by_1_min=min(pw_by_1);
            [pw_by_1_max,i_pw_by_1_max]=max(pw_by_1);
            
            if pw_by_1_min>=pw_by_1_max*pw_rat &&  i_pw_by_1_max==i_1+1 && i1_by>1
                i_1=i_1+1;
                
                
            else
                key_by_1=0;
               
            end
        end
        key_by_2=1;
        while key_by_2
            i2_by=i_loc_max_h2+i_2;
         
            by_2_w=i2_by-i_loc_max_h2;
            pw_by_2=pwr_h2(i_loc_max_h2+1:i2_by);
            pw_by_2_min=min(pw_by_2);
            pw_by_2_max=max(pw_by_2);
            
            if pw_by_2_min>=pw_lmax*pw_rat &&  pw_by_2_max < pw_lmax  && i2_by<L_fr
                i_2=i_2+1;
                
                
            else
                key_by_2=0;
            end
        end
        pw_by_0=[pw_by_1;pw_by_2];
        L_pw_by_0=length(pw_by_0);
        pw_by=pw_by_0(2:L_pw_by_0-1);
    pw_by_av=mean(pw_by);
    L_pw_by=length(pw_by);
    if L_pw_by>1
        by_w_ar2(i_lmax,:)=[L_pw_by,pw_by_av];
    end
    end
    
    [by_w_max,ii_by_w_max]=max(by_w_ar2(:,2));
    i_by_w_max=f_loc_max_h2(ii_by_w_max)+i_2-1;
    cf2_0=fr_e(i_by_w_max);
else
    cf2_0=F2;
end
    cf_1=(cf2_0-cf_0)/(h2-h1);
    if cf_1<=0
        cf_1=-cf_1;
    end
d_cf_0=cf_0/3;
d_cf_1=cf_1/3;
d_cf_0_min=cf_0*1e-4;
d_cf_1_min=cf_1*1e-4;
num_itr=1000;
key_appr=1;
i_itr=0;
while key_appr
cf_0u=cf_0+d_cf_0;
cf_0d=cf_0-d_cf_0;
cf_1u=cf_1+d_cf_1;
cf_1d=cf_1-d_cf_1;
f_h=find(h_e>=h1 & h_e<=h2);
L_f_h=length(f_h);
pwr_h=pwr(:,f_h);
h_h=h_e(f_h);
fr_h=cf_0+cf_1*(h_h-h1);
fr_h_uu=cf_0u+cf_1u*(h_h-h1);
fr_h_ud=cf_0u+cf_1d*(h_h-h1);
fr_h_du=cf_0d+cf_1u*(h_h-h1);
fr_h_dd=cf_0d+cf_1d*(h_h-h1);
fr1_h=fr_h-fr_w;
fr2_h=fr_h+fr_b;
fd1_h=fr_h-fd_w;
fd2_h=fr_h+fd_b;
% uu
fr1_h_uu=fr_h_uu-fr_w;
fr2_h_uu=fr_h_uu+fr_b;
fd1_h_uu=fr_h_uu-fd_w;
fd2_h_uu=fr_h_uu+fd_b;
% ud
fr1_h_ud=fr_h_ud-fr_w;
fr2_h_ud=fr_h_ud+fr_b;
fd1_h_ud=fr_h_ud-fd_w;
fd2_h_ud=fr_h_ud+fd_b;
% du
fr1_h_du=fr_h_du-fr_w;
fr2_h_du=fr_h_du+fr_b;
fd1_h_du=fr_h_du-fd_w;
fd2_h_du=fr_h_du+fd_b;
%  dd
fr1_h_dd=fr_h_dd-fr_w;
fr2_h_dd=fr_h_dd+fr_b;
fd1_h_dd=fr_h_dd-fd_w;
fd2_h_dd=fr_h_dd+fd_b;
pwc=zeros(100000,10)-1e11;
dpc=zeros(L_f_h,10,2)-1e11;

A_up_dn=zeros(1,5);
i_d_2=0;
i_u_2=0;
i_d_2_uu=0;
i_u_2_uu=0;
i_d_2_ud=0;
i_u_2_ud=0;
i_d_2_du=0;
i_u_2_du=0;
i_d_2_dd=0;
i_u_2_dd=0;



for i_f_h=1:L_f_h
    fr_hd=fr1_h(i_f_h);
    fr_hc=fr_h(i_f_h);
    fr_hu=fr2_h(i_f_h);
   f_fr_d=find(fr_e>=fr_hd & fr_e<fr_hc);  
   L_fr_d=length(f_fr_d);
   f_fr_u=find(fr_e<=fr_hu & fr_e>fr_hc);  
   L_fr_u=length(f_fr_u);
   i_d_1=i_d_2+1;
   i_d_2=i_d_1+L_fr_d-1;
   i_u_1=i_u_2+1;
   i_u_2=i_u_1+L_fr_u-1;
   pwc(i_d_1:i_d_2,1)=pwr_h(f_fr_d,i_f_h);
   pwc(i_u_1:i_u_2,2)=pwr_h(f_fr_u,i_f_h);
   % dP/dF 
    fd_hd=fd1_h(i_f_h);
    fd_hu=fd2_h(i_f_h);
   f_fd_d=find(fr_e>=fd_hd & fr_e<fr_hc);  
   L_fd_d=length(f_fd_d);
   f_fd_u=find(fr_e<=fd_hu & fr_e>fr_hc);  
   L_fd_u=length(f_fd_u);
   if L_fd_u>2 && L_fd_d>2
       fr_fd_d=fr_e(f_fd_d);
       fr_fd_u=fr_e(f_fd_u);
       pwr_h_u=pwr_h(f_fd_u,i_f_h);
       pwr_h_d=pwr_h(f_fd_d,i_f_h);
       P_dpu=polyfit(fr_fd_u',pwr_h_u,1);
       P_dpd=polyfit(fr_fd_d',pwr_h_d,1);
       dpc(i_f_h,1,:)=P_dpd;
       dpc(i_f_h,2,:)=P_dpu;
   end

   %uu
   fr_hd_uu=fr1_h_uu(i_f_h);
    fr_hc_uu=fr_h_uu(i_f_h);
    fr_hu_uu=fr2_h_uu(i_f_h);
   f_fr_d_uu=find(fr_e>=fr_hd_uu & fr_e<fr_hc_uu);  
   L_fr_d_uu=length(f_fr_d_uu);
   f_fr_u_uu=find(fr_e<=fr_hu_uu & fr_e>fr_hc_uu);  
   L_fr_u_uu=length(f_fr_u_uu);
   i_d_1_uu=i_d_2_uu+1;
   i_d_2_uu=i_d_1_uu+L_fr_d_uu-1;
   i_u_1_uu=i_u_2_uu+1;
   i_u_2_uu=i_u_1_uu+L_fr_u_uu-1;
   pwc(i_d_1_uu:i_d_2_uu,3)=pwr_h(f_fr_d_uu,i_f_h);
   pwc(i_u_1_uu:i_u_2_uu,4)=pwr_h(f_fr_u_uu,i_f_h);
   % dP/dF 
    fd_hd_uu=fd1_h_uu(i_f_h);
    fd_hu_uu=fd2_h_uu(i_f_h);
   f_fd_d_uu=find(fr_e>=fd_hd_uu & fr_e<fr_hc_uu);  
   L_fd_d_uu=length(f_fd_d_uu);
   f_fd_u_uu=find(fr_e<=fd_hu_uu & fr_e>fr_hc_uu);  
   L_fd_u_uu=length(f_fd_u_uu);
   if L_fd_u_uu>2 && L_fd_d_uu>2
       fr_fd_d_uu=fr_e(f_fd_d_uu);
       fr_fd_u_uu=fr_e(f_fd_u_uu);
       pwr_h_u_uu=pwr_h(f_fd_u_uu,i_f_h);
       pwr_h_d_uu=pwr_h(f_fd_d_uu,i_f_h);
       P_dpu_uu=polyfit(fr_fd_u_uu',pwr_h_u_uu,1);
       P_dpd_uu=polyfit(fr_fd_d_uu',pwr_h_d_uu,1);
       dpc(i_f_h,3,:)=P_dpd_uu;
       dpc(i_f_h,4,:)=P_dpu_uu;
   end
   %ud
   fr_hd_ud=fr1_h_ud(i_f_h);
    fr_hc_ud=fr_h_ud(i_f_h);
    fr_hu_ud=fr2_h_ud(i_f_h);
   f_fr_d_ud=find(fr_e>=fr_hd_ud & fr_e<fr_hc_ud);  
   L_fr_d_ud=length(f_fr_d_ud);
   f_fr_u_ud=find(fr_e<=fr_hu_ud & fr_e>fr_hc_ud);  
   L_fr_u_ud=length(f_fr_u_ud);
   i_d_1_ud=i_d_2_ud+1;
   i_d_2_ud=i_d_1_ud+L_fr_d_ud-1;
   i_u_1_ud=i_u_2_ud+1;
   i_u_2_ud=i_u_1_ud+L_fr_u_ud-1;
   pwc(i_d_1_ud:i_d_2_ud,5)=pwr_h(f_fr_d_ud,i_f_h);
   pwc(i_u_1_ud:i_u_2_ud,6)=pwr_h(f_fr_u_ud,i_f_h);
    % dP/dF 
    fd_hd_ud=fd1_h_ud(i_f_h);
    fd_hu_ud=fd2_h_ud(i_f_h);
   f_fd_d_ud=find(fr_e>=fd_hd_ud & fr_e<fr_hc_ud);  
   L_fd_d_ud=length(f_fd_d_ud);
   f_fd_u_ud=find(fr_e<=fd_hu_ud & fr_e>fr_hc_ud);  
   L_fd_u_ud=length(f_fd_u_ud);
   if L_fd_u_ud>2 && L_fd_d_ud>2
       fr_fd_d_ud=fr_e(f_fd_d_ud);
       fr_fd_u_ud=fr_e(f_fd_u_ud);
       pwr_h_u_ud=pwr_h(f_fd_u_ud,i_f_h);
       pwr_h_d_ud=pwr_h(f_fd_d_ud,i_f_h);
       P_dpu_ud=polyfit(fr_fd_u_ud',pwr_h_u_ud,1);
       P_dpd_ud=polyfit(fr_fd_d_ud',pwr_h_d_ud,1);
       dpc(i_f_h,5,:)=P_dpd_ud;
       dpc(i_f_h,6,:)=P_dpu_ud;
   end
   %du
   fr_hd_du=fr1_h_du(i_f_h);
    fr_hc_du=fr_h_du(i_f_h);
    fr_hu_du=fr2_h_du(i_f_h);
   f_fr_d_du=find(fr_e>=fr_hd_du & fr_e<fr_hc_du);  
   L_fr_d_du=length(f_fr_d_du);
   f_fr_u_du=find(fr_e<=fr_hu_du & fr_e>fr_hc_du);  
   L_fr_u_du=length(f_fr_u_du);
   i_d_1_du=i_d_2_du+1;
   i_d_2_du=i_d_1_du+L_fr_d_du-1;
   i_u_1_du=i_u_2_du+1;
   i_u_2_du=i_u_1_du+L_fr_u_du-1;
   pwc(i_d_1_du:i_d_2_du,7)=pwr_h(f_fr_d_du,i_f_h);
   pwc(i_u_1_du:i_u_2_du,8)=pwr_h(f_fr_u_du,i_f_h);
    % dP/dF 
    fd_hd_du=fd1_h_du(i_f_h);
    fd_hu_du=fd2_h_du(i_f_h);
   f_fd_d_du=find(fr_e>=fd_hd_du & fr_e<fr_hc_du);  
   L_fd_d_du=length(f_fd_d_du);
   f_fd_u_du=find(fr_e<=fd_hu_du & fr_e>fr_hc_du);  
   L_fd_u_du=length(f_fd_u_du);
   if L_fd_u_du>2 && L_fd_d_du>2
       fr_fd_d_du=fr_e(f_fd_d_du);
       fr_fd_u_du=fr_e(f_fd_u_du);
       pwr_h_u_du=pwr_h(f_fd_u_du,i_f_h);
       pwr_h_d_du=pwr_h(f_fd_d_du,i_f_h);
       P_dpu_du=polyfit(fr_fd_u_du',pwr_h_u_du,1);
       P_dpd_du=polyfit(fr_fd_d_du',pwr_h_d_du,1);
       dpc(i_f_h,7,:)=P_dpd_du;
       dpc(i_f_h,8,:)=P_dpu_du;
   end
   %dd
   fr_hd_dd=fr1_h_dd(i_f_h);
    fr_hc_dd=fr_h_dd(i_f_h);
    fr_hu_dd=fr2_h_dd(i_f_h);
   f_fr_d_dd=find(fr_e>=fr_hd_dd & fr_e<fr_hc_dd);  
   L_fr_d_dd=length(f_fr_d_dd);
   f_fr_u_dd=find(fr_e<=fr_hu_dd & fr_e>fr_hc_dd);  
   L_fr_u_dd=length(f_fr_u_dd);
   i_d_1_dd=i_d_2_dd+1;
   i_d_2_dd=i_d_1_dd+L_fr_d_dd-1;
   i_u_1_dd=i_u_2_dd+1;
   i_u_2_dd=i_u_1_dd+L_fr_u_dd-1;
   pwc(i_d_1_dd:i_d_2_dd,9)=pwr_h(f_fr_d_dd,i_f_h);
   pwc(i_u_1_dd:i_u_2_dd,10)=pwr_h(f_fr_u_dd,i_f_h);
   % dP/dF 
    fd_hd_dd=fd1_h_dd(i_f_h);
    fd_hu_dd=fd2_h_dd(i_f_h);
   f_fd_d_dd=find(fr_e>=fd_hd_dd & fr_e<fr_hc_dd);  
   L_fd_d_dd=length(f_fd_d_dd);
   f_fd_u_dd=find(fr_e<=fd_hu_dd & fr_e>fr_hc_dd);  
   L_fd_u_dd=length(f_fd_u_dd);
   if L_fd_u_dd>2 && L_fd_d_dd>2
       fr_fd_d_dd=fr_e(f_fd_d_dd);
       fr_fd_u_dd=fr_e(f_fd_u_dd);
       pwr_h_u_dd=pwr_h(f_fd_u_dd,i_f_h);
       pwr_h_d_dd=pwr_h(f_fd_d_dd,i_f_h);
       P_dpu_dd=polyfit(fr_fd_u_dd',pwr_h_u_dd,1);
       P_dpd_dd=polyfit(fr_fd_d_dd',pwr_h_d_dd,1);
       dpc(i_f_h,9,:)=P_dpd_dd;
       dpc(i_f_h,10,:)=P_dpu_dd;
   end
end
f_pwc_d=find(pwc(:,1)>0);
L_pwc_d=length(f_pwc_d);
f_pwc_u=find(pwc(:,2)>0);
L_pwc_u=length(f_pwc_u);
pw_u_av=mean(pwc(1:L_pwc_u,2));
pw_d_av=mean(pwc(1:L_pwc_d,1));
A_up_dn(1)=pw_d_av-pw_u_av;
%uu
f_pwc_d_uu=find(pwc(:,3)>0);
L_pwc_d_uu=length(f_pwc_d_uu);
f_pwc_u_uu=find(pwc(:,4)>0);
L_pwc_u_uu=length(f_pwc_u_uu);
pw_u_av_uu=mean(pwc(1:L_pwc_u_uu,4));
pw_d_av_uu=mean(pwc(1:L_pwc_d_uu,3));
A_up_dn(2)=pw_d_av_uu-pw_u_av_uu;
%uu
f_pwc_d_ud=find(pwc(:,5)>0);
L_pwc_d_ud=length(f_pwc_d_ud);
f_pwc_u_ud=find(pwc(:,6)>0);
L_pwc_u_ud=length(f_pwc_u_ud);
pw_u_av_ud=mean(pwc(1:L_pwc_u_ud,6));
pw_d_av_ud=mean(pwc(1:L_pwc_d_ud,5));
A_up_dn(3)=pw_d_av_ud-pw_u_av_ud;
%du
f_pwc_d_du=find(pwc(:,7)>0);
L_pwc_d_du=length(f_pwc_d_du);
f_pwc_u_du=find(pwc(:,8)>0);
L_pwc_u_du=length(f_pwc_u_du);
pw_u_av_du=mean(pwc(1:L_pwc_u_du,8));
pw_d_av_du=mean(pwc(1:L_pwc_d_du,7));
A_up_dn(4)=pw_d_av_du-pw_u_av_du;
%dd
f_pwc_d_dd=find(pwc(:,9)>0);
L_pwc_d_dd=length(f_pwc_d_dd);
f_pwc_u_dd=find(pwc(:,10)>0);
L_pwc_u_dd=length(f_pwc_u_dd);
pw_u_av_dd=mean(pwc(1:L_pwc_u_dd,10));
pw_d_av_dd=mean(pwc(1:L_pwc_d_dd,9));
A_up_dn(5)=pw_d_av_dd-pw_u_av_dd;
f_up_dn_ok=find(isnan(A_up_dn)==0);
f_up_dn_nn=find(isnan(A_up_dn)>0);
L_up_dn_ok=length(f_up_dn_ok);
L_up_dn_nn=length(f_up_dn_nn);
    d_pwc_av_d=-mean(dpc(:,1:2:9,1));
    d_pwc_av_u=sqrt(mean(dpc(:,2:2:10,1).^2));
    d_pwc_av_rat=d_pwc_av_d./d_pwc_av_u;
    f_pwc_nn=find(d_pwc_av_u==0);
    f_pwc_nz=find(d_pwc_av_u>0);
    L_pwc_nz=length(f_pwc_nz);
    if L_pwc_nz>0
       d_pwc_av_rat_ok=d_pwc_av_rat(f_pwc_nz);
       d_pwc_av_rat_ok_av=sqrt(mean(d_pwc_av_rat_ok.^2));
    end
    L_pwc_nn=length(f_pwc_nn);
    if L_pwc_nn>0
        d_pwc_av_rat(f_pwc_nn)=-1e11;
    end
if L_up_dn_ok==0
    i_up_dn_max=0;
else
    
    A_up_dn_ok=A_up_dn;
    if L_up_dn_ok>0
    A_up_dn_ok2=A_up_dn_ok(f_up_dn_ok);
    A_up_dn_av=sqrt(mean(pwr_h1.^2));
    end
    if L_up_dn_nn>0
        A_up_dn_ok(f_up_dn_nn)=-1e11;
    
    end
    

end
if cf_pw_dev==1 && L_up_dn_ok>0
    Fn_up_dn=A_up_dn_ok;
else
    if cf_pw_dev==0 && L_pwc_nz>0
        Fn_up_dn=d_pwc_av_rat;
    elseif L_pwc_nz>0 && L_up_dn_ok>0
        Fn_up_dn=cf_pw_dev*A_up_dn_ok/A_up_dn_av+(1-cf_pw_dev)*d_pwc_av_rat/d_pwc_av_rat_ok_av;
    end
end

    [A_up_dn_max,i_up_dn_max]=max(Fn_up_dn);    


switch i_up_dn_max
    case 0
        d_cf_0=d_cf_0/3;
        d_cf_1=d_cf_1/3;
    case 1
        d_cf_0=d_cf_0/3;
        d_cf_1=d_cf_1/3;
    case 2
        cf_0=cf_0+d_cf_0;
        cf_1=cf_1+d_cf_1;
    case 3
        cf_0=cf_0+d_cf_0;
        cf_1=cf_1-d_cf_1;
    case 4
        cf_0=cf_0-d_cf_0;
        cf_1=cf_1+d_cf_1;
    case 5
        cf_0=cf_0-d_cf_0;
        cf_1=cf_1-d_cf_1;
end
if d_cf_0<=d_cf_0_min && d_cf_1<=d_cf_1_min || i_itr>num_itr
    key_appr=0;
else
    i_itr=i_itr+1;
end
end
A_up_dn_t=A_up_dn_max;
cf_lin=[cf_0,cf_1];
end
end
