⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yhzfiltdes.m

📁 信号实验常用的simulink模型和mfile,可直接在matlan下运行。
💻 M
📖 第 1 页 / 共 4 页
字号:
function varargout=yhzfiltdes(action,varargin);
%yhzfiltdes Filter Designer.
%   This graphical tool allows you to design lowpass, highpass, bandpass, 
%   and bandstop digital filters.
%
%   Type 'sptool' to start the Signal Processing GUI Tool and access
%   the Filter Designer.
%
%   [B,A] = yhzfiltdes('getfilt') returns the numerator coefficients in B 
%   and denominator coefficients in A of the current filter.
%
%   [B,A,Fs] = yhzfiltdes('getfilt') returns the sampling frequency Fs also.
%
%   See also SPTOOL, SIGBROWSE, FILTVIEW, SPECTVIEW.

%   Author: T. Krauss, 3/7/94
%   Copyright (c) 1988-97 by The MathWorks, Inc.
%   $Revision: 1.39 $

%if nargin==0 
    %if isempty(findobj(0,'tag','sptool'))
    %    disp('Type ''sptool'' to start the Signal GUI.')
    %else
    %    disp('To use the Filter Designer, click on the ''Design New'' button')
    %    disp('under the ''Filters'' column in the ''SPTool''.')
    %end
    %return
%    yhzfdesini;
%end

%tempfilt = [];

if nargin == 0
    %if ~isstr(action)
    %    tempfilt = action;
        action = 'initialize';
    %end
end

switch action
case 'initialize'
% -------------------------------------------------------------------------

    %if ~isempty(tempfilt)
    %    fdinit(tempfilt)
    %else
        yhzfdesini;
    %end
    
    shh=get(0,'showhiddenhandles');
    set(0,'showhiddenhandles','on')
    ud = get(gcf,'userdata');
 
    set(ud.toolbar.zoomgroup,'visible','on')
    set(ud.toolbar.helpgroup,'visible','on')
    set(0,'showhiddenhandles',shh)

case 'changefir'
%--------------------------------------------------------------------------
% yhzfiltdes('changefir')
%  callback of fir choose radiobutton
%
    ud=get(gcf,'UserData');
    if ~get(ud.ht.changefir,'value')
       set(ud.ht.changefir,'value',1);
       return
    end
    ud.specs.ir=[1,0];
    set(ud.ht.changeiir,'value',0);
    set([ud.ht.firNHndl,ud.ht.firmethod],'enable','on');
    set([ud.ht.iirmethod1,ud.ht.iirmethod2,ud.ht.iirorderHndl],'enable','off');
    set(gcf,'UserData',ud);
    yhzfiltdes('specchange');
    yhzfdzoom('zoomout',gcf)
case 'changeiir'
%--------------------------------------------------------------------------
% yhzfiltdes('changeiir')
%  callback of iir choose radiobutton
%
    ud=get(gcf,'UserData');
    if ~get(ud.ht.changeiir,'value')
       set(ud.ht.changeiir,'value',1);
       return
    end
    if ud.specs.f(1,:)==ud.specs.f(2,:)
       disp('请先输入临界频率,fp,fs');
       set(gco,'value',0);
       return
    end   
    ud.specs.ir=[0,1];
    set(ud.ht.changefir,'value',0);
    set([ud.ht.firNHndl,ud.ht.firmethod],'enable','off');
    set([ud.ht.iirmethod1,ud.ht.iirmethod2,ud.ht.iirorderHndl],'enable','on');
    set(gcf,'UserData',ud);
    yhzfiltdes('specchange');
    yhzfdzoom('zoomout',gcf)

case 'changeiirmethod1'
%--------------------------------------------------------------------------
% yhzfiltdes('changeiirmethod1')
%   callback of IIR FILTER DESIGN METHOD popup 1
%
    i1=get(gcbo,'value');
    % 1: buttor 2: cheby1 3: cheby2
    ud = get(gcf,'UserData');
    i11=ud.specs.iirmethod(1);
    if i1~=i11
        ud.specs.iirmethod(1) = i1;
        %ud.specs.specialparamsmode = 1;  % use 'auto' mode for special parameters
        set(gcf,'userdata',ud);
        yhzfiltdes('specchange');  % redesign or invalidate filter
        yhzfdzoom('zoomout',gcf);
    end
case 'changeiirmethod2'
%--------------------------------------------------------------------------
% yhzfiltdes('changeiirmethod2')
%   callback of IIR FILTER DESIGN METHOD popup 2
%
    i2=get(gcbo,'value');
    % 1: buttor 2: cheby1 3: cheby2
    ud = get(gcf,'UserData');
    i22=ud.specs.iirmethod(2);
    if i2==i22 | ud.specs.FType~=1;
       set(ud.ht.iirmethod2,'value',i22);
       return;
    else
        ud.specs.iirmethod(2) = i2;
        %ud.specs.specialparamsmode = 1;  % use 'auto' mode for special parameters
        set(gcf,'userdata',ud);
        yhzfiltdes('specchange');  % redesign or invalidate filter
        yhzfdzoom('zoomout',gcf);
    end
        
case 'changefirmethod'
% -------------------------------------------------------------------------
% yhzfiltdes('changemethod')
%   callback of FILTER DESIGN METHOD popup
%
    v = get(gco,'value');  % 1 = boxcar, 2 = hanning,3 = hamming,
                           % 4 = blackman 5 = kaiser, 6 = FS
    ud = get(gcf,'userdata');
    vv = ud.specs.firmethod;   % old value
    if vv~=v
        %if v == 2   % FIRLS - turn off Auto order selection mode
        %    set(ud.ht.btn1Hndl,'enable','off')
        %    set(ud.ht.btn1Hndl,'value',0);
        %    set(ud.ht.btn2Hndl,'value',1)
        %    ud.specs.ordermode = 2;
        %else   % ALL OTHERS - change back to auto order selection mode
        %    set(ud.ht.btn1Hndl,'enable','on')
        %    set(ud.ht.btn1Hndl,'value',1);
        %    set(ud.ht.btn2Hndl,'value',0)
        %    ud.specs.ordermode = 1;
        %end
        ud.specs.firmethod = v;
        %ud.specs.specialparamsmode = 1;  % use 'auto' mode for special parameters
        set(gcf,'userdata',ud);
        yhzfiltdes('specchange');  % redesign or invalidate filter
        yhzfdzoom('zoomout',gcf);
        %if ( ((v<=2)&(vv>=3)) | ((vv<=2)&(v>=3)) | ...
        %     ((v==3)&(vv>=4)) | ((vv==3)&(v>=4)) )
        %    yhzfiltdes('copyspecials')  % copy the estimated special parameters
               % to the 'set' special parameters
               % if it doesn't make sense to use the old 'set' params anymore
        %end
    end

case 'changeFType'  
% -------------------------------------------------------------------------
% yhzfiltdes('changeFType')
%   callback of FILTER TYPE popup
%   POPUP value: 1 = lp, 2 = hp, 3 = bp,%% 4 = bs
%
    ud = get(gcf,'UserData');
    Fs = ud.specs.Fs;     % sampling frequency
    v = get(ud.ht.FType,'value');  
    % v contains new filter type: 1 = lp, 2 = hp, 3 = bp, 4 = bs
    oldv = ud.specs.FType;
    if v~=oldv
        f = ud.specs.f;
        str = get(ud.ht.specHndl,'string');      % old string
        if v == 1  % to lowpass 
            if oldv == 3   % from bandpass
               f=[f(2,1);f(1,1)];
               %f(1:2) = [];
            elseif oldv == 2  % from highpass %%bandstop
               f=[f(2);f(1)];
            end
        elseif v == 2  % to highpass 
            if oldv == 3   % from bandpass
               f=[f(1,1);f(2,1)];
               %f(3:4) = [];
            elseif oldv == 1  % from LP %%bandstop
               f=[f(2);f(1)];
               %f(1:2) = [];
            end
        elseif v == 3  % to bandpass
            if oldv == 1   % from lowpass
                %splitwidth = .15;
                %xd = [0 f(1)];
                %dxd = diff(xd);
                %xd = xd(1)+dxd/2 + [-dxd*splitwidth/2;  dxd*splitwidth/2];
                ffp1=min(f(2),Fs/2-f(2));
                ffp2=max(f(2),Fs/2-f(2));
                ffs1=ffp1/2;
                ffs2=Fs/2-ffs1;
                f = [ffp1,ffp2;ffs1,ffs2];
            elseif oldv == 2  % from highpass
                %splitwidth = .15;
                %xd = [f(1) Fs/2];
                %dxd = diff(xd);
                %xd = xd(1)+dxd/2 + [-dxd*splitwidth/2;  dxd*splitwidth/2];
                %f = [ f(1:2); xd];
                ffp1=min(f(1),Fs/2-f(1));
                ffp2=max(f(1),Fs/2-f(1));
                ffs1=ffp1/2;
                ffs2=Fs/2-ffs1;
                f = [ffp1,ffp2;ffs1,ffs2];
                %f = [f(1),Fs/2-f(1);f(2),Fs/2-f(2)];
            end
        %elseif v == 4  % to bandstop
        %    if oldv == 1   % from lowpass
        %        splitwidth = .15;
        %        xd = [f(1) Fs/2];
        %        dxd = diff(xd);
        %        xd = xd(1)+dxd/2 + [-dxd*splitwidth/2;  dxd*splitwidth/2];
        %        f = [ f(1:2); xd];
        %    elseif oldv == 2  % from highpass
        %        splitwidth = .15;
        %        xd = [0 f(1)];
        %        dxd = diff(xd);
        %        xd = xd(1)+dxd/2 + [-dxd*splitwidth/2;  dxd*splitwidth/2];
        %        f = [ xd;  f];
        %    end
        end
        ud.specs.f = f;
        ud.specs.FType = v;
        if v~=1 & ud.specs.iirmethod(2)==2
           ud.specs.iirmethod(2)=1;
           set(ud.ht.iirmethod2,'value',1)
        end           
        set(ud.ht.specHndl,'String',Localfdspecstr(ud.specs))
        %ud.specs.specialparamsmode = 1;  % use 'auto' mode for special parameters
        set(gcf,'userdata',ud)
        yhzfiltdes('specchange')
        yhzfdzoom('zoomout',gcf);
        % if ( ((v<=2)&(oldv>=3)) | ((oldv<=2)&(v>=3)) )
       %     yhzfiltdes('copyspecials')
       % end
    end

case 'setfirN'
% yhzfiltdes('setfirN')
%    callback for firN edit uicontrol
%
   ud=get(gcf,'UserData');
   oldN=ud.specs.firN;
%   oldbeta=ud.specs.kaiserbeta;
   NStr=get(ud.ht.firNHndl,'string');
  % if oldN==str2num(NStr)
  %    return;
  % end   
   %if ud.specs.firmethod<=5  %the single line for N, window
   sameFlag=1;
   if ud.specs.firmethod==5  %kaiser
         oldbeta=ud.specs.kaiserbeta;
         %ent=sprintf('\n');
         %entind=findstr(NStr,ent);
         N=str2num(NStr(1,:)); %entind-1));
         beta=str2num(NStr(2,:)); %entind+1:end));
         if beta~=oldbeta
            ud.specs.kaiserbeta=beta;
            sameFlag=0;
         end
      else  N=str2num(NStr);
      end   
      if ~rem(N,2) % the length of window is even
         %disp('窗口大小需为奇数!');
         N=N+1;
         if ud.specs.firmethod==5
            NStr=sprintf('%s\n%s',num2str(N),num2str(beta));
         else 
            NStr=sprintf('%s',num2str(N));
         end   
         set(ud.ht.firNHndl,'string',NStr);
       end      
       if N~=oldN %| ud.specs.firmethod~=5)
          sameFlag=0;
       end
       if sameFlag | fix(N)~=N | N<=0
          disp('请重新输入');
          return;
       end   
       ud.specs.firN=N;
 set(gcf,'UserData',ud);
 yhzfiltdes('specchange');
 yhzfdzoom('zoomout',gcf)

   %elseif
case 'setiirorder'
%------------------------------------------------------------------------   
% yhzfiltdes('setiirorder');
% call back of iirorder edit uicontrol
%
   newOrd=str2num(get(gcbo,'string'));
   ud=get(gcf,'UserData');
   oldOrd=ud.specs.iirorder;
   if oldOrd==newOrd | fix(newOrd)~=newOrd | newOrd<=0
      set(ud.ht.iirorderHndl,'string',num2str(oldOrd));
      return;
   end
   ud.specs.iirorder = newOrd;
   set(gcf,'UserData',ud);
   yhzfiltdes('specchange');
   yhzfdzoom('zoomout',gcf)

case 'specchange'
% -------------------------------------------------------------------------
% yhzfiltdes('specchange',fig)
%   spec has changed - either   redesign filter   OR   invalidate old filter
%   (depending on 'Update Automatically' mode)
%   also, CLEARS 'saved' FLAG
%   if fig is present, uses that figure number - else uses gcf
%
    if nargin < 2
        fig = gcf;
    else
        fig = varargin{1};
    end
    ud = get(fig,'userdata');
    wbmf = get(fig,'windowbuttonmotionfcn');
    set(fig,'windowbuttonmotionfcn','')  % temporarily clear this
    setptr(fig,'watch');
    yhzfiltdes('drawspeclines',fig)
    % clear 'saved' flag here:
    ud.saved = 0;
    set(fig,'userdata',ud)
    %yhzfiltdes('fixlabels',fig)
    if gcbo~=ud.ht.firNHndl & gcbo~=ud.ht.iirorderHndl & ud.needEst
       yhzfiltdes('estimate',fig)
    else
       ud.needEst=1;
    end   
    FType = ud.specs.FType;     %   1 = lp, 2 = hp, 3 = bp, 4 = bs
    %method = ud.specs.method; % filter design method
    %if get(ud.ht.btn2Hndl,'value')   % in manual order select mode
    %    order = ud.specs.order.manual;
    %    if method <= 3   % REMEZ, FIRLS, OR KAISER
    %        if type == 2 | type == 4  % high pass or band stop
    %            if rem(order(1),2)   % can't use odd order for these types 
    %                order(1) = order(1) + 1;
    %                ud.specs.order.manual = order;
    %                set(fig,'userdata',ud)
    %            end
    %        end
    %    end
    %end
 %   if strcmp(get(ud.ht.Omenu1,'checked'),'on')   % if autodesign mode
        set(ud.ht.magline,'linestyle',':')
        %set(get(ud.ht.ax1,'title'),'string','Designing filter ...')
        %xstr = get(get(ud.ht.ax1,'xlabel'),'string');
        %set(get(ud.ht.ax1,'xlabel'),'string',...
        %    '(Type Control-C in Command window to interrupt)')
        drawnow    
        % if the estimated order is too big, DO SOMETHING HERE!
        %elseif (n>max_n)|(isnan(n))|(isinf(n))
        %set(ord2Hndl,'UserData',reasonable_n,'string',num2str(reasonable_n))
        %set(btn1Hndl,'value',0,'userdata',2)  % switch radio buttons
        %set(btn2Hndl,'value',1)
        %order = get(ord2Hndl,'UserData'); % use specified Filter order
        %end
        [title_str,errflag] = yhzfiltdes('design',fig);
        %set(get(ud.ht.ax1,'title'),'string',[ud.label ': ' title_str])
        %set(get(ud.ht.ax1,'xlabel'),'string',xstr)
        if ~errflag
            yhzfiltdes('response',fig)
            set(ud.ht.magline,'linestyle','-')
        else
           % set(ud.ht.designmenu,'enable','on') 
        end
    
        %ud = get(fig,'userdata');
        %tempfilt = sptool('Filters',0);  % get current filter
        %tempfilt.tf.num = ud.num;        % assign new num,den and specs
        %tempfilt.tf.den = ud.den;
        %tempfilt.label = ud.label;
        %tempfilt.specs = ud.specs;
        %tempfilt.zpk = [];
        %tempfilt.ss = [];
        %tempfilt.sos = [];
        %ud.filt = tempfilt;
        set(fig,'userdata',ud)
        %sptool('import',tempfilt,1)
        % update filter viewer here!

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -