-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprincax.m
49 lines (41 loc) · 1.65 KB
/
princax.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function [theta,maj,min,wr]=princax(w);
% PRINCAX Principal axis, rotation angle, principal ellipse
%
% [theta,maj,min,wr]=princax(w)
%
% Input: w = complex vector time series (u+i*v)
%
% Output: theta = angle of maximum variance, math notation (east == 0, north=90)
% maj = major axis of principal ellipse
% min = minor axis of principal ellipse
% wr = rotated time series, where real(wr) is aligned with
% the major axis.
%
% For derivation, see Emery and Thompson, "Data Analysis Methods
% in Oceanography", 1998, Pergamon, pages 325-327. ISBN 0 08 0314341
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Version 1.0 (12/4/1996) Rich Signell ([email protected])
% Version 1.1 (4/21/1999) Rich Signell ([email protected])
% fixed bug that sometimes caused the imaginary part
% of the rotated time series to be aligned with major axis.
% Also simplified the code.
% Version 1.2 (3/1/2000) Rich Signell ([email protected])
% Simplified maj and min axis computations and added reference
% to Emery and Thompson book
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% use only the good (finite) points
ind=find(isfinite(w));
wr=w;
w=w(ind);
% find covariance matrix
cv=cov([real(w(:)) imag(w(:))]);
% find direction of maximum variance
theta=0.5*atan2(2.*cv(2,1),(cv(1,1)-cv(2,2)) );
% find major and minor axis amplitudes
term1=(cv(1,1)+cv(2,2));
term2=sqrt((cv(1,1)-cv(2,2)).^2 + 4.*cv(2,1).^2);
maj=sqrt(.5*(term1+term2));
min=sqrt(.5*(term1-term2));
% rotate into principal ellipse orientation
wr(ind)=w.*exp(-i*theta);
theta=theta*180./pi;