%Joe Katz
%ECE301 HW 1 Problem 1
%1/18/2011

%Part 1:

%Parameters:
Fs = 8192;
Tempo = 112; %Tempo in beats per minute

%Note selection
NoteLengths = [ 1, 1, 1.5, 1, 1, 0.5, 2, 1, 1, 1.5, 1, 3.5]; %beats
NoteValues = [ -2, 1, 3, -2, 1, 4, 3, -2, 1, 3, 1, -2]; %half steps above A4

%Sound matrix realization
BeatLength = 60/Tempo;
SongLength = round(BeatLength*sum(NoteLengths)*Fs)+1; %(sec/beat)x(#beats)x(samples/sec)
Song1 = zeros(SongLength,2);
Song1(:,1) = 1:SongLength;
for l=1:length(NoteLengths)
NoteStart = round(BeatLength*(sum(NoteLengths(1:l))-NoteLengths(l))*Fs)+1;
NoteStop = round(BeatLength*sum(NoteLengths(1:l))*Fs);
Fnote = (2^(NoteValues(l)/12))*440;
Song1(NoteStart:NoteStop,2) = sin(2*pi*Fnote*(Song1(NoteStart:NoteStop,1))./Fs);
end

%Part 2:
BeatLength = 60/(2*Tempo);
SongLength = round(BeatLength*sum(NoteLengths)*Fs)+1; %(sec/beat)x(#beats)x(samples/sec)
Song2 = zeros(SongLength,2);
Song2(:,1) = 1:SongLength;
for l=1:length(NoteLengths)
NoteStart = round(BeatLength*(sum(NoteLengths(1:l))-NoteLengths(l))*Fs)+1;
NoteStop = round(BeatLength*sum(NoteLengths(1:l))*Fs);
Fnote = (2^(NoteValues(l)/12))*440;
Song2(NoteStart:NoteStop,2) = sin(2*pi*Fnote*(Song2(NoteStart:NoteStop,1))./Fs);
end

%Part 3:
BeatLength = 60/(2*Tempo);
SongLength = round(BeatLength*sum(NoteLengths)*Fs)+1; %(sec/beat)x(#beats)x(samples/sec)
Song3 = zeros(SongLength,2);
Song3(:,1) = 1:SongLength;
for l=1:length(NoteLengths)
NoteStart = round(BeatLength*(sum(NoteLengths(1:l))-NoteLengths(l))*Fs)+1;
NoteStop = round(BeatLength*sum(NoteLengths(1:l))*Fs);
Fnote = (2^(NoteValues(l)/12))*440;
Song3(NoteStart:NoteStop,2) = sin(2*pi*Fnote*(2*Song3(NoteStart:NoteStop,1))./Fs);
end

Song = [Song1(:,2);Song2(:,2);Song3(:,2)];

sound(Song(:),Fs)
wavwrite(Song(:),Fs,'HW1_prob1.wav');


%Joe Katz
%ECE301 HW 1 Problem 2
%1/18/2011

% Part 1: "Number Nine"

% Part 2:
[y, Fs] = wavread('Beatles.wav');
y_back = flipud(y);
sound(y_back,Fs)
wavwrite(y_back,Fs,'HW1_prob2.wav')
% It doesn't sound like anything in English to me. "Hurry on there man" is
% the best I could get, but it doesn't sound good enough to really sound
% like words to me.

Alumni Liaison

Correspondence Chess Grandmaster and Purdue Alumni

Prof. Dan Fleetwood