-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunwrap3.m
103 lines (88 loc) · 2.19 KB
/
unwrap3.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
function [pdU, h] = unwrap3(pd, params)
% params.upThresh and params.upCor. Points where postive jump is greater
% than upThresh will be corrected by upCor (so upCor should probably be
% negative)
%
% params.downThresh and params.upCor. Points where postive jump is greater
% than downThresh will be corrected by downCor (so downCor should probably
% be positive)
%% Set params
% upThresh: Threshold for up jumps
if nargin < 2 || ~isfield(params, 'upThresh')
upThresh = 180;
else
upThresh = params.upThresh;
end
% upCor: Correction mag for up jumps (showld be down)
if nargin < 2 || ~isfield(params, 'upCor')
upCor = -360;
else
upCor = params.upCor;
end
% downThresh: Threshold for down jumps
if nargin < 2 || ~isfield(params, 'downThresh')
downThresh = -180;
else
downThresh = params.downThresh;
end
% downCor: Correction mag for down jumps (showld be up)
if nargin < 2 || ~isfield(params, 'downCor')
downCor = 360;
else
downCor = params.downCor;
end
% norm: Normalise start to zero?
if nargin < 2 || ~isfield(params, 'norm')
norm = true;
else
norm = params.norm;
end
% pltOn: Plot?
if nargin < 2 || ~isfield(params, 'plotOn')
plotOn = true;
else
plotOn = params.plotOn;
end
inRow = size(pd,2)>1;
%% Run
% If input col, switch to row
if ~inRow
pd = pd';
end
% Find differences
pdDiff = [0, diff(pd)];
% Find differences above thresholds
pdDiffLog = any([(pdDiff > 0) & (pdDiff > upThresh); ...
(pdDiff < 0) & (pdDiff < downThresh)]);
pdU = pd;
if any(pdDiffLog)
% At each of these locations, add or subtract to point and each
% subsequent point
for p = 1:length(pd)
if pdDiffLog(p)
% Find direction to adjust
if pdDiff(p) > 0
adjust = upCor; % Correct down
else
adjust = downCor; % Correct up
end
pdU(p:end) = pdU(p:end) + adjust;
end
end
end
% Roughly normalise phase
% Adjust to 0 start using mean of first 2 points
if norm
pdU = pdU - mean(pdU(1:2));
end
% If input was a col, return a col
if ~inRow
pdU = pdU';
end
% If plot requested, plot
if plotOn
h = figure;
plot(pd)
hold on
plot(pdU)
end