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!
10 次查看(过去 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);
0 个评论
回答(1 个)
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 个评论
ashish kumar
2018-12-4
use 'coder.extrinsic' for 'analoginput' in your MATLAB function
coder.extrinsic('analoginput')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!