# Digital Signal Processing

**DSP Design Projects
**

In this project, you design a system which encodes and decodes a message consisting of digits from 0 to9 using three tones. The table below shows how each digit is coded using different tone:

Digit | Frequencies (Hz) |

1 | 100, 200, 300 |

2 | 100, 200, 350 |

3 | 100, 200, 400 |

4 | 100, 250, 300 |

5 | 100, 250, 350 |

6 | 100, 250, 400 |

7 | 150, 200, 300 |

8 | 150, 200, 350 |

9 | 150, 200, 400 |

0 | 150, 250, 300 |

- At the encoder, the duration of each digit is 50 milliseconds, with 50 milliseconds silence betweensubsequent digits.

· At the decoder, in addition to the frequencies, it is required to ensure that the digits are present forat least 40 milliseconds and not for longer than 55 milliseconds. Also, the distance between two digitsis required to be at least 40 milliseconds and not longer than 60 milliseconds.

· In order to test that your decoder examines those duration margins, your encoder should be able toproduce digits with tone and silence durations other than 50 milliseconds.

· Amplitude of each tone is a random variable between 1 and 2. For each tone, the amplitude is

different.

· For each digit, the amplitudes of different tones are different.

· The noise is added to the produced signal at the coder.

· The Design will be done in both Matlab and Simulink. Both designs should be identical.

· You need to show how the minimum required durations is considered in your design and how you canshow that with your design those digits with less duration will not be decoded.

· You need to add your noise to the output of your encoder and study the effect of noise on the

performance of the decoder. Show that for low levels of noise, the decoder works perfectly and asnoise power is increased; the decoder starts to make errors more and more.

· Note that your design should be distinct from others. Similar projects will be given the lowest markgiven divided by the number of such projects. Keep your design confidential!

· Your design should be justifiable both theoretically and practically.

· You will be submitting a midterm report on (TBD) in the lecture. In your midterm report, you areexpected to have come up with the mathematical theory and the block diagram of your design andprovide a clear explanation of how the system works as whole and also how each block performs thetask it is meant to perform in your design.

· You will be submitting a final report on TBD. 2. Your final report should be clearly and professionallywritten. See below for the marking scheme.

· In your final report, you are required to show how**10 topics**from the DSP course taught in the lecturesare used in your design.

· Each group consists of 7 students. Three students will be working on the Simulink and three studentswill be working on the Matlab code. The seventh student will lead the whole group by co‐ordinatingthe two sub‐groups.

· Note that all the mark will be based on your report. It is your responsibility to bring examples andfigures from the output of Simulink and Matlab code and properly and clearly explain them.

· Although we will require that you submit the Matlab and Simlink models, we will not run them. Theseare required in case we need to double check your clear and correct results as they appear in yourreport. If your report does not show a functional and theoretically correct design, we will not run yourcode or your Simulink model.

· Do NOT copy Matlab code or Simulink models in your report.

· Your final report should have six distinct sections: Introduction, the theory behind your design, Matlabimplementation, Simulink implementation, Conclusion and references.

· In the sections on Matlab and Simulink implementations, you will bring different examples withcorresponding signal plots (plot as many signal as needed to explain the signal flow clearly) whichclearly shows how your design is working. Do not spare any efforts to elaborate on your figures.

· You need to have a seventh section where you explain clearly what topics from the DSP as taught inthe lecture you have used and point to that topic in your design. Avoid teaching DSP in your reportand show where these topics have been used in your design.

**Solution**** **

function [B] = decode(Z,A,fa,d)

%

% Thisprogramm analyze a frequency coded signalvector and sort the won

% frequencies its corresponding digit.

%

% example: [B] = decode(Z,A,4000)

%

% Z : frequency coded signalvector

% A : dtmf key vector

% fa : sampling rate in hz

% d : time in seconds

% B : digit

n=length(A);

digit = ‘#’;

for k=1:n;

% fft analyze of the frequency coded signalvector

temp=abs(fft(Z(k,:)));

plot(temp);

% Analyze to the half sampling rate, because the frequencies after are

% not interesting for analyze. They were created by sampling the signal

x=1:(fa/2)*d;

%plot(temp(x));

% maximum of the array

% frequency 1

[a,i]=max(temp(x));

f1=(i/(1.8*d))-1;

%freq 2

i=i-20:i+20;

temp(i)=0;

[a,i]=max(temp(x));

f2=(i/(1.8*d))-1;

%freq 3

i=i-20:i+20;

temp(i)=0;

[a,i]=max(temp(x));

f3=(i/(1.8*d))-1;

%sort frequencies

Freq = [f1 f2 f3];

sorted = sort(Freq);

f1 = sorted(1);

f2 = sorted(2);

f3 = sorted(3);

% include frequency tolerance and sort the key frequencies (the actuall

% tolerance are at with time: >= 0.04 and < 0.055s

if ((f1>50) && (f1<125))

f1=100;

elseif ((f1>=125) && (f1<200))

f1=150;

end

if ((f2>150) && (f2<225))

f2=200;

elseif ((f2>225) && (f2<300))

f2=250;

end

if ((f3>250) && (f3<325))

f3=300;

elseif ((f3>=325) && (f3<375))

f3=350;

elseif ((f3>=375) && (f3<450))

f3=400;

end

% msgbox(num2str(f1))

% msgbox(num2str(f2))

% sort the evaluated frequencies to the keys

disp(f1);

disp(f2);

disp(f3);

switch(f1);

case{100};

switch(f2);

case{200};

switch(f3);

case{300}

digit=’1′;

case{350};

digit=’2′;

case{400};

digit=’3′;

end;

case{250};

switch(f3);

case{300};

digit=’4′;

case{350};

digit=’5′;

case{400};

digit=’6′;

end;

end

case{150};

switch(f2);

case{200};

switch(f3);

case{300}

digit=’7′;

case{350};

digit=’8′;

case{400};

digit=’9′;

end

case{250};

switch(f3);

case{300}

digit=’0′;

end;

end

end

% the evaluated key into vector

B(k)=digit;

% increase variable

k=k+1;

end** **

Triple Tone digital Encoder/Decoder

### Abstract

This document describes and elaborates on the work done to synthetize a digit encoder/decoder. The aim of the application is to encode digits from 0 to 9, each into a three frequencies temporal signal. The message contains more than one digit and each is separated from the next one by a time determined silence. The effect of the sampling, noise and decoding parameters on the performances of the system will be discussed.** **

### Table of Contents

5 Conclusions and recommendations. 13

### List of Figures

Figure 1 – Encoder/Decoder simulink. 7

Figure 2 – Encoded digit ‘0’ 8

Figure 3 – Encoded signal with random noise. 9

Figure 4 – Frequency spectrum of the encoded signal 10 ** **

### List of Tables

Tableau 1 – Digits to frequencies correspondance. 6

Tableau 2 – Sampling frequency effect for duration of 0.5. 12

Tableau 3 – Sampling frequencies and signal duration effect on encoding ability. 12

Tableau 5 – Effect of the frequency picking coeficient. 13** **

### 1 Introduction

The studied digits encoding method is based on the premise of representing each digit of a numerical message with three audible sinusoidal signals of frequencies f1, f2 and F3. Each digit or symbol represented in Table. 1 has distinct frequencies components.

Tableau 1 – Digits to frequencies correspondance

Digit | Frequencies f1 f2 f3 (Hz) | ||

1 | 100 | 200 | 300 |

2 | 100 | 200 | 350 |

3 | 100 | 200 | 400 |

4 | 100 | 250 | 300 |

5 | 100 | 250 | 350 |

6 | 100 | 250 | 400 |

7 | 150 | 200 | 300 |

8 | 150 | 200 | 350 |

9 | 150 | 200 | 400 |

0 | 150 | 250 | 300 |

Thus each frequency triplet (f1, f2, f3) uniquely identifies the corresponding digit.

### 2 Theory

Each digitpassed to the encoder can be represented as a discrete time signal of form Where N is defined as number of samples taken. Typically in the sampling frequency, according to Shannon law, is minimum twice the highest used frequency, in this case, 800 Hz. Thus if the three individual frequency components of the signal can be identified then the number dialed can be decoded.

### 2.1 Encoder

The encoder is coded so that it inserts silence samples in between each digits so that they are separated from each other.

Starting position = (total number of digits be encoded-1)*(length of each dual tone + number of silence samples to be inserted) + 1.

In the temporal domain the digit is composed of three signals:

A1*sin(2*pi*f1*t)

A2*sin(2*pi*f2*t)

A3*sin(2*pi*f3*t)

Silence

On the frequency spectrum a digit (d[n]) is represented as such:

d[n] = A1*sin[2*pi*f1*n] + A2*sin[2*pi*f1*n] + A3*sin[2*pi*f3*n], 0 ≤ n ≤ N-1

### 2.2 Decoder

The input to the decoder is a vector containing the generated tones that are encoded by the encoder. A low pass filter is implemented which is higher than the highest frequency of interest in order to eliminate high frequencies signal produced by the sampling. The decoding process takes place in iterative form. Starting from first detected peak, smooth the area around it, and then to the next peak until collecting the sum of the three peaks representing the three frequencies in the spectrum.

Oppositely to the encoding strategy the decoder first return the index of the peaks and then the index is divided by the duration of the signal and 1 is subtracted.

The decoding coefficient is calculated at 2.5.

### 3 Implementation

Figure 1 – Encoder/Decodersimulink

### 3.1 Encoder

- rng(‘shuffle’);%Initializerandomgenerator
- r = randperm(4,3)/2; %Create a vector of three numbers (amplitudes) between 1 and 2
- t = 0:Ta:d;
- N = 3*d/Ta;
- x =

[r(1)*sin(2*pi*f1*t) r(2)*sin(2*pi*f2*t) r(3)*sin(2*pi*f3*t) zeros(1, N)]

;

The encoder takes the duration of the digit (d), the sampling frequency (1/Ta) and then codes the digit into three tones with three different frequencies and adds a silent 50 seconds at the end of the signal. An encoded signal is illustrated in figure 2.

Figure 2 – Encoded digit ‘0’

If the noise parameter is set to one a random noise is added to the signal (figure 3).

Figure 3 – Encoded signal with random noise

As shown in figure 2, each tone has a random amplitude between 1 and 2 assigned. Hence values like 1, 1.5 and 2 are possible. The permrand function is used to ensure than the tones of the same digits have different amplitudes.

A simple Fourrier transformation shows the signal distribution on the frequency spectrum. If you notice, a pair of symmetric frequencies is showing on the higher end of the spectrum. This is due to the sampling (figure 4).

Figure 4 – Frequency spectrum of the encoded signal

### 3.1 Decoder

First step while decoding the digits is to analyze to the half sampling rate, because the frequencies after are not interesting for analyze.

% maximum of the array

% frequency 1

[a,i]=max(temp(x));

f1=(i/(**2***d))-1;

%freq 2

i=i-20:i+20;

temp(i)=0;

[a,i]=max(temp(x));

f2=(i/(**2***d))-1;

%freq 3

i=i-20:i+20;

temp(i)=0;

[a,i]=max(temp(x));

f3=(i/(**2***d))-1;

Frequencies from f1 to f3 are extracted by scanning for the peak on the spectrum, then smoothing the area around it.

Then the frequencies are sorted since f1<f2<f3.

The coefficient (in red) theoretically calculated to 2 gives best performances and decoding efficiency when it is set to 2.

if ((f1>50) && (f1<125))

f1=100;

elseif ((f1>=125) && (f1<200))

f1=150;

end

if ((f2>150) && (f2<225))

f2=200;

elseif ((f2>225) && (f2<300))

f2=250;

end

if ((f3>250) && (f3<325))

f3=300;

elseif ((f3>=325) && (f3<375))

f3=350;

elseif ((f3>=375) && (f3<450))

f3=400;

end

Then, we include frequency tolerance and sort the key frequencies: the actualtolerance is synchronous with digit duration d>= 0.04 and < 0.055s.

Finally, the frequencies triplet is matched to a digit following the coding scheme in Table. 1.

The decoding is done in an iterative manner, the decoder returns then an array containing all the digits without the silence.

### 4 Results and analysis

The performances of the system were tested using a simple message to be encoded then decoded: 1234567890. An output such as (‘##########’) means the system was unable to decode the message giving all the constraint and functioning conditions.

Tableau 2 – Sampling frequency effect for duration of 0.5

Sampling Frequency | Input | Output |

1000 Hz | 1234567890 | ‘##########’ |

1500 Hz | 1234567890 | ‘1224557880’ |

2500 Hz | 1234567890 | ‘1234567890’ |

5000 Hz | 1234567890 | ‘1234567890’ |

Tableau 3 – Sampling frequencies and signal duration effect on encoding ability

Sampling Frequency | Duration of the signal | Input | Output |

2500 Hz | 0.05 | 1234567890 | Unable to decode |

2500 Hz | 0.25 | 1234567890 | ‘1234567890’ |

5000 Hz | 0.05 | 1234567890 | Unable to decode |

5000 Hz | 0.25 | 1234567890 | ‘1234567890’ |

Tableau 4 – Noise effect

Noise type | Input | Output |

White noise, SNR = 10 | ‘1234567890’ | ‘1234567890’ |

White noise, SNR = 3 | ‘1234567890’ | ‘1234557890’ |

Random Noise, same width as the encoded digit | ‘1234567890’ | ‘1233567790’ |

Tableau 5 – Effect of the frequency picking coeficient

Frequency scan coefficient | Input | Output |

2.5 (theoretical) | ‘1234567890’ | ‘#111111111’ |

2 | ‘1234567890’ | ‘1234567890’ |

1.8 | ‘1234567890’ | ‘2335668999’ |

2.2 | ‘1234567890’ | ‘1124457780’ |

### 5 Conclusions and recommendations

In order to decode a string of dialed digits the decoding step assumes that the encoder has inserted silence between each dual tone. Each dual tone length is tracked and silence lengths are calculated. The decoding step loops over for the number of digits to be decoded that is easily calculated from the total length of the signal divided by the sum of individual dual tone length and silence length. Thus after decoding the first digit certain number of samples are skipped and the next set of samples are decoded.

This means sampling and decoding a small part of the resulting waveform in each iteration, where one iteration relates to one key pressed.

This means that our system is clearly functioning on assumption and the decoder is coherently dependent of the encoder.

The encoder/decoder’s performances were analyzed as a function of the noise, the sampling frequency, the picking coefficient (equivalent to filter coefficients), and signal duration which all proved to be critical effects.

function [y,t] = encode(key,d,Ta, noise)

%

% [x,t] = encode(key,d,Ta)

%

% key: key

% d: time in seconds

% Ta; sampling time

if d<=0;

disp(‘ ‘);

disp(‘ERROR: time must be greater 0 !’);

disp(‘ ‘)

return;

else;

switch (key);

case {1 2 3 4 5 6};

f1=100;

case {0 7 8 9};

f1=150;

otherwise;

disp(‘ ‘);

disp(‘ERROR: incorrect entry !’);

disp(‘only entry: 1 2 3 4 5 6 7 8 9 0’);

disp(‘ ‘);

return;

end;

switch (key);

case {1 2 3 7 8 9};

f2=200;

case {0 4 5 6};

f2=250;

otherwise;

disp(‘ ‘);

disp(‘ERROR: incorrect entry !’);

disp(‘only entry: 1 2 3 4 5 6 7 8 9 0’);

disp(‘ ‘);

return;

end;

switch (key);

case {0 1 4 7};

f3=300;

case {2 5 8};

f3=350;

case {3 6 9};

f3=400;

otherwise;

disp(‘ ‘);

disp(‘ERROR: incorrect entry !’);

disp(‘only entry: 1 2 3 4 5 6 7 8 9 0’);

disp(‘ ‘);

return;

end;

rng(‘shuffle’);%Initialize random generator

r = randperm(4,3)/2; %Create a vector of three numbers (amplitudes) between 1 and 2

t = 0:Ta:d;

N = 3*d/Ta;

x =

[r(1)*sin(2*pi*f1*t) r(2)*sin(2*pi*f2*t) r(3)*sin(2*pi*f3*t) zeros(1, N)]

;

clf;

%plot(x);

if(noise == 1)

y = x + randn(size(x));

elseif(noise == 0)

y= x;

end;

plot (y);

ifnargout==0

sound(x,1/Ta);

end;

end;

**….**

function [Z] = genfcs(A,d,fa,noise)

%

% This program gives a digit vector to function “encode.m”, and generate

% frequency coded signalvector with the parameter from “encode.m”.

%

%

% fa : sampling rate in hz

% d : time in seconds

% A : digits vector

% Z : frequency coded signalvector

% sampling time

Ta=1/fa;

n=length(A);

for k=1:n

% elements dtmf key vector to function “encode.m” and save won

% parameter into x,t

[x,t]=encode(A(k),d,Ta,noise);

% generate frequency coded signalvector

Z(k,:)=x;

% increase variable

k=k+1;

end

**……**** **

clear;

clc;

A = [1,2,3,4,5,6,7,8,9,0];

%A = [1,2,3,4,5,6,7,8];

noise = 0; %set to 0 if you don’t want noise

d=0.5;

fa=2500;

[Z]=genfcs(A,d/3,fa,noise);

% function call analyze

[B]=decode(Z,A,fa,d);