# Digital media project

**Published:** **Last Edited:**

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

### DIGITAL MEDIA PROJECT

In this digital media project we are given some selected songs and we have to generate these songs using matlab functions with different frequencies. The audio signals are stored in digital form but to hear this sound must be converted in analog. In maatlab, we use sound (xx, Fs) function to hear the sound. Where Fs is a sampling rate and its values are 8000 or 11025 samples/seconds. We know that sinusoidal waveform is in the form of

x(t) = Acos(?0t + f)

1. (a) compute a vector x1 of samples of a sinusoidal signal with *A *= 100, *?0* = 2f(1100), and *f *= 0. Use a sampling rate of 8000 samples/second, and compute a total number of samples equivalent to 2 seconds time duration Using sound() play the resulting vector through the D-to-A converter of your computer,

Assuming that the hardware can support the *fs *= 8000 Hz rate (or *fs *= 11025 Hz). Listen

Ans: A = 100;

fs = 8000;

f = 1100;

tt = 0: 1/ Fs : 2;

xx = A cos(2*pi*f*tt)

sound(xx, Fs)

b. Now compute a vector x2 of samples (again, 2 seconds time duration) of the sinusoidal signal for the case A = 100, ?0 = 2f(1650), and f = f/3. Listen to the signal reconstructed from these samples. How does it compare to the signal in part (a)? Put both signals together in a new vector defined with the following MATLAB statement (assuming that both x1 and x2 are row vectors):

xx = [x1 zeros(1,2000) x2];

Ans. A = 100;

fs = 8000;

f = 1100;

tt = 0: 1/Fs:2;

xx1 = A cos(2*pi*f*tt + ?/ 3);

sound(xx1, Fs);

xx2 = [ xx zeros(1, 2000) xx1];

sound [ xx2 , Fs)

In this first we generate sound of one signal by applying phase angle ?/3 and then we put two signals together in a new vector and hear the sound.

c. Now send the vector xx to the D to A converter again, but double the sampling rate in sound ( ) to 16000 samples / second. Do not recomputed the samples in xx , just tell the D to A converter that the sampling rate is 16000 samples/ seconds. Describe what you heard. Observe how the duration and pitch of the signal changed. Explain.

Ans: In this we will use sampling rate 16000samples/rate instead of 8000.

A = 100;

fs = 16000;

f = 1100;

tt = 0: 1/Fs:2;

xx3 = A cos(2*pi*f*tt + ?/ 3);

sound(xx3, Fs);

2. In this section we will find frequency for every piano note.

the piano is divided in to octave. Each octave contain 12 notes ( 7 white and 5 black).we count the keys from A-440.

(a) Generate a sinusoidal of 2 seconds duration to represent the note E5 above A-440( key number 56). Choose the appropriate values for Ts and Fs. Remember that Fs should be at least twice as high as the frequency of the sinusoid you are generating. Also Ts, and Fs must "match" in order for the note played out of the D to A converter to sound correct.

Ans: In this case key E5( key number 56) is given and the frequency for this key will be

f = 440 ˜ 27/12 = 440 -27/12 = 660Hz

fs = 1320(double of frequency)

A = 100;

f = 660Hz

fs = 1320

tt = 0: 1/Fs:2;

xx4 = A cos(2*pi*f*tt )

sound(xx4, Fs);

b. Now write an MATLAB function to produce a desired note for a given duration. Your MATLAB funcmcos fution called note.m. You may want to call the sumcos function. your function should have the following form:

function tone = note(keynum,dur)

%note produce a sinusoidal waveform corresponding to a given piano key

%number

%

% usage: tone = note (keynum,2)

%

% tone = the output sinusoidal waveform

% keynum = the piano keyboard number of the desired note

% dur = the duration (in seconds)of the output note

%

fs = 8000;

tt = 0:(1/fs):dur;

freq = 440*(2^((keynum-49)/12));

tone = cos(2*pi*freq*tt)

In this we find the frequency as we find previously using formula 440*(2^((keynum-49)/12)) then we will save this file as note.m

(c) The following is a matlab function that will play scales.

%---play_scale.m

%---

keys = [40 42 44 45 47 49 51 52];

%---notes: C D E F G A B C

% key #40 is middle-C

%

dur = 0.25 * ones(1,length(keys));

fs = 8000;

xx = zeros(1,sum(dur)*fs+1);

n1 = 1;

for kk = 1:length(keys)

keynum = keys(kk);

tone = note(keynum,dur(kk));

n2= n1 + length(tone) -1;

xx(n1:n2)= xx(n1:n2)+tone;

n1 = n2;

end

sound(xx,fs)

3.2 Determine the notes that are played in Fur Elise, by mapping each note to a key number and then synthesize sine waves to recreate the piece. Use either the short form (Fig. 4) or the long form found on the CD. Use sine waves sampled at 8000 samples/sec (for UNIX) or 11025 samples/sec

Beethoven's Fur Elise

In this first we find the key numbers by identifying notes then we will create m function to produce a desired function and at last will write program to play scales.

function tone = note(keynum,dur)

%note produce a sinusoidal waveform corresponding to a given piano key

%number

%

% usage: tone = note (keynum,2)

%

% tone = the output sinusoidal waveform

% keynum = the piano keyboard number of the desired note

% dur = the duration (in seconds)of the output note

%

fs = 8000;

tt = 0:(1/fs):dur;

freq = 440*(2^((keynum-49)/12));

tone = cos(2*pi*freq*tt)

we will save this file as note.m

%---play_scale.m

%---

a = [ 56 55 56 55 56 51 54 52 49 0 40 44 49 51 0 44 48 51 52 0 44 56 55];

td = [.5 .5 .5 .5 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5];

fs = 11025;

xx = zeros(1,fs*sum(td));

n1 = 1;

for kk = 1:length(a)

keynum = a(kk);

kl=td(kk)/2;

tone = note(keynum,kl);%<=== FILL IN THIS LINE

n2 = n1 + length(tone) - 1;

xx(n1:n2) = xx(n1:n2) + tone;

n1 = n2;

end

b=[0 0 0 0 0 0 0 0 25 32 37 0 0 20 32 36 0 0 25 32 37 0 0];

bd=[.5 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1];

fs = 11025;

yy = zeros(1,fs*sum(bd));

n1 = 1;

for kk = 1:length(b)

keynum = b(kk);

kl=bd(kk)/2;

tone = note(keynum,kl);%<=== FILL IN THIS LINE

n2 = n1 + length(tone) - 1;

yy(n1:n2) = yy(n1:n2) + tone;

n1 = n2;

end

zz = xx+yy;

sound(zz, fs );

specgram(zz,fs);

3.5

function tone = note(keynum,dur)

%note produce a sinusoidal waveform corresponding to a given piano key

%number

%

% usage: tone = note (keynum,2)

%

% tone = the output sinusoidal waveform

% keynum = the piano keyboard number of the desired note

% dur = the duration (in seconds)of the output note

%

fs = 8000;

tt = 0:(1/fs):dur;

freq = 440*(2^((keynum-49)/12));

tone = cos(2*pi*freq*tt)

we will save this file as note.m

keysh = [56 55 56 55 56 51 54 52 49 49 1 40 44 49 51 51 1 44 48 51 52 52 1 ...

44 56 55 56 55 56 51 54 52 49 49 1 40 44 49 51 51 1 44 52 51 49 49 1 ...

51 52 54 56 56 56 47 57 56 54 54 54 45 56 54 52 52 52 44 54 52 51 51 1 ...

44 56 1 1 56 68 1 1 55 56 1 1 55 56 54 56 55 56 55 56 51 54 52 49 49 1 ...

40 44 49 51 51 1 44 48 51 52 52 1 44 56 55 56 55 56 51 54 52 49 49 1 ...

40 44 49 51 51 1 44 52 51 49 49 49 49];

keysl = [ 1 1 1 1 1 1 1 1 25 32 37 1 1 1 20 32 37 1 1 1 25 32 37 1 1 1 ...

1 1 1 1 1 1 25 32 37 1 1 1 20 32 36 1 1 1 25 32 37 1 28 35 40 1 1 1 1 1 ...

23 35 39 1 1 1 25 32 37 1 1 1 20 32 44 1 1 44 56 1 1 55 56 1 1 55 56 1 1 1 ...

1 1 1 1 1 1 25 32 37 1 1 1 20 32 36 1 1 1 25 32 37 1 1 1 1 1 1 1 1 1 ...

25 32 37 1 1 1 20 32 36 1 1 1 25 32 37 1];

dur = 0.5* ones(1,length(keysh));

durl = length(dur);

fs = 11025;

tt = 0:1/fs:dur;

xxh = zeros(1, sum(dur)*fs+length(keysh));

n1 = 1;

for kkh = 1:length(keysh)

keynum = keysh(kkh);

tone = note(keynum,0.5);

n2 = n1 + length(tone) - 1;

xxh(n1:n2) = xxh(n1:n2) + tone; %<=== Insert the note

n1 = n2;

end

xxl = zeros(1, sum(dur)*fs+length(keysh));

m1 = 1;

for kkl = 1:length(keysl)

keynum = keysl(kkl);

tone = note(keynum,0.5);

m2 = m1 + length(tone) - 1;

xxl(m1:m2) = xxl(m1:m2) + tone; %<=== Insert the note

m1 = m2;

end

xx = xxh + xxl;

soundsc(xx, fs);