function [Z2,Z2int] = stat_Z2(p)

%% License:
% "This work is licensed under the Creative Commons Attribution 4.0
% International (CC BY 4.0) License. To view a copy of this license, visit:
% http://creativecommons.org/licenses/by/4.0/"

%% please cite 
% --> Kristofer Marsch (2020)
% --> DOI for this data available at https://orcid.org/0000-0002-1857-506X


%% calculating Z2 
% based on Myers (1962)

%% --parameters-- %%
% p: array of profile, first column horizontal values, second column heights
% profile must have constant sampling interval for Z2, don't for Z2int
% profile must be de-trended

%% calculate Z2
ey = diff(p(:,2));
ex = diff(p(:,1));
Z2 = sqrt(1/length(ey)*sum((ey./ex).^2));

y = (diff(p(:,2))./diff(p(:,1))).^2;
x = p(1:end-1,1);
Q = trapz(x,y); %discrete integration
Z2int = sqrt(1/max(p(:,1))*Q);

