I545 R DEMO AND ASSIGNMENT #1 - Don Byrd - rev. 21 Jan. 2008 R version 2.5.1 (2007-06-27) ... Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. The header stuff R spits out every time you launch is actually helpful! For example, try > demo(graphics) Now to music. First, look at my "Musical Pitches" webpage http://www.informatics.indiana.edu/donbyrd/Teach/MusicalPitchesTable.htm Notice the "MIDI" columns, which give the MIDI _note numbers_ for the pitches; also the formula to convert MIDI note number _m_ to frequency in Hz (short for "Hertz", i.e., cycles per second): freq = 440 * 2^((m-69)/12) . It turns out that equal musical intervals -- in this case, a half-step -- don't _add_ a constant to the frequency, they _multiply_ it by a constant. (Are you uncertain about this? Check out the Intervals.r sample program.) When you add one to the note number, meaning to go up a semitone, the frequency gets multiplied by 2^(1/12), i.e., the 12th root of 2; do that 12 times and you're up an octave, at twice the original frequency. But what is the 12th root of 2? > 2^(1/12) [1] 1.059463 OK, so a semitone is a change in frequency of a little less than 6%. (If this seems like it's not enough for doing it only 12 times to double the frequency, it's like compound interest. $1000 at 6% simple interest for 12 years earns $60 a year, so you'd end up with $1720. But with compound interest, you get interest on the previously-earned interest, and you have over $2000.) Now, give R the formula (from my "Musical Pitches" webpage) to convert MIDI note number to frequency in Hz. > freq = 440 * 2^((m-69)/12) Error: object "m" not found Oops! That's the error message R gives for an "undefined variable". Also, we want to use "<-" instead of "=" to assign a value to a variable in R; there's too much potential for confusion with "=". Tell R we want middle C (note no. 60), and have it convert and tell us the result. > m <- 60 > freq <- 440 * 2^((m-69)/12) > freq [1] 261.6256 Now a more realistic case: let's compute frequencies for the octave from middle C up. First set mV to a vector of integers from 60 to 72, then convert to a vector of frequencies...and plot (show a graph of) the results. (It'd be very nice to hear it, too! We'll learn how to do that very soon.) > mV <- seq(60, 72) > mV [1] 60 61 62 63 64 65 66 67 68 69 70 71 72 > freqV <- 440 * 2^((mV-69)/12) > freqV [1] 261.6256 277.1826 293.6648 311.1270 329.6276 349.2282 369.9944 391.9954 [9] 415.3047 440.0000 466.1638 493.8833 523.2511 > plot(mV, freqV) If you typed the above line rather than pasting it in, notice how, as soon as you type the "(", R gives you the matching ")". It also automatically matches quote marks, etc.; ; this is a new feature in R that's obviously supposed to help, and it often does :-) . (But apparently it's only on OS X, not Windows. What about Linux, Michel?) You can get help on any of R's built-in features by just typing the word "help" and the name. In most cases, you can just type "?" and the name: "help(plot)" and "?plot" do the same thing. > ?plot Ah, so we can get other kinds of graphs that might be more appropriate for this! > plot(mV, freqV, type="l") > plot(mV, freqV, type="s") Here's how to create a vector of 13 random numbers that are _uniformly distributed_ (all values are equally likely) between 0 and 1. > randV = runif(13) > randV [1] 0.87241204 0.04565209 0.89553221 0.65125919 0.89682140 0.88236162 [7] 0.76059688 0.70089813 0.24045678 0.15249084 0.19569958 0.02001709 [13] 0.68929681 This is silly, but: > plot(mV, randV) ----------------------------------------------------------------------------- HOMEWORK: Now, on your own, try a couple of more interesting things. 1. Instead of a boring 13-note chromatic scale in the octave starting at middle C, generate 13 random notes in the octave starting at middle C, and plot their frequencies in order. I recommend using plot type="s". The plot should look something like HW2Item2Pic.png in the same directory as this assignment. Hint: you'll need the random numbers to cover a range from 0 to 12, not just 0 to 1; that means multiplying the values runif gives by 12. Of course the numbers won't be integers, so you'll have microtonal pitches! Don't worry about that, but if you want, try the "round" or "trunc" function to make them integers. 2. Do the same thing, but with something slightly different: a _permutation_ (random order) of the notes of the chromatic scale. Big hint: the "sample" function used in "A fun example", near the end of the R Tutorial, does the job. (Note that if you did this with a 12-note chromatic scale instead of 13, you'd have a tone row, ala Schoenberg's infamous 12-tone technique.) INFORMATICS AND COMPUTER SCIENCE MAJORS (and anyone that feels ambitious), also do this: 3. Modify ChromaticWanderings.r to make it do _diatonic_ wandering instead. Choose a tonic -- for simplicity, you might as well use C -- and a scale (major, any form of minor, or whatever you want, but NOT whole-tone). Then change the program so it either emphasizes notes in the scale (by playing them more often or for a longer duration when it does play them, etc.), or so it plays _only_ those notes. Be sure to add a comment saying what scale your program plays. You don't need to make it work for any values of the low and high notes of the range, just those in the range it plays, but it'd be even better if it works for any possible range of MIDI notes. 4. Have #1 and #2 play the notes as well as plotting them. One easy way to do this is to add the code for #1 and #2 to ChromaticWanderings.r. EVERYONE, here's what to submit: * For #1 and 2, and #4 if you do it, save the R Console output (use R's Save command) on a file, and send the file to me as an e-mail attachment; it should be a nice small file. If you made a lot of mistakes when you were doing it, please quit R, start over, redo it with no (or fewer) mistakes, and send me that. That will save me time looking for the good stuff, as well as reducing the chance I'll give you a low grade because I never noticed it :-) . * If you do #3, send me your source code as an e-mail attachment.