when making array of several sound signals, it doesn't give correct sounds when indexing each element

15 views (last 30 days)
Mariam Atef
Mariam Atef on 26 Nov 2021
Edited: DGM on 26 Nov 2021
Hello, I'm a matlab beginner. For my school assignemnt, we're asked to write a matlab code that generates a DTMF sound for the correspondign phone number entered. to create the tones signals, I've done as follow:
%tones signals
one = 0.15*(cos(piT*lfs(1))+cos(piT*hfs(1)));
two = 0.15*(cos(piT*lfs(1))+cos(piT*hfs(2)));
three = 0.15*(cos(piT*lfs(1))+cos(piT*hfs(3)));
four = 0.15*(cos(piT*lfs(2))+cos(piT*hfs(1)));
five = 0.15*(cos(piT*lfs(2))+cos(piT*hfs(2)));
six = 0.15*(cos(piT*lfs(2))+cos(piT*hfs(3)));
seven = 0.15*(cos(piT*lfs(3))+cos(piT*hfs(1)));
eight = 0.15*(cos(piT*lfs(3))+cos(piT*hfs(2)));
nine = 0.15*(cos(piT*lfs(3))+cos(piT*hfs(3)));
asterisk = 0.15*(cos(piT*lfs(4))+cos(piT*hfs(1)));
zero = 0.15*(cos(piT*lfs(4))+cos(piT*hfs(2)))
hash = 0.15*(cos(piT*lfs(4))+cos(piT*hfs(3)));
However, I don't like the redundance and I wish if I could place them all in an array (using 2 nested for loops) then index the required tone through that one array when needed. I've failed in my trial as I recieve wrong sounds afterwards whenever i try to play any of them.
Can anybody help me what should I exactly do?
Thanks in advance

Accepted Answer

DGM on 26 Nov 2021
Edited: DGM on 26 Nov 2021
Something like this
keys = '8675309';
toneduration = 0.4;
spaceduration = 0.1;
fs = 8000;
sourcetones = [697 770 852 941 1209 1336 1477 1633];
symbols = {'1','2','3','A';'4','5','6','B';'7','8','9','C';'*','0','#','D'};
t = linspace(0,toneduration,round(toneduration*fs));
signal = [];
for k = 1:numel(keys)
% associate key with high and low frequency pair
[row col] = find(strcmpi(keys(k),symbols));
FL = sourcetones(row);
FH = sourcetones(col+4);
% append space where necessary
if k~=1
signal = [signal zeros(1,round(spaceduration*fs))];
% append tone segment
signal = [signal 0.5*(sin(2*pi*FL*t) + sin(2*pi*FH*t))];
There are plenty of examples of encoders/decoders out there.

More Answers (0)




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!