function xx = dtmfdial(keyNames,fs)
%DTMFDIAL Create a signal vector of tones which will dial
% a DTMF (Touch Tone) telephone system.
%
% usage: xx = dtmfdial(keyNames,fs)
% keyNames = vector of characters containing valid key names
% fs = sampling frequency
% xx = signal vector that is the concatenation of DTMF tones.
%
dtmf.keys = ...
['1','2','3','A';
 '4','5','6','B';
 '7','8','9','C';
 '*','0','#','D'];
%dtmf.colTones = ones(4,1)*[1209,1336,1477,1633];
%dtmf.rowTones = [697;770;852;941]*ones(1,4);
dtmf.colTones = [1209,1336,1477,1633];
dtmf.rowTones = [697,770,852,941];
% For loop: from k=1 to length(inputstr)
% do find(inputstr(k) ==dtmf.keys)
% generate the sum of two tones
% Add silence and sound to the output

silence = 0.05;
silence_num_samp = fix(silence*fs);

toneDuration = 0.2;
toneDur_num_samp = fix(toneDuration*fs);

xx = [];

for i = 1:length(keyNames)
    xx = [xx, zeros(1,silence_num_samp)];
    [ii, jj] = find(keyNames(i) == dtmf.keys);
    xx = [xx, (cos(2*pi*dtmf.colTones(jj)*(0:toneDur_num_samp)/fs) + cos(2*pi*dtmf.rowTones(ii)*(0:toneDur_num_samp)/fs))/2.001];
    %xx = [xx, (cos(2*pi*dtmf.colTones(ii, jj)*(0:toneDur_num_samp)/fs) + cos(2*pi*dtmf.rowTones(ii, jj)*(0:toneDur_num_samp)/fs))/2.001];
end

soundsc(xx, fs);

wavwrite(xx,fs,16,'dtmfdial.wav');