Vous êtes sur la page 1sur 3

ENGR121 Music in MATLAB Part the First

SS Moor

A musical note consists can be described in terms if its frequency (pitch), its amplitude (volume) and its shape (its character). For a simple model of sound we are starting with a simple sine wave. A single note could be represented by the function: A sin( 2 ) ft where: f is the frequency (cycles per second) t is the elapsed time (seconds) A is the amplitude (dimensionless for our case) Creating a simple tone in MATLAB: The set of commands below will create a sine wave for a frequency of 440Hz (middle A or A4), with a sampling frequency of 8000 and a duration of a second. . >> % first we will set input variables for the sampling frequency (sd) and duration (dur) >> sf = 8000 >> dur = 0.5 >> % we set up the time vector that is dur seconds long and >> % has times every 1/sf sec. >> t = 0:1/sf:dur; >> % next we create an amplitude series for a sine wave. >> a=sin(2*pi*440*t); >> % finally we can play the tone we have created >> sound(a,sf) You could also use the wavwrite command to save this file as a windows wav file. The form for this command would be wavwrite(song, 8000, file_name.wav) Notes and Frequencies for an Equal-Tempered Scale: (adapted from Bryan H. Suits, Frequencies for equal-tempered scale, n.d. http://www.phy.mtu.edu/~suits/notefreqs.html, accessed August 2004). Octave 0 1 2 3 4 5 6 7 C 16.35 32.7 65.41 130.81 261.63 523.25 1046.5 2093 1108.7 C#/Db 17.32 34.65 69.3 138.59 277.18 554.37 3 2217.46 1174.6 D 18.35 36.71 73.42 146.83 293.66 587.33 6 2349.32 1244.5 D#/Eb 19.45 38.89 77.78 155.56 311.13 622.25 1 2489.02 1318.5 E 20.6 41.2 82.41 164.81 329.63 659.26 1 2637.02 1396.9 F 21.83 43.65 87.31 174.61 349.23 698.46 1 2793.83 # b F /G 23.12 46.25 92.5 185 369.99 739.99 1479.9 2959.96

ENGR121 8 1567.9 8 1661.2 2 1760 1864.6 6 1975.5 3

SS Moor

G G#/Ab A A#/Bb B

24.5 25.96 27.5 29.14 30.87

49 51.91 55 58.27 61.74

98 103.83 110 116.54 123.47

196 207.65 220 233.08 246.94

392 415.3 440 466.16 493.88

783.99 830.61 880 932.33 987.77

3135.96 3322.44 3520 3729.31 3951.07

ENGR121 A simple song script file: Notice the nature of the comments in the program.

SS Moor

% Program song1.m the file name used. This is followed % Prepared by S. Scott Moor, IPFW August 2004 by the authors name and the date. % Based on suggestions from Shreekanth Mandayam, % Department of Electrical and Computer Engineering, Rowan University % see http://users.rowan.edu/~shreek/networks1/music.html % Describe the purpose and basic % This program creates sine waves for a series of standard approach of the program % notes. Each note is set up to be 0.5 seconds long at a % sample rate of 8000 Hz. The notes are assembled into a % song that is then played by the computer speaker. % variables used: will be printed in the command % sf = sampling frequency (samples/sec) window if you type >> help song1 % x = time vector (sec.) % a, b, cs, d, e, & fs = the amplitude series for the notes used in the song % line1, line2, line3 = the amplitude series for each line of the song Define the variables used % song = the amplitude series for the entire song. % set up time series sf = 8000; x = [0:1/sf:0.5]; % define each note a=sin(2*pi*440*x); b=sin(2*pi*493.88*x); cs=sin(2*pi*554.37*x); d=sin(2*pi*587.33*x); e=sin(2*pi*659.26*x); fs=sin(2*pi*739.99*x); % assembling the notes into a song line1 = [a,a,e,e,fs,fs,e,e]; line2 = [d,d,cs,cs,b,b,a,a]; line3 = [e,e,d,d,cs,cs,b,b]; song = [line1,line2,line3,line3,line1,line2]; % now we play the song sound(song, sf)
Through out the program include comments that describe the function of each part of the program Comments before the first blank line

Start with the word program and

in the script. Sometimes this list is divided into input, output and intermediate variables