Data Acquisition Toolbox

Acquiring Data with a Sound Card

A typical data acquisition session consists of these four steps:

  1. Initialization: Creating a device object.
2. Configuration: Adding channels and controlling acquisition behavior with properties.
3. Execution: Starting the device object and acquiring or sending data.
4. Termination: Deleting the device object.

To verify that the fundamental frequency for the tuning fork is 440Hz, a tone is acquired and then analyzed in MATLAB. This is the setup for the example described below.

For this example, we will verify that the fundamental (lowest) frequency of a tuning fork is 440 Hz. To do this, we will use a microphone and a sound card to collect sound level data. Next, we will perform an FFT on the acquired data to find the frequency components of the tuning fork.

We begin by acquiring two seconds of sound level data on one sound card channel. Since the tuning fork vibrates at a nominal frequency of 440 Hz, the sound card sampling rate can be set to its lowest sampling rate of 8000 Hz.

After we have set the tuning fork vibrating and placed it near the microphone, we will trigger the acquisition. The complete data acquisition session for the sound card is shown below.


The first step is to create the analog input object (AI) for the sound card.

AI = analoginput('winsound');


Next, we add a single channel to AI, and set the sample rate to 8000 Hz with an acquisition duration of 2 seconds:

addchannel(AI, 1);
Fs = 8000;              % Sample Rate is 8000 Hz
set (AI, 'SampleRate', Fs)
duration = 2;           % 2 second acquisition
set(AI, 'SamplesPerTrigger', duration*Fs);


Now, we are ready to start the acquisition. The default trigger behavior is to start collecting data as soon as the start command is issued. Before doing so, you should strike the tuning fork to begin supplying a tone to the microphone (whistling will work as well).


To retrieve all the data

data = getdata(AI);


The acquisition ends once all the data is acquired. To end the acquisition session, we can delete the AI object from the workspace:



Let's now determine the frequency components of the tuning fork and plot the results. First, we calculate the absolute value of the FFT of the data.

xfft = abs(fft(data));

Next we convert the absolute value into dB magnitude and extract the real frequency components:

mag = 20*log10(xfft);
mag = mag(1:end/2);

The results show the fundamental frequency to be around 440 Hz and the first overtone to be around 880 Hz. A simple way to find actual fundamental frequency is:


The answer is 441 Hz.

Using Different Hardware

This example could also be repeated using different hardware by simply changing two lines of code. For example, if we were to use a National Instruments multifunction card then we could create the analog input object using:


Likewise, if we were to use a Measurement Computing board to acquire the data, the code would read:


More Examples

To see more Data Acquisition Toolbox example applications, please visit the repository of applications submitted by users and developers on MATLAB Central.