Code covered by the BSD License  

Highlights from
Real-time DAQ and sound card visualization

Real-time DAQ and sound card visualization

by

 

Read analog signal coming from a daq or a sound card. Display the data in real-time or at the end.

daq_sound_card.m
% Matlab code to read analog signal coming from a daq or from your
% soundcard. This code allow to display the data in real-time or when the
% acquisition finished.
% The real-time visualization method is better for monitoring.
% The non real-time visualization method is better for accurate
% acquisitions.
% Nizar Ouarti 11-2009, France

% Duration and type of the acquisition
duration=15;% duration of the acquisition in seconds
real_time_vis=1;% real time visualization
%real_time_vis=0;% visualization at the end of the acquisition

% based on your hardware use: 'advantech' 'hpe1432' 'keithley' 'mcc' 'nidaq' 'winsound'
hardware='nidaq';
%hardware='winsound';
hw_inf = daqhwinfo(hardware);% hw_inf structure containing information about the hardware
boardId=hw_inf.InstalledBoardIds{1};% choose the installed board, here the first element was chosen

% analog input object was created using hardware and the board selected
ai = analoginput(hardware,boardId); 
% ai have many properties that can be read or changed:
% ex:
%   get(ai) %list of the ai properties
%   set(ai) %description of the properties
%   ai.TimeOut=2; %change the property

%channels=[1:2];% with winsound
channels=[1:6];
addchannel(ai, channels);% determine the channel(s) to be used, here 1 to 6


% Configure the analog input for single-ended or differential mode
% Not valid with winsound
set(ai,'InputType','SingleEnded');
% set(ai,'InputType','Differential');

% Set the sample rate and samples per trigger
nbrSeconds=duration;% duration of the acquisition
ai.SampleRate = 512;% sample frequency 
ai.SamplesPerTrigger = ai.SampleRate*nbrSeconds;% total of samples to acquire

start(ai);% Start acquisition
if(~real_time_vis)
    wait(ai,nbrSeconds+2); %wait nbrSeconds+2 seconds before causing a time out error 
    data = getdata(ai);% data acquisition: blocking function!
     plot(data); % plot data at the end of the acquisition 
else
    warning ('off');% in the beginning of the acquisition the number of samples is not always available
    figure(1)
    while isrunning(ai) % while ai is running
        data = peekdata(ai,ai.SampleRate);% non blocking function
        plot(data); % plot data real time
        drawnow; % forces MATLAB to update the figure
    end
    warning ('on');
end

stop(ai);% ensure the device is safely stoped
delete(ai);% delete the object ai

Contact us