% Generation of a DTMF test signal
% (pause between pulse=t_p (ms), tone pulse duracion=t_t (ms))
% with noise and interferences
% ready to use with octave
% to use with MATLAB you must replace play_so.m by sound.m
%
% March 17th 2017 

close 'all',clear,clc

%%  parameters
for test=[0:12]
% test 3Vpp
% noise Vpp
An=0;
% others Vp
Ab=0;Ar=0;Ad=0;
% amplitude of each DTMF tone
amp=(1-An/2-Ab-Ar-Ad)/2;

switch test
    case 0,t_p=100;t_t=10000;tone=['1'];leng=zeros(size(tone));leng(:)=t_t;leng(tone==" ")=t_p;leng=leng*1e-3;An=0;Ab=0;Ar=0;Ad=0;amp=(1-An/2-Ab-Ar-Ad)/2;
    case 1,t_p=100;t_t=100;tone=[' 7 6 3 1 0'];leng=zeros(size(tone));leng(:)=t_t;leng(tone==" ")=t_p;leng=leng*1e-3;
        An=0;Ab=0;Ar=0;Ad=0;amp=(1-An/2-Ab-Ar-Ad)/2;
    case 2,t_p=100;t_t=100;tone=[' 7 6 3881 0'];leng=zeros(size(tone));leng(:)=t_t;leng(tone==" ")=t_p;leng=leng*1e-3;
        An=0;Ab=0;Ar=0;Ad=0;amp=(1-An/2-Ab-Ar-Ad)/2;
    case 3,t_p=25;t_t=25;tone=[' 7 6 3 1 0 '];leng=zeros(size(tone));leng(:)=t_t;leng(tone==" ")=t_p;leng=leng*1e-3;
        An=0;Ab=0;Ar=0;Ad=0;amp=(1-An/2-Ab-Ar-Ad)/2;
    case 4,t_p=10;t_t=10;tone=[' 7 6 3 1 0'];leng=zeros(size(tone));leng(:)=t_t;leng(tone==" ")=t_p;leng=leng*1e-3;
        An=0;Ab=0;Ar=0;Ad=0;amp=(1-An/2-Ab-Ar-Ad)/2;
    case 5,t_p=100;t_t=100;tone=[' 7 6 3 1 0'];leng=zeros(size(tone));leng(:)=t_t;leng(tone==" ")=t_p;leng=leng*1e-3;
        An=0;Ab=0;Ar=0;Ad=0;amp=(1-An/2-Ab-Ar-Ad)/2;
        amp=amp*.3;
    case 6,t_p=100;t_t=100;tone=[' 1 2 3 4 5 6 7 8 9 0 A B C D  * # b r d'];leng=zeros(size(tone));leng(:)=t_t;leng(tone==" ")=t_p;leng=leng*1e-3;
        An=0;Ab=0;Ar=0;Ad=0;amp=(1-An/2-Ab-Ar-Ad)/2;
    case 7,t_p=100;t_t=100;tone=[' 1 2 3 4 5 6 7 8 9 0 A B C D  * #'];leng=zeros(size(tone));leng(:)=t_t;leng(tone==" ")=t_p;leng=leng*1e-3;
        An=1;Ab=0;Ar=0;Ad=0;amp=(1-An/2-Ab-Ar-Ad)/2;  
    case 8,t_p=100;t_t=100;tone=[' 1 2 3 4 5 6 7 8 9 0 A B C D  * #'];leng=zeros(size(tone));leng(:)=t_t;leng(tone==" ")=t_p;leng=leng*1e-3;
        An=1;Ab=0;Ar=0.25;Ad=0;amp=(1-An/2-Ab-Ar-Ad)/2;%amp=.125
    case 9,t_p=100;t_t=100;tone=[' 1 2 3 4 5 6 7 8 9 0 A B C D  * #'];leng=zeros(size(tone));leng(:)=t_t;leng(tone==" ")=t_p;leng=leng*1e-3;
        An=1;Ab=0;Ar=0;Ad=0.25;amp=(1-An/2-Ab-Ar-Ad)/2;
    case 10,t_p=100;t_t=100;tone=[' 1 2 3 4 5 6 7 8 9 0 A B C D  * #'];leng=zeros(size(tone));leng(:)=t_t;leng(tone==" ")=t_p;leng=leng*1e-3;
        An=1;Ab=0.25;Ar=0;Ad=0;amp=(1-An/2-Ab-Ar-Ad)/2;   
    case 11,t_p=100;t_t=100;tone=[' 7 7 3 0 8'];leng=zeros(size(tone));leng(:)=t_t;leng(tone==" ")=t_p;leng=leng*1e-3;
        An=0;Ab=0;Ar=0;Ad=0;amp=(1-An/2-Ab-Ar-Ad)/2;
    case 12,t_p=50;t_t=50;tone=['AAAAAAAAAAAAAAAA'];leng=zeros(size(tone));leng(:)=t_t;leng(tone==" ")=t_p;leng=leng*1e-3;
        An=0;Ab=0;Ar=0;Ad=0;amp=(1-An/2-Ab-Ar-Ad)/2;
        
end

% show parameters
test,amp,An,others=Ab+Ar+Ad

Fs=48e3;
t=0:1/Fs:sum(leng)-1/Fs;

% DTMF synthesis
f_col=[697  770  852  941 425]';
f_row=[1209 1336 1477 1633];

% Matrices
M=['1','2','3','A';
   '4','5','6','B';
   '7','8','9','C';
   '*','0','#','D';
   'b','r','d'," "];
F1=repmat(f_col,1,4);
F2=repmat(f_row,5,1);
A1=amp*ones(5,4);A1(5,4)=0;
A2=amp*ones(5,4);A2(5,:)=0;
leng2=[0 leng];

%% signal synthesis
x=0*t;
for i=1:length(tone)
    f1=F1(M==tone(i));
    f2=F2(M==tone(i));
    a1=A1(M==tone(i));
    a2=A2(M==tone(i));
    tin=sum(leng2(1:i));
    tfin=tin+leng(i);
    ti=t(t>=tin&t<tfin);
    x(t>=tin&t<tfin)=a1*cos(2*pi*f1*ti)+a2*cos(2*pi*f2*ti);
end

%% noise synthesis
n=rand(size(x));n=n-mean(n);n=An*n;

%% others
% ring invitation to call (Ab)
b=Ab*cos(2*pi*F1(M=='b')*t);

% error (Ar)
d_r=200e-3;
d_r2=2*d_r;
T_r=1200e-3;N_r=round(T_r*Fs);

ir=t<d_r|(t>d_r2&t<d_r2+d_r);in=1:length(t);ir=in(ir);
ir2=0:N_r:length(t)-N_r;Ir=repmat(ir,1,length(ir2))';
Ir2=repmat(ir2,length(ir),1);Ir2=Ir2(:);in=Ir+Ir2;
r=0*t;r(in)=Ar*cos(2*pi*F1(M=='b')*t(in));

% occupied (Ad)
d_r=200e-3;
d_r2=2*d_r;
T_r=4*d_r;N_r=round(T_r*Fs);

ir=t<d_r|(t>d_r2&t<d_r2+d_r);in=1:length(t);ir=in(ir);
ir2=0:N_r:length(t)-N_r;Ir=repmat(ir,1,length(ir2))';
Ir2=repmat(ir2,length(ir),1);Ir2=Ir2(:);in=Ir+Ir2;
d=0*t;d(in)=Ad*cos(2*pi*F1(M=='b')*t(in));


%% addition of signal, noise and others
y=x+n+b+r+d;
y=y*1;
play_so(y,Fs),pause

%name=['test',num2str(test),'.wav'];
%wavwrite(y,Fs,name)

end
