%--------------------------------------------------
% Tony Hyun Kim
% 6.003: PS #6, Matlab assignment
% Simple, brute-force touch-tone decoder. 
%   Lots of ugly hacks included!
%
%--------------------------------------------------
% OUTPUT:
%--------------------------------------------------
% digit = ttdecode([d7 space d4 space d5 space...
%                   d5 space d5 space d0 space d2])
% digit =
%      7     4     5     5     5     0     2
%--------------------------------------------------
function digits = ttdecode(x_in)

digits = zeros(1,7);

%--------------------------------------------------
% We will first parse the input signal according to
% the format: 1000 samples of touch-tone for each 
% digit, separated by 100 samples of silence.
%--------------------------------------------------
x = zeros(7,1000);
for k = 1:7
    startIndex = 1 + 1100*(k-1);
    endIndex   = startIndex + 999;
    x(k,:)       = x_in([startIndex:endIndex]);
end

%--------------------------------------------------
% Here is the touch-tone information we will be 
% utilizing in our decoder.
%--------------------------------------------------
w0   = 2*pi/2048;
d0_k = round(1/w0*[0.7217 1.0247]); 
d1_k = round(1/w0*[0.5346 0.9273]);
d2_k = round(1/w0*[0.5346 1.0247]); 
d3_k = round(1/w0*[0.5346 1.1328]); 
d4_k = round(1/w0*[0.5906 0.9273]); 
d5_k = round(1/w0*[0.5906 1.0247]);
d6_k = round(1/w0*[0.5906 1.1328]); 
d7_k = round(1/w0*[0.6535 0.9273]); 
d8_k = round(1/w0*[0.6535 1.0247]); 
d9_k = round(1/w0*[0.6535 1.1328]);

d_k = [d0_k; d1_k; d2_k; d3_k; d4_k; d5_k; d6_k; d7_k; d8_k; d9_k];

%--------------------------------------------------
% Enter main decoding loop
%--------------------------------------------------
for k = 1:7
    %--------------------------------------------------
    % STEP 1: Take the transform of the k-th digit
    %--------------------------------------------------
    D = fft(x(k,:),2048);
    
    %--------------------------------------------------    
    % STEP 2: Find the two highest peaks.
    %--------------------------------------------------
    % I found that some of the FFTs do not produce
    % the same magnitude peak for the two frequencies in  
    % the tone. So we'll use a different technique
    % than the one I utilized in the previous part. 
    % (i.e. Problem (g).)
    %--------------------------------------------------    
    p1  = 0;
    p2  = 0;
        
    % First pass
    max = 0;
    for p = 1:length(D)/2 
        if(abs(D(p)) > max)
            p1 = p;
            max = abs(D(p));
        end
    end
    
    % Neutralize the first peak
    D([p1-50:p1+50]) = zeros(1,101);
    
    % Second pass
    max = 0;
    for p = 1:length(D)/2 
        if(abs(D(p)) > max)
            p2 = p;
            max = abs(D(p));
        end
    end
    
    % Correct for the Matlab 1-based indexing
    p1 = p1 - 1;
    p2 = p2 - 1;
    P = sort([p1 p2]);
    
    %--------------------------------------------------    
    % STEP 3: Find the matching digit.
    % Note: I allow for +/-10 tolerance in the index
    %--------------------------------------------------
    tol = 10;
    for j = 1:10
        if((abs(d_k(j,1)-P(1))<tol) && (abs(d_k(j,2)-P(2))<tol))
            digits(k) = j-1;
        end
    end
end