How to stream audio data to a scope in simulink -- I have working Matlab code, stuck when I try to convert it into a Simulink model. Please help!

7 views (last 30 days)
I would like to acquire and display audio data in a continuous fashion from my sound card using Simulink. The following code does this in Matlab (see below). How can I do this in Simulink?
I am aware of this example, which uses blocks from the DAQ toolbox:
However, I would like to use a user-defined block of some kind rather than the DAQ block. The reason is that I am using this code as a way to learn how to work with data from a different device, and the DAQ block does not work with the other device.
It seems like changing the code to simulink should require 1. using the model callback "InitFcn" to initially trigger a program that creates the ai object and initializes its parameters 2. using some sort of user-defined function block to execute the " data = peekdata(ai,ai.SampleRate); " line, i.e. to get the data. This is where I'm stuck... 3. using the model callback "CloseFcn" to run the final cleanup commands
The problem I'm having with (2) is: The way to do this seems to be a "Matlab Function" block. This requires an input and an output. The output would obviously be what I'm calling "data" in the code below. But what would the input be? And how does this function know about ai? Does not seem to be possible to pass ai in...
Thanks in advance for any help!
duration=10; % seconds
ai = analoginput('winsound',0);
addchannel(ai, 1);
ai.SampleRate = 8000; % sample frequency
ai.SamplesPerTrigger = ai.SampleRate*duration;% total of samples to acquire
% Start acquisition
warning ('off');
while isrunning(ai)
data = peekdata(ai,ai.SampleRate);
ylim([-.1 .1]);
warning ('on');

Answers (1)

Shankar Subramanian
Shankar Subramanian on 25 Jun 2015
Hi Brandon,
Can you specify what hardware that you are using that does not work with Simulink but works with MATLAB?
If you are acquiring, you need to have the ML Function block as a source block (no inputs). Remove the default input (u) from underlying code and you should not have the input port. To create the object, try using a persistent variable that creates it once in the underlying function itself.
Something like this:
persistent daqObj
if isempty(daqObj)
% Create the object
% Set Properties
% Start.
% Acquire and assign to y.

Sign in to comment.


Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!