Revision as of 11:29, 4 September 2008 by Thomas34 (Talk)

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

Hail Purdue! The first line of our old battle cry can be found here!

MATLAB Code

% Brian Thomas (thomas34 _ nospam _ purdue _ edu)
% Sept. 4, 2008
% ECE301, HW#1.1
% Play the opening line of 'Hail Purdue' in a variety of ways

% Clear everything
clear; clc;

% Define music note frequencies (hz) ( source: http://www.phy.mtu.edu/~suits/notefreqs.html )
Eb = 311.13;
E  = 329.63;
F  = 349.23;
Gb = 369.99;
G  = 392.00;
Ab = 415.30;
A  = 440.00;
Bb = 466.16;
B  = 493.88;
C  = 523.25;
Db = 554.37;
D  = 587.33;


% Define music speed (beats / min.) -- tempo di marcia
tempo = 160;

% Define note lengths
sampleRate = 44100; %hz
quarter = 60/tempo; % quarter note will last for 60/tempo seconds
half = 2*quarter;
eighth = 0.5*quarter;


% 2-D array for music: Notes and corresponding lengths of time
music = [ half, Eb; %Hail
    quarter, F; %Hail
    quarter, G; %to
    quarter+eighth, Ab; %old
    eighth, Bb; %Pur-
    quarter, C; %due!
    quarter, C; %All
    quarter, Db; %hail
    eighth, Db; %to
    eighth, Db; %our
    quarter, Ab; %old
    eighth, Bb; %gold
    eighth, B; %and
    half+quarter, C]; %black!

% Make an array for song output
fullSong = [];

% a. Play music normally
for i = 1 : 1 : length(music)
    t = 0:1/sampleRate:music(i,1);
    note = sin(2*pi*t*music(i,2));
    sound(note, sampleRate);
    fullSong=[fullSong, note]; % Append note to the full song
end

% 1 second pause between songs
pause(1);
fullSong=[fullSong, zeros(1,sampleRate)];

% b. Play music twice as fast.  We will do this by halving the length of
% each note as defined in music(i,1).
for i = 1 : 1 : length(music)
    t = 0:1/sampleRate:music(i,1)/2;
    note = sin(2*pi*t*music(i,2));
    sound(note, sampleRate);
    fullSong=[fullSong, note]; % Append note to the full song
end

% 1 second pause between songs
pause(1);
fullSong=[fullSong, zeros(1,sampleRate)];

% c. Play music, rescalling by y(t) = x(2t).  In other words, the sample
% rate doubles.  If sound were to simply be played by speakers, the line
% sound(note, sampleRate) would be changed to sound(note,
% (sampleRate*2), but since this would make the wav output wrong, the sin
% wave frequency will be doubled, and the time to complete each note will
% be halved to produce the same effect.
for i = 1 : 1 : length(music)
    t = 0:1/sampleRate:music(i,1)/2;
    note = sin(2*pi*(2*t)*music(i,2));
    sound(note, sampleRate);
    fullSong=[fullSong, note]; % Append note to the full song
end

% Save song as a wav file
wavwrite(fullSong, sampleRate, 16, 'puSong.wav');

Alumni Liaison

Questions/answers with a recent ECE grad

Ryne Rayburn