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
chan = addchannel(ao,0);
Where 0 is the output channel number
Next, set the update rate to 4000Hz.
Set the buffer repeat to 50 times in a row.
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
ai = analoginput('mcc',0);
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
The start command will start the analog output
And to read back the signal I will start the analog input, get the data and plot the data.
yy = getdata(ai);
Now save the script as outputdata. Now let’s test the script…
That's all there is to it! Thanks for watching! Visit our website for more information about Data Acquisition devices and the software that controls them. 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); chan = addchannel(ao,0);
%set analog output update rate set(ao,'SampleRate',4000);
%repeat the buffer 50 times set(ao,'RepeatOutput',50);
%use analog input to read the waveform ai = analoginput('mcc',0); ch = addchannel( ai, 0 );
%use the transpose function and send the data to ao putdata(ao,transpose(y));
%start the waveform start(ao);
%read it back in using analog input start(ai);
yy = getdata(ai); plot(yy);