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!

5 ビュー (過去 30 日間)
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
start(ai);
warning ('off');
figure(1)
while isrunning(ai)
data = peekdata(ai,ai.SampleRate);
plot(data);
ylim([-.1 .1]);
drawnow;
end
warning ('on');
stop(ai);
delete(ai);

回答 (1 件)

Shankar Subramanian
Shankar Subramanian 2015 年 6 月 25 日
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
end
% Set Properties
% Start.
% Acquire and assign to y.
Thanks
Shankar
  2 件のコメント
Brandon
Brandon 2015 年 6 月 26 日
編集済み: Brandon 2015 年 6 月 27 日
Shankar,
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?
Thanks!
function y = fcn
%#codegen
persistent ai
if isempty(ai)
% Create the object
ai = analoginput('winsound',0);
end
% 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
start(ai);
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
end
warning ('on');
stop(ai);% ensure the device is safely stoped
delete(ai);% delete the object ai
ashish kumar
ashish kumar 2018 年 12 月 4 日
use 'coder.extrinsic' for 'analoginput' in your MATLAB function
coder.extrinsic('analoginput')

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEmbedded Coder Supported Hardware についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by