Data Acquisition Toolbox | ![]() ![]() |
Acquiring Data with a Sound Card
Suppose you must verify that the fundamental (lowest) frequency of a tuning fork is 440 Hz. To perform this task, you will use a microphone and a sound card to collect sound level data. You will then perform a fast Fourier transform (FFT) on the acquired data to find the frequency components of the tuning fork. The setup for this task is shown below.
Configuring the Data Acquisition Session
For this example, you will acquire 1 second of sound level data on one sound card channel. Since the tuning fork vibrates at a nominal frequency of 440 Hz, you can configure the sound card to its lowest sampling rate of 8000 Hz. Even at this lowest rate, you should not experience any aliasing effects since the tuning fork will not have significant spectral content above 4000 Hz, which is the Nyquist frequency. After you set the tuning fork vibrating and place it near the microphone, you will trigger the acquisition one time using a manual trigger.
You can run this example by typing daqdoc4_1
at the MATLAB command line.
1. Create a device object - Create the analog input object AI
for a sound card. The installed adaptors and hardware IDs are found with daqhwinfo
.
AI = analoginput('winsound');
2. Add channels - Add one channel to AI
.
chan = addchannel(AI,1);
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',8000) 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 tuning fork into the sound card.
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 example, analysis consists of finding the frequency components of the tuning fork and plotting the results. To do so, the function daqdocfft
was created. This function calculates the FFT of data
, and requires the values of SampleRate
and SamplesPerTrigger
as well as data
as inputs.
[f,mag] = daqdocfft(data,Fs,blocksize);
daqdocfft
outputs the frequency and magnitude of data
, which you can then plot. daqdocfft
is shown below.
function [f,mag] = daqdocfft(data,Fs,blocksize) % [F,MAG]=DAQDOCFFT(X,FS,BLOCKSIZE) calculates the FFT of X % using sampling frequency FS and the SamplesPerTrigger % provided in BLOCKSIZE xfft = abs(fft(data)); mag = 20*log10(xfft); mag = mag(1:blocksize/2); f = (0:length(mag)-1)*Fs/blocksize; f = f(:);
plot(f,mag) grid on ylabel('Magnitude (dB)') xlabel('Frequency (Hz)') title('Frequency Components of Tuning Fork')
![]()
The plot shows the fundamental frequency around 440 Hz and the first overtone around 880 Hz. A simple way to find actual fundamental frequency is
[ymax,maxindex]= max(mag); maxindex maxindex = 441
![]() | Analog Input Examples | Acquiring Data with a National Instruments Board | ![]() |