Code covered by the BSD License  

Highlights from
CircuitTool (a Test and Measurement Demo)

image thumbnail

CircuitTool (a Test and Measurement Demo)

by

 

07 Sep 2004 (Updated )

A demonstration test and measurement application with system identification.

circuit_tool(varargin)
function varargout = circuit_tool(varargin)
% CIRCUIT_TOOL M-file for circuit_tool.fig
%      CIRCUIT_TOOL, by itself, creates a new CIRCUIT_TOOL or raises the existing
%      singleton*.
%
%      H = CIRCUIT_TOOL returns the handle to a new CIRCUIT_TOOL or the handle to
%      the existing singleton*.
%
%      CIRCUIT_TOOL('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CIRCUIT_TOOL.M with the given input arguments.
%%      CIRCUIT_TOOL('Property','Value',...) creates a new CIRCUIT_TOOL or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before circuit_tool_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to circuit_tool_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

% Copyright 2002-2004 The MathWorks, Inc.

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

% Last Modified by GUIDE v2.5 21-Jun-2004 15:39:38

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @circuit_tool_OpeningFcn, ...
                   'gui_OutputFcn',  @circuit_tool_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(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 circuit_tool is made visible.
function circuit_tool_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 circuit_tool (see VARARGIN)

% Choose default command line output for circuit_tool
handles.output = hObject;

movegui(handles.figure1,'center')
% Create and configure function generator interface
[interfaceObj,fcngen] = localConnectInstrument;

% Create and configure the analoginput object
ai = localCreateAI(handles);

% Initialize stripchart to show system input
Fs = ai.SampleRate;
AxesWidth = str2num(get(handles.CaptureLengthEdit,'String'));
NChannels = length(ai.channel);
% stripchart(handles.InputAxes,Fs,AxesWidth,2);    % Initialize stripchart
% stripchart(handles.InputAxes,Fs,AxesWidth,1);    % Initialize stripchart
daqstopbutton(handles.PreviewStartStopButton,ai);  % Initialize the start/stop button
% set(handles.InputAxes,'YLim',[-.05 .05])


localInitStripCharts(handles,Fs,AxesWidth);

% Initialize second strip chart, for predicted response
% stripchart(handles.ResponseAxes,Fs,AxesWidth,2);    % Initialize stripchart
% set(handles.ResponseAxes,'YLim',[-.05 .05])

% Initialize third strip chart, for model error
% stripchart(handles.ErrorAxes,Fs,AxesWidth,1);    % Initialize stripchart
% set(handles.ErrorAxes,'YLim',[-.05 .05])

% Create custom legend
axis(handles.LegendAxes,[0 1 0 1])
line([.05 .3],[.7 .7],'Color','b','Parent',handles.LegendAxes)
line([0.05 .3],[.3 .3],'Color',[0 .5 0],'Parent',handles.LegendAxes)
text(.4,.7,'Measured','Parent',handles.LegendAxes)
text(.4,.3,'Predicted','Parent',handles.LegendAxes)
set(handles.LegendAxes,'XTick',[],'YTick',[],'XColor','w','YColor','w');


% Store devices in handles
handles.fcngen = fcngen;
handles.interfaceObj = interfaceObj;
handles.ai = ai;


% Store linear system model in handles structure
handles.sys = [];   % This is populated when model is developed.
handles.tf = [];

% Update GUI to reflect current instrument state
localUpdateConfiguration(handles)


% Update handles structure
guidata(hObject, handles);

% UIWAIT makes circuit_tool wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = circuit_tool_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



function AmplitudeEdit_Callback(hObject, eventdata, handles)
% hObject    handle to AmplitudeEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of AmplitudeEdit as text
%        str2double(get(hObject,'String')) returns contents of AmplitudeEdit as a double

val = str2num(get(hObject,'String'));
try
    set(handles.fcngen, 'Amplitude', val);
catch
    errordlg(lasterr)
end


% --- Executes during object creation, after setting all properties.
function AmplitudeEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to AmplitudeEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in WaveformPopup.
function WaveformPopup_Callback(hObject, eventdata, handles)
% hObject    handle to WaveformPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns WaveformPopup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from WaveformPopup

% Set the waveform on the function generator
strng = get(hObject,'String');
wfm = strng{get(hObject,'Value')};
localWaveformUpdate(wfm,handles)

        

% --- Executes during object creation, after setting all properties.
function WaveformPopup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to WaveformPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function FrequencyEdit_Callback(hObject, eventdata, handles)
% hObject    handle to FrequencyEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of FrequencyEdit as text
%        str2double(get(hObject,'String')) returns contents of FrequencyEdit as a double
val = str2num(get(hObject,'String'));
try
    set(handles.fcngen, 'Frequency', val);
catch
    errordlg(lasterr)
end



% --- Executes during object creation, after setting all properties.
function FrequencyEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to FrequencyEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end




% --- Executes on button press in FSEnableCheckbox.
function FSEnableCheckbox_Callback(hObject, eventdata, handles)
% hObject    handle to FSEnableCheckbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of FSEnableCheckbox
val = get(hObject,'Value');
if val==1
    fs_enable = 'on';
elseif val==0
    fs_enable = 'off';
end;

localFrequencySweepUpdate(fs_enable,handles)


% --- Executes on selection change in FSFrequencySpacingPopup.
function FSFrequencySpacingPopup_Callback(hObject, eventdata, handles)
% hObject    handle to FSFrequencySpacingPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

strng = lower(get(hObject,'String'));
val = strng{get(hObject,'Value')};
set(handles.fcngen.Frequencysweep(1), 'Spacing', val);

% --- Executes during object creation, after setting all properties.
function FSFrequencySpacingPopup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to FSFrequencySpacingPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function FSStartFrequencyEdit_Callback(hObject, eventdata, handles)
% hObject    handle to FSStartFrequencyEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of FSStartFrequencyEdit as text
%        str2double(get(hObject,'String')) returns contents of FSStartFrequencyEdit as a double
val = str2num(get(hObject,'String'));
try
    set(handles.fcngen.Frequencysweep(1), 'Start', val);
catch
    errordlg(lasterr)
end


% --- Executes during object creation, after setting all properties.
function FSStartFrequencyEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to FSStartFrequencyEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function FSStopFrequencyEdit_Callback(hObject, eventdata, handles)
% hObject    handle to FSStopFrequencyEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of FSStopFrequencyEdit as text
%        str2double(get(hObject,'String')) returns contents of FSStopFrequencyEdit as a double
val = str2num(get(hObject,'String'));
try
    set(handles.fcngen.Frequencysweep(1), 'Stop', val);
catch
    errordlg(lasterr)
end


% --- Executes during object creation, after setting all properties.
function FSStopFrequencyEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to FSStopFrequencyEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function FSSweepTimeEdit_Callback(hObject, eventdata, handles)
% hObject    handle to FSSweepTimeEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of FSSweepTimeEdit as text
%        str2double(get(hObject,'String')) returns contents of FSSweepTimeEdit as a double
val = str2num(get(hObject,'String'));
try
    set(handles.fcngen.Frequencysweep(1), 'Time', val);
catch
    errordlg(lasterr)
end

% --- Executes during object creation, after setting all properties.
function FSSweepTimeEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to FSSweepTimeEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on button press in DevelopModelButton.
function DevelopModelButton_Callback(hObject, eventdata, handles)
% hObject    handle to DevelopModelButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Develop a model of the circuit
axis(handles.ResultsAxes,'off')
cla(handles.ResultsAxes)
legend(handles.ResultsAxes,'off')
set(handles.figure1,'Pointer','watch');

%% Modify ai object
ai = handles.ai;

% Record current state of ai object
state.TriggerRepeat = get(ai,'TriggerRepeat');
state.TriggerFcn = get(ai,'TriggerFcn');
state.TriggerType = get(ai,'TriggerType');
state.SamplesPerTrigger = get(ai,'SamplesPerTrigger');
state.SampleRate = get(ai,'SampleRate');
state.SamplesAcquiredFcn = get(ai,'SamplesAcquiredFcn');
state.SamplesAcquiredFcnCount = get(ai,'SamplesAcquiredFcnCount');

% Stop if necessary
if strcmp(ai.running,'On')
    stop(ai);
    waittilstop(ai,2);        % Be sure that it's done before moving on!
    set(handles.PreviewStartStopButton, ...
        'Value',0,'String','Start')
end;

% Modify ai object so that one trigger captures desired record length
set(ai,'StandardSampleRates','Off');
set(ai,'SampleRate',96000);
Fs = get(ai,'SampleRate');
l = str2num(get(handles.CaptureLengthEdit,'String'));
Ns = l*Fs;
set(ai,'SamplesPerTrigger',Ns);
set(ai,'TriggerRepeat',0);
set(ai,'TriggerType','manual');
set(ai,'TriggerFcn','');
set(ai,'SamplesAcquiredFcn','');

%% Collect data
start(ai)
pause(.1)       % Let the card settle
trigger(ai)
[data,time] = getdata(ai);
input = data(:,1);
response = data(:,2);
response(:,2) = NaN;    %Don't show predicted response yet.
clear data
localInitStripCharts(handles,Fs,str2num(get(handles.CaptureLengthEdit,'String')))
stripchart(handles.InputAxes,input);
stripchart(handles.ResponseAxes,response);
% This line is wrong if I change sample rate
drawnow

%% Develop model
% [T_measured,T_predicted,F] = circuit_id(data(:,2),data(:,1),1/ai.SampleRate);
[T_measured,T_predicted,F,sys] = circuit_idsp(response(:,1),input,1/ai.SampleRate);
% Scott: Come up with a model using signal processing toolbox instead
% [b,a] = invfreqs(h,w,n,m).  Then can use filter to test it out.

%% Discretize Model
% For real-time performance, we use filter instead of lsim.  
% Discretize the filter.  Use the sample rate for continuous running, not
% the sample rate used to build the model
sysD = c2d(sys,1/state.SampleRate); 
num = sysD.num{1};
den = sysD.den{1};


% Compute cut-off frequency
fc_mw = circuit_computecutoff(F,T_measured);                   
fc_ss = circuit_computecutoff(F,T_predicted);

%% Display the results
% Workaround to fix legend being too smooshed - Add a 3rd line in between
% the two lines.  Make this line white so it doesn't show in legend.  note
% - have to use real data for the line.  If it's just NaN, legend ignores
% it and gets goofed up.
axis(handles.ResultsAxes,'on')
hLine = semilogx(F,20*log10(T_measured),1000,2,'w',F,20*log10(T_predicted), ...
    'Parent',handles.ResultsAxes,'LineWidth',2);
xlabel(handles.ResultsAxes,'Frequency (Hz)');
ylabel(handles.ResultsAxes,'Transfer Function Estimate (dB)');
yl = [-25 3];
axis(handles.ResultsAxes,[0 20000 yl]);
hLegend = legend(handles.ResultsAxes, ...
    {['Measured:  fc = ' num2str(fc_mw,4)],' ',['Predicted:   fc = ' num2str(fc_ss,4)]},  ...
    'Location','SouthWest', ...
    'FontSize',16,...
    'FontWeight','bold', ...
    'EdgeColor','w');

line([fc_mw fc_mw;fc_ss fc_ss]',[yl;yl]', ...
    'Parent',handles.ResultsAxes,'LineStyle',':')

%% Restore the object state
set(ai,'TriggerRepeat',state.TriggerRepeat);
set(ai,'TriggerFcn',state.TriggerFcn);
set(ai,'TriggerType',state.TriggerType);
set(ai,'SamplesPerTrigger',state.SamplesPerTrigger);
set(ai,'SampleRate',state.SampleRate);
set(ai,'SamplesAcquiredFcn',state.SamplesAcquiredFcn);

set(handles.figure1,'Pointer','arrow');

% Enable checkbox to show predicted response
% set(handles.PredictedResponseCheckbox,'Enable','on');

% Store linear system model in handles structure
handles.sys = sys;
handles.tf.num = num;
handles.tf.den = den;

handles.time = time - time(1);
guidata(hObject,handles);

function [interfaceObj,deviceObj] = localConnectInstrument
% Create a GPIB object.
interfaceObj = instrfind('Type', 'gpib', 'BoardIndex', 0, 'PrimaryAddress', 4, 'Tag', '');

% Create the GPIB object if it does not exist
% otherwise use the object that was found.
if isempty(interfaceObj)
    try
        interfaceObj = gpib('NI', 0, 4);
    catch
        errordlg('Function Generator not found at expected location (NI board 0, address 4)');
    end;
        
else
    fclose(interfaceObj);
    interfaceObj = interfaceObj(1);
end

% Create a device object. 
deviceObj = icdevice('agilent_33120a.mdd', interfaceObj);

% Connect
connect(deviceObj)


% --- Executes on button press in PreviewStartStopButton.
function PreviewStartStopButton_Callback(hObject, eventdata, handles)
% hObject    handle to PreviewStartStopButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
delete(hObject);

delete([handles.fcngen handles.interfaceObj]);
delete(handles.ai)

function localUpdateConfiguration(handles)
fcngen = handles.fcngen;

% Update the GUI with the current instrument state
wfm = get(fcngen,'Waveform');
ampl = get(fcngen,'Amplitude');
freq = get(fcngen,'Frequency');
duty = get(fcngen, 'Dutycycle');
fs_enable = get(fcngen.Frequencysweep(1), 'Enable');
fs_start = get(fcngen.Frequencysweep(1), 'Start');
fs_stop = get(fcngen.Frequencysweep(1), 'Stop');
fs_time = get(fcngen.Frequencysweep(1), 'Time');
fs_spacing = get(fcngen.Frequencysweep(1), 'Spacing');

% Do edit boxes first, since they are easy.
set(handles.AmplitudeEdit,'String',num2str(ampl));
set(handles.FrequencyEdit,'String',num2str(freq));
set(handles.DutyCycleEdit,'String',num2str(duty));
set(handles.FSStartFrequencyEdit,'String',num2str(fs_start));
set(handles.FSStopFrequencyEdit,'String',num2str(fs_stop));
set(handles.FSSweepTimeEdit,'String',num2str(fs_time));

% Waveform popup
localWaveformUpdate(wfm,handles)

% Frequency Sweep Checkbox
localFrequencySweepUpdate(fs_enable,handles)

% Frequency Sweep Spacing Popup
strng = lower(get(handles.FSFrequencySpacingPopup,'String'));
val = strmatch(fs_spacing,strng);
set(handles.FSFrequencySpacingPopup,'Value',val);

function localWaveformUpdate(wfm,handles)
strng = get(handles.WaveformPopup,'String');
val = strmatch(wfm,strng);
set(handles.WaveformPopup,'Value',val);
% If waveform is noise, hide frequency.  Otherwise, show
switch wfm
    case 'noise'
        set([handles.FrequencyEdit handles.FrequencyText],'Visible','off')
        set(handles.FrequencySweepPanel,'Visible','off')
        set(handles.FSEnableCheckbox,'Visible','off')
    otherwise
        set([handles.FrequencyEdit handles.FrequencyText],'Visible','on')
        set(handles.FSEnableCheckbox,'Visible','on')
        
        % Show frequency sweep panel, if frequency sweep is enabled
        if get(handles.FSEnableCheckbox,'Value')
            set(handles.FrequencySweepPanel,'Visible','on')
        end;
        
end;

% If waveform is square, show dutycycle
switch wfm
    case 'square'
        set([handles.DutyCycleEdit handles.DutyCycleText],'Visible','on')
    otherwise
        set([handles.DutyCycleEdit handles.DutyCycleText],'Visible','off')
end;

set(handles.fcngen,'Waveform',wfm)

function localFrequencySweepUpdate(fs,handles)
set(handles.fcngen.Frequencysweep(1),'Enabled',fs)

switch fs
    case 'on'
        set(handles.FSEnableCheckbox,'Value',1);
        set(handles.FrequencySweepPanel,'Visible','on')
     otherwise
        set(handles.FSEnableCheckbox,'Value',0);
        set(handles.FrequencySweepPanel,'Visible','off')
end;


function [ai,chan] = localCreateAI(handles,Fs)
%%
% Create analoginput object
ai = analoginput('winsound');
chan = addchannel(ai,1:2);

%%
% Configure analoginput object
set(ai,'SampleRate',44100);
% set(ai,'SamplesPerTrigger',floor(.1*44100));
set(ai,'SamplesPerTrigger',inf);

%%
% Configure acquisition to update scope
% set(ai,'TriggerRepeat',inf)
% set(ai,'TriggerFcn',{@localUpdatePlot,handles.figure1});
set(ai,'SamplesAcquiredFcn',{@localUpdatePlot,handles.figure1});
set(ai,'SamplesAcquiredFcnCount',4096*2);


function localUpdatePlot(ai,event,hFig);
% Callback function for analoginput object to update the plot
% Additional arguments - handle to the figure, and fft length

handles = guidata(hFig);
% Only update if enough samples are ready
% if get(ai,'SamplesAvailable') >= get(ai,'SamplesPerTrigger')
%     if strcmp(ai.running,'On')
% Get data
% [d,time] = getdata(ai,get(ai,'SamplesAcquiredFcnCount'));
% d = peekdata(ai,get(ai,'SamplesAcquiredFcnCount'));
if ~strcmp(ai.running,'On')
    return
end
    
d = peekdata(ai,get(ai,'SamplesAvailable'));

% If too much data, keep only the end
parms = getappdata(handles.InputAxes,'StripChartParameters');
if length(d) >  parms.Ns
    d = d(end - parms.Ns+1:end,:);
end;

flushdata(ai)
input = d(:,1);
response = d(:,2);
clear d

% Update input data strip chart
stripchart(handles.InputAxes,input);

% Update response strip chart.
% If model exists, show both measured and predicted response
% Otherwise, use NaN for predicted response
%         if get(handles.PredictedResponseCheckbox,'Value');
% if strfind('tf',class(handles.sys))
% if ~isempty(handles.sys)
%     time = (0:1:length(input)-1)/get(ai,'SampleRate');
%     dp = lsim(handles.sys,input,handles.time);
% 
%     response = [response dp];
%     err = response(:,2) - response(:,1);
%     %                 stripchart(handles.ResponseAxes,[d(:,1) dp]);
% else
%     response(:,2) = NaN;
% end;

if ~isempty(handles.tf)
    dp = filter(handles.tf.num,handles.tf.den,input);
    
%     time = (0:1:length(input)-1)'/ai.SampleRate;
%     dpt = lsim(handles.sys,input,time);
    response = [response dp];
    err = response(:,2) - response(:,1);
    %                 stripchart(handles.ResponseAxes,[d(:,1) dp]);
else
    response(:,2) = NaN;
end;


%         end;
stripchart(handles.ResponseAxes,response);

% Update error strip chart
% Only if model exists
% if strfind('tf',class(handles.sys))
if ~isempty(handles.sys)
    stripchart(handles.ErrorAxes,err)
end;

drawnow

%     end;
% end;




function DutyCycleEdit_Callback(hObject, eventdata, handles)
% hObject    handle to DutyCycleEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of DutyCycleEdit as text
%        str2double(get(hObject,'String')) returns contents of DutyCycleEdit as a double
val = str2num(get(hObject,'String'));
try
    set(handles.fcngen, 'Dutycycle', val);
catch
    errordlg(lasterr)
end


% --- Executes during object creation, after setting all properties.
function DutyCycleEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to DutyCycleEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function CaptureLengthEdit_Callback(hObject, eventdata, handles)
% hObject    handle to CaptureLengthEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of CaptureLengthEdit as text
%        str2double(get(hObject,'String')) returns contents of CaptureLengthEdit as a double

l = str2num(get(hObject,'String'));

% Update stripchart display width
cla(handles.InputAxes)
cla(handles.ResponseAxes)

cla(handles.ErrorAxes)
localInitStripCharts(handles,handles.ai.SampleRate,l);
% cla(handles.InputAxes)
% stripchart(handles.InputAxes,handles.ai.SampleRate,l,1);    % Initialize stripchart
% set(handles.InputAxes,'YLim',[-.05 .05])
% 
% % Update stripchart display width
% cla(handles.ResponseAxes)
% stripchart(handles.ResponseAxes,handles.ai.SampleRate,l,2);    % Initialize stripchart
% set(handles.ResponseAxes,'YLim',[-.05 .05])
% 
% % Update stripchart display width
% cla(handles.ErrorAxes)
% stripchart(handles.ErrorAxes,handles.ai.SampleRate,l,1);    % Initialize stripchart
% set(handles.ErrorAxes,'YLim',[-.05 .05])

% --- Executes during object creation, after setting all properties.
function CaptureLengthEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to CaptureLengthEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



% --- Executes on button press in PredictedResponseCheckbox.
function PredictedResponseCheckbox_Callback(hObject, eventdata, handles)
% hObject    handle to PredictedResponseCheckbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of PredictedResponseCheckbox

function localInitStripCharts(handles,Fs,AxesWidth)

% Initialize input strip chart 
stripchart(handles.InputAxes,Fs,AxesWidth,1);    % Initialize stripchart
set(handles.InputAxes,'YLim',[-1 1])
xlabel('')
ylabel('Input');

% Initialize response strip chart 
stripchart(handles.ResponseAxes,Fs,AxesWidth,2);    % Initialize stripchart
set(handles.ResponseAxes,'YLim',[-1 1])
xlabel('')
ylabel('Response');
% legend('Measured','Predicted');       % Too slow, every time I update the plot!

% Initialize error strip chart
stripchart(handles.ErrorAxes,Fs,AxesWidth,1);    % Initialize stripchart
set(handles.ErrorAxes,'YLim',[-1 1])
ylabel('Model error')

% Turn on zoom
% Link axes
% On x only
linkaxes([handles.InputAxes handles.ResponseAxes handles.ErrorAxes])
zoom(handles.figure1,'on')

Contact us