clc;
clear all;
close all;

Ref = xlsread('Reference.xlsx');
A0 = xlsread('Freq_Calib_20paths_0dB.xlsx');
A1 = xlsread('Freq_Calib_20paths_1dB.xlsx');
A2 = xlsread('Freq_Calib_20paths_2dB.xlsx');
A3 = xlsread('Freq_Calib_20paths_3dB.xlsx');
A4 = xlsread('Freq_Calib_20paths_4dB.xlsx');
A5 = xlsread('Freq_Calib_20paths_5dB.xlsx');
A6 = xlsread('Freq_Calib_20paths_6dB.xlsx');
A7 = xlsread('Freq_Calib_20paths_7dB.xlsx');
A8 = xlsread('Freq_Calib_20paths_8dB.xlsx');
A9 = xlsread('Freq_Calib_20paths_9dB.xlsx');
A10 = xlsread('Freq_Calib_20paths_10dB.xlsx');
A11 = xlsread('Freq_Calib_20paths_11dB.xlsx');
A12 = xlsread('Freq_Calib_20paths_12dB.xlsx');
A13 = xlsread('Freq_Calib_20paths_13dB.xlsx');
A14 = xlsread('Freq_Calib_20paths_14dB.xlsx');
A15 = xlsread('Freq_Calib_20paths_15dB.xlsx');
A16 = xlsread('Freq_Calib_20paths_16dB.xlsx');
A17 = xlsread('Freq_Calib_20paths_17dB.xlsx');
A18 = xlsread('Freq_Calib_20paths_18dB.xlsx');
A19 = xlsread('Freq_Calib_20paths_19dB.xlsx');
A20 = xlsread('Freq_Calib_20paths_20dB.xlsx');
A21 = xlsread('Freq_Calib_20paths_21dB.xlsx');
A22 = xlsread('Freq_Calib_20paths_22dB.xlsx');
A23 = xlsread('Freq_Calib_20paths_23dB.xlsx');
A24 = xlsread('Freq_Calib_20paths_24dB.xlsx');
A25 = xlsread('Freq_Calib_20paths_25dB.xlsx');
A26 = xlsread('Freq_Calib_20paths_26dB.xlsx');
A27 = xlsread('Freq_Calib_20paths_27dB.xlsx');
A28 = xlsread('Freq_Calib_20paths_28dB.xlsx');
A29 = xlsread('Freq_Calib_20paths_29dB.xlsx');
A30 = xlsread('Freq_Calib_20paths_30dB.xlsx');
A31 = xlsread('Freq_Calib_20paths_31dB.xlsx');

Freq = A0(358:567,1);
Ref = Ref(358:567,2);

Ref = 10.^(Ref/20);

figure
plot(Freq, Ref)

PA_out0 = A0(358:567,2);
ns_0dB = A0(358:567,3:22);
ns_0 = 10.^(ns_0dB/20);

PA_out1 = A1(358:567,2);
ns_1dB = A1(358:567,3:22);
ns_1 = 10.^(ns_1dB/20);

PA_out2 = A2(358:567,2);
ns_2dB = A2(358:567,3:22);
ns_2 = 10.^(ns_2dB/20);

PA_out3 = A3(358:567,2);
ns_3dB = A3(358:567,3:22);
ns_3 = 10.^(ns_3dB/20);

PA_out4 = A4(358:567,2);
ns_4dB = A4(358:567,3:22);
ns_4 = 10.^(ns_4dB/20);

PA_out5 = A5(358:567,2);
ns_5dB = A5(358:567,3:22);
ns_5 = 10.^(ns_5dB/20);

PA_out6 = A6(358:567,2);
ns_6dB = A6(358:567,3:22);
ns_6 = 10.^(ns_6dB/20);

PA_out7 = A7(358:567,2);
ns_7dB = A7(358:567,3:22);
ns_7 = 10.^(ns_7dB/20);

PA_out8 = A8(358:567,2);
ns_8dB = A8(358:567,3:22);
ns_8 = 10.^(ns_8dB/20);

PA_out9 = A9(358:567,2);
ns_9dB = A9(358:567,3:22);
ns_9 = 10.^(ns_9dB/20);

PA_out10 = A10(358:567,2);
ns_10dB = A10(358:567,3:22);
ns_10 = 10.^(ns_10dB/20);

PA_out11 = A11(358:567,2);
ns_11dB = A11(358:567,3:22);
ns_11 = 10.^(ns_11dB/20);

PA_out12 = A12(358:567,2);
ns_12dB = A12(358:567,3:22);
ns_12 = 10.^(ns_12dB/20);

PA_out13 = A13(358:567,2);
ns_13dB = A13(358:567,3:22);
ns_13 = 10.^(ns_13dB/20);

PA_out14 = A14(358:567,2);
ns_14dB = A14(358:567,3:22);
ns_14 = 10.^(ns_14dB/20);

PA_out15 = A15(358:567,2);
ns_15dB = A15(358:567,3:22);
ns_15 = 10.^(ns_15dB/20);

PA_out16 = A16(358:567,2);
ns_16dB = A16(358:567,3:22);
ns_16 = 10.^(ns_16dB/20);

PA_out17 = A17(358:567,2);
ns_17dB = A17(358:567,3:22);
ns_17 = 10.^(ns_17dB/20);

PA_out18 = A18(358:567,2);
ns_18dB = A18(358:567,3:22);
ns_18 = 10.^(ns_18dB/20);

PA_out19 = A19(358:567,2);
ns_19dB = A19(358:567,3:22);
ns_19 = 10.^(ns_19dB/20);

PA_out20 = A20(358:567,2);
ns_20dB = A20(358:567,3:22);
ns_20 = 10.^(ns_20dB/20);

PA_out21 = A21(358:567,2);
ns_21dB = A21(358:567,3:22);
ns_21 = 10.^(ns_21dB/20);

PA_out22 = A22(358:567,2);
ns_22dB = A22(358:567,3:22);
ns_22 = 10.^(ns_22dB/20);

PA_out23 = A23(358:567,2);
ns_23dB = A23(358:567,3:22);
ns_23 = 10.^(ns_23dB/20);

PA_out24 = A24(358:567,2);
ns_24dB = A24(358:567,3:22);
ns_24 = 10.^(ns_24dB/20);

PA_out25 = A25(358:567,2);
ns_25dB = A25(358:567,3:22);
ns_25 = 10.^(ns_25dB/20);

PA_out26 = A26(358:567,2);
ns_26dB = A26(358:567,3:22);
ns_26 = 10.^(ns_26dB/20);

PA_out27 = A27(358:567,2);
ns_27dB = A27(358:567,3:22);
ns_27 = 10.^(ns_27dB/20);

PA_out28 = A28(358:567,2);
ns_28dB = A28(358:567,3:22);
ns_28 = 10.^(ns_28dB/20);

PA_out29 = A29(358:567,2);
ns_29dB = A29(358:567,3:22);
ns_29 = 10.^(ns_29dB/20);

PA_out30 = A30(358:567,2);
ns_30dB = A30(358:567,3:22);
ns_30 = 10.^(ns_30dB/20);

PA_out31 = A31(358:567,2);
ns_31dB = A31(358:567,3:22);
ns_31 = 10.^(ns_31dB/20);



for i=1:32
    for j=1:32
        S(i,j) = 0;
    end
end

Sum(1:32) = 0;

for i=1:32
    for j=1:20
        Sum_S(i) = Sum(i)+S(i,j);
    end
end

NS= [ns_0 ns_1 ns_2 ns_3 ns_4 ns_5 ns_6 ns_7 ns_8 ns_9 ns_10 ns_11 ns_12 ns_13 ns_14 ns_15 ns_16 ns_17 ns_18 ns_19 ns_20 ns_21 ns_22 ns_23 ns_24 ns_25 ns_26 ns_27 ns_28 ns_29 ns_30 ns_31];

for i=1:32
    NS_delay1(:,i) = NS(:,1+20*(i-1));
    NS_delay2(:,i) = NS(:,1+20*(i-1));
    NS_delay3(:,i) = NS(:,2+20*(i-1));
    NS_delay4(:,i) = NS(:,3+20*(i-1));
    NS_delay5(:,i) = NS(:,4+20*(i-1));
    NS_delay6(:,i) = NS(:,5+20*(i-1));
    NS_delay7(:,i) = NS(:,6+20*(i-1));
    NS_delay8(:,i) = NS(:,7+20*(i-1));
    NS_delay9(:,i) = NS(:,8+20*(i-1));
    NS_delay10(:,i) = NS(:,9+20*(i-1));
    NS_delay11(:,i) = NS(:,10+20*(i-1));
    NS_delay12(:,i) = NS(:,11+20*(i-1));
    NS_delay13(:,i) = NS(:,12+20*(i-1));
    NS_delay14(:,i) = NS(:,13+20*(i-1));
    NS_delay15(:,i) = NS(:,14+20*(i-1));
    NS_delay16(:,i) = NS(:,15+20*(i-1));
    NS_delay17(:,i) = NS(:,16+20*(i-1));
    NS_delay18(:,i) = NS(:,17+20*(i-1));
    NS_delay19(:,i) = NS(:,18+20*(i-1));
    NS_delay20(:,i) = NS(:,19+20*(i-1));
end

S(2,1) = 1;

V1 = NS_delay1(:,:)*S(:,1);
V2 = NS_delay2(:,:)*S(:,2);
V3 = NS_delay3(:,:)*S(:,3);
V4 = NS_delay4(:,:)*S(:,4);
V5 = NS_delay5(:,:)*S(:,5);
V6 = NS_delay6(:,:)*S(:,6);
V7 = NS_delay7(:,:)*S(:,7);
V8 = NS_delay8(:,:)*S(:,8);
V9 = NS_delay9(:,:)*S(:,9);
V10 = NS_delay10(:,:)*S(:,10);
V11 = NS_delay11(:,:)*S(:,11);
V12 = NS_delay12(:,:)*S(:,12);
V13 = NS_delay13(:,:)*S(:,13);
V14 = NS_delay14(:,:)*S(:,14);
V15 = NS_delay15(:,:)*S(:,15);
V16 = NS_delay16(:,:)*S(:,16);
V17 = NS_delay17(:,:)*S(:,17);
V18 = NS_delay18(:,:)*S(:,18);
V19 = NS_delay19(:,:)*S(:,19);
V20 = NS_delay20(:,:)*S(:,20);

Vopt = V1+V2+V3+V4+V5+V6+V7+V8+V9+V10+V11+V12+V13+V14+V15+V16+V17+V18+V19+V20;

Error = Ref - NS_delay1(:,5);
figure
plot(Freq,20*log10(Error))

Ref(78,1)
V1(78)
FreqResp.Constraints.sum = Sumt <= 1;
cost =1;
FreqResp.Objective = cost;

solve(FreqResp);

% for i=1:20
% td1(:,i) = PA_out - ns_p5dB(:,i);
% end 
% 
% 
% figure
% hold on
% plot(Freq,td1(:,1),'k');
% plot(Freq,td1(:,2),'r');
% plot(Freq,td1(:,3),'b');
% 
figure
hold on
plot(Freq,Ref,'k');
plot(Freq,NS_delay1(:,5),'r');
