function [phone_number] = ttdecode(phone_signal, verbose)
% TTDECODE
% [phone_number] = ttdecode(phone_signal, verbose)
%
% This functions decodes a touch-tone signal into a phone number.
% It assumes the signal is formatted as a varying number of
% samples of touch-tone for each digit separated by a varying
% number of samples of total silence.
% 
% As defined in Section 5.2 (93-96) of Buck, Daniel and Singer
% the touch-tone frequencies are
%
%     w row             w column
% index  (rad)        index  (rad)
% 1      0.5346       1      0.9273
% 2      0.5906       2      1.0247
% 3      0.6535       3      1.1328
% 4      0.7217
%
% and the digits are coded as a sum of two sines, one with a row
% frequency and one with a column frequency:
% 
%            | column index
% -------------------------
%  row index |    1 2 3
% -------------------------
%      1          1 2 3 
%      2          4 5 6
%      3          7 8 9
%      4            0
%
% Inputs:
%   phone_signal -- The signal to decode.
%   verbose      -- Whether the function prints debugging information
%                   (optional, false by default).
%
% Outputs:
%   phone_number -- The decoded phone number, as a vector of digits.
%                   -1 is used in place of a digit when the tone
%                   cannot be decoded.
%

if ~exist('verbose')
  verbose = 0;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create a list of the frequency indices that correspond to a
% touch-tone frequency.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

w_row = [0.5346 0.5906 0.6535 0.7217];
w_col = [0.9273 1.0247 1.1328];
sig_ws = [w_row w_col];

sig_ks = find_index(sig_ws);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Identify the regions in the samples that represent a tone.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

N = length(phone_signal);

nzi = find(abs(phone_signal)>0);
gaps = find(diff(nzi)>1);
end_indices = nzi(gaps);
start_indices = [1 end_indices];
end_indices = [end_indices N];
n_digits = length(end_indices);

if verbose
  fprintf(1, '%d digits\n', n_digits);
  fprintf(1, 'start indicies: ');
  fprintf(1, '%d ', start_indices);
  fprintf(1, '\n');
  fprintf(1, 'end indicies: ');
  fprintf(1, '%d ', end_indices);
  fprintf(1, '\n');  
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Decode each tone into a digit.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
phone_number = zeros(1,n_digits);
for i=1:n_digits,
  d = phone_signal([start_indices(i):end_indices(i)]);
  D = fft(d, 2048);
  Dabs = abs(D);
  Dsq = Dabs.*Dabs;
  Den = Dsq(sig_ks);
  max2 = sort(Den, 2, 'descend');
  s = find((max2(1)==Den) | (max2(2)==Den));
  v = -1;
  switch s(1),
    case 1,
      switch s(2),
	case 5,
	  v = 1;
	case 6,
	  v = 2;
	case 7,
	  v = 3;	  
      end
    case 2,
      switch s(2),
	case 5,
	  v = 4;	  
	case 6,
	  v = 5;	  
	case 7,
	  v = 6;  
      end
    case 3,
      switch s(2),
	case 5,
	  v = 7;	  
	case 6,
	  v = 8;	  
	case 7,
	  v = 9;	  
	end      
      case 4,
	switch s(2),
	  case 6,
	    v = 0;	  
	end
    end
  if v==-1
    warning(sprintf('could not decode digit at position %d', i));
  end
  phone_number(i) = v;
end
