📄 yhzfiltdes.m
字号:
% dstr = [dstr sprintf('\n') ...
% ' b = firls(order,w,[0 0 1 1]).*wind; a = 1;' ];
% dstr1 = ['b = firls(order,[0 wn([2 2])*2/Fs 1],[0 0 1 1])'...
% '.*kaiser(order+1,wn(1))''; a = 1;'];
%elseif type == 3 % band pass
% dstr = [dstr sprintf('\n') ...
% ' w = [' sprintf('%1.18e\n ',[0 wn([2 2 3 3])*2/Fs 1]) '];'];
% dstr = [dstr sprintf('\n') ...
% ' wind = kaiser(order+1,' sprintf('%1.18e',wn(1)) ');'];
% dstr = [dstr sprintf('\n') ...
% ' b = firls(order,w,[0 0 1 1 0 0]).*wind; a = 1;' ];
% dstr1 = ['b = firls(order,[0 wn([2 2 3 3])*2/Fs 1],'...
% '[0 0 1 1 0 0]).*kaiser(order+1,wn(1))'';'...
% 'a = 1;'];
%elseif type == 4 % band stop
% dstr = [dstr sprintf('\n') ...
% ' w = [' sprintf('%1.18e\n ',[0 wn([2 2 3 3])*2/Fs 1]) '];'];
% dstr = [dstr sprintf('\n') ...
% ' wind = kaiser(order+1,' sprintf('%1.18e',wn(1)) ');'];
% dstr = [dstr sprintf('\n') ...
% ' b = firls(order,w,[1 1 0 0 1 1]).*wind; a = 1;' ];
% dstr1 = ['b = firls(order,[0 wn([2 2 3 3])*2/Fs 1],'...
% '[1 1 0 0 1 1]).*kaiser(order+1,wn(1))'';'...
% 'a = 1;'];
%end
%title_str = sprintf('Order %g FIR Filter designed with FIRLS',order);
else % firmethod=6 Frequece Sampling Method
b=yhzFirSamp(fp/Fs,spec.firN,FType);
a=1;
title_str=sprintf('FIR Filter Design with Frequency Sampling Points: %g',spec.firN);
end %end of fir filter
else %iir
if iirmethod2==1 %BLT
if iirmethod1==1 % buttor
Proto='butterworth';
switch FType
case 1 %LP
[b,a] = butter(spec.iirorder,fp*2/Fs);
case 2 %HP
[b,a] = butter(spec.iirorder,fp*2/Fs,'high');
case 3 %BP
[b,a] = butter(spec.iirorder,fp*2/Fs);
end %switch FType
elseif iirmethod1==2 %cheby1
Proto='chebyshevI';
if FType==2
[b,a] = cheby1(spec.iirorder,Rp,fp*2/Fs,'high');
else %LP and Bp
[b,a] = cheby1(spec.iirorder,Rp,fp*2/Fs);
end %FTypp
else %iirmethod1=3
Proto='chebyshevII';
if FType==2
[b,a] = cheby2(spec.iirorder,Rs,fp*2/Fs,'high');
else %LP and Bp
[b,a] = cheby2(spec.iirorder,Rs,fp*2/Fs);
end %FTypp
end %end of BLT
title_str=sprintf('IIR Filter Design with Bilinear Transfer Method and LPAF Prototype:%s',Proto);
else %iirmethod==2 Impulse Response Invariance--LP
if iirmethod1==1 %butter
Proto='butterworth';
[b,a] = butterImp(spec.iirorder,fp*2/Fs);
elseif iirmethod1==2 %cheby1
Proto='chebyshevI';
[b,a] = cheby1Imp(spec.iirorder,Rp,fp*2/Fs);
else %cheby2
Proto='chebyshevII';
[b,a] = cheby2Imp(spec.iirorder,Rp,fp*2/Fs);
end
title_str=sprintf('IIR Filter Design with Impulse Response Invariance Method and LPAF Prototype: %s',Proto);
end % end of IIR
end %end of FIR and IIR
%end
%if (method == 1)|(method == 2)
% if type == 1 % low pass
% f = [0 f1 f2 Fs/2]*2/Fs; m = [1 1 0 0];
% elseif type == 2 % high pass
% f = [0 f1 f2 Fs/2]*2/Fs; m = [0 0 1 1];
% elseif type == 3 % band pass
% f = [0 f1 f2 f3 f4 Fs/2]*2/Fs; m = [0 0 1 1 0 0];
% elseif type == 4 % band stop
% f = [0 f1 f2 f3 f4 Fs/2]*2/Fs; m = [1 1 0 0 1 1];
% end
% dstr = [' order = ' sprintf('%g',order) ';'];
% dstr = [dstr sprintf('\n') ' f = [' sprintf('%1.18e\n ',f) '];'];
% dstr = [dstr sprintf('\n') ' m = [' sprintf('%1.18e\n ',m) '];'];
% dstr = [dstr sprintf('\n') ' wn = [' sprintf('%1.18e\n ',wn) '];'];
% if method == 1
% dstr1 = 'b = remez(order,f,m,wn); a = 1;';
% title_str = sprintf('Order %g FIR Filter designed with REMEZ',order);
% dstr = [dstr sprintf('\n') ' b = remez(order,f,m,wn); a = 1;'];
% else
% dstr1 = 'b = firls(order,f,m,wn); a = 1;';
% title_str = sprintf('Order %g FIR Filter designed with FIRLS',order);
% dstr = [dstr sprintf('\n') ' b = firls(order,f,m,wn); a = 1;'];
% end
% elseif method == 4 % butterworth
% wn = wn*2/Fs;
% dstr = [' order = ' sprintf('%g',order) ';'];
% dstr = [dstr sprintf('\n') ...
% ' wn = [' sprintf('%1.18e\n ',wn) '];'];
% if type == 1 % lowpass
% dstr = [dstr sprintf('\n') ...
% ' [b,a] = butter(order,wn);' ];
% dstr1 = '[b,a] = butter(order,wn);';
% elseif type == 2 % highpass
% dstr = [dstr sprintf('\n') ...
% ' [b,a] = butter(order,wn,''high'');' ];
% dstr1 = '[b,a] = butter(order,wn,''high'');';
% elseif type == 3 % bandpass
% dstr = [dstr sprintf('\n') ...
% ' [b,a] = butter(order,wn);' ];
% dstr1 = '[b,a] = butter(order,wn);';
% elseif type == 4 % bandstop
% dstr = [dstr sprintf('\n') ...
% ' [b,a] = butter(order,wn,''stop'');' ];
% dstr1 = '[b,a] = butter(order,wn,''stop'');';
% end
% title_str = sprintf('Order %g Butterworth IIR Filter',order);
% elseif method == 5 % chebyshev type 1
% wn = wn*2/Fs;
% dstr = [' order = ' sprintf('%g',order) ';'];
% dstr = [dstr sprintf('\n') ' Rp = ' sprintf('%g',Rp) ';'];
% dstr = [dstr sprintf('\n') ...
% ' wn = [' sprintf('%1.18e\n ',wn) '];'];
% if type == 1 % lowpass
% dstr = [dstr sprintf('\n') ...
% ' [b,a] = cheby1(order,Rp,wn);' ];
% dstr1 = '[b,a] = cheby1(order,Rp,wn);';
% elseif type == 2 % highpass
% dstr = [dstr sprintf('\n') ...
% ' [b,a] = cheby1(order,Rp,wn,''high'');' ];
% dstr1 = '[b,a] = cheby1(order,Rp,wn,''high'');';
% elseif type == 3 % bandpass
% dstr = [dstr sprintf('\n') ...
% ' [b,a] = cheby1(order,Rp,wn);' ];
% dstr1 = '[b,a] = cheby1(order,Rp,wn);';
% elseif type == 4 % bandstop
% dstr = [dstr sprintf('\n') ...
% ' [b,a] = cheby1(order,Rp,wn,''stop'');' ];
% dstr1 = '[b,a] = cheby1(order,Rp,wn,''stop'');';
% end
% title_str = sprintf('Order %g Chebyshev Type I IIR Filter',order);
% elseif method == 6 % chebyshev type II
% wn = wn*2/Fs;
% dstr = [' order = ' sprintf('%g',order) ';'];
% dstr = [dstr sprintf('\n') ' Rs = ' sprintf('%g',Rs) ';'];
% dstr = [dstr sprintf('\n') ...
% ' wn = [' sprintf('%1.18e\n ',wn) '];'];
% if type == 1 % lowpass
% dstr = [dstr sprintf('\n') ...
% ' [b,a] = cheby2(order,Rs,wn);' ];
% dstr1 = '[b,a] = cheby2(order,Rs,wn);';
% elseif type == 2 % highpass
% dstr = [dstr sprintf('\n') ...
% ' [b,a] = cheby2(order,Rs,wn,''high'');' ];
% dstr1 = '[b,a] = cheby2(order,Rs,wn,''high'');';
% elseif type == 3 % bandpass
% dstr = [dstr sprintf('\n') ...
% ' [b,a] = cheby2(order,Rs,wn);' ];
% dstr1 = '[b,a] = cheby2(order,Rs,wn);';
% elseif type == 4 % bandstop
% dstr = [dstr sprintf('\n') ...
% ' [b,a] = cheby2(order,Rs,wn,''stop'');' ];
% dstr1 = '[b,a] = cheby2(order,Rs,wn,''stop'');';
% end
% title_str = sprintf('Order %g Chebyshev Type II IIR Filter',order);
%elseif method == 7 % elliptic
% wn = wn*2/Fs;
% dstr = [' order = ' sprintf('%g',order) ';'];
% dstr = [dstr sprintf('\n') ' Rp = ' sprintf('%g',Rp) ';'];
% dstr = [dstr sprintf('\n') ' Rs = ' sprintf('%g',Rs) ';'];
% dstr = [dstr sprintf('\n') ...
% ' wn = [' sprintf('%1.18e\n ',wn) '];'];
% if type == 1 % lowpass
% dstr = [dstr sprintf('\n') ...
% ' [b,a] = ellip(order,Rp,Rs,wn);' ];
% dstr1 = '[b,a] =ellip(order,Rp,Rs,wn);';
% elseif type == 2 % highpass
% dstr = [dstr sprintf('\n') ...
% ' [b,a] = ellip(order,Rp,Rs,wn,''high'');' ];
% dstr1 = '[b,a] =ellip(order,Rp,Rs,wn,''high'');';
% elseif type == 3 % bandpass
% dstr = [dstr sprintf('\n') ...
% ' [b,a] = ellip(order,Rp,Rs,wn);' ];
% dstr1 = '[b,a] =ellip(order,Rp,Rs,wn);';
% elseif type == 4 % bandstop
% dstr = [dstr sprintf('\n') ...
% ' [b,a] = ellip(order,Rp,Rs,wn,''stop'');' ];
% dstr1 = '[b,a] =ellip(order,Rp,Rs,wn,''stop'');';
% end
% title_str = sprintf('Order %g Elliptic IIR Filter',order);
% end
if nargout > 0
varargout{1} = title_str;
end
if titleFlag
if nargout > 1
varargout{2} = 0;
end
return
end
%eval(dstr1,'b=yhzfiltdes(''error'');')
if isstr(b) % an error or user interruption occurred!
set(findobj(fig,'tag','designnow'),'userdata',dstr)
title_str = b;
errflag = 1; % set error flag
else
% ud.designstr = dstr;
ud.num = b;
ud.den = a;
set(fig,'userdata',ud)
errflag = 0; % set error flag
end
if nargout > 1
varargout{2} = errflag;
end
%------------------------------------------------------------------------
% yhzfiltdes('help')
% Callback of help button in toolbar
case 'help'
fig = gcf;
ud = get(fig,'userdata');
if ud.pointer ~= 2 % if not in help mode
% enter help mode
saveEnableControls = [ud.ht.FsHndl];
ax = [ud.ht.ax1 ud.toolbar.toolbar];
titleStr = 'Filter Designer Help';
helpFcn = 'yhzfdhelpstr';
yhzspthelp('enter',fig,saveEnableControls,ax,titleStr,helpFcn)
else
yhzspthelp('exit')
end
%switch action
case 'mdown'
% -------------------------------------------------------------------------
% yhzfiltdes('mdown',whichline)
% Mouse down function for line objects
% whichline == 1 --> passband click
% whichline == 2 --> stopband click
% whichline == 3 --> click on frequency response line - pan if ud.pointer=0
% Depending on pointer type, set up different motion and up callbacks
%
ud = get(gcf,'userdata');
pt = get(ud.ht.ax1,'currentpoint');
if nargin == 1
return
end
if varargin{1} == 3 % button down of frequency response line
if ~Localjustzoom(gcf)
switch ud.pointer
case 0 % panfcn
invis = [ud.ht.specline1 ud.ht.specline2 ud.ht.magline];
xd = get(ud.ht.magline,'xdata');
yd = get(ud.ht.magline,'ydata');
n = length(yd);
ind = floor((0:99)*n/100) + 1;
set(ud.ht.panfcnline,'visible','on',...
'xdata',xd(ind),'ydata',yd(ind))
panfcn('Ax',ud.ht.ax1,...
'Bounds',ud.limits,...
'Immediate',1,...
'Invisible',invis);
set(ud.ht.panfcnline,'visible','off')
case 2 % help mode - call fdhelp
yhzspthelp('exit','response')
end
end
return
end
switch yhzfdregion(gcf,pt(1,1:2))
case 1 % mouse over band edge
set(ud.ht.specline1,'erasemode','xor')
%set passband line's erase mode to xor
set(ud.ht.specline2,'erasemode','xor') % stop band
spec = ud.specs;
f = spec.f;
pt = get(ud.ht.ax1,'currentpoint'); % in data units, top axes
pt = pt(1,1:2);
[dum,e] = min(abs(f-pt(1))); % e is the index of the closest band edge
set(gcf,'windowbuttonmotionfcn',['sbswitch(''yhzfdedrag'',' ...
num2str(e) ')'])
set(gcf,'windowbuttonupfcn',['sbswitch(''yhzfdedrag'',' num2str(e) ',0)'])
case 2 % mouse over band
if varargin{1} == 1
set(ud.ht.specline1,'erasemode','xor')
%set passband line's erase mode to xor
else
set(ud.ht.specline2,'erasemode','xor') % stop band
end
set(gcf,'windowbuttonmotionfcn',...
['sbswitch(''yhzfdrdrag'',' num2str(varargin{1}) ')'])
set(gcf,'windowbuttonupfcn',...
['sbswitch(''yhzfdrdrag'',' num2str(varargin{1}) ',0)'])
end
case 'error'
% -------------------------------------------------------------------------
% estr = yhzfiltdes('error')
% catch callback for filter design - in case of error in design
% or user interruption
% estr returns a string describing the error
%
varargout{1} = 'Filter design was interrupted or failed due to an error.';
otherwise
% -------------------------------------------------------------------------
% yhzfiltdes(action)
% default action - not recognized - show warning in command window
%
disp(sprintf( ...
'yhzfiltdes: action string ''%s'' not recognized, no action taken.',action))
end
function label = uniqueDefaultLabel(labelList,defaultLabel)
% Inputs:
% labelList - list of strings
% defaultLabel - string; such as 'sig','filt'
% Output:
% label - unique identifier such as 'sig1', 'sig2'
i=1;
label = [defaultLabel sprintf('%.9g',i)];
while ~isempty(findcstr(labelList,label))
i=i+1;
label = [defaultLabel sprintf('%.9g',i)];
end
function str = Localfdspecstr(spec)
%Localfdspecstr Returns string for multi-line editbox of Filter Design tool.
% The edit box contains band edges and ripple
% Copyright (c) 1988-97 by The MathWorks, Inc.
% $Revision: 1.3 $
% Tom Krauss, 1/19/96
% $Revision: 1.3 $
if spec.FType <= 2
str = sprintf('%4.3g\n%4.3g\n\n%4.3g\n%4.3g',spec.f(1),spec.f(2),...
spec.Rp,spec.Rs);
else
str = sprintf('%4.3g%s%4.3g\n%4.3g%s%4.3g\n\n%4.3g\n%4.3g',spec.f(1,1),' ',...
spec.f(1,2),spec.f(2,1),' ',spec.f(2,2),...
spec.Rp,spec.Rs);
%str = sprintf('%g\n%g\n%g\n%g\n\n%g\n%g',spec.f(1),spec.f(2),...
% spec.f(3),spec.f(4),spec.Rp,spec.Rs);
end
function flag = Localjustzoom(fig)
%JUSTZOOM Determines whether the current buttondownfcn follows a
% zoom windowbuttondownfcn for the signal browser in figure fig.
% Meant to be called by any buttondownfcn (eg, pickfcn, ruldown,
% pandown) that might be called AFTER the windowbuttondownfcn.
% Writes userdata and clears flag if so.
% Copyright (c) 1988-97 by The MathWorks, Inc.
% $Revision: 1.3 $
if nargin < 1
fig = gcf;
end
ud = get(fig,'userdata');
if isequal(ud.justzoom,get(fig,'currentpoint'))
flag = 1;
ud.justzoom = [0 0];
set(fig,'userdata',ud)
else
flag = 0;
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -