function [fim_det,fim_trace,fim_cond,placement_result...
    ,fai_initial,direction,del_sensor_tag_r...
    ,fai_left,tmse]=...
    random_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
% random_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
%--------------------------------------------------------------------
% 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
%--------------------------------------------------------------------
% Exclude and Delete Row No.1, bcz it is all zeros(always static)
if strcmp(file_name,'wall_76_xz.xls')
blank_loop=0;
else
fai_i_exc(1,:)=[];
end
%--------------------------------------------------------------------
% To build a MODE SHAPE matrix
fai_no_unit=fai_i_exc(:,1:num_mod_exp);
% 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;
fai_i_exc_with_row1=cat(1,zeros(1,fi_sz_y),fai_no_unit);
%--------------------------------------------------------------------
% Memory Allocation
credit=ones(1,num_cand-1);
fim_det=zeros(1,num_cand-num_targ+1);
sum_mac_output=zeros(1,num_cand-num_targ+1);
fim_trace=zeros(1,num_cand-num_targ+1);
fim_cond=zeros(1,num_cand-num_targ+1);
del_sensor_tag=zeros(1,1);
[noi_fai_sz_x,noi_fai_sz_y]=size(fai);
%--------------------------------------------------------------------
% Noise Synthesization 
fai_initial_a=fai;
[fai_ini_x_d,fai_ini_y_d]=size(fai_initial_a);
%--------------------------------------------------------------------
% Generate the homogeneously distributed sensor tag vectors
tag_delta=0;
num_delta=num_cand-num_targ+1;
seq_rand=randperm(num_cand);
tag_delta=seq_rand(1,1:num_delta);
%--------------------------------------------------------------------
% EFI Iteration
for m=0:(num_cand-num_targ)
     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_clean_mid=zeros(1,num_mod_exp);
     fai_clean_new=zeros(1,num_mod_exp);
%--------------------------------------------------------------------
     for n=1:num_cand-1
         if (credit(n)==0)
             fai_mid=fai_mid;
             fai_clean_mid=fai_clean_mid;
          else
             fai_mid=cat(1,fai_mid,fai_initial_a(n,:));
             fai_clean_mid=cat(1,fai_clean_mid,fai(n,:));
          end
     end
     for i=1:(num_cand-m-1)
         fai_new(i,:)=fai_mid(i+1,:);
         fai_clean_new(i,:)=fai_clean_mid(i+1,:);
     end
     if m==num_cand-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;%awgn already added
      fim_matrix_clean_new=fai_clean_new'*fai_clean_new;%clean, i.e. without noise
%--------------------------------------------------------------------
% 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_trace(1,m+1)=trace(fim_matrix_clean_new);
     fim_cond(1,m+1)=cond(fim_matrix_clean_new);
%--------------------------------------------------------------------
% MAC
for i=1:num_mod_exp
    for j=1:num_mod_exp
        xi=fai_new(:,i);
        yj=fai_new(:,j);
        if (xor(direction(1,i),direction(1,j))==0)
            mac(i,j)=det(xi'*yj).^2/(det(xi'*xi)*det(yj'*yj));
        else
            mac(i,j)=0;
        end
    end
end
sum_mac_output(1,m+1)=sum(sum(mac));
%--------------------------------------------------------------------
% CREDIT
[posit_r_new,posit_c_new]=min(abs(eid_new'));
          if m<num_cand-num_targ
              credit(1,tag_delta(1,m+1))=0;
          else
              credit=credit;
          end
%--------------------------------------------------------------------
     tmse=0;     
end
%--------------------------------------------------------------------
% del_sensor_tag_r=tag_delta;
del_sensor_tag_r=tag_delta(1,1:num_cand-num_targ);
[initial_x,initial_y]=size(fai_initial_a);
fai_initial=cat(1,zeros(1,initial_y),fai_initial_a);
%--------------------------------------------------------------------
[sz_del_stx,sz_del_sty]=size(del_sensor_tag_r);
credit_final=ones(1,num_cand);
for i=1:num_cand
    for j=1:sz_del_sty
        if i==del_sensor_tag_r(1,j)
            credit_final(1,i)=0;            
        else
            credit_final;            
        end
    end   
end
placement_result=credit_final;