%%wavefilter源函数
function [varargout]=wavefilter(wname,type)
%wavefilter创造一个小波分解和重构滤波器
%例如:
%[ld,hd,lr,hr]=wavefilter('haar')得到一个小波名为haar的低通和高通的重构和分解滤波器
%[ld,hd]=wavefilter('haar','d')得到低通和高通的分解滤波器
%[lr,hr]=wavefilter('haar','r')得到低通和高通的重构滤波器
%inputs:
%wname 小波名称
%'haar'or'dbl' haar
%'db4' 4th order Daubechies
%'sym4' 4th order Symlets
%'bior6.8' Cohen-Daubechies-Feauveau biorthogonal
%'jpeg9.7' Antonini-Barlaud-Mathieu-Daubechies
%类型 滤波器类型
%'d' 重构滤波器
%'r' 分解滤波器
%检查输入和输出参数
error(nargchk(1,2,nargin));
%nargin用来判断输入变量个数的函数,
%当nargin的值大于2时,nargchk返回字符串'Too many input arguments';当nargin的值小于1时,nargchk返回字符串'Not enough input arguments';
%当nargin的值在1到2之间,nargchk返回空字符串。error以错误的方式显示警告字符串(以红色字体显示)。
if (nargin==1 & nargout ~=4 ) | (nargin==2 & nargout ~=2)
%若输入参数个数为1且输出参数个数不等于4或者输入参数个数等于2且输出参数个数不等于2,则显示错误。
error('Invalid number of output arguments.');
end
if nargin==1 & ~ischar(wname)%若输入参数个数为1且wname类型不是字符串,则显示错误
error('WNAME must be a string');
end
if nargin==2 & ~ischar(type)%若输入参数个数为2且type类型不是字符串,则显示错误
error('TYPE must be a string.');
end
%创造满足小波需要滤波器
switch lower(wname)%从低开始
case{'haar','dbl'}
ld=[1 1]/sqrt(2); hd=[-1 1]/sqrt(2);
lr=ld; hr=-hd;
case'db4'
ld=[-1.059740178499728e-002 3.288301166698295e-00 ...
3.084138183598697e-002 -1.870348117188811e-001...
-2.79837641698385e-002 6.308807679295904-001...
7.148465705525415e-001 2.303778133088552e-001];
t=(0:7);
hd=ld; hd(end:-1:1)=cos(pi*t).*ld;
lr=ld; lr(end:-1:1)=ld;
hr=cos(pi*t).*ld;
case'sym4'
ld=[-7.576571478927333e-002 -2.963552764599851e-002...
4.976186676320155e-001 8.037387518059161e-001...
2.978577956052774e-001 -9.921954357684722e-002...
-1.260396726203783e-002 3.222310060404270e-002];
t=(0:7);
hd=ld; hd(end:-1:1)=cos(pi*t).*ld;
lr=ld; lr(end:-1:1)=ld;
hr=cos(pi*t).*ld;
case'bior6.8'
ld=[0 1.908831736481291e-003 -1.914286129088767e-003...
-1.699063986760234e-002 1.193456527972926e-002...
4.973290349094079e-002 -7.726317316720414e-002...
-9.405920439573646e-002 4.207962846098268e-001...
8.259229974584023e-001 4.207962846098268e-001...
-9.405920920349573646e-002 -7.726317316720414e-002...
4.973290349094079e-002 1.19345652792926e-002...
-1.699063986760234e-002 -1.914286129088767e-003...
1.908831736481291e-003];
hd=[0 0 0 1.442628250562444e-002 -1.446750489679015e-002...
-7.872200106262882e-002 4.036797903033992e-002...
4.178491091502746e-001 -7.589077294536542e-001...
4.178491091502746e-001 4.036797903033992e-002...
-7.872200106262882e-002 -1.446750489679015e-002...
1.442628250562444e-002 0 0 0 0];
t=(0:17);
lr=cos(pi*(t+1)).*hd;
hr=cos(pi*t).*ld;
case'jpeg.7'
ld=[0 0.2674875741080976 -0.01686411844287495...
-0.07822326652898785 0.2668641184428723...
0.6029490182363579 0.2668641184428723...
-0.07822326652898785 -0.01686411844287495...
0.02674875741070976];
hd=[0 -0.9127176311424948 0.05754352622849957...
0.5912717631142470 -1.115087052456994...
0.5912717631142470 0.05754352622849957...
-0.09127176311424948 0 0];
t=(0:9);
lr=cos(pi*(t+1).*hd);
hr=cos(pi*t).*ld;
otherwise
error('Unrecognizable wavelet name (WNAME).');
end
%输出需要的滤波器
if (nargin==1)
varargout(1:4)={ld,hd,lr,hr};
else
switch lower(type(1))
case'd'
varargout={ld,hd};
case'r'
varargout={lr,hr};
otherwise
error('Unrecognizable filter TYPE.');
end
end
wavefilter.m
最新推荐文章于 2022-09-19 20:09:10 发布