function [REM,p_residual,h_echo] = ureflections_test_1(f,s11)
%Evaluate micro-reflections
%Usage:
%  ureflections_test(f,s11)
%where <f> is frequency and <s11> is S11 parameters
%The function returns the metric value and plots the response.
%This function also uses the function RHJf2t().
%Version 1.0 -- March 9, 2021
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
% OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
% THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
% DEALINGS IN THE SOFTWARE.

%%% sampling interval and bin size %%%
f_s = 14062500000;
T = 1/f_s;    %% sampling interval
N_bins = 512;
L_bin = 4;    %% four samples per bin
N_discard = 12;
t_bin = L_bin*T;   
t_max = N_bins * L_bin * T;

%%% number of samples and time vector %%%
N_samples = round(N_bins * L_bin)*2;  %even number of samples
t = [0:N_samples-1]*T;

%%% calculate echo impulse response and power %%%
h_echo = cy_f2t(s11,f,T,N_samples);

%%% find power in each time bin %%%
h2 = h_echo.^2;
p_bin = zeros(1,N_bins);
m1 = 0;
for n=1:N_bins,
  m0 = m1 + 1;
  m1 = round(n*L_bin);
  p_bin(n) = mean(h2(m0:m1))*(L_bin);
end

%%% calculate effect of increasing number of bins %%% 
[p_sort,sort_ix] = sort(p_bin);
p_sum = cumsum(p_sort);
p_residual = p_sum(end:-1:1);

%%% convert to dB %%%
REM = 10*log10(p_residual(N_discard));

end %%% End of ureflections_test

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function h = cy_f2t(H,f,T,N)

%RJf2t - Impulse (time) response for a given frequency response.
%Usage:
%  h = RJf2t(H,f,T,N)
% where <H> is the frequency response given at frequencies <f>,
% <T> is the sampling interval, and <N> is the number of output
% samples (must be even).

%%% find size %%%
NN = prod(size(H));

%%% test arguments %%%
if( nargin < 2 )
  f = [0:NN-1]./(NN-1)*pi;
end
if( nargin < 3 )
  T = 1;
end
if( nargin < 4 )
  N = 256;
end
N2 = ceil(N/2);

%%% find problem spots %%%
ix = find(H == H);
H = H(ix);
f = f(ix);

%%% re-shape arguments %%%
H = H(:);

%%% interpolate frequency response %%%
Hs1 = cr_spline(f*T,H,[0:N2]/N2/2);
ang_N = angle(Hs1(N2+1));
x0 = ang_N/(pi);
Hs1 = Hs1.*exp(-j*2*pi*x0*[0:N2]/N2/2);
Hs = [real(Hs1(1)) Hs1(2:N2) real(Hs1(N2+1)) conj(Hs1(N2:-1:2))];

%%% find impulse response from IDFT %%%
h = real(ifft(Hs));

end %%% End of cy_f2t

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function s = cr_spline(x,y,xq)
% Catmull-Rom spline
%   s = cr_ spline(f*T,H,[0:N2]/N2/2);
% See more at https://en.wikipedia.org/wiki/Cubic_Hermite_spline

%%% initialize %%%
N_x = length(x);
[M_xq,N_xq] = size(xq);

%%% reshape arguments %%%
x = x(:);
y = y(:);
xq = xq(:);

%%% Catmull-Rom spline coefficients %%%
cr0 = [0 0 1 0];
cr1 = [0 1 0 -1]/2;
cr2 = [-1 4 -5 2]/2;
cr3 = [1 -3 3 -1]/2;

%%% find closest points for x-values %%%
xm = (x(1:end-1) + x(2:end) - 1e-10)/2; %% midpoint in interval 
[d,m]=min(abs(reshape(xm,N_x-1,1) - reshape(xq,1,N_xq*M_xq)));
u = (xq - x(m))./(x(m+1) - x(m));

%%% Use Farrow Structure to implement spline %%%
y0 = conv(y, cr0);
y1 = conv(y, cr1);
y2 = conv(y, cr2);
y3 = conv(y, cr3);

s = y3(m+2).*(u.^3)+y2(m+2).*(u.^2) + y1(m+2).*u + y0(m+2);
s = reshape(s,M_xq,N_xq);

end %%% End of cr_spline
