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)
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.
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.
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.
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.
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.