Revision as of 12:53, 4 September 2008 by Thouliha (Talk)

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

The wavwrite command would not work due to some errors in sprintf, but the code plays the song correctly

Matlab code


%Tyler Houlihan(thouliha@purdue.edu) 0016165728
%9/3/08
%ECE301 HW 1
%-------------------------------------------------

%This program will play the theme to Hail Purdue 
%I have created a note system with which to do it,
%and this program will play the song at various 
%speeds and pitches.




%clearing the vars
clear;

%Note times
%----------
Q=60/150; %Quarter note bpm is 150
H=2*Q;    %Half
W=4*Q;    %Whole
EI=.5*Q;   %Eighth
S=.25*Q;  %Sixteenth
DH=3*Q;   %Dotted Half
DQ=1.5*Q; %Dotted Quarter

%Note Frequencies
%----------------
%Frequency amounts define Frequency(Octave, #of note starting with C)
%This creates a table for frequencies for 5 octaves
for N=1:12
  %using algorithm at http://www.phy.mtu.edu/~suits/notefreqs.html 
  %and A4=440Hz
  Hz=32.7031957*1.059463094359^(N-1);
  
  for X=1:5
    Frequency(N,X)=Hz*2^(X-1);
  end
end




%Now, for naming the notes for convention:
for X=1:5
  C(X)=Frequency(1,X);
  Db(X)=Frequency(2,X);
  D(X)=Frequency(3,X);
  Eb(X)=Frequency(4,X);
  E(X)=Frequency(5,X);
  F(X)=Frequency(6,X);
  Gb(X)=Frequency(7,X);
  G(X)=Frequency(8,X);
  Ab(X)=Frequency(9,X);
  A(X)=Frequency(10,X);
  Bb(X)=Frequency(11,X);
  B(X)=Frequency(12,X);
end

%Now part 1a, play the song using notes and times, citing help from Ben
%Laskowski's homework @
%http://kiwi.ecn.purdue.edu/ECE301Fall2008mboutin/index.php/HW1.1_Ben_Laskowski




%Making the song notes and durations
%-----------------------------------
%Done as heard on youtube, in key of B-flat
notes=[Bb(4),G(4),A(4),Bb(4),C(5),D(5),D(5),Eb(5),Eb(5),Eb(5),Bb(4),C(5),Db(5),D(5),D(5),A(4),C(5),Bb(4),C(5),Bb(4),Bb(4),C(5),G(4),A(4),Bb(4),A(4),G(4),C(5),Bb(4),G(4),A(4),Bb(4),C(5),D(5),D(5),Eb(5),Eb(5),Eb(5),Bb(4),C(5),Db(5),D(5),G(4),A(4),Bb(4),G(4),F(4),Bb(4),D(5),F(4),G(4),D(5),C(5),Bb(4),Bb(4)];
times=[H,Q,Q,DQ,EI,Q,Q,Q,EI,EI,Q,EI,EI,W,H,Q,Q,DQ,EI,Q,Q,Q,EI,EI,Q,EI,EI,W,H,Q,Q,DQ,EI,Q,Q,Q,EI,EI,Q,EI,EI,W,DQ,EI,Q,Q,Q,Q,Q,Q,DQ,EI,DQ,EI,W];
delta=1/20000;%The sampling rate


%1(a)
%----------------
%Play the song
for x=1:55 %3 notes
	t=0:delta:times(x);	
	wave=sin(2*pi*t*notes(x));	%makes the wave
	sound(wave,1/delta);		%plays the sound(s)
end

pause(2)  %Pauses for 2 seconds

%1(b)
%-----------------
%Now play it twice as fast
times=times*.5;

for x=1:55 %3 notes
	t=0:delta:times(x);	
	wave=sin(2*pi*t*notes(x));	%makes the wave
	sound(wave,1/delta);		%plays the sound(s)
end


%pausing and resetting time
pause(2)
times=times/.5;


%1(c)
%---------------
%Take the signal and transform it to y(t)=wave(2t)
%so just plug in 2t wherever t is

for x=1:55 %3 notes
	t=0:delta:times(x);	
	wave=sin(2*pi*2*t*notes(x));	%makes the new waves
	sound(wave,1/delta);		%plays the sound(s)
end

%I noticed this played the song an octave higher, my ears are murdered
%right now

Alumni Liaison

Questions/answers with a recent ECE grad

Ryne Rayburn