作者:桂。
時間:2017-03-03 23:57:29
鏈接:http://www.cnblogs.com/xingshansi/articles/6498913.html

前言
Hilbert通常用來得到解析信號,基於此原理,Hilbert可以用來對窄帶信號進行解包絡,並求解信號的瞬時頻率,但求解包括的時候會出現端點效應,本文對於這幾點分別做了簡單的理論探討。
本文內容多有借鑒他人,最后一並附上鏈接。
一、基本理論
A-Hilbert變換定義
對於一個實信號$x(t)$,其希爾伯特變換為:
$\tilde x(t) = x(t) * \frac{1}{\pi t}$
式中*表示卷積運算。
Hilbert本質上也是轉向器,對應頻域變換為:
$\frac{1}{{\pi t}} \Leftrightarrow j\cdot \;sign(\omega )$
即余弦信號的Hilbert變換時正弦信號,又有:
$\frac{1}{{\pi t}}*\frac{1}{{\pi t}} \Leftrightarrow j \cdot \;sign(\omega ) \cdot j \cdot \;sign(\omega ) = - 1$
即信號兩次Hilbert變換后是其自身相反數,因此正弦信號的Hilbert是負的余弦。
對應解析信號為:
$z(t) = x(t) + j\tilde x(t)$
此操作實現了信號由雙邊譜到單邊譜的轉化。
B-Hilbert解調原理
設有窄帶信號:
$x(t) = a(t)\cos [2\pi {f_s}t + \varphi (t)]$
其中$f_s$是載波頻率,$a(t)$是$x(t)$的包絡,$\varphi (t)$是$x(t)$的相位調制信號。由於$x(t)$是窄帶信號,因此$a(t)$也是窄帶信號,可設為:
$a(t) = \left[ {1 + \sum\limits_{m = 1}^M {{X_m}\cos (2\pi {f_m}t + {\gamma _m})} } \right]$
式中,$f_m$為調幅信號$a(t)$的頻率分量,${\gamma _m}$為$f_m$的各初相角。
對$x(t)$進行Hilbert變換,並求解解析信號,得到:
$z(t) = {e^{j\left[ {2\pi {f_s} + \varphi \left( t \right)} \right]}}\left[ {1 + \sum\limits_{m = 1}^M {{X_m}\cos (2\pi {f_m}t + {\gamma _m})} } \right]$
設
$A(t) = \left[ {1 + \sum\limits_{m = 1}^M {{X_m}\cos (2\pi {f_m}t + {\gamma _m})} } \right]$
$\Phi \left( t \right) = 2\pi {f_s}t + \varphi \left( t \right)$
則解析信號可以重新表達為:
$z(t) = A(t){e^{j\Phi \left( t \right)}}$
對比$x(t)$表達式,容易發現:
$a(t) = A(t) = \sqrt {{x^2}(t) + {{\tilde x}^2}(t)} $
$\varphi (t) = \Phi (t) - 2\pi {f_s}t = \arctan \frac{{x(t)}}{{\tilde x(t)}} - 2\pi {f_s}t$
由此可以得出:對於窄帶信號$x(t)$,利用Hilbert可以求解解析信號,從而得到信號的幅值解調$a(t)$和相位解調$\varphi (t)$,並可以利用相位解調求解頻率解調$f(t)$。因為:
$f\left( t \right) = \frac{1}{{2\pi }}\frac{{d\varphi (t)}}{{dt}} = \frac{1}{{2\pi }}\frac{{d\Phi (t)}}{{dt}} - {f_s}$
C-相關MATLAB指令
- hilbert
功能:將實數信號x(n)進行Hilbert變換,並得到解析信號z(n).
調用格式:z = hilbert(x)
- instfreq
功能:計算復信號的瞬時頻率。
調用格式:[f, t] = insfreq(x,t)
示例:
z = hilbert(x); f = instfreq(z);
二、應用實例
例1:給定一正弦信號,畫出其Hilbert信號,直接給代碼:
clc
clear all
close all
ts = 0.001;
fs = 1/ts;
N = 200;
f = 50;
k = 0:N-1;
t = k*ts;
% 信號變換
% 結論:sin信號Hilbert變換后為cos信號
y = sin(2*pi*f*t);
yh = hilbert(y); % matlab函數得到信號是合成的復信號
yi = imag(yh); % 虛部為書上定義的Hilbert變換
figure
subplot(211)
plot(t, y)
title('原始sin信號')
subplot(212)
plot(t, yi)
title('Hilbert變換信號')
ylim([-1,1])
對應效果圖:

例2:已知信號$x(t) = (1 + 0.5\cos (2\pi 5t))\cos (2\pi 50t + 0.5\sin (2\pi 10t))$,求解該信號的包絡和瞬時頻率。
分析:根據解包絡原理知:
信號包絡:$(1 + 0.5\cos (2\pi 5t))$
瞬時頻率:$\frac{2\pi 50t + 0.5\sin (2\pi 10t)}{2\pi}$
那么問題來了,實際情況是:我們只知道$x(t)$的結果,而不知道其具體表達形式,這個時候,上文的推導就起了作用:可以借助信號的Hilbert變換,從而求解信號的包絡和瞬時頻率。
對應代碼:
clear all; clc; close all;
fs=400; % 采樣頻率
N=400; % 數據長度
n=0:1:N-1;
dt=1/fs;
t=n*dt; % 時間序列
A=0.5; % 相位調制幅值
x=(1+0.5*cos(2*pi*5*t)).*cos(2*pi*50*t+A*sin(2*pi*10*t)); % 信號序列
z=hilbert(x'); % 希爾伯特變換
a=abs(z); % 包絡線
fnor=instfreq(z); % 瞬時頻率
fnor=[fnor(1); fnor; fnor(end)]; % 瞬時頻率補齊
% 作圖
pos = get(gcf,'Position');
set(gcf,'Position',[pos(1), pos(2)-100,pos(3),pos(4)]);
subplot 211; plot(t,x,'k'); hold on;
plot(t,a,'r--','linewidth',2);
title('包絡線'); ylabel('幅值'); xlabel(['時間/s' 10 '(a)']);
ylim([-2,2]);
subplot 212; plot(t,fnor*fs,'k'); ylim([43 57]);
title('瞬時頻率'); ylabel('頻率/Hz'); xlabel(['時間/s' 10 '(b)']);
其中instfreq為時頻工具包的代碼,可能有的朋友沒有該代碼,這里給出其程序:
function [fnormhat,t]=instfreq(x,t,L,trace);
%INSTFREQ Instantaneous frequency estimation.
% [FNORMHAT,T]=INSTFREQ(X,T,L,TRACE) computes the instantaneous
% frequency of the analytic signal X at time instant(s) T, using the
% trapezoidal integration rule.
% The result FNORMHAT lies between 0.0 and 0.5.
%
% X : Analytic signal to be analyzed.
% T : Time instants (default : 2:length(X)-1).
% L : If L=1, computes the (normalized) instantaneous frequency
% of the signal X defined as angle(X(T+1)*conj(X(T-1)) ;
% if L>1, computes a Maximum Likelihood estimation of the
% instantaneous frequency of the deterministic part of the signal
% blurried in a white gaussian noise.
% L must be an integer (default : 1).
% TRACE : if nonzero, the progression of the algorithm is shown
% (default : 0).
% FNORMHAT : Output (normalized) instantaneous frequency.
% T : Time instants.
%
% Examples :
% x=fmsin(70,0.05,0.35,25); [instf,t]=instfreq(x); plot(t,instf)
% N=64; SNR=10.0; L=4; t=L+1:N-L; x=fmsin(N,0.05,0.35,40);
% sig=sigmerge(x,hilbert(randn(N,1)),SNR);
% plotifl(t,[instfreq(sig,t,L),instfreq(x,t)]); grid;
% title ('theoretical and estimated instantaneous frequencies');
%
% See also KAYTTH, SGRPDLAY.
% F. Auger, March 1994, July 1995.
% Copyright (c) 1996 by CNRS (France).
%
% ------------------- CONFIDENTIAL PROGRAM --------------------
% This program can not be used without the authorization of its
% author(s). For any comment or bug report, please send e-mail to
% f.auger@ieee.org
if (nargin == 0),
error('At least one parameter required');
end;
[xrow,xcol] = size(x);
if (xcol~=1),
error('X must have only one column');
end
if (nargin == 1),
t=2:xrow-1; L=1; trace=0.0;
elseif (nargin == 2),
L = 1; trace=0.0;
elseif (nargin == 3),
trace=0.0;
end;
if L<1,
error('L must be >=1');
end
[trow,tcol] = size(t);
if (trow~=1),
error('T must have only one row');
end;
if (L==1),
if any(t==1)|any(t==xrow),
error('T can not be equal to 1 neither to the last element of X');
else
fnormhat=0.5*(angle(-x(t+1).*conj(x(t-1)))+pi)/(2*pi);
end;
else
H=kaytth(L);
if any(t<=L)|any(t+L>xrow),
error('The relation L<T<=length(X)-L must be satisfied');
else
for icol=1:tcol,
if trace, disprog(icol,tcol,10); end;
ti = t(icol); tau = 0:L;
R = x(ti+tau).*conj(x(ti-tau));
M4 = R(2:L+1).*conj(R(1:L));
diff=2e-6;
tetapred = H * (unwrap(angle(-M4))+pi);
while tetapred<0.0 , tetapred=tetapred+(2*pi); end;
while tetapred>2*pi, tetapred=tetapred-(2*pi); end;
iter = 1;
while (diff > 1e-6)&(iter<50),
M4bis=M4 .* exp(-j*2.0*tetapred);
teta = H * (unwrap(angle(M4bis))+2.0*tetapred);
while teta<0.0 , teta=(2*pi)+teta; end;
while teta>2*pi, teta=teta-(2*pi); end;
diff=abs(teta-tetapred);
tetapred=teta; iter=iter+1;
end;
fnormhat(icol,1)=teta/(2*pi);
end;
end;
end;
對應的結果圖為:

可以看到信號的包絡、瞬時頻率,均已完成求解。
例3:例2中信號包絡為規則的正弦函數,此處給定任意形式的包絡(以指數形式為例),並利用Hilbert求解包絡以及瞬時頻率,並給出對應的Hilbert譜。
程序:
clc
clear all
close all
ts = 0.001;
fs = 1/ts;
N = 200;
k = 0:N-1;
t = k*ts;
% 原始信號
f1 = 10;
f2 = 70;
% a = cos(2*pi*f1*t); % 包絡1
a = 2 + exp(0.2*f1*t); % 包絡2
% a = 1./(1+t.^2*50); % 包絡3
m = sin(2*pi*f2*t); % 調制信號
y = a.*m; % 信號調制
figure
subplot(241)
plot(t, a)
title('包絡')
subplot(242)
plot(t, m)
title('調制信號')
subplot(243)
plot(t, y)
title('調制結果')
% 包絡分析
% 結論:Hilbert變換可以有效提取包絡、高頻調制信號的頻率等
yh = hilbert(y);
aabs = abs(yh); % 包絡的絕對值
aangle = unwrap(angle(yh)); % 包絡的相位
af = diff(aangle)/2/pi; % 包絡的瞬時頻率,差分代替微分計算
% NFFT = 2^nextpow2(N);
NFFT = 2^nextpow2(1024*4); % 改善柵欄效應
f = fs*linspace(0,1,NFFT);
YH = fft(yh, NFFT)/N; % Hilbert變換復信號的頻譜
A = fft(aabs, NFFT)/N; % 包絡的頻譜
subplot(245)
plot(t, aabs,'r', t, a)
title('包絡的絕對值')
legend('包絡分析結果', '真實包絡')
subplot(246)
plot(t, aangle)
title('調制信號的相位')
subplot(247)
plot(t(1:end-1), af*fs)
title('調制信號的瞬時頻率')
subplot(244)
plot(f,abs(YH))
title('原始信號的Hilbert譜')
xlabel('頻率f (Hz)')
ylabel('|YH(f)|')
subplot(248)
plot(f,abs(A))
title('包絡的頻譜')
xlabel('頻率f (Hz)')
ylabel('|A(f)|')
對應結果圖:

從結果可以觀察,出了邊界誤差較大,結果值符合預期。對於邊界效應的分析,見擴展閱讀部分。注意:此處瞬時頻率求解,沒有用instfreq函數,擴展閱讀部分對該函數作進一步討論。
三、擴展閱讀
A-瞬時頻率求解方法對比
對於離散數據,通常都是用差分代替微分,因此瞬時頻率也可根據概念直接求解。此處對比分析兩種求解瞬時頻率的方法,給出代碼:
clc
clear all
close all
ts = 0.001;
fs = 1/ts;
N = 200;
k = 0:N-1;
t = k*ts;
% 原始信號
f1 = 10;
f2 = 70;
% a = cos(2*pi*f1*t); % 包絡1
a = 2 + exp(0.2*f1*t); % 包絡2
% a = 1./(1+t.^2*50); % 包絡3
m = sin(2*pi*f2*t); % 調制信號
y = a.*m; % 信號調制
figure
yh = hilbert(y);
aangle = unwrap(angle(yh)); % 包絡的相位
af1 = diff(aangle)/2/pi; % 包絡的瞬時頻率,差分代替微分計算
af1 = [af1(1),af1];
subplot 211
plot(t, af1*fs);hold on;
plot(t,70*ones(1,length(t)),'r--','linewidth',2);
title('直接求解調制信號的瞬時頻率');
legend('頻率估值','真實值','location','best');
subplot 212
af2 = instfreq(yh.').';
af2 = [af2(1),af2,af2(end)];
plot(t, af2*fs);hold on;
plot(t,70*ones(1,length(t)),'r--','linewidth',2);
title('instfreq求解調制信號的瞬時頻率');
legend('頻率估值','真實值','location','best');
結果圖:

可以看出,兩種方式結果近似,但instfreq的結果更為平滑一些。
B-端點效應分析
對於任意包絡,求解信號的包絡以及瞬時頻率,容易出現端點誤差較大的情況,該現象主要基於信號中的Gibbs現象,限於篇幅,擬為此單獨寫一篇文章,具體請參考:Hilbert端點效應分析。
C-VMD、EMD
Hilbert經典應用總繞不開HHT(Hilbert Huang),HHT基於EMD,近年來又出現了VMD分解,擬為此同樣寫一篇文章,略說一二心得,具體參考:EMD、VMD的一點小思考。
D-解包絡方法
需要認識到,Hilbert不是解包絡的唯一途徑,低通濾波(LPF)等方式一樣可以達到該效果,只不過截止頻率需要調參。
給出一個Hilbert、低通濾波解包絡的代碼:
function y=envelope(signal,Fs)
%Example:
% load('s4.mat');
% signal=s4;
% Fs=12000;
% envelope(signal,Fs);
clc;
close all;
%Normal FFT
y=signal;
figure();
N=2*2048;T=N/Fs;
sig_f=abs(fft(y(1:N)',N));
sig_n=sig_f/(norm(sig_f));
freq_s=(0:N-1)/T;
subplot 311
plot(freq_s(2:250),sig_n(2:250));title('FFT of Original Signal');
%Envelope Detection based on Low pass filter and then FFT
[a,b]=butter(2,0.1);%butterworth Filter of 2 poles and Wn=0.1
%sig_abs=abs(signal); % Can be used instead of squaring, then filtering and
%then taking square root
sig_sq=2*signal.*signal;% squaring for rectifing
%gain of 2 for maintianing the same energy in the output
y_sq = filter(a,b,sig_sq); %applying LPF
y=sqrt(y_sq);%taking Square root
%advantages of taking square and then Square root rather than abs, brings
%out some hidden information more efficiently
N=2*2048;T=N/Fs;
sig_f=abs(fft(y(1:N)',N));
sig_n=sig_f/(norm(sig_f));
freq_s=(0:N-1)/T;
subplot 312
plot(freq_s(2:250),sig_n(2:250));title('Envelope Detection: LPF Method');
%Envelope Detection based on Hilbert Transform and then FFT
analy=hilbert(signal);
y=abs(analy);
N=2*2048;T=N/Fs;
sig_f=abs(fft(y(1:N)',N));
sig_n=sig_f/(norm(sig_f));
freq_s=(0:N-1)/T;
subplot 313
plot(freq_s(2:250),sig_n(2:250));title('Envelope Detection : Hilbert Transform')
結果圖:

效果是不是也不錯?
Hilbert硬件實現思路:
思路1(時域處理):借助MATLAB fdatool實現,Hilbert transform,導出濾波器系數
思路2(頻域處理):

參考:
了凡春秋:http://blog.sina.com.cn/s/blog_6163bdeb0102e1wv.html#cmt_3294265
