1 ## Copyright (C) 2000 Paul Kienzle <pkienzle@users.sf.net>
2 ## Copyright (C) 2007 Peter L. Soendergaard
4 ## This program is free software; you can redistribute it and/or modify it under
5 ## the terms of the GNU General Public License as published by the Free Software
6 ## Foundation; either version 3 of the License, or (at your option) any later
9 ## This program is distributed in the hope that it will be useful, but WITHOUT
10 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 ## You should have received a copy of the GNU General Public License along with
15 ## this program; if not, see <http://www.gnu.org/licenses/>.
18 ## @deftypefn {Function File} {@var{h} =} hilbert (@var{f},@var{N},@var{dim})
19 ## Analytic extension of real valued signal
21 ## @code{@var{h}=hilbert(@var{f})} computes the extension of the real
22 ## valued signal @var{f} to an analytic signal. If @var{f} is a matrix,
23 ## the transformation is applied to each column. For N-D arrays,
24 ## the transformation is applied to the first non-singleton dimension.
26 ## @code{real(@var{h})} contains the original signal @var{f}.
27 ## @code{imag(@var{h})} contains the Hilbert transform of @var{f}.
29 ## @code{hilbert(@var{f},@var{N})} does the same using a length @var{N}
30 ## Hilbert transform. The result will also have length @var{N}.
32 ## @code{hilbert(@var{f},[],@var{dim})} or
33 ## @code{hilbert(@var{f},@var{N},@var{dim})} does the same along
37 function f=hilbert(f, N = [], dim = [])
39 % ------ PRE: initialization and dimension shifting ---------
41 if (nargin<1 || nargin>3)
46 warning ('HILBERT: ignoring imaginary part of signal');
59 % We have a vector, find the dimension where it lives.
64 if (numel(dim)~=1 || ~isnumeric(dim))
65 error('HILBERT: dim must be a scalar.');
68 error('HILBERT: dim must be an integer.');
71 error('HILBERT: dim must be in the range from 1 to %d.',D);
76 if (numel(N)>1 || ~isnumeric(N))
77 error('N must be a scalar.');
78 elseif (~isempty(N) && rem(N,1)~=0)
79 error('N must be an integer.');
83 order=[dim, 1:dim-1,dim+1:D];
85 % Put the desired dimension first.
92 % If N is empty it is set to be the length of the transform.
97 % Remember the exact size for later and modify it for the new length
101 % Reshape f to a matrix.
102 f=reshape(f,size(f,1),numel(f)/size(f,1));
109 % ------- actual computation -----------------
127 % ------- POST: Restoration of dimensions ------------
129 % Restore the original, permuted shape.
130 f=reshape(f,permutedsize);
133 % Undo the permutation.
140 %! % notice that the imaginary signal is phase-shifted 90 degrees
141 %! t=linspace(0,10,256);
142 %! z = hilbert(sin(2*pi*0.5*t));
143 %! grid on; plot(t,real(z),';real;',t,imag(z),';imag;');
146 %! % the magnitude of the hilbert transform eliminates the carrier
147 %! t=linspace(0,10,1024);
148 %! x=5*cos(0.2*t).*sin(100*t);
149 %! grid on; plot(t,x,'g;z;',t,abs(hilbert(x)),'b;|hilbert(z)|;');