-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetPhaseShape.m
executable file
·64 lines (59 loc) · 1.83 KB
/
GetPhaseShape.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
%find phase of a sample in a signal based on the shape analysis
%first peaks are detected, then phase is found as a ratio between peaks
%otc - averaged signal
%N - sample to compute phase
%delta - shortest size to find peaks, usualy 10% of peak-peak
%OTC should be theta filtered
function phMod = GetPhaseShape(otc,N)
%[maxes,mins] = peakdet(otc,delta);
%mx = maxes(:,1);
%mi = mins(:,1);
mx = find((otc > [otc(1) otc(1:(end-1))]) & (otc >= [otc(2:end) otc(end)]));
mi = find((otc < [otc(1) otc(1:(end-1))]) & (otc <= [otc(2:end) otc(end)]));
%{
figure;
hold on;
plot(otc,'g');
plot(mx,otc(mx),'r.')
plot(mi,otc(mi),'b.')
%}
%find closest extreme
% = length(otc)/2;
[dmx,kmx] = min(abs(mx-N));
[dmi,kmi] = min(abs(mi-N));
%min closer
if dmi <= dmx
%min is right of center, find phase from left max and min
%min is left of center, find phase bw min and right max
miSel = mi(kmi);
if miSel >= N
maSel = mx(mx<N);
if isempty(maSel); phMod = nan; return; end;
maSel = maSel(end);
phMod = (N-maSel+1) / (miSel-maSel+1);
phMod = phMod * pi;
else
maSel = mx(mx>N);
if isempty(maSel); phMod = nan; return; end;
maSel = maSel(1);
phMod = (maSel-N+1) / (maSel-miSel+1);
phMod = phMod * (-pi);
end
else %max closer
%max is righ of center, find phase between min on left and max
%max is left of center, find phase between max and right min
maSel = mx(kmx);
if N <= maSel
miSel = mi(mi<N);
if isempty(miSel); phMod = nan; return; end;
miSel = miSel(end);
phMod = (maSel-N+1) / (maSel-miSel+1);
phMod = phMod * (-pi);
else
miSel = mi(mi>N);
if isempty(miSel); phMod = nan; return; end;
miSel = miSel(1);
phMod = (N-maSel+1) / (miSel-maSel+1);
phMod = phMod * pi;
end
end