Clear Filters
Clear Filters

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!

9 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.
Brandon on 26 Jun 2015
Edited: Brandon on 27 Jun 2015
Thanks for your reply. The hardware I am using that does not work with Simulink but works with MATLAB is a an EEG amplifier made by g.tec, called g.USBamp.
I'm afraid I don't fully understand your answer. Trying to follow your suggestion, I created a simulink "MATLAB function" block containing the following code. I connected the output of this block to a scope, in hopes of seeing the sound signals streaming out.
This arrangement did result in a block with an output but no input, as desired. However, I get an error saying "The 'analoginput' class does not support code generation."
Can you help me see what I'm still missing?
function y = fcn
persistent ai
if isempty(ai)
% Create the object
ai = analoginput('winsound',0);
% Set Properties
channels=[1:2];% with winsound
addchannel(ai, channels);% determine the channel(s) to be used, here 1 to 6
% Set the sample rate and samples per trigger
ai.SampleRate = 8000;% sample frequency
ai.SamplesPerTrigger = ai.SampleRate*150;% total of samples to acquire
% Start acquisition
warning ('off');% in the beginning of the acquisition the number of samples is not always available
% Acquire and assign to y.
while isrunning(ai) % while ai is running
y= peekdata(ai,ai.SampleRate);% non blocking function
warning ('on');
stop(ai);% ensure the device is safely stoped
delete(ai);% delete the object ai

Sign in to comment.


Find more on Simultaneous and Synchronized Operations 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!