Easy to Use | Easy to Integrate | Easy to Support

# Measurement Computing Data Acquisition Knowledgebase

Home Tags Glossary
Welcome Guest ( )

# MatLab Analog Output Video Script

Welcome to using Measurement Computing devices with Matlab. Today I will be using the USB-1608GX-2AO to output a complex waveform.
Let’s begin…Start a new script file to hold the commands.
First, I am going to create a variable to represent the size or length of the waveform.
>>count = 1000;

Now use count to calculate the angle per point or step.
>>angle = 2 * pi / count;

Now create an array of steps
>>x = (0:count-1) * angle;

In order to generate a waveform I need to create an array of discrete data points that represents a simple function of time.
>>y = .8 * sin(x) + 1.5 * sin(x * 2) + .8 * sin(x * 3);
Here I have an array that has three sinewaves added together

Now I need to get the analog output object which contains the output settings.
>>ao = analogoutput( ‘mcc’, 0 );
The string ‘mcc’ is the driver identifier and the 0 is the board number assigned by the InstaCal program.

Now I need to add a channel to the analog output
Where 0 is the output channel number

Next, set the update rate to 4000Hz.
>>set(ao,'SampleRate',4000);

Set the buffer repeat to 50 times in a row.
>>set(ao,'RepeatOutput',50);

Next, for output verification use a wire connected from analog output to analog input.
Now add the analog input and channel objects. I will use the default settings since analog input starts immediately and samples for one second at 1000Hz. This means my data will have four cycles of the waveform
>>ch = addchannel( ai, 0 );

Before sending the data to the analog output it must be reformatted to a single column so use the transpose function and send the data to the buffer
>>putdata(ao,transpose(y));

The start command will start the analog output
>>start(ao);

And to read back the signal I will start the analog input, get the data and plot the data.
>>start(ai);
>>yy = getdata(ai);
>>plot(yy);

Now save the script as outputdata. Now let’s test the script…
>>outputdata;
For a full copy of this script please refer to the transcript listing for this video.

Generator script
%Waveform length
count = 1000;

%calculate complex waveform
angle = 2*pi/count;
x = (0:count-1) * angle;

% Hz = Update Rate / Waveform length so x = 4Hz
y = .8 * sin(x) + 1.5 * sin(x * 2) + .8 * sin(x * 3);

%get analog output object
ao = analogoutput('mcc',0);

%set analog output update rate
set(ao,'SampleRate',4000);

%repeat the buffer 50 times
set(ao,'RepeatOutput',50);

ch = addchannel( ai, 0 );

%use the transpose function and send the data to ao
putdata(ao,transpose(y));

%start the waveform
start(ao);

start(ai);

yy = getdata(ai);
plot(yy);