clear
clc
close all force

%% Construct Digital Signals

%  A Digital Signal can be thought of as sampling a continuous time signal
%  at evenly spaced intervals (Ts)


% Often times DSP engineers will refer to the sampling frequency (fs) of the
% signal which is 1/Ts

fs  =   8000;      % here the sampling frequency is 8000Hz, this is used for traditional phone lines
Ts  =   1/fs;

n   =   (0:1024-1)';   %create an index vector of samples
t   =   n*Ts;      %multiplying the Sample Period by the index maps to time

x = cos(2*pi*200*t); % create a 200 Hz Sine Wave

figure(1)
plot(t,x)
title('Time Domain Plot of 200 Hz Sine Wave')
xlabel('time seconds')
ylabel('amplitude')
grid on
axis( [ 0 5/200 -1.5 1.5]) % zoom in on 5 Periods of the Sine Wave
fft_computed    = zeros(1024,1);
for index = 0:1024-1
   
    current_frequency       =   (index/1024)*fs;
    complex_phasor          =   exp(-1i*2*pi*current_frequency*t);
    fft_computed(index+1)   =   sum( complex_phasor.*x); 
        
end

fft_matlab=fft(x);

FFT_Error =  max(abs(fft_computed-fft_matlab));
disp('Maximum FFT_Error') %Note Error on the order of 10^-11 is often referred to as machine error on floating point calculations
disp(FFT_Error)
figure(2)
fvec=-fs/2:fs/1024:fs/2-fs/1024;
plot(fvec,fftshift(20*log10(abs(fft_matlab))));
grid on

%% Generating DTMF Tones
% A DTMF signal consists of the sum of two sinusoids - or tones - with
% frequencies taken from two mutually exclusive groups. These frequencies
% were chosen to prevent any harmonics from being incorrectly detected
% by the receiver as some other DTMF frequency. Each pair of tones
% contains one frequency of the low group (697 Hz, 770 Hz, 852 Hz, 941 Hz)
% and one frequency of the high group (1209 Hz, 1336 Hz, 1477Hz) and
% represents a unique symbol. The frequencies allocated to the push-buttons
% of the telephone pad are shown below:
%
%                          1209 Hz   1336 Hz   1477 Hz   
%                         _ _ _ _ _ _ _ _ _ _ _ _ _ _ _    
%                        |         |         |         |  
%                        |         |   ABC   |   DEF   |  
%                697 Hz  |    1    |    2    |    3    |     
%                        |_ _ _ _ _|_ _ _ _ _|_ _ _ _ _|  
%                        |         |         |         | 
%                        |   GHI   |   JKL   |   MNO   |  
%                770 Hz  |    4    |    5    |    6    |       
%                        |_ _ _ _ _|_ _ _ _ _|_ _ _ _ _|   
%                        |         |         |         |  
%                        |   PRS   |   TUV   |   WXY   |  
%                852 Hz  |    7    |    8    |    9    |         
%                        |_ _ _ _ _|_ _ _ _ _|_ _ _ _ _|   


dtmf_signal             =       cos(2*pi*770*t) + cos(2*pi*1477*t);

dtmf_signal_spectrum    =       fft(dtmf_signal);
figure(3)
plot(fvec,fftshift(20*log10(abs(dtmf_signal_spectrum))));
grid on

row_filter=[exp(-1i*2*pi*697*t');exp(-1i*2*pi*770*t');exp(-1i*2*pi*852*t')]; %
col_filter=[exp(-1i*2*pi*1209*t');exp(-1i*2*pi*1336*t');exp(-1i*2*pi*1477*t')];

row_output=abs( row_filter*dtmf_signal);     
col_output=abs( col_filter*dtmf_signal);   

disp('  697     770     852')
disp(round(row_output'))







