www.gusucode.com > 利用MATLAB GUI设计滤波器界面,可以设计IIR滤波器 > AFD/GuiFilterOrder.m

    function varargout = GuiFilterOrder(varargin)
% GUIFILTERORDER M-file for guiFilterOrder.fig
%      GUIFILTERORDER, by itself, creates a new GUIFILTERORDER or raises the existing
%      singleton*.
%
%      H = GUIFILTERORDER returns the handle to a new GUIFILTERORDER or the handle to
%      the existing singleton*.
%
%      GUIFILTERORDER('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUIFILTERORDER.M with the given input arguments.
%
%      GUIFILTERORDER('Property','Value',...) creates a new GUIFILTERORDER or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before guiFilterOrder_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to guiFilterOrder_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%

% Last Modified by GUIDE v2.5 04-Jan-2003 22:52:45

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GuiFilterOrder_OpeningFcn, ...
                   'gui_OutputFcn',  @GuiFilterOrder_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before guiFilterOrder is made visible.
function GuiFilterOrder_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to guiFilterOrder (see VARARGIN)

% Choose default command line output for guiFilterOrder

global strFilterObject
if isempty(strFilterObject)
   load('matlab');
   disp([mfilename ' called in debug mode using matlab.mat datafile'])
end

% set the figure's title
set(handles.figFilterOrder,'Name','Analog Filter Designer')

% set fc
fFc = strFilterObject.fFc;
if fFc >= 1e6
    set(handles.uipmFc,'Value',4)
    set(handles.uitxFc, 'String', num2str(fFc/1e6))
elseif fFc >= 1000
    set(handles.uipmFc,'Value',3)
    set(handles.uitxFc, 'String', num2str(fFc/1e3))
elseif fFc >= 1
    set(handles.uipmFc,'Value',2)
    set(handles.uitxFc, 'String', num2str(fFc))
elseif strFilterObject > 0
    set(handles.uipmFc,'Value',1)
    set(handles.uitxFc, 'String', num2str(fFc*1000))
else
    error(['Fc in ' mfilename ' is negative'])
end

% set f1
switch strFilterObject.sPurpose
    case 'Lowpass'
        fF1 = 5*fFc; % so f1 is in the cutoff band
    case 'Highpass'
        fF1 = fFc/5; % so f1 is in the cutoff band
    otherwise error(['Unidentified sPurpose of strFilterObject in ' mfilename])
end
if fF1 >= 1e6
    set(handles.uipmF1,'Value',4)
    set(handles.uitxF1, 'String', num2str(fF1/1e6))
elseif fF1 >= 1000
    set(handles.uipmF1,'Value',3)
    set(handles.uitxF1, 'String', num2str(fF1/1e3))
elseif fF1 >= 1
    set(handles.uipmF1,'Value',2)
    set(handles.uitxF1, 'String', num2str(fF1))
elseif strFilterObject > 0
    set(handles.uipmF1,'Value',1)
    set(handles.uitxF1, 'String', num2str(fF1*1000))
else
    error(['fF1 in ' mfilename ' is negative'])
end

% set the gain at f1
set(handles.uitxGain,'String','-60')

% set the status bar
set(handles.uitxStatus,'String','Choose stop/passband attenuation and press Calculate');


function varargout = GuiFilterOrder_OutputFcn(hObject, eventdata, handles)
varargout{1} = [];


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                              Calculate
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function uipbCalculate_Callback(hObject, eventdata, handles)
global strFilterObject

OMAX = 15; % The maximum filter order checked

% get the information from the gui
fp = str2num(get(handles.uitxFc,'String'))*10^((get(handles.uipmFc,'Value')-2)*3);
fs = str2num(get(handles.uitxF1,'String'))*10^((get(handles.uipmF1,'Value')-2)*3);
if any([fp fs]<=0)
    errordlg('Frequencies must be postive','Error','modal')
    return
end
rs = abs(str2num(get(handles.uitxGain,'String')));
if rs<=3
    errordlg('The gain in the stopband must be less than -3dB','Error','modal')
    return
end

% if HP make LP
if fp > fs
    temp = fs;
    fs = fp;
    fp = temp;
end    

% determine the required Bessel filter order
Utility_GetDefaultFilterObject;
strFilterObject.sPurpose = 'Lowpass';
strFilterObject.sType = 'Bessel';
strFilterObject.fFc = fp;
oBe = 1;
for oBe = 1:OMAX+1
    strFilterObject.nOrder = oBe;
    strFilterObject = Utility_zpk(strFilterObject);
    z = strFilterObject.vZeros;
    p = strFilterObject.vPoles;
    k = strFilterObject.fK;
    w=fs*2*pi;
    curRs = -20*log10(abs(k*prod(j*w-z)/prod(j*w-p)));
    if curRs > rs
        break
    end
end

% determine the required Butterworth filter order
wa=fs/fp;
if rs>1
    oBu = ceil(log10((10.^(0.1*abs(rs))-1)./(10.^(0.1*3.01)-1))/(2*log10(wa)));
    if oBu<1, oBu=1; end
else
    oBu=1;
end

% determine the required Chebychev filter order
oCh=ceil(acosh(sqrt((10^(.1*abs(rs))-1)/(10^(.1*3.01)-1)))/acosh(wa));
if oCh<1, oCh=1; end

% determine the required Elliptic filter order
epsilon = sqrt(10 .^ (0.1*3.01)-1);
k1 = epsilon./sqrt(10 .^ (0.1*rs)-1);
k = 1/wa;
m = [k.^2 1-k.^2 (k1 .^2) 1-(k1 .^2)];
if any(m<0 | m>1)
    oEl=1;
else
    capk = ellipke(m);
    oEl = ceil(capk(1)*capk(4)/(capk(2)*capk(3)));
    if oEl<1, oEl=1; end
end

% send the result to the gui
if oBe > OMAX, sBe = sprintf('>%g',OMAX); else sBe = num2str(oBe); end
set(handles.uitxBe,'String',sBe)

if oBu > OMAX, sBu = sprintf('>%g',OMAX); else sBu = num2str(oBu); end
set(handles.uitxBu,'String',sBu)

if oCh > OMAX, sCh = sprintf('>%g',OMAX); else sCh = num2str(oCh); end
set(handles.uitxC1,'String',sCh)
set(handles.uitxC2,'String',sCh)

if oEl > OMAX
    sEl = sprintf('>%g',OMAX); 
elseif isnan(oEl)
    sEl = '?';
else 
    sEl = num2str(oEl); 
end
set(handles.uitxEl,'String',sEl)