(New page: == Matlab Code == == Sound File ==) |
(→Sound File) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== Matlab Code == | == Matlab Code == | ||
+ | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br> | ||
+ | % Kathleen Schremser %<br> | ||
+ | % ECE 301 %<br> | ||
+ | % Homework 1, due Friday, September 5, 2008 %<br> | ||
+ | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br> | ||
+ | <br> | ||
+ | % Clear the screen <br> | ||
+ | clear<br> | ||
+ | clc<br> | ||
+ | <br> | ||
+ | % Define delta as used in class<br> | ||
+ | delta = 0.00005;<br> | ||
+ | <br> | ||
+ | % Define the tempo you wish to use (Number of Quarter notes per minute)<br> | ||
+ | % Note: The time signature for this piece is taken to be 2/4<br> | ||
+ | % Also, create a blank Song array to store the completed song (to be used later)<br> | ||
+ | Tempo = 152;<br> | ||
+ | Song = [];<br> | ||
+ | <br> | ||
+ | % Define the length of each Eighth, Quarter, Dotted Quarter, Half, and Tied<br> | ||
+ | % Note (A Tied note consists of 3 Quarter notes)<br> | ||
+ | E = 1/2 * 60/Tempo;<br> | ||
+ | Q = 60/Tempo;<br> | ||
+ | DQ = 1.5 * 60/Tempo;<br> | ||
+ | H = 2 * 60/Tempo;<br> | ||
+ | T = 3/2 * 60/Tempo;<br> | ||
+ | <br> | ||
+ | % Define the frequency of each note<br> | ||
+ | % The key of Hail Purdue is D flat<br> | ||
+ | Ef = 311.13;<br> | ||
+ | F = 349.23;<br> | ||
+ | G = 392.00;<br> | ||
+ | Af = 415.30;<br> | ||
+ | Bf = 466.16;<br> | ||
+ | B = 493.88;<br> | ||
+ | C = 523.25;<br> | ||
+ | Df = 554.36;<br> | ||
+ | <br> | ||
+ | % The chorus of "Hail Purdue" consists of four strains. The four strains<br> | ||
+ | % and corresponding notes are set into arrays here:<br> | ||
+ | Strain1 = [Ef, F, G, Af, Bf, C, C, Df, Df, Df, Af, Bf, B, C] ;<br> | ||
+ | Strain2 = [C, C, Bf, Af, Bf, C, C, Bf, F, G, Af, G, F, Bf] ;<br> | ||
+ | Strain3 = [Ef, Ef, F, G, Af, Bf, C, C, C, Df, Df, Af, Bf, C] ;<br> | ||
+ | Strain4 = [F, G, Af, F, Ef, Af, C, Ef, F, C, Bf, Af, Af];<br> | ||
+ | <br> | ||
+ | % The associated length of each note is set into 3 different arrays here:<br> | ||
+ | Time1 = [H, Q, Q, DQ, E, Q, Q, Q, E, E, Q, E, E, T] ; % Used for strains 1 and 2<br> | ||
+ | Time2 = [DQ, E, Q, Q, DQ, E, Q, E, E, Q, Q, Q, Q, T]; <br> | ||
+ | Time3 = [DQ, E, Q, Q, Q, Q, Q, Q, DQ, E, DQ, E, T];<br> | ||
+ | <br> | ||
+ | % Because the song is going to have to be played a total of 3 times,<br> | ||
+ | % putting the playing part in a loop would make the code a lot shorter. We<br> | ||
+ | % can use the counter (j) to tell it to change the things we want to change<br> | ||
+ | % each time as well.<br> | ||
+ | for j = 1:3<br> | ||
+ | |||
+ | % The second time, the tempo has to increase | ||
+ | if j == 2 | ||
+ | E = E/2; | ||
+ | Q = Q/2; | ||
+ | DQ = DQ/2; | ||
+ | H = H/2; | ||
+ | T = T/2; | ||
+ | Time1 = [H, Q, Q, DQ, E, Q, Q, Q, E, E, Q, E, E, T] ; % Used for strains 1 and 2 | ||
+ | Time2 = [DQ, E, Q, Q, DQ, E, Q, E, E, Q, Q, Q, Q, T]; | ||
+ | Time3 = [DQ, E, Q, Q, Q, Q, Q, Q, DQ, E, DQ, E, T]; | ||
+ | end | ||
+ | |||
+ | % Now make each note a full scale higher for the third run | ||
+ | if j == 3 | ||
+ | Ef = Ef*2; | ||
+ | F = F*2; | ||
+ | G = G*2; | ||
+ | Af = Af*2; | ||
+ | Bf = Bf*2; | ||
+ | B = B*2; | ||
+ | C = C*2; | ||
+ | Df = Df*2; | ||
+ | Strain1 = [Ef, F, G, Af, Bf, C, C, Df, Df, Df, Af, Bf, B, C] ; | ||
+ | Strain2 = [C, C, Bf, Af, Bf, C, C, Bf, F, G, Af, G, F, Bf] ; | ||
+ | Strain3 = [Ef, Ef, F, G, Af, Bf, C, C, C, Df, Df, Af, Bf, C] ; | ||
+ | Strain4 = [F, G, Af, F, Ef, Af, C, Ef, F, C, Bf, Af, Af]; | ||
+ | end | ||
+ | % For each strain, a loop creates the length of time needed for the note | ||
+ | % and then plays it | ||
+ | for i = 1:14 | ||
+ | time = 0 : delta : Time1(i);<br> | ||
+ | sound(sin(2*pi*Strain1(i)*time), 1/delta) | ||
+ | Song = [Song, sin(2*pi*Strain1(i)*time)]; | ||
+ | end<br> | ||
+ | |||
+ | % A quarter rest separates each strain | ||
+ | Rest = ones(Q/delta, 1); | ||
+ | sound(Rest, 1/delta) | ||
+ | Song = [Song, Rest]; | ||
+ | |||
+ | % Second strain | ||
+ | for i = 1:14 | ||
+ | time = 0 : delta : Time1(i); | ||
+ | sound(sin(2*pi*Strain2(i)*time), 1/delta) | ||
+ | Song = [Song, sin(2*pi*Strain2(i)*time)]; | ||
+ | end | ||
+ | |||
+ | % Quarter Rest | ||
+ | sound(Rest, 1/delta) | ||
+ | Song = [Song, Rest]; | ||
+ | |||
+ | % Third Strain | ||
+ | for i = 1:14 | ||
+ | time = 0 : delta : Time2(i); | ||
+ | sound(sin(2*pi*Strain3(i)*time), 1/delta) | ||
+ | Song = [Song, sin(2*pi*Strain3(i)*time)]; | ||
+ | end | ||
+ | |||
+ | % Quarter Rest | ||
+ | sound(Rest, 1/delta) | ||
+ | Song = [Song, Rest]; | ||
+ | |||
+ | % Last Strain | ||
+ | for i = 1:13 | ||
+ | time = 0 : delta : Time3(i); | ||
+ | sound(sin(2*pi*Strain4(i)*time), 1/delta) | ||
+ | Song = [Song, sin(2*pi*Strain4(i)*time)]; | ||
+ | end | ||
+ | |||
+ | % If it's the second time, recreate the original tempo | ||
+ | if j == 2 | ||
+ | E = E*2; | ||
+ | Q = Q*2; | ||
+ | DQ = DQ*2; | ||
+ | H = H*2; | ||
+ | T = T*2; | ||
+ | Time1 = [H, Q, Q, DQ, E, Q, Q, Q, E, E, Q, E, E, T] ; % Used for strains 1 and 2 | ||
+ | Time2 = [DQ, E, Q, Q, DQ, E, Q, E, E, Q, Q, Q, Q, T]; | ||
+ | Time3 = [DQ, E, Q, Q, Q, Q, Q, Q, DQ, E, DQ, E, T]; | ||
+ | end | ||
+ | |||
+ | Song = [Song, Rest, Rest, Rest, Rest, Rest, Rest]; | ||
+ | |||
+ | end | ||
+ | |||
+ | % Use the completed Song array that has been building with each additional<br> | ||
+ | % note and write to a .wav file<br> | ||
+ | wavwrite(Song, 44100, 'Hail_Purdue.wav')<br> | ||
== Sound File == | == Sound File == | ||
+ | |||
+ | [[Media:Hail_Purdue_ECE301Fall2008mboutin.wav|here!]] |
Latest revision as of 16:18, 4 September 2008
Matlab Code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Kathleen Schremser %
% ECE 301 %
% Homework 1, due Friday, September 5, 2008 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Clear the screen
clear
clc
% Define delta as used in class
delta = 0.00005;
% Define the tempo you wish to use (Number of Quarter notes per minute)
% Note: The time signature for this piece is taken to be 2/4
% Also, create a blank Song array to store the completed song (to be used later)
Tempo = 152;
Song = [];
% Define the length of each Eighth, Quarter, Dotted Quarter, Half, and Tied
% Note (A Tied note consists of 3 Quarter notes)
E = 1/2 * 60/Tempo;
Q = 60/Tempo;
DQ = 1.5 * 60/Tempo;
H = 2 * 60/Tempo;
T = 3/2 * 60/Tempo;
% Define the frequency of each note
% The key of Hail Purdue is D flat
Ef = 311.13;
F = 349.23;
G = 392.00;
Af = 415.30;
Bf = 466.16;
B = 493.88;
C = 523.25;
Df = 554.36;
% The chorus of "Hail Purdue" consists of four strains. The four strains
% and corresponding notes are set into arrays here:
Strain1 = [Ef, F, G, Af, Bf, C, C, Df, Df, Df, Af, Bf, B, C] ;
Strain2 = [C, C, Bf, Af, Bf, C, C, Bf, F, G, Af, G, F, Bf] ;
Strain3 = [Ef, Ef, F, G, Af, Bf, C, C, C, Df, Df, Af, Bf, C] ;
Strain4 = [F, G, Af, F, Ef, Af, C, Ef, F, C, Bf, Af, Af];
% The associated length of each note is set into 3 different arrays here:
Time1 = [H, Q, Q, DQ, E, Q, Q, Q, E, E, Q, E, E, T] ; % Used for strains 1 and 2
Time2 = [DQ, E, Q, Q, DQ, E, Q, E, E, Q, Q, Q, Q, T];
Time3 = [DQ, E, Q, Q, Q, Q, Q, Q, DQ, E, DQ, E, T];
% Because the song is going to have to be played a total of 3 times,
% putting the playing part in a loop would make the code a lot shorter. We
% can use the counter (j) to tell it to change the things we want to change
% each time as well.
for j = 1:3
% The second time, the tempo has to increase if j == 2 E = E/2; Q = Q/2; DQ = DQ/2; H = H/2; T = T/2; Time1 = [H, Q, Q, DQ, E, Q, Q, Q, E, E, Q, E, E, T] ; % Used for strains 1 and 2 Time2 = [DQ, E, Q, Q, DQ, E, Q, E, E, Q, Q, Q, Q, T]; Time3 = [DQ, E, Q, Q, Q, Q, Q, Q, DQ, E, DQ, E, T]; end % Now make each note a full scale higher for the third run if j == 3 Ef = Ef*2; F = F*2; G = G*2; Af = Af*2; Bf = Bf*2; B = B*2; C = C*2; Df = Df*2; Strain1 = [Ef, F, G, Af, Bf, C, C, Df, Df, Df, Af, Bf, B, C] ; Strain2 = [C, C, Bf, Af, Bf, C, C, Bf, F, G, Af, G, F, Bf] ; Strain3 = [Ef, Ef, F, G, Af, Bf, C, C, C, Df, Df, Af, Bf, C] ; Strain4 = [F, G, Af, F, Ef, Af, C, Ef, F, C, Bf, Af, Af]; end
% For each strain, a loop creates the length of time needed for the note % and then plays it
for i = 1:14 time = 0 : delta : Time1(i);
sound(sin(2*pi*Strain1(i)*time), 1/delta) Song = [Song, sin(2*pi*Strain1(i)*time)]; end
% A quarter rest separates each strain
Rest = ones(Q/delta, 1); sound(Rest, 1/delta) Song = [Song, Rest];
% Second strain
for i = 1:14 time = 0 : delta : Time1(i); sound(sin(2*pi*Strain2(i)*time), 1/delta) Song = [Song, sin(2*pi*Strain2(i)*time)]; end
% Quarter Rest
sound(Rest, 1/delta) Song = [Song, Rest];
% Third Strain
for i = 1:14 time = 0 : delta : Time2(i); sound(sin(2*pi*Strain3(i)*time), 1/delta) Song = [Song, sin(2*pi*Strain3(i)*time)]; end
% Quarter Rest
sound(Rest, 1/delta) Song = [Song, Rest];
% Last Strain
for i = 1:13 time = 0 : delta : Time3(i); sound(sin(2*pi*Strain4(i)*time), 1/delta) Song = [Song, sin(2*pi*Strain4(i)*time)]; end
% If it's the second time, recreate the original tempo if j == 2 E = E*2; Q = Q*2; DQ = DQ*2; H = H*2; T = T*2; Time1 = [H, Q, Q, DQ, E, Q, Q, Q, E, E, Q, E, E, T] ; % Used for strains 1 and 2 Time2 = [DQ, E, Q, Q, DQ, E, Q, E, E, Q, Q, Q, Q, T]; Time3 = [DQ, E, Q, Q, Q, Q, Q, Q, DQ, E, DQ, E, T]; end Song = [Song, Rest, Rest, Rest, Rest, Rest, Rest];
end
% Use the completed Song array that has been building with each additional
% note and write to a .wav file
wavwrite(Song, 44100, 'Hail_Purdue.wav')