# Solution to I590/N560 R assignment #2, due 13 Oct. 2006 - Don Byrd # setwd("Documents/Music+AudioIR+DLs/RProgramming") # OS X (Don's G4; use YOUR path!) setwd("Desktop/TODAY") # OS X (M373 Mac) # setwd("C:/Documents and Settings/donbyrd.ADS/Music+AudioIR/Teaching+ClassGuestLectures/INFON560-Fall2006/IntervalFreqRAssignment") # Windows (Don's PC) # Read in the file and grab the number of notes it contains. noteTable = read.table("Dichterliebe01PMIDI.txt", header=TRUE) noteCount = length(noteTable$DataByte) # Create array of length to hold count for each interval, and initialize all the # elements to zero. intervalCount = seq(from=0, to=0, length.out=12) # For each note after the first, compute the interval from the previous note; then # compute the pitch class (0 to 11) of its abs. value, and add one to the count for # that pitch class. Unfortunately, arrays in R start with index 1 instead of 0, so # we have to add 1 to all the indices. for (i in 1:noteCount-1) { diff = noteTable$DataByte[i+1]-noteTable$DataByte[i] diffPC = abs(diff) %% 12 intervalCount[diffPC+1] = intervalCount[diffPC+1]+1 } # Finally, display the interval counts (a) numerically, and (b) in a histogram # labelled with the correct intervals (0 to 11, not 1 to 12). intervalCount interval = seq(from=0, to=11, length.out=12) plot(interval, intervalCount, type="h", xlab="interval (half-steps)")