MATLAB Examples

Acquire Acoustic Data from a Sound Card with the DAQ Toolbox Example

This example shows how to acquire and view acoustic data from a sound card using functionality from the Data Acquisition Toolbox.

Contents

Step 1: Install Support Package for Hardware Device

Use the installer to download the support package for your hardware:

>> supportPackageInstaller

I am going to install the DirectSound Audio support package which enables access to the Windows Sound Card.

Step 2: Discover Connected Hardware Devices

Next, we can run daq.getDevices to verify if our device is installed:

devices = daq.getDevices

% This command indicates that I have the Windows Sound Card support package
% installed.
% For more information on this, see the following documentation example:
%
% <matlab:web(fullfile(docroot,'daq/discover-hardware-devices.html')) Discover Hardware Devices Example>
%
% This lists all of the data acquisition devices that I can access using my
% sound card.
devices = 

Data acquisition devices:

index   Vendor    Device ID                      Description                      
----- ----------- --------- ------------------------------------------------------
1     directsound Audio0    DirectSound Primary Sound Capture Driver
2     directsound Audio1    DirectSound Microphone (Realtek High Definition Audio)
3     directsound Audio2    DirectSound HP 4120 Microphone (HP 4120)
4     directsound Audio3    DirectSound Transmit (4- Plantronics C610-M)
5     directsound Audio4    DirectSound Primary Sound Driver
6     directsound Audio5    DirectSound Receive (4- Plantronics C610-M)
7     directsound Audio6    DirectSound Speaker/HP (Realtek High Definition Audio)
8     directsound Audio7    DirectSound HP 4120 (HP 4120)

Step 3: Choose the Right Interface

There are two ways to communicate to a DAQ using the Data Acquisition Toolbox, "Session-Based Interface" and "Legacy Interface". The Session-Based Interface is newer and generally supports more functionality.

Choose the Right Interface - Here is a chart showing how to pick which interface to use.

This guide will use the Session-Based Interface.

Step 4: Create a Session

Next, we will create session with a microphone that is connected to my computer and is acquired by my soundcard.

s = daq.createSession('directsound')

% To see an example in the documentation, see the following:
%
% <matlab:web(fullfile(docroot,'daq/create-a-session-.html')) Create a Session Example>
s = 

Data acquisition session using DirectSound hardware:
   Will run for 1 second (44100 scans) at 44100 scans/second.
   No channels have been added.
   

Step 5: Acquire Data

There are 3 different data types that can be acquired, Analog, Digital and Audio. I know that I am using a soundcard. Therefore, I will acquire audio data.

Since I want to use the DirectSound Microphone, I will take a look at the second device:

devices(2)

% I will use the information that is returned to fill out the fields for
% the addAudioInputChannel method.

addAudioInputChannel(s,'Audio1','1','Audio')

% There are a couple of different ways to acquire data, Foreground vs.
% Background, and One-Time vs. Continuous.
%
% * Foreground blocks MATLAB until the data is received
% * Background does not block MATLAB
% * One-Time acquires the data once for a certain specified period of time or samples.
% * Continuous acquires data continuously.
%
% Generally, Foreground is performed with a one-time execution to prevent
% locking up MATLAB forever. Background tasks can be performed as a
% one-time or continuous execution.
%
ans = 

directsound: DirectSound Microphone (Realtek High Definition Audio) (Device ID: 'Audio1')
   Audio input subsystem supports:
      -1.0 to +1.0  range
      Rates from 80.0 to 1000000.0 scans/sec
      2 channels ('1','2')
      'Audio' measurement type
   



ans =

Data acquisition session using DirectSound hardware:
   Will run for 1 second (44100 scans) at 44100 scans/second.
   Number of channels: 1
      index Type Device Channel MeasurementType     Range     Name
      ----- ---- ------ ------- --------------- ------------- ----
      1     audi Audio1 1       Audio           -1.0 to +1.0
   


Foreground Acquisition

To acquire data in the Foreground, I can use the startForeground method:

tf = 5;
s.DurationInSeconds = tf;

% Have a speaker emit a sound witha a certain frequency near your laptop's
% speaker, then start the foreground acquisition.

display('Starting foreground acquisition');
data = startForeground(s);
display('Foreground acquisition finished');

% The frequency of this sound will be displayed here in the FFT:
t = linspace(0,tf,length(data));
h = plot(t,data);
helper_continuous_fft(data,s.Rate,h);
title('Discrete FFT Plot using Foreground Acquisition');
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
grid on;
Starting foreground acquisition
Foreground acquisition finished

Background Acquisition

To acquire data in the Background continuously, I can use the addlistener and startBackground methods:

Prepare session for continuous operation

s.IsContinuous = false;

% Set up the plot for an FFT of the live input
hf = figure('visible','off');
hp = plot(zeros(1000,1));
T = title('Discrete FFT Plot using Background Acquisition');
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
grid on;

% Add the listener method which shows the Fast Fourier Transform of the
% signal whenever data is available
plotFFT = @(src, event) helper_continuous_fft(event.Data, src.Rate, hp);
hl = addlistener(s, 'DataAvailable', plotFFT);

% Start the acquisition
display('Starting background acquisition');
startBackground(s);

% relaunch the figure
figure(hf);
pause(5);

% Stop the session
if s.IsDone
    display('Background acquisition finished');
    stop(s);
    s.IsContinuous = false;
    delete(hl);
end
Starting background acquisition