MATLAB: Help modifying a User-defined-function

udfupwardcrossingzerocrossing

I am trying to compute the mean max and mean min amplitude of time series datasets. These timeseries are arranged in each column of a matrix, so that the matrix is 351×117 . So 117 timeseries. I have found a user defined function that can do just that.The problem is that this UDF is meant for only 1 timeseries array at a time, x. How can I modify the following UDF to accomodate for a matrix,A, instead of an array x. Note that the desired output will be in temp78. I will need to compute the average of the fist two columns of temp78 in order to find the avg max and min amplitudes for every timeseries.

Best Answer

try something like the following code. Save this in a single file named zero_cross.m
function [temp80,temp78,NT]=zero_cross(x,N,dt)
m = size(x,2); % num columns
temp80 = cell(1,m);
temp78 = cell(1,m);
NT = cell(1,m);
for i=1:m
[temp80{i}, temp78{i}, NT{i}] = zero_cross1(x(:,i), N, dt);
end
end
function [temp80,temp78,NT]=zero_cross1(x,N,dt)
% find zero-crossing points
temp70 = x(1:N-1).*x(2:N);
temp70(N) = 1.0;
temp72=find(temp70<0);
temp73 = zeros(N,1);
temp74 = zeros(N,1);
temp75 = find(x<0);
temp73(temp72) = 1;
temp74(temp75) = 1;
temp76 = and(temp73,temp74);
temp77 = find(temp76>0);
temp555 = temp77/24;
temp78 = zeros(N,4);
% total number of complete waves
NT = sum(temp76)-1;
% find local maxima, minima, duration and occurance
% Hmax Hmin Tdur Tocur
for i = 1:NT;
temp78(temp77(i),1) = max(x(temp77(i):temp77(i+1)));
temp78(temp77(i),2) = min(x(temp77(i):temp77(i+1)));
temp78(temp77(i),3) = temp77(i+1) - temp77(i);
temp78(temp77(i),4) = temp77(i);
end
%output
temp78=temp78(find(temp78(:,4)>0),:); %raw upcrossing data
temp79(:,1)=temp78(:,1)-temp78(:,2); %wave height
temp79(:,2)=temp78(:,3)*dt; %wave period
temp80=flipud(sortrows(temp79,1)); %sorted H and T
end