【问题标题】:Creating a continuous tone in MATLAB whose frequency varies in real-time depending on user input在 MATLAB 中创建一个连续音调,其频率根据用户输入实时变化
【发布时间】:2013-06-28 18:54:08
【问题描述】:

我目前正在 MATLAB 中开发一个绘图程序,该程序接受输入并使用此输入将一个点映射到 x-y 空间。但是,程序还应该输出一个连续的音调,其频率会根据点的位置而变化。

我能够完成音调生成,但由于程序的性质,我无法让音调连续工作。 (音调生成之间的代码)我想我可以使用 parfor 循环来解决这个问题,该循环的代码改变循环的一次迭代中的频率,以及在另一个循环中生成音调但似乎无法得到它的代码,原因如下错误:

警告:临时可变频率将在 parfor 循环的每次迭代的开始。分配给的任何值 它在循环将丢失之前。如果在使用频率之前使用它 在 parfor 循环中分配,会发生运行时错误。见并行 MATLAB 中的 for 循环,“临时变量”。

在 multiThreadingtest 5 错误 using multiThreadingtest (line 5) Reference to a clear variable frequency.

原因: 引用已清除的变量 频率。

还有我的代码:

global frequency

frequency = 100;

parfor ii=1:2
    if ii==1
        Fs = 1000;
        nSeconds = 5;
        y = 100*sin(linspace(0, nSeconds*frequency*2*pi, round(nSeconds*Fs)));
        sound(y, Fs);
    elseif ii==2
        frequency = 100
        pause(2);
        frequency = 200
        pause(2);
        frequency = 300
        pause(2);
    end
end

【问题讨论】:

标签: multithreading matlab parfor tone-generator


【解决方案1】:

解决方案可能不是来自多线程,而是来自使用另一个函数来输出音调(audioplayer, play, stop)。 “audioplayer/play”能够输出时间重叠的声音。所以基本上,一个伪代码是:

get the value of the input  
generate/play a corresponding 5 second tone  
detect if any change in the input  
 if no change & elapsed time close to 5 seconds  
  generate/play an identical 5 second tone  
 if change   
  generate a new 5 second tone  
  %no overlapping
    stop old  
    play new  
  %overlapping (few milliseconds)
    play new
    stop old  

显示“声音”/“播放”差异的 matlab 代码。

Fs = 1000;  
nSeconds = 5;  

frequency = 100;  
y1 = 100*sin(linspace(0, nSeconds*frequency*2*pi, round(nSeconds*Fs)));  
aud1 = audioplayer(y1, Fs);  

frequency = 200;  
y2 = 100*sin(linspace(0, nSeconds*frequency*2*pi, round(nSeconds*Fs)));  
aud2 = audioplayer(y2, Fs);  


% overlapping sound impossible  
sound(y1, Fs);  
pause(1)  
sound(y2, Fs);  

% overlapping sound possible  
play(aud1);  
pause(1);  
disp('can compute here');
play(aud2);  

pause(1);  
stop(aud1);  
pause(1);  
stop(aud2);  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    相关资源
    最近更新 更多