Data Acquisition Toolbox

Introduction to Analog Output

This example shows how to generate signals using an analog output object. This is shown by configuring analog output properties, adding a channel to the analog output object and outputting data to a sound card.

See also ANALOGOUTPUT, ADDCHANNEL, PUTDATA, DAQDEVICE/SET, DAQDEVICE/START, DAQHELP, PROPINFO.

Note: This can only be run using the 32-bit version of MATLAB® and Data Acquisition Toolbox™. To learn more about using data acquisition devices on other platforms, see this example.

First, find any running data acquisition objects and stop them. This stops all running data acquisition objects from interfering with this example. This code is usually not necessary outside this example unless there are multiple data acquisition objects running.

if (~isempty(daqfind))
    stop(daqfind)
end

In this example, you will output data to the sound card on your computer. To get started, you first need to verify that your environment is set up so that you can send data to your sound card. If you are unsure about this, refer to Appendix A of the Data Acquisition Toolbox™ User's Guide.

In this example, a sine wave with two different frequencies will be output to the sound card.

To begin, let's create an analog output object associated with the winsound device. Two channels will be added to the analog output object, ao. This will allow the winsound device to run in stereo mode.

ao = analogoutput('winsound');
addchannel(ao, [1 2]);

Let's configure the analog output object to output the data at 8000 Hz.

set(ao, 'SampleRate', 8000);

Now, let's create the data to output to the sound card - a sine wave at two different frequencies.

t = linspace(0,2*pi,16000);
y = sin(500*t);
y1 = sin(800*t);

OUTPUTTING DATA. Outputting data involves functions that queue the data in the data acquisition engine, and functions that execute the analog output object.

QUEUING DATA. For data to be output, you must first queue it in the data acquisition engine with the PUTDATA command.

putdata(ao, data);

data is an m-by-n matrix where m is the number of samples to be output and n is the number of output channels.

putdata(ao, [y' y']);
putdata(ao, [y1' y1']);

The analog output object and the data acquisition engine are started with the START command. Starting the output object means that the hardware device and the data acquisition engine are both running. Running does not necessarily mean that the data is being output. For data to be output, a trigger must occur.

Let's configure the analog output object to trigger immediately after a START command is issued.

set(ao, 'TriggerType', 'Immediate');

Remember that you can always type the name of the object to get a summary of its current configuration.

ao
Display Summary of Analog Output (AO) Object Using 'NVIDIA(R) nForce(TM) Audio'.
       Output Parameters:  8000 samples per second on each channel.
      Trigger Parameters:  1 'Immediate' trigger on START.
           Engine status:  Waiting for START.
                           4 total sec. of data currently queued for START.
                           32000 samples currently queued by PUTDATA.
                           0 samples sent to output device since START.
AO object contains channel(s):
   Index:  ChannelName:  HwChannel:  OutputRange:  UnitsRange:  Units:   
   1       'Left'        1           [-1 1]        [-1 1]       'Volts'  
   2       'Right'       2           [-1 1]        [-1 1]       'Volts'  

The analog output object is started, and wait up to 5 seconds for the output to complete.

start(ao);
wait(ao,5);

You can repeatedly output the queued data by using the RepeatOutput property. If RepeatOutput is greater than 0, then all data queued before START is issued will be repeated the specified number of times.

Let's configure the analog output object to send the data to the sound card two times (or repeated once).

set(ao, 'RepeatOutput', 1);
putdata(ao, [y' y']);
putdata(ao, [y1' y1']);

The analog output object is started, and wait up to 9 seconds for the output to complete.

start(ao);
wait(ao,9);

This completes the introduction to analog output objects. Since the analog output object is no longer needed, you should delete it with the DELETE command to free memory and other physical resources.

delete(ao);