Matlab Code


% Ben Moeller
% ECE 301 Homework 1
% Beethoven's 5th

%Clear Memory
clear all;
clc;

%set loop counter
i = 1;

%Designate note frequencies according to function Freq = note x 2^(N/12)
while (i < 22)
    notes(i) = (65.41)*2^((i-1)/12)*2;
    i= i + 1;
end

%assign note frequencies accordingly (not all will be used)
c = notes(1);
cs = notes(2);
d = notes(3);
ds = notes(4);
e = notes(5);
f = notes(6);
fs = notes(7);
g = notes(8);
ab = notes(9);
a = notes(10);
as = notes(11);
b = notes(12);
c2 = notes(13);
cs2 = notes(14);
d2 = notes(15);
ds2 = notes(16);
e2 = notes(17);
f2 = notes(18);
fs2 = notes(19);
g2 = notes(20);
%rest has no frequency
r = 0;
%user enters desired playing tempo
tempo = input('Please enter a tempo: ');
%assign values accordingly for quarter and whole notes, as they are the
%only types of notes in this song.
Q = 60/tempo;
W = Q*4;

%create the numerical "score"
score = [r g g g ds r f f f d ...
         r g g g ds ab ab ab g ds2 ds2 ds2 c2 ...
         r g g g d ab ab ab g f2 f2 f2 d2 ...
         r g g f ds ds2 ds2 f2 g2 g g f ds ds2 ds2 f2 ...
         g2 g g f ds];
%designate how long each note/rest will last
time = [Q Q Q Q W Q Q Q Q W ...
        Q Q Q Q Q Q Q Q Q Q Q Q W ...
        Q Q Q Q Q Q Q Q Q Q Q Q W ...
        Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q ...
        Q Q Q Q W];
%as decided in class...     
del = 1/20000;

%reset loop counter
i = 1;

%Play at normal tempo
while (i <= length(score))
    t = 0:del:time(i);
    x = sin(2*pi*t*score(i));
    sound(x, 1/del);
    i = i + 1;
end
%reset loop counter
i = 1;

%play at 2x tempo
while (i <= length(score))
    t = 0:del:time(i);
    x = sin(2*pi*t*score(i));
    sound(x, 1/del*2);
    i = i + 1;
end
%reset loop counter
i = 1;
%play at 2x frequency
while (i <= length(score))
    t = 0:del:time(i);
    x = sin(2*2*pi*t*score(i));
    sound(x, 1/del);
    i = i + 1;
end

Alumni Liaison

Ph.D. 2007, working on developing cool imaging technologies for digital cameras, camera phones, and video surveillance cameras.

Buyue Zhang