image thumbnail
from Data Acq Toolbox Demo by Rene Messier
Using Data Acquisition Toolbox to process data from hardware

ToolBoxDataAcq(varargin)
%By:  Rene Messier
%Co:  Data Translation
%Date: 9/27/06

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

% Last Modified by GUIDE v2.5 25-Sep-2006 14:41:09

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ToolBoxDataAcq_OpeningFcn, ...
                   'gui_OutputFcn',  @ToolBoxDataAcq_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 ToolBoxDataAcq is made visible.
function ToolBoxDataAcq_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 ToolBoxDataAcq (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

function dataMissHandle(obj, event, errorcount)
    ai0 = obj;
    disp('ERROR:  DataMissed error occurred');
    errorcount=errorcount+1;
    stop(ai0);
    set(handles.startbtn,'Enable', 'on');
    set(handles.stopbtn,'Enable', 'off');
    clear ai0;


function runTimeHandle(obj, event, errorcount)
    ai0 = obj;
    disp('ERROR:  Run Time error occurred');
    errorcount=errorcount+1;
    stop(ai0);
    set(handles.startbtn,'Enable', 'on');
    set(handles.stopbtn,'Enable', 'off');
    clear ai0;

function samplesReceivedHandle(obj, event, handles)   
    
    ai0 = obj;
    datasize = get(ai0, 'SamplesAcquiredFcnCount');
    data = getdata(ai0, datasize);
    
    %get the max data point
    maxval = strcat(num2str(max(data)),' V');
    set(handles.CH0Peak, 'String', maxval); 
    
    blocksize = datasize; 
    Fs = get(ai0, 'SampleRate');
    [f,mag] = daqdocfft(data,Fs,blocksize, handles);
    
    %find the max index to display the fundamental frequency
    [ymax,maxindex]= max(mag);
    FreqStr = strcat(num2str((Fs/FFTSize)* maxindex),' Hz');
    set(handles.CH0Freq, 'String', FreqStr);
     
    %set plots
    plot(handles.plot1,data);              
    set(handles.plot1, 'XGrid', 'on');
    set(handles.plot1, 'YGrid', 'on');
    
    plot(handles.fft1,f,mag);
    set(handles.fft1, 'XScale', 'lin'); %or select log
    set(handles.fft1, 'XGrid', 'on');
    set(handles.fft1, 'YGrid', 'on');
    
%get the fundamental frequency
function [f,mag] = daqdocfft(data,Fs,blocksize,handles)
    %    [F,MAG]=DAQDOCFFT(X,FS,BLOCKSIZE) calculates the FFT of X
    %    using sampling frequency FS and the SamplesPerTrigger
    %    provided in BLOCKSIZE
   
    %fft function requires a one column set of data
    %before performing an fft function we need to strip out any
    %extra channels if acquiring more than one channel
    global chan_nums
    global FFT_Chan
    global ai0
    
    i = length(ai0.Channel);
    dataOneChannel = data;
    
    while (i > 0)
         %sort out the channels here
        if (chan_nums(i) ~= FFT_Chan)
           dataOneChannel(:,i) = [];
        end
        i = i - 1;   
    end    
    
    %window the data
    WindowTypeIndex = get(handles.WinFunction, 'Value');
    switch WindowTypeIndex
        case 1
        ffttype = window(@rectwin,blocksize);
        case 2
        ffttype = window(@blackman,blocksize);
        case 3
        ffttype = window(@blackmanharris,blocksize);
        case 4
        ffttype = window(@hann,blocksize);
        case 5
        ffttype = window(@hamming,blocksize);
    end
    xfft = ffttype .* dataOneChannel;
    
    %perform fft and obtain the magnitude data
    windowedXfft = abs(fft(xfft));
    
    % Avoid taking the log of 0.
    index = find(windowedXfft == 0);
    windowedXfft(index) = 1e-17;
    
    % mag to db 
    mag = 20*log10(windowedXfft);
    mag = mag(1:floor(blocksize/2));
    
    f = (0:length(mag)-1)*Fs/blocksize;
    f = f(:);
    
    % for a 16bit board the max scale will be 74db.  offset the data
    % to be 0db down
    mag = -74+mag(:);
    
% --- Outputs from this function are returned to the command line.
function varargout = ToolBoxDataAcq_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 FileMenu_Callback(hObject, eventdata, handles)
% hObject    handle to FileMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to OpenMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
file = uigetfile('*.fig');
if ~isequal(file, 0)
    open(file);
end

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

% --------------------------------------------------------------------
function CloseMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to CloseMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],...
                     ['Close ' get(handles.figure1,'Name') '...'],...
                     'Yes','No','Yes');
if strcmp(selection,'No')
    return;
end

delete(handles.figure1)


% --- Executes on selection change in toolboxmenu.
function toolboxmenu_Callback(hObject, eventdata, handles)
% hObject    handle to toolboxmenu (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 toolboxmenu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from toolboxmenu

global myoutput
global di

popup_sel_index = get(handles.toolboxmenu, 'Value');
list = get(handles.toolboxmenu, 'String');
popup_sel_type = list{popup_sel_index};

try
    di = daqhwinfo(char(popup_sel_type));
    myoutput = ['AdapterName: ', di.AdaptorName,char(13)];
    set(handles.edit1,'String', myoutput);

    myoutput = [myoutput, 'AdapterDllName: ', di.AdaptorDllName, char(13)]; 
    set(handles.edit1,'String', myoutput);

    myoutput = [myoutput, 'AdapterDllVersion: ', di.AdaptorDllVersion, char(13)]; 
    set(handles.edit1,'String', myoutput); 

    boards = di.BoardNames;
    set(handles.boardmenu, 'String', boards);

    myoutput = [myoutput, 'Boards found: ', boards,char(13)]; 
    set(handles.edit1,'String', myoutput);
catch
    disp('ERROR: Can not find adaptor!');
    myoutput = 'ERROR: Can not find adaptor!';
    set(handles.edit1,'String', myoutput);
    return;
end

% --- Executes during object creation, after setting all properties.
function toolboxmenu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to toolboxmenu (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
set(hObject, 'String', {'dtol', 'nidaq', 'nidaqmx'});

function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double

function plot1_CreateFcn(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function frequency_Callback(hObject, eventdata, handles)
% hObject    handle to frequency (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 frequency as text
%        str2double(get(hObject,'String')) returns contents of frequency as a double


% --- Executes during object creation, after setting all properties.
function frequency_CreateFcn(hObject, eventdata, handles)
% hObject    handle to frequency (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in boardmenu.
function boardmenu_Callback(hObject, eventdata, handles)
% hObject    handle to boardmenu (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 boardmenu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from boardmenu

% set some globals we will need in other functions
global di
global myoutput
global ai0
global FFT_Chan
FFT_Chan = 0;
board_index = get(handles.boardmenu, 'Value');
boardlist = get(handles.boardmenu, 'String');
board_type = boardlist{board_index};

try
    ai0 = analoginput (char(di.AdaptorName), board_index-1);
    myoutput = [myoutput, 'Board selected: ', board_type];
    set(handles.edit1,'String', myoutput);
catch
    disp('ERROR: Cannot open AD subsystem!');
    myoutput = [myoutput, 'ERROR: Cannot open AD subsystem!'];
    set(handles.edit1,'String', myoutput);
    return;
end

% --- Executes during object creation, after setting all properties.
function boardmenu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to boardmenu (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over toolboxmenu.
function toolboxmenu_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to toolboxmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% --- Executes on button press in startbtn.
function startbtn_Callback(hObject, eventdata, handles)
% hObject    handle to startbtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global ai0
global errorcount
global myoutput
global chan_nums
global numchannels

% set the input type (single ended or differential)
inpt_index = get(handles.inputtype, 'Value');
inptlist = get(handles.inputtype, 'String');
inpt_type = inptlist{inpt_index};
set(ai0, 'InputType', inpt_type);

% set the sample rate
rate = str2double(get(handles.frequency, 'String'));
maxRate = daqhwinfo(ai0,'MaxSampleRate');
minRate = daqhwinfo(ai0,'MinSampleRate');
if (rate < minRate)
    myoutput = [myoutput, 'ERROR: Board minSampleRate is ',minRate];
    set(handles.edit1,'String', myoutput);
elseif (rate > maxRate)
    myoutput = [myoutput, 'ERROR: Board maxSampleRate is ',maxRate];
    set(handles.edit1,'String', myoutput);
end
try
    set(ai0, 'SampleRate', rate); 
catch
    disp('ERROR: SampleRate greater than max!');
    return;
end
    
%remove currently set channels
numchannels = length(ai0.Channel);
while (numchannels > 0)
    delete(ai0.channel(1))
    numchannels = numchannels - 1;
end

% set the new channels
channels = get(handles.channels, 'String');
chan_nums = str2num(channels);
addchannel(ai0, chan_nums)
    
% fill in the FFT channel dropdown with the new channels
set(handles.FFTChan, 'String', chan_nums);

%continually acquire data until we call stop
set(ai0, 'BufferingMode', 'Auto');
set(ai0, 'TriggerType', 'Immediate');
set(ai0, 'SamplesPerTrigger', inf);
set(ai0, 'SamplesAcquiredFcnCount', rate/10);
%declare some other callbacks
set(ai0, 'StopFcn', {@daqcallback});
set(ai0, 'StartFcn', {@daqcallback});
set(ai0, 'DataMissedFcn', {@dataMissHandle, errorcount});
set(ai0, 'RuntimeErrorFcn', {@runTimeHandle, errorcount});
set(ai0, 'SamplesAcquiredFcn', {@samplesReceivedHandle, handles});

%display all properties assoc with ai0
%get(ai0)

myoutput = [myoutput, 'AD Started '];
set(handles.edit1,'String', myoutput);

set(handles.toolboxmenu,'Enable', 'off');
set(handles.boardmenu,'Enable', 'off');
set(handles.frequency,'Enable', 'off');
set(handles.inputtype,'Enable', 'off');
set(handles.channels,'Enable', 'off');
set(handles.startbtn,'Enable', 'off');
set(handles.FFTChan,'Enable', 'on');
set(handles.WinFunction,'Enable', 'on');
set(handles.stopbtn,'Enable', 'on');

%finally start the AD
start(ai0)

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

%stop the AD subsystem immediatly
stop(ai0)

set(handles.FFTChan,'Enable', 'off');
set(handles.WinFunction,'Enable', 'off');
set(handles.toolboxmenu,'Enable', 'on');
set(handles.boardmenu,'Enable', 'on');
set(handles.frequency,'Enable', 'on');
set(handles.inputtype,'Enable', 'on');
set(handles.channels,'Enable', 'on');
set(handles.startbtn,'Enable', 'on');
set(handles.stopbtn,'Enable', 'off');

myoutput = [myoutput, 'AD Stopped '];
set(handles.edit1,'String', myoutput);

% --- Executes on selection change in inputtype.
function inputtype_Callback(hObject, eventdata, handles)
% hObject    handle to inputtype (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 inputtype contents as cell array
%        contents{get(hObject,'Value')} returns selected item from inputtype


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

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


% --- Executes on selection change in listbox3.
function listbox3_Callback(hObject, eventdata, handles)
% hObject    handle to listbox3 (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 listbox3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox3


function channels_Callback(hObject, eventdata, handles)
% hObject    handle to channels (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 channels as text
%        str2double(get(hObject,'String')) returns contents of channels as a double


% --- Executes during object creation, after setting all properties.
function channels_CreateFcn(hObject, eventdata, handles)
% hObject    handle to channels (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
%set(0,'defaults')
%colordef black;
%colormap('default')


% --- Executes on selection change in WinFunction.
function WinFunction_Callback(hObject, eventdata, handles)
% hObject    handle to WinFunction (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 WinFunction contents as cell array
%        contents{get(hObject,'Value')} returns selected item from WinFunction


% --- Executes during object creation, after setting all properties.
function WinFunction_CreateFcn(hObject, eventdata, handles)
% hObject    handle to WinFunction (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in FFTChan.
function FFTChan_Callback(hObject, eventdata, handles)
% hObject    handle to FFTChan (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 FFTChan contents as cell array
%        contents{get(hObject,'Value')} returns selected item from FFTChan
global FFT_Chan

FFT_index = get(handles.FFTChan, 'Value');
FFTlist = get(handles.FFTChan, 'String');
FFT_Chan = str2num(FFTlist(FFT_index))

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

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


Contact us at files@mathworks.com