Switch to unified view

a b/preprocessOfApneaECG/BioSigKit/Algorithms/energyop.m
1
function [ey,ex]=energyop(sig,gr)
2
%% %calculates the energy operator of a signal
3
4
%% %input
5
6
%1. Raw signal (Vector)
7
%2. gr (Plot or not plot)
8
9
%% %Output
10
11
%Energy operator signal (ey)
12
%Teager operator (ex)
13
%% %Method
14
15
%The Teager Energy Operator is determined as
16
    %(x(t)) = (dx/dt)^2+ x(t)(d^2x/dt^2) (1.1)
17
%in the continuous case (where x_ means the rst derivative of x, and x means the second
18
%derivative), and as
19
    %[x[n]] = x^2[n] + x[n - 1]x[n + 1] (1.2)
20
%in the discrete case.
21
%% Method
22
%Note that the function is vectorized for optimum processing speed(Keep calm and vectorize)
23
%Author : Hooman Sedghamiz 
24
25
%% hoose792@student.liu.se
26
%%
27
if nargin<2
28
    gr=0;
29
end
30
31
sig=sig(:);
32
33
34
35
%% (x(t)) = (dx/dt)^2+ x(t)(d^2x/dt^2) 
36
%Operator 1
37
y=diff(sig);
38
y=[0;y];
39
squ=y(2:length(y)-1).^2;
40
oddi=y(1:length(y)-2);
41
eveni=y(3:length(y));
42
ey=squ - (oddi.*eveni);
43
%% [x[n]] = x^2[n] - x[n - 1]x[n + 1] 
44
%operator ex
45
squ1=sig(2:length(sig)-1).^2;
46
oddi1=sig(1:length(sig)-2);
47
eveni1=sig(3:length(sig));
48
ex=squ1 - (oddi1.*eveni1);
49
ex = [ex(1); ex; ex(length(sig)-2)]; %make it the same length
50
51
%% plots
52
53
if gr
54
figure,ax(1)=subplot(211);plot((sig/max(sig))-mean(sig/max(sig)),'b'),
55
hold on,
56
plot((ey/max(ey))-mean(ey/max(ey)),'Linewidth',2,'LineStyle','--','color','r'),
57
axis tight;
58
hleg1=legend('Original Signal','Energy Operator');
59
set(hleg1,'Location','NorthWest')
60
ax(2)=subplot(212);plot((sig/max(sig))-mean(sig/max(sig)),'b'),
61
hold on,
62
plot((ex/max(ex))-mean(ex/max(ex)),'Linewidth',2,'LineStyle','--','color','g'),
63
hleg2=legend('Original Signal','Teager Energy');
64
set(hleg2,'Location','NorthWest')
65
axis tight,
66
zoom on;
67
linkaxes(ax,'x');
68
end