信号处理 - 希尔伯特变换和频谱分析
作者:桂。
时间: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
宋知用:《MATLAB在语音信号分析和合成中的应用》
下一篇: 微型企业地区代理返利系统开发详情
推荐阅读
-
数字信号处理系列实验 3--信号的频谱分析
-
基于 MATLAB 的数字信号处理(3) 通过 FFT 进行信号频谱分析
-
信号处理 - 希尔伯特变换和频谱分析
-
心电信号]基于 Matlab 的心电信号特征提取 + 分析和处理 [附 Matlab 源代码 289 期
-
数字图像处理实验(II)|图像变换 {离散傅立叶变换 fft2、离散余弦变换 dct2、频谱移位 fftshift}(附实验代码和截图)
-
脑科学磁共振成像 (MRI) 入门必读 - 脑功能网络、小世界网络、FDR 校正、脑电信号频率变换、模板、假设检验、广义线性模型、独立成分分析、成像组织学、任务-状态和休息-状态方法总结 "建议收藏
-
信号处理趣闻 D8 - 拉氏变换和频谱图的全部内容
-
时域离散信号和系统的变换域分析
-
windows下进程间通信的(13种方法)-摘 要 本文讨论了进程间通信与应用程序间通信的含义及相应的实现技术,并对这些技术的原理、特性等进行了深入的分析和比较。 ---- 关键词 信号 管道 消息队列 共享存储段 信号灯 远程过程调用 Socket套接字 MQSeries 1 引言 ---- 进程间通信的主要目的是实现同一计算机系统内部的相互协作的进程之间的数据共享与信息交换,由于这些进程处于同一软件和硬件环境下,利用操作系统提供的的编程接口,用户可以方便地在程序中实现这种通信;应用程序间通信的主要目的是实现不同计算机系统中的相互协作的应用程序之间的数据共享与信息交换,由于应用程序分别运行在不同计算机系统中,它们之间要通过网络之间的协议才能实现数据共享与信息交换。进程间通信和应用程序间通信及相应的实现技术有许多相同之处,也各有自己的特色。即使是同一类型的通信也有多种的实现方法,以适应不同情况的需要。 ---- 为了充分认识和掌握这两种通信及相应的实现技术,本文将就以下几个方面对这两种通信进行深入的讨论:问题的由来、解决问题的策略和方法、每种方法的工作原理和实现、每种实现方法的特点和适用的范围等。 2 进程间的通信及其实现技术 ---- 用户提交给计算机的任务最终都是通过一个个的进程来完成的。在一组并发进程中的任何两个进程之间,如果都不存在公共变量,则称该组进程为不相交的。在不相交的进程组中,每个进程都独立于其它进程,它的运行环境与顺序程序一样,而且它的运行环境也不为别的进程所改变。运行的结果是确定的,不会发生与时间相关的错误。 ---- 但是,在实际中,并发进程的各个进程之间并不是完全互相独立的,它们之间往往存在着相互制约的关系。进程之间的相互制约关系表现为两种方式: ---- (1) 间接相互制约:共享CPU ---- (2) 直接相互制约:竞争和协作 ---- 竞争——进程对共享资源的竞争。为保证进程互斥地访问共享资源,各进程必须互斥地进入各自的临界段。 ---- 协作——进程之间交换数据。为完成一个共同任务而同时运行的一组进程称为同组进程,它们之间必须交换数据,以达到协作完成任务的目的,交换数据可以通知对方可以做某事或者委托对方做某事。 ---- 共享CPU问题由操作系统的进程调度来实现,进程间的竞争和协作由进程间的通信来完成。进程间的通信一般由操作系统提供编程接口,由程序员在程序中实现。UNIX在这个方面可以说最具特色,它提供了一整套进程间的数据共享与信息交换的处理方法——进程通信机制(IPC)。因此,我们就以UNIX为例来分析进程间通信的各种实现技术。 ---- 在UNIX中,文件(File)、信号(Signal)、无名管道(Unnamed Pipes)、有名管道(FIFOs)是传统IPC功能;新的IPC功能包括消息队列(Message queues)、共享存储段(Shared memory segment)和信号灯(Semapores)。 ---- (1) 信号 ---- 信号机制是UNIX为进程中断处理而设置的。它只是一组预定义的值,因此不能用于信息交换,仅用于进程中断控制。例如在发生浮点错、非法内存访问、执行无效指令、某些按键(如ctrl-c、del等)等都会产生一个信号,操作系统就会调用有关的系统调用或用户定义的处理过程来处理。 ---- 信号处理的系统调用是signal,调用形式是: ---- signal(signalno,action) ---- 其中,signalno是规定信号编号的值,action指明当特定的信号发生时所执行的动作。 ---- (2) 无名管道和有名管道 ---- 无名管道实际上是内存中的一个临时存储区,它由系统安全控制,并且独立于创建它的进程的内存区。管道对数据采用先进先出方式管理,并严格按顺序操作,例如不能对管道进行搜索,管道中的信息只能读一次。 ---- 无名管道只能用于两个相互协作的进程之间的通信,并且访问无名管道的进程必须有共同的祖先。 ---- 系统提供了许多标准管道库函数,如: pipe——打开一个可以读写的管道; close——关闭相应的管道; read——从管道中读取字符; write——向管道中写入字符; ---- 有名管道的操作和无名管道类似,不同的地方在于使用有名管道的进程不需要具有共同的祖先,其它进程,只要知道该管道的名字,就可以访问它。管道非常适合进程之间快速交换信息。 ---- (3) 消息队列(MQ) ---- 消息队列是内存中独立于生成它的进程的一段存储区,一旦创建消息队列,任何进程,只要具有正确的的访问权限,都可以访问消息队列,消息队列非常适合于在进程间交换短信息。 ---- 消息队列的每条消息由类型编号来分类,这样接收进程可以选择读取特定的消息类型——这一点与管道不同。消息队列在创建后将一直存在,直到使用msgctl系统调用或iqcrm -q命令删除它为止。 ---- 系统提供了许多有关创建、使用和管理消息队列的系统调用,如: ---- int msgget(key,flag)——创建一个具有flag权限的MQ及其相应的结构,并返回一个唯一的正整数msqid(MQ的标识符); ---- int msgsnd(msqid,msgp,msgsz,msgtyp,flag)——向队列中发送信息; ---- int msgrcv(msqid,cmd,buf)——从队列中接收信息; ---- int msgctl(msqid,cmd,buf)——对MQ的控制操作; ---- (4) 共享存储段(SM) ---- 共享存储段是主存的一部分,它由一个或多个独立的进程共享。各进程的数据段与共享存储段相关联,对每个进程来说,共享存储段有不同的虚拟地址。系统提供的有关SM的系统调用有: ---- int shmget(key,size,flag)——创建大小为size的SM段,其相应的数据结构名为key,并返回共享内存区的标识符shmid; ---- char shmat(shmid,address,flag)——将当前进程数据段的地址赋给shmget所返回的名为shmid的SM段; ---- int shmdr(address)——从进程地址空间删除SM段; ---- int shmctl (shmid,cmd,buf)——对SM的控制操作; ---- SM的大小只受主存限制,SM段的访问及进程间的信息交换可以通过同步读写来完成。同步通常由信号灯来实现。SM非常适合进程之间大量数据的共享。 ---- (5) 信号灯 ---- 在UNIX中,信号灯是一组进程共享的数据结构,当几个进程竞争同一资源时(文件、共享内存或消息队列等),它们的操作便由信号灯来同步,以防止互相干扰。 ---- 信号灯保证了某一时刻只有一个进程访问某一临界资源,所有请求该资源的其它进程都将被挂起,一旦该资源得到释放,系统才允许其它进程访问该资源。信号灯通常配对使用,以便实现资源的加锁和解锁。 ---- 进程间通信的实现技术的特点是:操作系统提供实现机制和编程接口,由用户在程序中实现,保证进程间可以进行快速的信息交换和大量数据的共享。但是,上述方式主要适合在同一台计算机系统内部的进程之间的通信。 3 应用程序间的通信及其实现技术 ---- 同进程之间的相互制约一样,不同的应用程序之间也存在竞争和协作的关系。UNIX操作系统也提供一些可用于应用程序之间实现数据共享与信息交换的编程接口,程序员可以通过自己编程来实现。如远程过程调用和基于TCP/IP协议的套接字(Socket)编程。但是,相对普通程序员来说,它们涉及的技术比较深,编程也比较复杂,实现起来困难较大。 ---- 于是,一种新的技术应运而生——通过将有关通信的细节完全掩盖在某个独立软件内部,即底层的通讯工作和相应的维护管理工作由该软件内部来实现,用户只需要将通信任务提交给该软件去完成,而不必理会它的具体工作过程——这就是所谓的中间件技术。 ---- 我们在这里分别讨论这三种常用的应用程序间通信的实现技术——远程过程调用、会话编程技术和MQSeries消息队列技术。其中远程过程调用和会话编程属于比较低级的方式,程序员参与的程度较深,而MQSeries消息队列则属于比较高级的方式,即中间件方式,程序员参与的程度较浅。 ---- 4.1 远程过程调用(RPC)
-
信号与系统实验室 V 信号的傅立叶分析和频谱