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

📄 yhzfiltdes.m

📁 信号实验常用的simulink模型和mfile,可直接在matlan下运行。
💻 M
📖 第 1 页 / 共 4 页
字号:
        %    wn_est = wn_est*Fs/oldFs;
        %    wn_sel = wn_sel*Fs/oldFs;
        %end
        %ud.specs.special.auto = wn_est;
        %ud.specs.special.manual = wn_sel;
        
        ud.filt.specs = ud.specs;
        
        % save the user data
        set(fig,'userdata',ud)

        % now ready to update 'Special Parameters' dialog box
        %fdspdlg('updateparamdialog',fig)
        
        % poke filt structure back into SPTool
        %sptool('import',ud.filt)
  else
    set(ud.ht.FsHndl,'string',ud.prefs.Fs)
    figure(fig)
  end


case 'setfilt'
%------------------------------------------------------------------------
% yhzfiltdes('setfilt',filt,fig)
% Sets the Filter Designer to the given filter.
% Inputs:
%    filt - filter structure; can be of type 'design' or 'imported'
%    fig - figure handle of filter designer
filt = varargin{1};
    fig = varargin{2};
    ud = get(fig,'userdata');
    %if isempty(filt) | ~strcmp(filt.type,'design')
    %   % disable yhzfiltdes
    %   disableList = [ud.ht.methodHndl ud.ht.typeHndl ud.ht.specHndl ...
    %                  ud.ht.ord1Hndl ud.ht.ord2Hndl ud.ht.btn1Hndl ...
    %                  ud.ht.btn2Hndl];
    %   set(disableList,'enable','off')
    %   set([ud.ht.magline ud.ht.specline1 ...
    %        ud.ht.specline2],'visible','off')
    %   if isempty(filt)
    %       set(get(ud.ht.ax1,'title'),'string','<no filter selected>')
    %       ud.label = '';
    %       set(ud.ht.FsHndl,'string','-')
    %   else
    %       ud.label = filt.label;
    %       ud.num = filt.tf.num;
    %       ud.den = filt.tf.den;
    %       set(get(ud.ht.ax1,'title'),'string',[ud.label ': <not a design>'])
    %       set(ud.ht.FsHndl,'string',sprintf('%.9g',filt.Fs))
    %   end
    %   ud.specs = [];
    %   ud.filt = filt;
    %   set(fig,'userdata',ud)
    %else
       % focus on selected filter
       ud.label = filt.label;
       ud.num = filt.tf.num;
       ud.den = filt.tf.den;
       filt.specs.Fs = filt.Fs;
       oldfilt = ud.filt;
       oldfilt.Fs = filt.Fs;
       ud.filt = filt;
       set(fig,'userdata',ud)
       if isequal(filt,oldfilt)   % ONLY Fs HAS CHANGED
           set(ud.ht.FsHndl,'string',sprintf('%.9g',filt.Fs))
           yhzfiltdes('setFs',fig)
       else
           yhzfiltdes('setspecs',filt.specs,0,fig)
           yhzfdzoom('zoomout',fig)
       end
    %end
case 'setspecs'
% -------------------------------------------------------------------------
% yhzfiltdes('setspecs',specs,designFlag)
% yhzfiltdes('setspecs',specs,designFlag,fig)
%   set specs according to first input argument (specifications structure)
% Inputs:
%    specs -   specifications structure
%    designFlag - 1 ==> redesign filter (default), 0 ==> just recompute response
%    fig - optional; defaults to gcf
    if nargin < 3
        designFlag = 1;
    else
        designFlag = 0;
    end
    if nargin < 4
        fig = gcf;
    else
        fig = varargin{3};
    end
    ud = get(fig,'userdata');

    newspecs = varargin{1};
    %if ~isfdt(newspecs)
    %    errordlg('Load error: invalid specifications structure');
    %    return
    %end

    % set 'saved' flag here:
    ud.saved = 1; 

    ud.specs = newspecs;
    set(fig,'userdata',ud)
    
    %oldFs = get(ud.ht.FsHndl,'userdata');  % old value of Fs sampling frequency
    Fs = ud.filt.Fs;
    %set(ud.ht.FsHndl,'string',sprintf('%.9g',Fs),'userdata',Fs)
    
    % update the "Full View" limits
    ud.limits.xlim = [0 Fs/2];
        
    ud.specs.Fs = Fs;        
    % save the user data
    set(fig,'userdata',ud)
                        
    set(ud.ht.FType,'value',newspecs.FType,'enable','on');
    set(ud.ht.changefir,'value',newspecs.ir(1));
    set(ud.ht.changeiir,'value',newspecs.ir(2));
    set(ud.ht.firmethod,'value',newspecs.firmethod);
    %set(ud.ht.firNHndl,'string',newspecs.firN);
    %set(ud.ht.iirorderHndl,'string',num2str(newspecs.iirorder));
    set(ud.ht.iirmethod1,'value',newspecs.iirmethod(1));
    set(ud.ht.iirmethod2,'value',newspecs.iirmethod(2));
    if get(ud.ht.changefir,'value')
       set([ud.ht.iirorderHndl,ud.ht.iirmethod1,ud.ht.iirmethod2],'enable','off');
    else
       set([ud.ht.firmethod,ud.ht.firNHndl],'enable','off');
    end   
   % set(ud.ht.methodHndl,'value',newspecs.method,'enable','on')
   % set(ud.ht.typeHndl,'value',newspecs.type,'enable','on')
   % set(ud.ht.specHndl,'string',Localfdspecstr(newspecs),'enable','on')
    
    %if ud.specs.ordermode == 1
    %    set(ud.ht.btn1Hndl,'value',1)
    %    set(ud.ht.btn2Hndl,'value',0)
    %else
    %    set(ud.ht.btn1Hndl,'value',0)
    %    set(ud.ht.btn2Hndl,'value',1)
    %end
    
    %if ud.specs.method == 2  % FIRLS
    %    set(ud.ht.ord1Hndl,'string','-','enable','on')
    %    set(ud.ht.btn1Hndl,'enable','off')
    %else
    %    set(ud.ht.ord1Hndl,'string',newspecs.order.auto,'enable','on')
    %    set(ud.ht.btn1Hndl,'enable','on')
    %end
    %set(ud.ht.ord2Hndl,'string',newspecs.order.manual,'enable','on')
    %set(ud.ht.btn2Hndl,'enable','on')    
    
    set([ud.ht.magline ud.ht.specline1 ud.ht.specline2],'visible','on')
    
    %fdspdlg('updateparamdialog',fig)

    %if designFlag
        yhzfiltdes('specchange',fig)
    %else
    %    yhzfiltdes('response',fig)              % compute frequency response
    %    title_str = yhzfiltdes('design',fig,1);
    %    set(get(ud.ht.ax1,'title'),'string',[ud.label ': ' title_str])
    %    yhzfiltdes('drawspeclines',fig)
        %yhzfiltdes('fixlabels',fig)
    %end
case 'drawspeclines'
% -------------------------------------------------------------------------
% yhzfiltdes('drawspeclines',fig)
%   set the x- and y-data of the specifications lines
%   if fig is not present, uses the gcf, else uses fig
%
    if nargin > 1
        fig = varargin{1};
    else
        fig = gcf;
    end
    ud = get(fig,'userdata');  % handle table
    FType = ud.specs.FType;   % 1 = lp, 2 = hp, 3 = bp, %%4 = bs
    Fs = ud.specs.Fs;          % sampling frequency
    %specHndl = ud.ht.specHndl;     % filter specs edit box
    %methodHndl = ud.ht.methodHndl;  % filter design method popup
    specline1 = ud.ht.specline1;   % passband ripple specifications line
    specline2 = ud.ht.specline2;   % stopband attenuation specifications line

    specs = ud.specs;
    Rp = specs.Rp;
    Rs = specs.Rs;
    if specs.ir(1)== 1    % FIR filter
        dev = [ (10^(Rp/20)-1)/(10^(Rp/20)+1)  10^(-Rs/20) ];
        above = 20*log10(1+dev(1)); below = 20*log10(1-dev(1));
    else   % IIR filter 
        above = 0;   below = -Rp;
    end
    fp = specs.f(1,:);
    fs = specs.f(2,:);
    if FType == 1  % lp 
        set(specline1,'xdata',[0 fp NaN 0 fp],...
                'ydata',[above above NaN below below])
        set(specline2,'xdata',[fs Fs/2],'ydata',[-Rs -Rs])
    elseif FType == 2  % hp
        set(specline1,'xdata',[fp Fs/2 NaN fp Fs/2],...
                'ydata',[above above NaN below below])
        set(specline2,'xdata',[0 fs],'ydata',[-Rs -Rs])
    elseif FType == 3  % bp
       % f3 = specs.f(3);  f4 = specs.f(4);
        set(specline1,'xdata',[fp(1) fp(2) NaN fp(1) fp(2)],...
                'ydata',[above above NaN below below])
        set(specline2,'xdata',[0 fs(1) NaN fs(2) Fs/2],...
                'ydata',[-Rs -Rs NaN -Rs -Rs])
    %elseif type == 4  % bs
    %    f3 = specs.f(3);  f4 = specs.f(4);
    %    set(specline1,'xdata',[0 f1 NaN 0 f1 NaN f4 Fs/2 NaN f4 Fs/2],...
    %     'ydata',[above above NaN below below NaN above above NaN below below])
    %    set(specline2,'xdata',[f2 f3],'ydata',[-Rs -Rs])
    end
    
 case 'response'
% -------------------------------------------------------------------------
% yhzfiltdes('response',fig)
%   compute frequency response of filter whose coefficients are stored in
%   ud.num and ud.den, and update ud.ht.magline's xdata and ydata
%
    if nargin > 1
        fig = varargin{1};
    else
        fig = gcf;
    end

   % get handles
    ud=get(fig,'Userdata');

   % initialize variables

    FType = ud.specs.FType;    %   1 = lp, 2 = hp, 3 = bp,%%4 = bs
    %method = ud.specs.method;    % filter design method
    Fs = ud.specs.Fs;     % sampling frequency
    spec = ud.specs;
    f1 = spec.f(1,:);
    f2 = spec.f(2,:);
    %if type > 2
    %    f3 = spec.f(3);
    %    f4 = spec.f(4);
    %end
    Rp = spec.Rp;
    Rs = spec.Rs;

    b = ud.num;
    a = ud.den;

    nfft = ud.prefs.nfft;
    emergencynfft = 2^nextpow2( 2*max(length(b),length(a)) );
    if nfft<emergencynfft
        %disp(sprintf(['%g is too few frequency points for this filter,'...
      %              ' using %g instead'],nfft, emergencynfft))
        nfft = emergencynfft;
    end
    [H,F] = freqz(b,a,nfft,Fs);
    magH = 20*log10(abs(H));
    set(ud.ht.magline,'xdata',F,'ydata',magH)
    %hold on;
case 'design'
% -------------------------------------------------------------------------
% [title_str,errflag] = yhzfiltdes('design',fig)
% [title_str,errflag] = yhzfiltdes('design',fig,titleFlag)
%   Design filter
%   JUST designs the filter and stores the coefficients in ud.num, ud.den
%   title_str is a string briefly describing the filter
%   errflag == 1 means an error occurred, errflag == 0 means no error
%   titleFlag - 1 ==> just return title (don't design), 0 ==> do design (default)
    if nargin < 2
        fig = gcf;
    else
        fig = varargin{1};
    end
    if nargin < 3
        titleFlag = 0;
    else
        titleFlag = varargin{2};
    end
    
  % get handles
    ud=get(fig,'Userdata');

  % initialize variables

    FType = ud.specs.FType;    %   1 = lp, 2 = hp, 3 = bp, %%4 = bs
    firmethod = ud.specs.firmethod;   % filter design method
    iirmethod1 = ud.specs.iirmethod(1);
    iirmethod2 = ud.specs.iirmethod(2);
    Fs = ud.specs.Fs;     % sampling frequency
    spec = ud.specs;
    fp = spec.f(1,:);
    fs = spec.f(2,:);
    %if type > 2
    %    f3 = spec.f(3);
    %    f4 = spec.f(4);
    %end
    Rp = spec.Rp;
    Rs = spec.Rs;
    
% %determine which order to use: estimated or specified
  %  if ud.specs.ordermode == 1    % use estimated order 
  %      order = spec.order.auto; 
  %  else             % use manually selected 
   %     order = spec.order.manual; 
   % end
    %if ud.specs.specialparamsmode == 1    % use estimated special parameters
    %    wn = spec.special.auto; 
    %else             % use manually selected special params
     %   wn = spec.special.manual; 
    %end

% Design filter
   % % 1 = REMEZ, 2 = FIRLS, 4 = BUTTER, 5 = CHEBY1, 6 = CHEBY2, 7 = ELLIP, 
   % %  3 = KAISER
   if spec.ir(1)   % FIR
      if firmethod<=5  %FIR-WINDOW
         winTypeS={'boxcar';'hanning';'hamming';'blackman';'kaiser'};
         winType=winTypeS(firmethod);
         %%elseif method == 3   % KAISER
         %fir1flag = 1;
         %if fir1flag 
             %dstr = [' window length = ' sprintf('%g',firN) ';'];
             if FType ~= 2   % low pass and band pass
             %   dstr = [dstr sprintf('\n') ...
             %      ' w = [' sprintf('%1.18e\n ',fp*2/Fs) '];'];
             %   dstr = [dstr sprintf('\n') ...
             %      ' wind = ',winType,'(window length+1,' sprintf('%1.18e',wn(1)) ');'];
            %dstr = [dstr sprintf('\n') ...
            %       ' b = fir1(order,w,wind,''noscale''); a = 1;' ];
               % wn=fp*2/Fs;
               if firmethod~=5
                  eval(['b=fir1(spec.firN-1,fp*2/Fs,',char(winType),'(spec.firN),''noscale'');']);
                  a = 1;
               else % Kaiser Window
                  eval(['b=fir1(spec.firN-1,fp*2/Fs,',...
                     char(winType),'(spec.firN,',num2str(spec.kaiserbeta),'),''noscale'');']);
                  a = 1;
               end;   
             else %type == 2   % high pass
            %dstr = [dstr sprintf('\n') ...
            %       ' w = [' sprintf('%1.18e\n ',wn(2)*2/Fs) '];'];
            %dstr = [dstr sprintf('\n') ...
            %       ' wind = kaiser(order+1,' sprintf('%1.18e',wn(1)) ');'];
            %dstr = [dstr sprintf('\n') ...
            %       ' b = fir1(order,w,''high'',wind,''noscale''); a = 1;' ];
               %dstr1 = ['b = fir1(order,wn(2)*2/Fs,''high'',' ...
               %      'kaiser(order+1,wn(1)),''noscale''); a = 1;'];
               if firmethod~=5
                  eval(['b=fir1(spec.firN-1,fp*2/Fs,''high'',',...
                     char(winType),'(spec.firN),''noscale'');']);
                  a = 1;
               else % Kaiser Window
                  b=fir1(spec.firN-1,fp*2/Fs,'high',kaiser(spec.firN,spec.kaiserbeta),'noscale');
                  a = 1;
               end; 
              %elseif type == 3   % band pass
            %dstr = [dstr sprintf('\n') ...
            %       ' w = [' sprintf('%1.18e\n ',wn(2:3)*2/Fs) '];'];
            %dstr = [dstr sprintf('\n') ...
            %       ' wind = kaiser(order+1,' sprintf('%1.18e',wn(1)) ');'];
            %dstr = [dstr sprintf('\n') ...
            %       ' b = fir1(order,w,wind,''noscale''); a = 1;' ];
            %dstr1 = ['b = fir1(order,wn(2:3)*2/Fs,kaiser(order+1,wn(1)),''noscale'');'...
            %         'a = 1;'];
        %elseif type == 4   % band stop
        %    dstr = [dstr sprintf('\n') ...
         %          ' w = [' sprintf('%1.18e\n ',wn(2:3)*2/Fs) '];'];
         %   dstr = [dstr sprintf('\n') ...
         %          ' wind = kaiser(order+1,' sprintf('%1.18e',wn(1)) ');'];
         %   dstr = [dstr sprintf('\n') ...
         %          ' b = fir1(order,w,''stop'',wind,''noscale''); a = 1;' ];
         %   dstr1 = ['b = fir1(order,wn(2:3)*2/Fs,''stop'',' ...
         %            'kaiser(order+1,wn(1)),''noscale''); a = 1;'];
              end
              title_str = sprintf('Order %g FIR Filter designed with Window %s',...
                                       spec.firN,char(winType));
           %else   %if fir1flag
       %dstr = [' order = ' sprintf('%g',order) ';'];
        %if type == 1    % low pass
         %   dstr = [dstr sprintf('\n') ...
         %          ' w = [0 ' sprintf('%1.18e\n ',wn(2)*2/Fs) ' ' ...
         %            sprintf('%1.18e\n ',wn(2)*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]).*wind; a = 1;' ];
         %   dstr1 = ['b = firls(order,[0 wn([2 2])*2/Fs 1],[1 1 0 0])'...
         %            '.*kaiser(order+1,wn(1))''; a = 1;'];
        %elseif type == 2   % high pass
        %    dstr = [dstr sprintf('\n') ...
        %           ' w = [0 ' sprintf('%1.18e\n ',wn(2)*2/Fs) ' ' ...
        %             sprintf('%1.18e\n ',wn(2)*2/Fs) ' 1];'];
        %    dstr = [dstr sprintf('\n') ...
        %           ' wind = kaiser(order+1,' sprintf('%1.18e',wn(1)) ');'];

⌨️ 快捷键说明

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