pls help me shortened my code.. i really need your help. .Especially in the if elseif statement.
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
here's what i have.
Fs = 8000;
t = 0.25;
n =1:ceil(t*Fs);
tone1 = sin(2*pi*(697/Fs)*n) + sin(2*pi*(1209/Fs)*n); %tone1=key one(1)
tone2 = sin(2*pi*(697/Fs)*n) + sin(2*pi*(1336/Fs)*n); %tone2=key two(2)
tone3 = sin(2*pi*(697/Fs)*n) + sin(2*pi*(1477/Fs)*n); %tone3=key three(3)
tone4 = sin(2*pi*(770/Fs)*n) + sin(2*pi*(1209/Fs)*n); %tone4=key four(4)
tone5 = sin(2*pi*(770/Fs)*n) + sin(2*pi*(1336/Fs)*n); %tone5=key five(5)
tone6 = sin(2*pi*(770/Fs)*n) + sin(2*pi*(1477/Fs)*n); %tone6=key six(6)
tone7 = sin(2*pi*(852/Fs)*n) + sin(2*pi*(1209/Fs)*n); %tone7=key seven(7)
tone8 = sin(2*pi*(852/Fs)*n) + sin(2*pi*(1336/Fs)*n); %tone8=key eight(8)
tone9 = sin(2*pi*(852/Fs)*n) + sin(2*pi*(1477/Fs)*n); %tone9=key nine(9)
tone0 = sin(2*pi*(941/Fs)*n) + sin(2*pi*(1336/Fs)*n); %tone0=key zero(0)
tone10 = sin(2*pi*(941/Fs)*n) + sin(2*pi*(1209/Fs)*n); %tone10=key asterisk(*)
tone11 = sin(2*pi*(941/Fs)*n) + sin(2*pi*(1477/Fs)*n); %tone11=key number sign(#)
v=input('Number of keys to enter: ');
display('KEY0 = 0');
display('KEY1 = 1');
display('KEY2 = 2');
display('KEY3 = 3');
display('KEY4 = 4');
display('KEY5 = 5');
display('KEY6 = 6');
display('KEY7 = 7');
display('KEY8 = 8');
display('KEY9 = 9');
display('KEY* = 10');
display('KEY# = 11');
w=input('Enter key/s: ' ,'s');
x=str2num(w); %#ok<ST2NM>
y=length(x);
while y ~= v;
display ('Error''Check the entered number');
w=input('Enter key/s again: ' ,'s');
x=str2num(w);
y=length(x);
end
if v==1; %when the number of key/s entered is/are one(1)
a=x(1,1);
if a==0;subplot(4,3,11);plot(tone0); sound(tone0);xlabel('KEY0');
elseif a==1;subplot(4,3,1); plot(tone1); sound(tone1);xlabel('KEY1');
elseif a==2; subplot(4,3,2);plot(tone2);sound(tone2);xlabel('KEY2');
elseif a==3;subplot(4,3,3);plot(tone3); sound(tone3);xlabel('KEY3');
elseif a==4;subplot(4,3,4);plot(tone4); sound(tone4);xlabel('KEY4');
elseif a==5;subplot(4,3,5);plot(tone5); sound(tone5);xlabel('KEY5');
elseif a==6;subplot(4,3,6);plot(tone6); sound(tone6);xlabel('KEY6');
elseif a==7;subplot(4,3,7);plot(tone7); sound(tone7);xlabel('KEY7');
elseif a==8;subplot(4,3,8);plot(tone8); sound(tone8);xlabel('KEY8');
elseif a==9;subplot(4,3,9);plot(tone9); sound(tone9);xlabel('KEY9');
elseif a==10;subplot(4,3,10);plot(tone10);sound(tone10);xlabel('KEY*');
elseif a==11;subplot(4,3,12);plot(tone11);sound(tone11);xlabel('KEY#');
end
elseif v==2; %when the number of key/s entered is/are tw0(2)
a=x(1,1);
if a==0;subplot(4,3,11);plot(tone0); sound(tone0);xlabel('KEY0');
elseif a==1;subplot(4,3,1); plot(tone1); sound(tone1);xlabel('KEY1');
elseif a==2; subplot(4,3,2);plot(tone2);sound(tone2);xlabel('KEY2');
elseif a==3;subplot(4,3,3);plot(tone3); sound(tone3);xlabel('KEY3');
elseif a==4;subplot(4,3,4);plot(tone4); sound(tone4);xlabel('KEY4');
elseif a==5;subplot(4,3,5);plot(tone5); sound(tone5);xlabel('KEY5');
elseif a==6;subplot(4,3,6);plot(tone6); sound(tone6);xlabel('KEY6');
elseif a==7;subplot(4,3,7);plot(tone7); sound(tone7);xlabel('KEY7');
elseif a==8;subplot(4,3,8);plot(tone8); sound(tone8);xlabel('KEY8');
elseif a==9;subplot(4,3,9);plot(tone9); sound(tone9);xlabel('KEY9');
elseif a==10;subplot(4,3,10);plot(tone10);sound(tone10);xlabel('KEY*');
elseif a==11;subplot(4,3,12);plot(tone11);sound(tone11);xlabel('KEY#');
end
pause(0.5);
b=x(1,2);
if b==0;subplot(4,3,11);plot(tone0); sound(tone0);xlabel('KEY0');
elseif b==1;subplot(4,3,1); plot(tone1); sound(tone1);xlabel('KEY1');
elseif b==2; subplot(4,3,2);plot(tone2);sound(tone2);xlabel('KEY2');
elseif b==3;subplot(4,3,3);plot(tone3); sound(tone3);xlabel('KEY3');
elseif b==4;subplot(4,3,4);plot(tone4); sound(tone4);xlabel('KEY4');
elseif b==5;subplot(4,3,5);plot(tone5); sound(tone5);xlabel('KEY5');
elseif b==6;subplot(4,3,6);plot(tone6); sound(tone6);xlabel('KEY6');
elseif b==7;subplot(4,3,7);plot(tone7); sound(tone7);xlabel('KEY7');
elseif b==8;subplot(4,3,8);plot(tone8); sound(tone8);xlabel('KEY8');
elseif b==9;subplot(4,3,9);plot(tone9); sound(tone9);xlabel('KEY9');
elseif b==10;subplot(4,3,10);plot(tone10);sound(tone10);xlabel('KEY*');
elseif b==11;subplot(4,3,12);plot(tone11);sound(tone11);xlabel('KEY#');
end
pause(0.5);
elseif v==3; %when the number of key/s entered is/are three(3)
a=x(1,1);
if a==0;subplot(4,3,11);plot(tone0); sound(tone0);xlabel('KEY0');
elseif a==1;subplot(4,3,1); plot(tone1); sound(tone1);xlabel('KEY1');
elseif a==2; subplot(4,3,2);plot(tone2);sound(tone2);xlabel('KEY2');
elseif a==3;subplot(4,3,3);plot(tone3); sound(tone3);xlabel('KEY3');
elseif a==4;subplot(4,3,4);plot(tone4); sound(tone4);xlabel('KEY4');
elseif a==5;subplot(4,3,5);plot(tone5); sound(tone5);xlabel('KEY5');
elseif a==6;subplot(4,3,6);plot(tone6); sound(tone6);xlabel('KEY6');
elseif a==7;subplot(4,3,7);plot(tone7); sound(tone7);xlabel('KEY7');
elseif a==8;subplot(4,3,8);plot(tone8); sound(tone8);xlabel('KEY8');
elseif a==9;subplot(4,3,9);plot(tone9); sound(tone9);xlabel('KEY9');
elseif a==10;subplot(4,3,10);plot(tone10);sound(tone10);xlabel('KEY*');
elseif a==11;subplot(4,3,12);plot(tone11);sound(tone11);xlabel('KEY#');
end
pause(0.5);
b=x(1,2);
if b==0;subplot(4,3,11);plot(tone0); sound(tone0);xlabel('KEY0');
elseif b==1;subplot(4,3,1); plot(tone1); sound(tone1);xlabel('KEY1');
elseif b==2; subplot(4,3,2);plot(tone2);sound(tone2);xlabel('KEY2');
elseif b==3;subplot(4,3,3);plot(tone3); sound(tone3);xlabel('KEY3');
elseif b==4;subplot(4,3,4);plot(tone4); sound(tone4);xlabel('KEY4');
elseif b==5;subplot(4,3,5);plot(tone5); sound(tone5);xlabel('KEY5');
elseif b==6;subplot(4,3,6);plot(tone6); sound(tone6);xlabel('KEY6');
elseif b==7;subplot(4,3,7);plot(tone7); sound(tone7);xlabel('KEY7');
elseif b==8;subplot(4,3,8);plot(tone8); sound(tone8);xlabel('KEY8');
elseif b==9;subplot(4,3,9);plot(tone9); sound(tone9);xlabel('KEY9');
elseif b==10;subplot(4,3,10);plot(tone10);sound(tone10);xlabel('KEY*');
elseif b==11;subplot(4,3,12);plot(tone11);sound(tone11);xlabel('KEY#');
end
pause(0.5);
c=x(1,3);
if c==0;subplot(4,3,11);plot(tone0); sound(tone0);xlabel('KEY0');
elseif c==1;subplot(4,3,1); plot(tone1); sound(tone1);xlabel('KEY1');
elseif c==2; subplot(4,3,2);plot(tone2);sound(tone2);xlabel('KEY2');
elseif c==3;subplot(4,3,3);plot(tone3); sound(tone3);xlabel('KEY3');
elseif c==4;subplot(4,3,4);plot(tone4); sound(tone4);xlabel('KEY4');
elseif c==5;subplot(4,3,5);plot(tone5); sound(tone5);xlabel('KEY5');
elseif c==6;subplot(4,3,6);plot(tone6); sound(tone6);xlabel('KEY6');
elseif c==7;subplot(4,3,7);plot(tone7); sound(tone7);xlabel('KEY7');
elseif c==8;subplot(4,3,8);plot(tone8); sound(tone8);xlabel('KEY8');
elseif c==9;subplot(4,3,9);plot(tone9); sound(tone9);xlabel('KEY9');
elseif c==10;subplot(4,3,10);plot(tone10);sound(tone10);xlabel('KEY*');
elseif c==11;subplot(4,3,12);plot(tone11);sound(tone11);xlabel('KEY#');
end
end
0 Commenti
Risposta accettata
Stephen23
il 17 Set 2015
Modificato: Stephen23
il 17 Set 2015
Here is the entire functionality in about twenty lines:
Fs = 8000;
t = 0.25;
N = 1:ceil(t*Fs);
% define DTMF:
R = [697,770,852,941]; % Hz, rows
C = [1209,1336,1477]; % Hz, columns
[Ra,Ca] = meshgrid(R,C); % Hz, all
Rb = 2*pi*(Ra(:)/Fs);
Cb = 2*pi*(Ca(:)/Fs);
T = sin(Rb*N) + sin(Cb*N);
% get user input:
J = '123456789*0#'; % subplot is by row
I = input('Enter keys/s [0:9*#]: ','s');
assert(all(ismember(I,J)),'An invalid key was entered')
% plot and play:
for k = 1:numel(I)
X = strfind(J,I(k));
subplot(4,3,X);
plot(T(X,:));
xlabel(sprintf('KEY%s',I(k)))
sound(T(X,:));
pause(0.5);
end
3 Commenti
Stephen23
il 17 Set 2015
Modificato: Stephen23
il 17 Set 2015
Easy, just replace the assert line with these three lines:
while ~all(ismember(I,J))
I = input('An invalid character was used. Enter all keys/s [0:9*#]: ','s');
end
Note that the only way to exit this loop is to enter only correct characters. You might want to consider some "quit" option as well.
Più risposte (1)
Walter Roberson
il 17 Set 2015
I recommend that you learn to use cell arrays. Or 2 dimensional arrays.
tone(1+0,:) = sin(2*pi*(941/Fs)*n) + sin(2*pi*(1336/Fs)*n); %tone0=key zero(0)
tone(1+1,:) = sin(2*pi*(697/Fs)*n) + sin(2*pi*(1209/Fs)*n); %tone1=key one(1)
tone(1+2,:) = sin(2*pi*(697/Fs)*n) + sin(2*pi*(1336/Fs)*n); %tone2=key two(2)
tone(1+3,:) = sin(2*pi*(697/Fs)*n) + sin(2*pi*(1477/Fs)*n); %tone3=key three(3)
and so on
then
sound(tone(1+c,:))
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!