I548/N560 R DEMO AND PRACTICE #1 Let's start with what I did in class on Monday, with some minor improvements. R version 2.5.1 (2007-06-27) Copyright (C) 2007 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. 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. First, give R the formula (from my "Musical Pitches" webpage) to convert MIDI note number _m_ to frequency in Hz (short for "Hertz"): freq = 440 * 2^((m-69)/12) . When you add one to the note number, the frequency gets multiplied by 2^(1/12), i.e., the 12th root of 2; that's a bit more than 1.059. 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. (If you'd like to see a demonstration in R of that fact, I have a simple program.) > 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 m 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.) > m <- seq(60, 72) > m [1] 60 61 62 63 64 65 66 67 68 69 70 71 72 > freq <- 440 * 2^((m-69)/12) > freq [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(m, freq) 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 :-) . 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(m, freq, type="l") > plot(m, freq, type="s") To change the subject for a minute, _random numbers_ are useful for a lot of things, especially in statistics (R's original raison d'etre), and R has a bunch of functions for generating them. Statisticians use different _distributions_ of random numbers; here's how to create a vector of 13 that are _uniformly distributed_ between 0 and 1, i.e., all values from 0 to 1 are equally likely. > rand <- runif(13) > rand [1] 0.69691881 0.41623580 0.89203569 0.01087129 0.76750285 0.88783949 [7] 0.55409075 0.32053543 0.82491674 0.89855120 0.84715513 0.75206692 [13] 0.62230993 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 that. 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. Or how about a random order of the notes of the chromatic scale? Big hint: the "sample" function used in "A fun example" on page 2 of the "An introduction to R" does the job. (Do it with a 12-note chromatic scale instead of 13 and you have a tone row, ala Schoenberg's infamous 12-tone technique.)