Code covered by the BSD License  

Highlights from
Analog Filter Design Toolbox

image thumbnail
from Analog Filter Design Toolbox by James Squire
GUI to design and simulate active (opamp) LP and HP Bessel, Butter, Cheby, and Elliptic filters.

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

% Edit the above text to modify the response to help GuiTimeResponse

% Last Modified by GUIDE v2.5 27-Dec-2003 17:24:24

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GuiTimeResponse_OpeningFcn, ...
                   'gui_OutputFcn',  @GuiTimeResponse_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 GuiTimeResponse is made visible.
function GuiTimeResponse_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for GuiTimeResponse
handles.output = hObject;

% Load data
global strFilterObject
if length(varargin) == 1
    DoWhat = varargin{1}; % Impulse, Step, Ramp, or Parabola
    strFilterObject=Utility_zpk(strFilterObject); % find poles, zeros
else
   temp=load('matlab');
   disp([mfilename ' called in debug mode using matlab.mat datafile'])
   strFilterObject = temp.strFilterObject;
   DoWhat = 'Impulse';
end
switch DoWhat
    case 'Impulse',  set(handles.uitxTitle,'String','Impulse Response')
    case 'Step',     set(handles.uitxTitle,'String','Step Response')
    case 'Ramp',     set(handles.uitxTitle,'String','Ramp Response')
    case 'Parabola', set(handles.uitxTitle,'String','Parabola Response')
end
handles.DoWhat = DoWhat;
set(handles.uiTimeResponse,'Name',strFilterObject.sTitle)


% save the basic variables
handles.TMin = 0;
handles.TMax = 3 * max(-1./real(strFilterObject.vPoles)); % 3 time constants
handles.DrawPeak = 0;
handles.DrawSettle = 0;
handles.fDrawSettle = 10; % 10%
handles.DrawFinal = 0;
if isempty(strFilterObject.vPoles1)
    handles.DrawStandard = 0;
    set(handles.uitxStandard,'Enable','off')
    set(handles.uicbStandard,'Enable','off')
    set(handles.uifrBox,'BackgroundColor',[.9 .9 .9]);
else
    handles.DrawStandard = 1;
    set(handles.uitxStandard,'Enable','on')
    set(handles.uicbStandard,'Enable','on')
    set(handles.uicbStandard,'Value',1)
end

% Calculate y and plot it
guidata(hObject, handles);
Recalculate(handles);


function varargout = GuiTimeResponse_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output; % default output

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                     Limit Callbacks   
%                    uitxTMin, uitxTMax
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function uitxTMin_Callback(hObject, eventdata, handles)
sTMin = get(hObject,'String');
if findstr('default',lower(sTMin))
    sTMin = '0';
    set(hObject,'String','default ')
end
TMin = str2num(sTMin);
if isempty(TMin)
    errordlg('Enter the simulation start time, or "default"','Error')
    set(hObject,'String',num2str(handles.TMin))
elseif TMin >= handles.TMax
    errordlg('Simulation start time must be at least zero but less than the maximum simulation time','Error')
    set(hObject,'String',num2str(handles.TMin));
else
    handles.TMin = TMin;
    guidata(hObject, handles);
    Recalculate(handles)
end

function uitxTMax_Callback(hObject, eventdata, handles)
global strFilterObject
sTMax = get(hObject,'String');
if findstr('default',lower(sTMax)) % 3 time constants
    sTMax = num2str(3 * max(-1./real(strFilterObject.vPoles)));
    set(hObject,'String','default ')
end
TMax = str2num(sTMax);
if isempty(TMax)
    errordlg('Enter the simulation end time, or "default"','Error')
    set(hObject,'String',num2str(handles.TMax))
elseif TMax <= handles.TMin
    errordlg('Simulation end time must be greater than the simulation start time','Error')
    set(hObject,'String',num2str(handles.TMax));
else
    handles.TMax = TMax;
    guidata(hObject, handles);
    Recalculate(handles)
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                     Analysis Callbacks   
%                uicbPeak, uitxPeakY, uitxPeakT
%                uicbSettle, uipmSettleY, uipmSettleT
%                uicbFinal, uitxFinal, uitxFinal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function uicbPeak_Callback(hObject, eventdata, handles)
handles.DrawPeak = get(hObject,'Value');
guidata(hObject, handles);
Recalculate(handles)

function uicbSettle_Callback(hObject, eventdata, handles)
handles.DrawSettle = get(hObject,'Value');
guidata(hObject, handles);
Recalculate(handles)

function uipmSettleY_Callback(hObject, eventdata, handles)
vcell = get(hObject,'String');
handles.fDrawSettle = str2num(vcell{get(hObject,'Value')});
guidata(hObject, handles);
Recalculate(handles)

function uicbFinal_Callback(hObject, eventdata, handles)
handles.DrawFinal = get(hObject,'Value');
guidata(hObject, handles);
Recalculate(handles)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                     Component Value Callback
%                           uicbStandard
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function uicbStandard_Callback(hObject, eventdata, handles)
handles.DrawStandard = get(hObject,'Value');
guidata(hObject, handles);
Recalculate(handles)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                        Helper functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function Recalculate(handles)
global strFilterObject
%---------------------------Setup---------------------------
DoWhat=handles.DoWhat; 
TMin = handles.TMin; 
TMax = handles.TMax;
z = strFilterObject.vZeros; 
p = strFilterObject.vPoles; 
k = strFilterObject.fK; 
tiny = 1e-40;
tol = 1e-10;
switch DoWhat
    case 'Impulse'
        yFinal = real(k*prod([tiny;tiny-z])/prod(tiny-p));
    case 'Step'
        yFinal=real(k*prod([tiny-z])/prod(tiny-p));
    case 'Ramp'
        yFinal=real(k*prod([tiny-z])/prod([tiny;tiny-p]));
    case 'Parabola'
        yFinal=real(k*prod([tiny-z])/prod([tiny;tiny;tiny-p]));
end
if abs(yFinal) < tol
    yFinal = 0;
elseif abs(yFinal) > 1/tol
    yFinal = inf;
end
%-----------------------DrawResponse-------------------------
[y,t] = Utility_CalculateTime(z,p,k,TMin,TMax,DoWhat);
if TMin>0   % eliminate result before TMin
    keep=find(t>=TMin);
    ydraw=y(keep);
    tdraw=t(keep);
else
    ydraw = y; tdraw=t;
end
plot(tdraw,ydraw,'k-')
axlimits=axis;
hold on
xlabel('time (s)')
%----------------------DrawImpulse-------------------------
 if isequal(DoWhat,'Impulse') & isequal(strFilterObject.sPurpose,'Highpass')
     xtext=axlimits(1)+(axlimits(2)-axlimits(1))*0.25; 
     ytext=mean(axlimits(3:4)); 
     stext=sprintf('plus an impulse at t=0 of area %g',k);
     text(xtext,ytext,stext)
 end
%-----------------------DrawPeak---------------------------
if handles.DrawPeak
    [yPeak,index]=max(y); 
    tPeak=t(index);
    vx = [TMin tPeak tPeak];   vy = [yPeak yPeak axlimits(3)];
    if yFinal < inf
        plot(vx,vy)
        set(handles.uitxPeakT,'String',num2str(tPeak))
        set(handles.uitxPeakY,'String',num2str(yPeak))
    else
        set(handles.uitxPeakT,'String','Infinity')
        set(handles.uitxPeakY,'String','Infinity')
    end
else
    set(handles.uitxPeakT,'String','')
    set(handles.uitxPeakY,'String','')
end 
%-----------------------DrawSettle---------------------------
if handles.DrawSettle
    % settles when y is within max-min% of the final value
    yRange = max(y) - min(y); % find in case DrawSettle is called with Tmin > o
    yLow = yFinal - handles.fDrawSettle*yRange/100;
    yHigh = yFinal + handles.fDrawSettle*yRange/100;
    if y(end) > yHigh | y(end) < yLow
        if yFinal == inf
            sTSettle = 'Infinity';
        else
            sTSettle = '?'; % increase the time plotted to find it
        end
    else
        index1=find(y<yHigh & y>yLow);
        if length(index1)==1
            tSettle = t(end);
        else
            index2=find(diff(index1)>1);
            if isempty(index2) % as soon as inbounds, it stays there
                tSettle = t(index1(1));
            else
                index2 = index2(end)+1;
                if index2>length(index1), index2=index1(end); end
                tSettle = t(index1(index2));
            end
        end
        sTSettle=num2str(tSettle);
        vx1 = [TMin TMax NaN TMin TMax NaN tSettle tSettle];
        vy1 = [yHigh yHigh NaN yLow yLow NaN axlimits(3) yHigh];
        plot(vx1,vy1,'g-')
    end
    set(handles.uitxSettleT,'String',sTSettle)
    set(handles.uipmSettleY,'Enable','on')
else
    set(handles.uitxSettleT,'String','')
    set(handles.uipmSettleY,'Enable','off')    
end

%-----------------------DrawFinal---------------------------
if handles.DrawFinal
    vx = [TMin TMax];  vy=[yFinal yFinal];
    plot(vx,vy,'r-')
    set(handles.uitxFinalT,'String','Infinity')
    if yFinal == inf        
        set(handles.uitxFinalY,'String','Infinity')
    else
        set(handles.uitxFinalY,'String',num2str(yFinal))
    end
else
    set(handles.uitxFinalT,'String','')
    set(handles.uitxFinalY,'String','')
end    
%-----------------------DrawStandard---------------------------
if handles.DrawStandard    % blue
    z1 = strFilterObject.vZeros1; p1 = strFilterObject.vPoles1; k1 = strFilterObject.fK1; 
    [yStandard,tStandard] = Utility_CalculateTime(z1,p1,k1,TMin,TMax,DoWhat);
    if TMin>0   % eliminate result before TMin
        keep=find(t>=TMin);
        yStandard=yStandard(keep);
        tStandard=tStandard(keep);
    end
    plot(tStandard,yStandard,'b-')
end
%-----------------------Cleanup---------------------------
axlimits=axis;
axlimits(1) = TMin;  axlimits(2) = TMax;

axis(axlimits)
hold off

Contact us at files@mathworks.com