Data Acquisition File Description
The following MatLab code (in SimpleAcq.m) performs the following operations in sequence:
%1) Create a data acquistion object
% (and specify single ended inputs)
ai=analoginput('nidaq', 'Dev1');
ai.InputType='SingleEnded';
%2) Add a channel
ch=addchannel(ai,3);
%3) Change Properties to suit our needs
ai.SampleRate=10;
ai.SamplesPerTrigger=50;
%4) Do acquisition
start(ai)
wait(ai,10);
[y,t]=getdata(ai);
%5) Clean up
delete(ai);
clear ai
%6) Save Data (and plot)
save myfile t y
plot(t,y)
Let's look at these lines in detail.
- The first set of lines creates an analog input object, "ai" that will allow us to acquire analog voltages. The argument is "nidaq" which specifies the hardware we are using (a National Instruments Data AcQuisition system). The inputs are defined to be "SingleEnded", as opposed to "Differential". Single ended inputs are voltages measured relative to ground, a differential measurement is the difference between two voltage (this is sometimes useful in the presence of noise - you should cover this in E14).
%1) Create a data acquistion object
% (and specify single ended inputs)
ai=analoginput('nidaq', 'Dev1');
ai.InputType='SingleEnded';
- The second set of lines specifies that we will be using only one input, in this case channel 3. If you look on the front of the data acquisition box, you will see 4 analog input channels (labeled 0 to 3) on the left side.
%2) Add a channel
ch=addchannel(ai,3);
- The third set of lines are used to specify how fast to collect data (the sample rate, in this case 10 samples per second), and the number of points to acquire (in this case 50 - for a total of 5 seconds of data collection).
%3) Change Properties to suit our needs
ai.SampleRate=10;
ai.SamplesPerTrigger=50;
- The fourth set of lines actually performs the acquisition (the "start" command). We then wait for the acquisition to complete (the "waittilstop" command). The second argument in the "waittilstop" function call is a failsafe time (in seconds). If the acquisition hasn't completed by the time listed, it will kill the process and allow you to continue. Make sure this time is longer than your anticipated collection time. The last line retrieves the data (and a time vector) from the data acquisition object.
%4) Do acquisition
start(ai)
wait(ai,10);
[y,t]=getdata(ai);
- The fifth set of lines cleans up by deleting the unneeded objects in memory. If you don't do this and run several acquisitions, you will eventually run out of memory as new objects are created while older ones are not destroyed.
%5) Clean up
delete(ai);
clear ai
- Finally we save the data in a file (This creates a file with the filename "myfile.mat", you should use something that won't get confused with other groups files -- don't put spaces in the filename, I don't know how MatLab would handle that). I also plot the data.
%6) Save Data (and plot)
save myfile t y
plot(t,y)
You will have to make some minor adjustments to this code, but can use it roughly as is.