Data Acquisition Toolbox | ![]() ![]() |
Acquiring Data with a National Instruments Board
Suppose you must verify that the nominal frequency of a sine wave generated by a function generator is 1.00 kHz. To perform this task, you will input the function generator signal into a National Instruments board. You will then perform a fast Fourier transform (FFT) on the acquired data to find the nominal frequency of the generated sine wave. The setup for this task is shown below.
Configuring the Data Acquisition Session
For this example, you will acquire 1 second of data on one input channel. The board is set to a sampling rate of 10 kHz, which is well above the frequency of interest. After you connect the input signal to the board, you will trigger the acquisition one time using a manual trigger.
You can run this example by typing daqdoc4_2
at the MATLAB command line.
1. Create a device object - Create the analog input object AI
for a National Instruments board. The installed adaptors and hardware IDs are found with daqhwinfo
.
AI = analoginput('nidaq',1);
2. Add channels - Add one channel to AI
.
chan = addchannel(AI,0);
3. Configure property values - Assign values to the basic setup properties, and create the variables blocksize
and Fs
, which are used for subsequent analysis. The actual sampling rate is retrieved since it may be set by the engine to a value that differs from the specified value.
duration = 1; %1 second acquisition set(AI,'SampleRate',10000) ActualRate = get(AI,'SampleRate'); set(AI,'SamplesPerTrigger',duration*ActualRate) set(AI,'TriggerType','Manual') blocksize = get(AI,'SamplesPerTrigger'); Fs = ActualRate;
4. Acquire data - Start AI
, issue a manual trigger, and extract all data from the engine. Before start
is issued, you should begin inputting data from the function generator into the data acquisition board.
start(AI) trigger(AI) data = getdata(AI);
5. Clean up - When you no longer need AI
, you should remove it from memory and from the MATLAB workspace.
delete(AI) clear AI
Analyzing the Data
For this experiment, analysis consists of finding the frequency of the input signal and plotting the results. You can find the signal frequency with daqdocfft
.
[f,mag] = daqdocfft(data,Fs,blocksize);
This function, which is shown in Analyzing the Data, calculates the FFT of data
, and requires the values of SampleRate
and SamplesPerTrigger
as well as data
as inputs. daqdocfft
outputs the frequency and magnitude of data
, which you can then plot.
plot(f,mag)
grid on ylabel('Magnitude (dB)') xlabel('Frequency (Hz)') title('Frequency Output by Function Generator')
![]()
This plot shows the nominal frequency around 1000 Hz. A simple way to find actual frequency is shown below.
[ymax,maxindex]= max(mag); maxindex maxindex = 994
![]() | Acquiring Data with a Sound Card | Evaluating the Analog Input Object Status | ![]() |