Revision as of 10:05, 27 August 2010 by Mboutin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

HW1, ECE301, Prof. Boutin

In this homework assignment, we were asked to write a Matlab code to play the "Hail Purdue" song with different speeds and pitch.

Sound File

The sound output from MATLAB was recorded using Audacity, then it was compressed and saved as an Ogg Vorbis file to make for quicker downloads. The file can be found at this link.

MATLAB Code

% Ben Laskowski (blaskows@purdue.edu)
% September 5, 2008
% ECE301 Section 2 HW1
%
% This file is supposed to play Hail Purdue.

% Begin by clearing the output console and all memory.
clear;
clc;

%Define note durations.
% Take the time signature as 4/4 and let the quarter note = 172bpm.
% Then we have 172 quarter notes per minute, so the quarter note duration is 60/172 minute.
Q=60/172;
%Use the quarter note as a reference for the other note types.
H=2*Q;	% Half note
W=4*Q;	% Whole note
En=Q/2;	%Eighth note
DQ=Q+En;%Dotted quarter
DH=H+Q;	%Dotted half

%Now, define frequencies for each note starting with the A below middle C and extending an octave.
for note=0:12
	Frequency(note+1)=220*2^(note/12);
end

%Define a constant for each note.
% Even though this will be in the key of D, I do not like sharp keys and will therefore define all accidentals as flats.
A=Frequency(1);
Bb=Frequency(2);
B=Frequency(3);
C=Frequency(4);
Db=Frequency(5);
D=Frequency(6);
Eb=Frequency(7);
E=Frequency(8);
F=Frequency(9);
Gb=Frequency(10);
G=Frequency(11);
Ab=Frequency(12);
A2=Frequency(13);

%Now, define arrays of notes and durations.
Notes=[A,B,Db,D,Db,D,Db,D,Db,D,E,D,A,B,Db, ...      %Cheer your call once more we rally, Alma Mater hear our praise 
       Db,D,Eb,E,Eb,E,Eb,E,Eb,E,Gb,E,A,G,Gb, ...    %Where the Wabash spreads its valley, filled with joy our voices raise
       A,B,Db,D,Db,D,Db,D,Db,D,A2,Gb,E,D,G, ...     %From the skies in swelling echoes come the cheers that tell the tale
       D,D,E,E,F,F,Gb,Gb,G,A2,Gb,E,D,Gb,E,E, ...    %Of your vict'ries and your heroes, all hail Purdue, we sing all hail
       A,B,Db,D,E,Gb,Gb,G,G,G,D,E,F,Gb, ...         %Hail, hail to old Purdue!  All hail to our old gold and black
       Gb,Gb,E,D,E,Gb,Gb,E,B,Db,D,Db,B,E, ...       %Hail, hail to old Purdue!  Our friendship may she never lack!
       A,A,B,Db,D,E,Gb,Gb,Gb,G,G,D,E,Gb, ...        %Ever grateful, ever true, thus we raise our song anew...
       B,Db,D,B,A,D,Gb,A,B,Gb,E,D,D];               %Of the days we spend with you, all hail our old Purdue!
 
Times=[Q,Q,Q,Q,Q,Q,En,En,En,En,Q,Q,Q,Q,DH, ... 
       Q,Q,Q,Q,Q,Q,En,En,En,En,Q,Q,Q,Q,DH, ... 
       Q,Q,Q,Q,Q,Q,En,En,En,En,Q,Q,Q,Q,DH, ...
       En,En,DQ,En,DQ,En,Q,En,En,En,En,En,En,H,H,W, ... 
       H,Q,Q,DQ,En,Q,Q,Q,En,En,Q,En,En,DH, ...
       H,Q,Q,DQ,En,Q,Q,Q,En,En,Q,En,En,W, ... 
       DQ,En,Q,Q,DQ,En,Q,En,En,Q,Q,Q,Q,W, ... 
       DQ,En,Q,Q,Q,Q,Q,Q,DQ,En,DQ,En,1.5*W];

%Let the sampling frequency be 20kHz, since this is what was used for the demo in class.
delta=1/20000;

%I count 116 notes in the above.

%First, play the song at normal speed.
for counter=1:116
	t=0:delta:Times(counter);	%Create a vector of times with appropriate duration
	d=sin(2*pi*t*Notes(counter));	%Generate a sine wave for the afmorementioned
	sound(d,1/delta);		%Play the sound.
end

pause(5)

%Second, play the song at double speed.
for counter=1:116
	t=0:delta:0.5*Times(counter);	%Create a vector of times with appropriate duration:
                                    %Here, we cut the "correct" times in half to speed up the song.                  
	d=sin(2*pi*t*Notes(counter));	%Generate a sine wave for the afmorementioned
	sound(d,1/delta);		%Play the sound.
end

pause(5)

%Finally, play the song according to the transformation y(t)=x(2t).
%This should have the effect of transposing up an octave, or doubling the
%frequency.
for counter=1:116
	t=0:delta:Times(counter);	%Create a vector of times with appropriate duration
	d=sin(2*2*pi*t*Notes(counter));	%Generate a sine wave for the afmorementioned
	sound(d,1/delta);		%Play the sound.
end

Alumni Liaison

Ph.D. on Applied Mathematics in Aug 2007. Involved on applications of image super-resolution to electron microscopy

Francisco Blanco-Silva