% Matlab code to verify the recovered DTMF sequences
% Usage: complete = verify_DTMF(recovered_DTMF, recovered_index,subject,condition)
% Bowon Lee 08/19/2004
% Modified for speech and hearing science data collection 11/22/2004


function verify_DTMF(recovered_DTMF, recovered_index,subject)

tone_sequence_digit = '3906784512';
tone_sequence_alphabet = '06784512390678451239067845';
tone_sequence_commands = '3906784512390678451';
tone_sequence_common = '9067845123906784512390678451239067845123906784512390678451239067845123906784512390678451239067845123';
tone_sequence_uncommon = '6784512390678451239067845123906784512390678451239067845123906784512390678451239067845123906784512390';


N_digit = length(tone_sequence_digit);
N_alphabet = length(tone_sequence_alphabet);
N_commands = length(tone_sequence_commands);
N_common = length(tone_sequence_common);
N_uncommon = length(tone_sequence_uncommon);
N_set = N_digit + N_alphabet + N_commands + N_common + N_uncommon;

N_offset = 0;

K = length(recovered_DTMF) - 8;

% recovered_DTMF % for test
disp(sprintf('Recovered DTMF sequence is as follows:\n%s',recovered_DTMF));

if K == N_set
    if (strcmp(tone_sequence_digit, recovered_DTMF(1:N_digit)))
        complete(1) = 1;
        disp_txt = sprintf('Sequences for Digit recovered successfully...');
        disp(disp_txt);
        N_offset = N_offset + N_digit + 2;
    else
        disp_txt = sprintf('Recovery of Sequences for Digit set 1 failed...');
        disp(disp_txt); recovered_DTMF= 'X'; return;
    end
    if (strcmp(tone_sequence_alphabet, recovered_DTMF(N_offset+1:N_offset+N_alphabet)))
        complete(2) = 1;        
        disp_txt = sprintf('Sequences for Alphabet recovered successfully...');
        disp(disp_txt);
        N_offset = N_offset + N_alphabet + 2;        
    else
        disp_txt = sprintf('Recovery of Sequences for Alphabet failed...');
        disp(disp_txt); recovered_DTMF = 'X'; return;
    end
    if (strcmp(tone_sequence_commands, recovered_DTMF(N_offset+1:N_offset+N_commands)))
        complete(3) = 1;        
        disp_txt = sprintf('Sequences for Commands recovered successfully...');
        disp(disp_txt);
        N_offset = N_offset + N_commands + 2;        
    else
        disp_txt = sprintf('Recovery of Sequences for Commands failed...');
        disp(disp_txt); recovered_DTMF = 'X'; return;
    end
    if (strcmp(tone_sequence_common, recovered_DTMF(N_offset+1:N_offset+N_common)))
        complete(4) = 1;               
        disp_txt = sprintf('Sequences for Common words recovered successfully...');
        disp(disp_txt);
        N_offset = N_offset + N_common + 2;        
    else
        disp_txt = sprintf('Recovery of Sequences for Common words sentence failed...');
        disp(disp_txt); recovered_DTMF = 'X'; return;
    end
    if (strcmp(tone_sequence_uncommon, recovered_DTMF(N_offset+1:N_offset+N_uncommon)))
        complete(5) = 1;
        disp_txt = sprintf('Sequences for Uncommon words recovered successfully...');
        disp(disp_txt);
    else
        disp_txt = sprintf('Recovery of Sequences for Uncommon words failed...');
        disp(disp_txt); recovered_DTMF= 'X'; return;
    end
else
    disp(sprintf('There is an error in the recovered DTMF tones.'));
    recovered_DTMF = 'X';
end
