DTMF Modulation & Demodulation

DTMF Modulation & Demodulation

 

 

Solution 

dtmfAssignment.m 

%DTMFASSIGNMENT generates DTMF using digital filters

% prepare

clear

close

clc

%%% Question One

% parameters

f0 = 1e3;       % fundamental frequency

A  = 1e4;       % amplitude

fs = 8e3;       % sampling frequency

t  = 0.5;       % duration of the signal

% generate signal

y  =singen(f0, fs, A, t);

% plot the output for 2.5ms

ti = 2.5e-3;

ns = fs*ti;

figure(‘name’, ‘Tone Generator Output’)

stem(1e3/fs*(0:ns-1)’, y(1:ns), ‘MarkerFaceColor’, ‘auto’, …

‘LineWidth’, 1.5)

xlabel(‘Time [ms]’)

ylabel(‘Amplitude’)

grid minor

grid

% Play audio

sound(y, fs)

pause(t+0.1)

%%% Question Two

% parmaers – chosen key is 6 with f1 = 770, f2 = 1477

f1 = 770;           % first frequency in Hz

f2 = 1477;          % second frequency in Hz

A  = 5000;          % amplitude

t  = 0.5;             % duration of the signal in seconds

% modulate

y = dtmfmod([f1 f2], fs, A, t);

y = sum(y, 2);

% listen

sound(y, fs);

pause(t+0.1);

%%% Question Three

% dial the number 8675309

txt = ‘8675309’;                    % dialled number

td  = 0.5*ones(length(txt),1);      % tone durations in seconds

tp  = 0.1;                          % pause duration in seconds

f0  =dtmfparams(txt);              % frequencies

y   = dtmfmod(f0, fs, A, td, tp);   % signal

% listen

sound(y, fs);                       % listen to the signal

pause(sum(td)+length(td)*tp)        % pause execution for sound to play

%%% Question Four

% decode the dtmf signal in Question 4 using the user defined function

% dtmfdecode

[~, X] = dtmfdecode(y);

%%% Question Five

% plot the frequency spectrum of each of the tone segments using abs(fft())

% obtain FFT

L = size(X, 1);

fvec = fs*(0:L/2)’/L;

Y = abs(fft(X))/L;

Y = Y(1:L/2+1,:);

Y(2:end-1, 🙂 = 2*Y(2:end-1,:);

% plot spectra

for n = 1:size(X,2)

figure

plot(fvec, Y(:,n), ‘LineWidth’, 1)

set(gca, ‘XLim’, [0 2e3])

nn = f0((n-1)*2 + [1 2]);

title([‘Key: ‘ txt(n) ‘, Freqs: ‘ num2str(nn(1)) ‘Hz and ‘ …

num2str(nn(2)) ‘Hz’])

xlabel(‘Frequency [Hz]’)

ylabel(‘Amplitude’)

grid

grid minor

end 

dtmfdecode.m

 function [varargout] = dtmfdecode(y, varargin)

%DTMFDECODE decoces DTMF signal to the dialed keys

%       inputs:

%               y – dtmf signal

%               varargin{1} – duration of each key

%               varargin{2} – pause between two keys

%               varargin{3} – sampling frequency

%       outputs:

%               varargout{1} – decoded keys as a string

%               varargout{2} – matrix of samples used for decoding

% keys and frequencies

% frequencies

f1 = [697 770 852 941];

f2 = [1209 1336 1477 1633];

f = [f1 f2];

% keys

K = cellstr({‘1’, ‘2’, ‘3’, ‘A’;

‘4’, ‘5’, ‘6’, ‘B’;

‘7’, ‘8’, ‘9’, ‘C’;

‘*’, ‘0’, ‘#’, ‘D’});

% determine if durations and pauses are present

td = 0.5;

tp = 0.1;

fs = 8e3;

% depending on the number of inputs, modify paramters

ifnargin> 1

td = varargin{1};

end

ifnargin> 2

tp = varargin{2};

end

ifnargin> 3

fs = varargin{3};

end

% read 0.1s bits of the signal

sLen = fix(td*fs);

len = fix(tp*fs) + sLen;

L   = ceil(length(y)/len);

N = fix(0.1*fs);

X = zeros(sLen,L);

k = f/fs*2*pi; % k terms for the keys

dec_key = repmat(‘ ‘, 1, L); % decoded keys

% decode and display

fprintf(‘\nThe decoded Key is: \n**************************\n’)

for m = 1:L

x = y((m-1)*len+(1:N));

X(:,m) = y((m-1)*len+(1:sLen));

% Goertzel algorithm

% courtesy of Wikipedia

P = zeros(size(f));

for mm = 1:length(k)

w = k(mm);

cr = cos(w);

coeff = 2*cr;

sprev = 0;

sprev2 = 0;

for n = 0:N-1

s = x(n+1) + coeff*sprev – sprev2;

sprev2 = sprev;

sprev = s;

end

P(mm) = sprev2^2 + sprev^2 – coeff*sprev*sprev2;

end

[~, index] = sort(P);

fdec = sort(f(index(end-1:end)));

dec_key(m) = K{find(f1==fdec(1)),find(f2==fdec(2))};

fprintf(‘%s’, dec_key(m));

end

fprintf(‘\n************************** \n\n’)

% return values (if requested)

ifnargout> 0

varargout{1} = dec_key;

end

ifnargout> 1

varargout{2} = X;

end 

dtmfmod.m 

function y = dtmfmod(f0, fs, A, t, varargin)

%DTMFMOD implements a DTMF modulator by generating two sinusoids of

%amplitude A and adding them together.

%       inputs:

%               f0 – an even length element vector of frequencies for each

%               key

%               fs – sampling frequency

%               A  – Amplitudes of the two sinusoids

%               t  – duration(s) of the modulated signal in seconds

%               varargin – duration in seconds pause between keys in a

%               sequence

%       outputs:

%               y  – modulated dtmf signal

%

%

% generate two sine waves

y = cell(size(t));

for n = 1:length(t)

index = (n-1)*2 + [1 2]; % f0 indices

if n == 1

y = singen(f0(index), fs, A, t(n));

else

y = [y; singen(f0(index), fs, A, t(n))];

end

% add pause

if n <length(t) &&nargin> 4

y = [y; zeros(fs*varargin{1}, length(index))];

end

end

% modulate

y = sum(y,2); 

dtmfparams.m 

function f0 = dtmfparams(txt)

%DTMFPARAMS determines the DTMF parameters of a dialed number

%       inputs:

%               txt – a string of text with the dialed keys

%       outputs:

%               f0  – frequencies for each key

% frequencies

f1 = [697 770 852 941];

f2 = [1209 1336 1477 1633];

[F2, F1] = meshgrid(f2, f1);

% keys

K = cellstr({‘1’, ‘2’, ‘3’, ‘A’;

‘4’, ‘5’, ‘6’, ‘B’;

‘7’, ‘8’, ‘9’, ‘C’;

‘*’, ‘0’, ‘#’, ‘D’});

% dialed frequencies

len = length(txt);

f0  = zeros(len*2,1);

for n = 1:length(txt)

g = strcmp(txt(n), K);

index       = (n-1)*2 + [1 2];

f0(index)   = [F1(g); F2(g)];

end 

singen.m 

function y = singen(f0, fs, A, t)

%SINGEN generates a sinusoidal waveform of frequency f0, sampled at

%freqeuncyfs with amplitude A for a duration t.

%       input:

%               f0 – a vector of fundamental frequencies for the desired

%               waveforms

%               fs – sampling frequency

%               A  – amplitudes vector

%               t  – duration of the signal in seconds

%       output:

%               y  – a vector (or matrix) of generated sinusoid(s)

% parameters

f0 = f0(:)’;

A  = A(:)’;

% generator

% initial conditions

y0  = 0;

y_1 = @(A,f0) -A.*sin(2*pi*f0/fs);

% constatnt

a1  = @(f0) 2*cos(2*pi*f0/fs);

% function

f = @(yprev, f0) a1(f0).*yprev(2,:)-yprev(1,:);

% generate for t seconds

Ns = fix(fs*t);                                 % number of samples

y  = zeros(Ns+1, length(f0));                   % samples initialized

y(1:2,:) = [y_1(A,f0); y0*ones(1,length(f0))];  % initialize samples

for n = 3:Ns+1

% update sample

y(n,:) = f(y(n-2:n-1,:), f0);

end

y = y(2:end,:);