函数回调('StopFcn','TimerFcn')对于MATLAB中的audiorecorder对象? [英] Function callback ('StopFcn' , 'TimerFcn' )for audiorecorder object in MATLAB?

查看:1143
本文介绍了函数回调('StopFcn','TimerFcn')对于MATLAB中的audiorecorder对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

所以我试试这个代码。

% assume fs,winsize,winshift is given.

T = 0.1; % in seconds
samples = cell{100,1};
r = audiorecorder(fs,16,1);
k=1;
r.TimerPeriod = 0.1;
r.StopFcn = 'samples{k} = getaudiodata(r);';
r.TimerFcn = {@get_pitch,samples{k},winsize,winshift};

while 1
     record(r,T);
     k=k+1;
end      



我想执行函数'get_pitch(samples,fs,winsize,winshift )',而在录制期间通过audiorecorder对象。

I want to execute the function 'get_pitch(samples,fs,winsize,winshift)' while during recording through audiorecorder object.

但是在执行期间发生下列异常。

But following exception occurs during execution.

1)执行记录(r,T)后。 (StopFcn现在调用)?在调用StopFcn(现在调用TimerFcn)之后,错误使用==> eval未定义的函数或变量'r'。

1) after record(r,T) is executed. (StopFcn is now called) ??? Error using ==> eval Undefined function or variable 'r'.

在这个阶段,get_pitch函数完全错误参数。例如,位置样本{k}中的参数更改为audiorecorder object。

2) after StopFcn is called (TimerFcn is now called) In this phase, get_pitch function have totally wrong parameters. For example, parameter in the position samples{k} change to 'audiorecorder object'.

似乎我不知道'StopFcn' 'TimerFcn'。

It seems that I do not know exact use of 'StopFcn' & 'TimerFcn'.

有没有人能给我一些建议?非常感谢您的所有意见。

Is there anyone who can give me some advice? I really appreciate all of your comments.

推荐答案

查看在文档中的示例我建议尝试在您的循环中调用getaudiodata(r),而不是使用CallBack。这样的东西:

Looking at the example in the documentation I would recommend trying to call getaudiodata(r) in your loop rather than with the CallBack. So something like this:

% assume fs,winsize,winshift is given.

T = 0.1; % in seconds
samples = cell{100,1};
r = audiorecorder(fs,16,1);
k=1;
r.TimerPeriod = 0.1;
r.StopFcn = 'disp(''Completed sample '', k)';
r.TimerFcn = {@get_pitch,samples{k - 1},winsize,winshift};

while 1
     record(r,T);
     samples{k} =  getaudiodata(r);
     k=k+1;
end   

注意我改变了 r.TimerFcn 使用 samples {k - 1} 而不是 k ,因为k会在timerfcn被调用之前递增。所以这可能给你的第一个样本的问题,你将不得不调整一点。这也是一个无限循环,我相信你会想解决。

Note I changed the r.TimerFcn to use samples{k - 1} instead of k because k will increment before the timerfcn gets called. So this might give you issues with your first sample, you'll have to tweak it a little. Also this is an infinite loop, which I'm sure you'll want to address.

这篇关于函数回调('StopFcn','TimerFcn')对于MATLAB中的audiorecorder对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆