function [fim_det,fim_trace,fim_cond,placement_result,fai_op,direction,del_sensor_tag,fai_left,tmse,mode_shape]=...
    efi_osp(num_mod,num_cand,num_targ,file_name,num_mod_exp)
%--------------------------------------------------------------------
% input num_mod= NUMber of MODes
% input num_cand= NUMber of CANDidate sensor nodes
% input num_targ= NUMber of TARGet sensor nodes\% input num_targ= NUMber of
% input num_mod_exp= NUMber of MODes EXPected to be calculated
% efi_osp(12,41,30,'beam2.xls',8);
%--------------------------------------------------------------------
clc;
format long;
load open_mat;
switch file_name
    case 'beam2.xls'
        modal_data=modal_data_beam2;
    case 'GNTVT.xls'
        modal_data=modal_data_gntvt;
    case 'wall_76_xz.xls'
        modal_data=modal_data_wall;
    otherwise
        errordlg('Data file does not exist.','Data Import Error')
end
fim_matrix=zeros(num_cand,num_mod);
%--------------------------------------------------------------------
% Construct the mode shape--fai
count=0;
direction=zeros(1,num_mod);
for i=1:2:2*num_mod
    for j=1:num_cand
        if abs(modal_data(j,i))<abs(modal_data(j,i+1))
           count=count+1;
        else
           count=count;
       end
    end
    if count>num_cand/3 
        fai_i(:,(i+1)/2)=modal_data(:,i+1);
        direction(1,(i+1)/2)=1;
    else
        fai_i(:,(i+1)/2)=modal_data(:,i);
        direction(1,(i+1)/2)=0;
    end
    count=0;
end
%--------------------------------------------------------------------
% Transfer Row No.2 (actually the end point sensor) to the last row of the matrix
if strcmp(file_name,'beam2.xls')
    fai_i_mid_exc=fai_i(2,:);
    fai_i_exc_mid=fai_i;
    fai_i_exc_mid(2,:)=[];
    fai_i_exc=cat(1,fai_i_exc_mid,fai_i_mid_exc);    
else
    fai_i_exc=fai_i;    
end
%--------------------------------------------------------------------
% Generate output mode shape
ms_output=fai_i_exc;
[fi_ms_x,fi_ms_y]=size(ms_output);
for i=1:fi_ms_y
    fai_max_ms=ms_output(:,i)';
    [max_ms_val,max_ms_pos]=max(abs(fai_max_ms));    
    fai_max_ms_norm=fai_max_ms./abs(max_ms_val);
    ms_output_norm(:,i)=fai_max_ms_norm;  
end
mode_shape=ms_output_norm;
fai_no_unit=fai_i_exc;
%--------------------------------------------------------------------
% Normalization
[fi_sz_x,fi_sz_y]=size(fai_no_unit);
for i=1:fi_sz_y
    fai_no_unit_max_c=fai_no_unit(:,i)';
    [max_disp_val,max_disp_pos]=max(abs(fai_no_unit_max_c));    
    fai_no_unit_max_c=fai_no_unit_max_c./abs(max_disp_val);
    fai_no_unit(:,i)=fai_no_unit_max_c;  
end
fai=fai_no_unit(:,1:num_mod_exp);
fai_i_exc_with_row1=cat(1,zeros(1,fi_sz_y),fai_no_unit);
[noi_fai_sz_x,noi_fai_sz_y]=size(fai);
%--------------------------------------------------------------------
% Noise Synthesization 
fai_op=fai;
fai_initial_a=fai;
[fai_ini_x_d,fai_ini_y_d]=size(fai_initial_a);
%--------------------------------------------------------------------
% To exclude the zeros rows(sensors with permanent zero displacement)
    sns_zro_flg=0;
    sns_zro_flg_y=0;
    for i=1:num_cand
        if (fai_initial_a(i,:)==0)
            sns_zro_flg_y=cat(2,sns_zro_flg_y,i);
        else
            sns_zro_flg=cat(2,sns_zro_flg,i);
        end
    end
%--------------------------------------------------------------------
    sns_zro_flg(:,1)=[];
    sns_zro_flg_y(:,1)=[];    
    [sns_zro_flg_szx,sns_zro_flg_szy]=size(sns_zro_flg);
%--------------------------------------------------------------------
    fai_initial_a_exclzro=zeros(sns_zro_flg_szy,num_mod_exp);
    fai_initial_a_full=zeros(sns_zro_flg_szy,num_mod);
%--------------------------------------------------------------------
    for i=1:sns_zro_flg_szy
        fai_initial_a_exclzro(i,:)=fai_initial_a(sns_zro_flg(1,i),:);
        fai_initial_a_full(i,:)=fai_no_unit(sns_zro_flg(1,i),:);
    end
%--------------------------------------------------------------------
    fai_initial_a=0;
    fai_initial_a=fai_initial_a_exclzro;
    [fai_ini_szx,fai_ini_szy]=size(fai_initial_a);
    num_cand_exc=fai_ini_szx;
    delta_num_cand=num_cand-num_cand_exc;
%--------------------------------------------------------------------
% Memory Allocation
    credit=ones(1,num_cand-delta_num_cand);
    fim_det=zeros(1,num_cand-num_targ+1-delta_num_cand);
    sum_mac_output=zeros(1,num_cand-num_targ+1-delta_num_cand);
    fim_trace=zeros(1,num_cand-num_targ+1-delta_num_cand);
    fim_cond=zeros(1,num_cand-num_targ+1-delta_num_cand);
%     fim_det_dirty=zeros(1,num_cand-num_targ+1-delta_num_cand);
    del_sensor_tag_mid=zeros(1,1);
%--------------------------------------------------------------------
% EFI Iteration
for m=0:(num_cand-num_targ-delta_num_cand)
     fai_new_spl_csi=zeros(fai_ini_x_d+1,fai_ini_y_d);
%--------------------------------------------------------------------
% EID and CREDIT computation for EFI method
     fai_mid=zeros(1,num_mod_exp);
     fai_new=zeros(1,num_mod_exp);
%--------------------------------------------------------------------
     fai_mid_full=zeros(1,num_mod);
     fai_new_full=zeros(1,num_mod);
%--------------------------------------------------------------------
     for n=1:num_cand_exc
         if (credit(n)==0)
             fai_mid=fai_mid;
             fai_mid_full=fai_mid_full;
          else
             fai_mid=cat(1,fai_mid,fai_initial_a(n,:));
             fai_mid_full=cat(1,fai_mid_full,fai_initial_a_full(n,:));
          end
     end
     fai_mid(1,:)=[];
     fai_mid_full(1,:)=[];
     fai_new=fai_mid;
     fai_new_full=fai_mid_full;
     if m==num_cand_exc-num_targ
         [fnew_x,fnew_y]=size(fai_new);
         fai_left=cat(1,zeros(1,fnew_y),fai_new);
     else
         fai_left=0;
     end
%--------------------------------------------------------------------
% To build the x coordinate for interpolation
[cre_x,cre_y]=size(credit);
x_spline1=0;
for cre_i=1:cre_y
    if (credit(cre_i)==1)
        x_spline1=cat(1,x_spline1,cre_i);
    else
        x_spline1=x_spline1;
    end
end
x_spline1(1,:)=[];
x_spline2=x_spline1;
x_spline=cat(1,1,x_spline2+1);
%--------------------------------------------------------------------
% FIM
      fim_matrix_new=fai_new'*fai_new;
%--------------------------------------------------------------------
% EID
     [v_new,d_new]=eig(fim_matrix_new); 
     k=size(d_new);
     for j=1:k
         d_new(j,j)=1/d_new(j,j);
     end
     eid_new=(fai_new*v_new).*(fai_new*v_new)*d_new*ones(num_mod_exp,1);
%--------------------------------------------------------------------
% Determinant,Trace and Condition Number
     fim_det(1,m+1)=det(fim_matrix_new);
     fim_det_dirty(1,m+1)=det(fim_matrix_new);
%--------------------------------------------------------------------
% CREDIT
[posit_r_new,posit_c_new]=min(abs(eid_new'));
[sz_fai_iaf_x,sz_fai_iaf_y]=size(fai_initial_a_full);
     for i=1:sz_fai_iaf_x
          if(fai_initial_a_full(i,:)==fai_new_full(posit_c_new,:))
              credit(1,i)=0;
              del_sensor_tag_mid=cat(2,del_sensor_tag_mid,i);
          else
              credit(1,i)=credit(1,i);
          end
     end
%--------------------------------------------------------------------
% Cubic Spline Interpolation
     [fai_new_sz_x,fai_new_sz_y]=size(fai_new);
     fai_new_spl_cat1=cat(1,zeros(1,fai_new_sz_y),fai_new);
     fai_new_spl_cat1;     
     for i=1:fai_new_sz_y
         fai_new_spl_csi(:,i)=spline(x_spline,fai_new_spl_cat1(:,i),1:1:fai_ini_x_d+1);
     end
     for i=1:fai_new_sz_y
         tmse_mid(m+1,i)=sum((fai_new_spl_csi(:,i)-fai_i_exc_with_row1(:,i)).^2)/(fai_ini_x_d+1);
     end     
     tmse=sum(tmse_mid');     
end
%--------------------------------------------------------------------
del_sensor_tag_mid(:,1)=[];
[del_stg_szx,del_stg_szy]=size(del_sensor_tag_mid);
del_sensor_tag=zeros(1,del_stg_szy);
for i=1:del_stg_szy
    del_sensor_tag(1,i)=sns_zro_flg(1,del_sensor_tag_mid(1,i));    
end
del_sensor_tag=cat(2,sns_zro_flg_y,del_sensor_tag);
[sz_del_stx,sz_del_sty]=size(del_sensor_tag);
del_sensor_tag(:,sz_del_sty)=[];
[sz_del_stx,sz_del_sty]=size(del_sensor_tag);
%--------------------------------------------------------------------
credit_final=ones(1,num_cand);
for i=1:num_cand
    for j=1:sz_del_sty
        if i==del_sensor_tag(1,j)
            credit_final(1,i)=0;            
        else
            credit_final;            
        end
    end   
end
placement_result=credit_final;
%--------------------------------------------------------------------
[dst_sz_x,dst_sz_y]=size(del_sensor_tag);
fim_det_fill=[];
for i=1:delta_num_cand
    fim_det_fill=cat(2,fim_det_fill,fim_det(1,1));   
end
fim_det=cat(2,fim_det_fill,fim_det);
%--------------------------------------------------------------------