clear
close all

load('2mic_8mm_23mm.mat');

N = Result.AcqSetup.nSamples;
fs = Result.AcqSetup.Fs;

freq = (0:N/2)'/N*fs;
Z0 = 1.2*343;
k = 2*pi*freq/343;

P_l = Result.FrequencyResponseFunction(:,1);
P_u = Result.FrequencyResponseFunction(:,2);

z_u = 2.3e-2;
z_l = 0.8e-2;

%0.8
%2.3
dz = z_u-z_l;
Pappr = (P_u+P_l)/2;
Vappr = (P_u-P_l)./(1i*2*pi*freq*1.2)/dz;

Zspec = Pappr./Vappr;
Z = (Zspec - 1i.*Z0.*tan(k*z_l))./(Z0- 1i*Zspec.*tan(k*z_l));

alpha_11 = 1- ( abs((Z-1)./(Z+1)) ).^2;

H12 = P_u./P_l;
R = ( H12.*exp(1i*k*z_l) - exp(1i*k*z_u) ) ./ ( exp(-1i*k*z_u) - H12.*exp(-1i*k*z_l));
alpha_21 = 1 -( abs(R) ).^2;
%%
Lf = 20;
alpha_f1_1 = conv(alpha_11,ones(Lf,1)/Lf,'same');
alpha_f2_1 = conv(alpha_21,ones(Lf,1)/Lf,'same');

%%
load('2mic_7mm_17mm_on_ground.mat');

N = Result.AcqSetup.nSamples;
fs = Result.AcqSetup.Fs;

freq = (0:N/2)'/N*fs;
Z0 = 1.2*343;
k = 2*pi*freq/343;

P_l = Result.FrequencyResponseFunction(:,1);
P_u = Result.FrequencyResponseFunction(:,2);

z_u = 1.7e-2;
z_l = 0.7e-2;

%0.8
%2.3
dz = z_u-z_l;
Pappr = (P_u+P_l)/2;
Vappr = (P_u-P_l)./(1i*2*pi*freq*1.2)/dz;

Zspec = Pappr./Vappr;
Z = (Zspec - 1i.*Z0.*tan(k*z_l))./(Z0- 1i*Zspec.*tan(k*z_l));

alpha_12 = 1- ( abs((Z-1)./(Z+1)) ).^2;

H12 = P_u./P_l;
R = ( H12.*exp(1i*k*z_l) - exp(1i*k*z_u) ) ./ ( exp(-1i*k*z_u) - H12.*exp(-1i*k*z_l));
alpha_22 = 1 -( abs(R) ).^2;

Lf = 20;
alpha_f1_2 = conv(alpha_12,ones(Lf,1)/Lf,'same');
alpha_f2_2 = conv(alpha_22,ones(Lf,1)/Lf,'same');
%%
figure
semilogx(freq,alpha_f1_1);
hold on
grid on
xlim([20,10000])
hold on
semilogx(freq,alpha_f2_2,'--');