PSYCHTOOLBOX工具箱及MATLAB编程实例
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人

4.3 函数示例

在前面的脚本示例中,程序编写好后,假如要使用其他的采样频率、声音频率或时间长度,均需要通过修改程序源代码后,才能得到想要的效果,接下来看函数在这方面的灵活性。

文件名:playnoisef.m(文件位于Psyfeng\Little_Examples目录下)

function playnoisef(sf,duration,lowfreqs,highfreqs,loops)
%Play some noise :)
%First generate the beep with random freq,
%then play it with function sound
%The meaning of the parameter as follows:
%playnoisef(sf,duration,freqs,loops)
%sf:samplerate,such as 44100
%duration:the duration of each noise in secs. such as 0.5
%freqs:the range of the freqs, such as [100 1000]
%loops:the number of loops
%We with the MakeBeep to generate the noise
for i=1:loops %循环播放
    r=randi([lowfreqs highfreqs]); %从范围中随机频率
    s1=MakeBeep(r,duration,sf); %生成噪音
    sound(s1,sf); %播放
end
end