Note before you begin: If you are having trouble viewing these lab instructions, I recommend using Firefox on a desktop computer or laptop (not mobile device)

Objectives

The objective of this lab is to investigate DTMF tone generation

Introduction

When you press a number on the key pad of a standard telephone, you generate a pair of audible tones, each with a different frequency. If your phone is connected to a phone line, then these tones let the phone company know what number you are pressing. As long as the telephone company can identify the two tones, it can identify the digits of the desired telephone number.

pic

A table that summarizes the tones associated with each button on a touch tone phone is shown below. This communication signaling is called dual tone multifrequency (DTMF) signaling. Note that the last column is not used on a typical touch-tone phone (digits A, B, C, and D), but can be used for data transmission.

Instructions

You are to generate an m-file called "song.m". When this file is executed it should play a song by playing a sequence of DTMF tones in order. As an example, the song "Marry had a Little Lamb" is generated by playing the following sequence of tones in order:

4, 5, 6, 5, 6, 6, 6, 5, 5, 5, 6, 6, 6, 4, 5, 6, 5, 6, 6, 6, 4, 5, 5, 6, 5, 4

In the case of "Mary had a Little Lamb", to sound correctly I've found that each number should have a duration of about 0.1 ms, and there should be a space (silence) between each number of about 0.5 ms. Of course, these values can be changed to suit one's musical tastes.

Matlab requirements

  1. Your song must have at least 20 numbers. Your song must not last longer than 1 minute.
  2. Use a sampling frequency of fs = 8000 KHz
  3. To generate a clean code, you must create and incorporate a function called generate DTMF that will create a DTMF tone. The inputs to the function are the digit, sampling frequency, and duration of the tone:

    x = generateDTMF(digit, duration, fs)

    The input values to the function are (1) digit, an integer corresponding to the DTMF tone, (2) duration, the time duration of the DTMF tone (in seconds), and (3) fs, the sampling frequency. The output signal in the example above is x.
  4. Create a second function called GenerateSilence similar to generate DTMF for creating the silent parts of the composition.
  5. Label and include clear comments in your code for maximum readability.
  6. Your entire musical composition should be saved as one long array s. See the example code below for an example of how to merge two arrays together to form a larger array.
  7. Generate and display a spectrogram of the song using the MATLAB command: spectrogram (this will be figure 1).
  8. You must demonstrate your song to me in class using the MATLAB command soundsc at the date specified in the Blackboard shell.
  9. Once everything works, change the sampling frequency to fs = 4000 Hz, listen to the song with the change, and generate the corresponding spectrogram (this will be figure 2). Next change the sampling frequency to fs = 1000 Hz, and generate the corresponding spectrogram (this will be figure 3). In your code, comment on if the song sounds different for fs = 4000 Hz and fs = 1000 Hz. Comment on why the song does or does not sound different for the two lower sampling rates. Think about the Nyquist frequency.

What to turn in

  1. Save all your work in a single m-file called "song.m" and publish it as a single PDF to upload to Blackboard. Make sure the functions generateDTMF and GenerateSilence are both local to the file song.m. To do this, you will need to turn your script into a function that takes no argument. See the appendix at the bottom of this page for more details. Submit your published PDF before the due date. If you write any other functions, include those as well in your main m-file.
  2. To earn full credit, you must demonstrate your song at the time and date specified on Blackboard.

Example

The following MATLAB code plays a tone with frequency 1000 Hz for 0.2 seconds, then plays a tone with frequency 2000 Hz for 0.3 seconds, then plays nothing (silence) for 0.5 seconds, then plays a tone with frequency 1000 Hz for 0.2 seconds. The sampling frequency is fs = 8000 Hz. The spectrogram of the signal is then displayed.

clc;clear all; clf;
fs=8000; %sampling frequency
t1=[0:1/fs:.2]; %time from 0 to 0.2 seconds
x1=cos(t1*2*pi*1000); %create cosine with frequency 1000
t2=[0:1/fs:.3]; %time from 0 to 0.3 seconds
x2=cos(t2*2*pi*2000); %create cosine with frequency 2000
t3=[0:1/fs:.5]; %time from 0 to 0.3 seconds
x3=t3*0; %create cosine with frequency 2000
t4=[0:1/fs:.2]; %time from 0 to 0.2 seconds
x4=cos(t1*2*pi*1000); %create cosine with frequency 1000
s=[x1,x2,x3,x4]; %combine two arrays into one array named "s"
soundsc(s,fs) %play the signal through speakers
res_time=.01 %Time resolution of spectrogram. The following
%spectrogram function works by taking an FFT of the signal
%over small "chunks" of time (equal to res_time) and then
%displays all the data on a time versus frequency axis.
%
% Great link to help you understand this better:
%http://stackoverflow.com/questions/5887366/matlab-spectrogram-params
%THE TEXTBOOK CHAPTER 13 IS ALSO VERY HELPFUL!
spectrogram(s, blackman(fs*res_time), 0, [], fs, 'yaxis');
%display spectrogram, Use blackman window, no window overlap.

pic

Appendix: How to include a script and a function definition in the same file

First, if you are unsure of the differences between a script and a function in MATLAB, you can read more about it here:

For this lab, you will be writing a MATLAB script (song.m) as well as MATLAB functions. Normally, each function is saved as a separate m-file, that you call from the script. In terms of uploading your assignment to Blackboard, however, it would be very cumbersome to upload all of your separate functions individually.

I would like you to turn in only one published m-file. Unfortunately, MATLAB will not allow you nest functions inside scripts locally. To get around this limitation we will exploit the fact that you can put functions inside other functions. Hence, the fix is to turn your script into a function that takes not argument. In order to do this, you add one line of code to the top of your original script. This one line of code is a function call with the name of your file, but no argument, as illustrate below for m-file named my_file.m:

function my_file
%script here...
a=1; b=2;
the_sum=add_em_up(a,b) %custom function
end
function out = add_em_up(x1,x2)
%function here...
out = x1+x2;
end
the_sum =
     3

With this small fix you can now run and publish the above file (my_file.m) as you would a normal script file.