|On this page…|
This section provides an example that builds a simple model using the block in conjunction with a block from another block library. It illustrates how to bring live analog data into Simulink® from a data acquisition device, in this case a sound card.
Note: You cannot use the legacy interface on 64-bit MATLAB®. See Working with the Session-Based Interface to acquire and generate data on a 64-bit MATLAB.
To use the Analog Input block, you must open the Data Acquisition Toolbox™ block library. To open the library, start the Simulink Library Browser and select Data Acquisition Toolbox software entry from the list displayed in the browser.
Note: You cannot use the legacy interface on 64-bit MATLAB. See Working with the Session-Based Interface to acquire and generate data on a 64-bit MATLAB.
To start the Simulink Library Browser, enter
at the MATLAB Command window. In the Simulink Library Browser, the left pane lists the available block libraries. To open the Data Acquisition Toolbox block library, click its icon.
To use a block, you must add it to an existing model or create a new model.
Create a new model by clicking the Create a new model button in the Simulink Library Browser.
To use the Analog Input block in a model, click the block in the library and, holding the left mouse button down, drag it into the Simulink editor. Note how the name on the block changes to reflect the first available analog device connected to your system.
To illustrate using the block, this example creates a simple model that acquires analog data from a microphone, via a sound card (the analog device), and then outputs the data to a scope, where you can see the intensity of the sound waves. To create this model, this example uses a Scope block from the basic Simulink block library.
Expand the Simulink block library by clicking Simulink at the top of the library list, if it is not already open. In the library window, open the Sinks group. From this group, click the Scope block in the library and, holding the left mouse button down, drag the block into the Simulink editor.
To specify Analog Input block parameter settings, double-click the block's icon in the Simulink editor. This opens the Source Block Parameters dialog box for the Analog Input block, shown in the following figure. Use the various fields to determine the current values of the Analog Input block parameters or to change the values.
In this example, keep the default settings for everything except Block size. Change the block size setting to 5, which means five samples will be acquired from each channel at every time step. As you can see in the dialog box, the acquisition will be asynchronous, and the left and right channels will both use the same port, since the 1 for all hardware channels option is selected for Number of ports.
After changing the block size to 5, click OK to close the dialog box. For more information on the options and the Analog Input block, see the Analog Input block reference page.
Connect the output from the Analog Input block to the Scope. Use the cursor in the model to drag a connection from the port of the Analog Input block to the scope.
Before running the simulation, change the run time to 20 seconds by editing the default of 10 seconds in the Simulink editortoolbar.
Open the scope by double-clicking the Scope block in the model. You will see live sound waves in the scope when the model is running.
Run the simulation by clicking the Run toolbar button. During the 20 seconds that the simulation is running, speak into the microphone.
While the simulation is running, the status bar at the bottom of the Simulink editor indicates the progress of the simulation. If you are speaking into the microphone, you will also see the live sound data plotted in the scope.
When the 20 seconds elapses, the model stops running and you will have 20 seconds of sound data displayed by the scope. Click the Autoscale toolbar button (binoculars icon) in the scope to see the portion of the collected data that has the most contrast or significance. It will look something like this:
Note in the above example that words were spoken into the microphone between the 7th and 8th second, and only ambient sound is picked up between the 9th and 10th second.
In the following example, you can see that the volume of the sound peaked around the 18th second, when shouting was picked up by the microphone.