-
Notifications
You must be signed in to change notification settings - Fork 6
/
hilbert_transform.m
61 lines (52 loc) · 1.81 KB
/
hilbert_transform.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
function [ h, amplitude, phase, omega, period ] = hilbert_transform( data, smooth, plots )
%HILBERT_TRANSFORM Perform Hilbert transform with or without smoothing of phase.
% Zero-phase FIR (Finite impulse response) filter with averaging (equals denominator coefficients) used 2 times for phase smoothing.
% Source data can be reconstructed through data = real((sum(amp.*exp(i*phase), 2)));
%
% Input:
% data - source data matrix
% smooth - boolean flag (0 or 1) for phase smoothing
% plots - boolean flag (0 or 1) for graphics plot
%
% Output:
% h - analytic signal
% amplitude - instantaneous amplitude
% phase - instantaneous phase
% omega - instantaneous frequency
% period - instantaneous period
%
% Copyright (c) 2014 by Dmitriy O. Afanasyev
% Versions:
% 1.0 2014.04.05: initial version
%
if (nargin < 2)
smooth = 0;
end
if (nargin < 3)
plots = 0;
end
h = hilbert(data);
amplitude = abs(h);
phase = unwrap(angle(h));
if(smooth > 0)
% smooth phase by applying zero-phase FIR (Finite impulse response) filter with averaging (equals denominator coefficients) 2 times
denominator = [1/3 1/3 1/3];
numerator = 1;
phase = filtfilt(denominator, numerator, phase);
phase = filtfilt(denominator, numerator, phase);
end
omega = (1/(2*pi)) * abs(diff(phase));
omega = [omega(1,:); omega];
period = 1./omega;
if(plots > 0)
figure;
subplot(2, 2, 1);
plot(amplitude);
subplot(2, 2, 2);
plot(omega);
subplot(2, 2, 3);
plot(phase);
subplot(2, 2, 4);
plot(period);
end
end