创建一个MATLAB函数,产生给定电话号码的触摸音(使用sound( ) 函数来实现)。你的函数必须接受以下输入: - 被拨打的电话号码(应能接受多达10位数的数字)
时间: 2024-05-22 22:12:47 浏览: 12
function touchTone(phoneNumber)
% create a map to get the frequency of each number
freqMap = containers.Map([1 2 3 4 5 6 7 8 9 0], ...
[1209 1336 1477 1633 697 770 852 941 1209 941]);
% create arrays for each row and column frequency
rowFreq = [697 770 852 941];
colFreq = [1209 1336 1477 1633];
% convert the phone number to an array of integers
numArray = str2double(num2str(phoneNumber))';
% loop through each number in the phone number
for i = 1:length(numArray)
% get the frequency for the row and column of the number
row = mod(numArray(i)-1, 4) + 1;
col = floor((numArray(i)-1)/4) + 1;
rowFreqVal = rowFreq(row);
colFreqVal = colFreq(col);
% generate the tone for the number
tone = sin(2*pi*rowFreqVal*(0:0.001:0.2)) + ...
sin(2*pi*colFreqVal*(0:0.001:0.2));
% play the tone
sound(tone);
% pause for a short time between each number
pause(0.1);
end
end