### freq_count1.r ### R program to do frequency count of a couple of pieces ### author: Ian Knopke, for I400 at IU, Feb 2007 library(tuneR) scale<-matrix( c( 0, 1, 2, 1, 4, 2, 5, 1, 7, 1, 9, 2, 11, 1, 12, 1 ),2,8) scale_pitches=scale[1,] scale_durations=scale[2,] ##need an empty array with an empty spot for each pitch ##in histogram parlance, these are the "bins" that we store ##results in scale_freqcount=rep(0,12) ##so now we have to loop through all of the pitches for(i in 1:length(scale_pitches)) { ##get one pitch from the array note=scale_pitches[i] ##keep it between 0 and 11. So 12 = 0, 13 = 1, -1 = 11, etc. note =note %% 12 ##stick our "corrected" pitch into the frequency count array by ##incrementing the number in that spot ##The only catch is that R indexes arrays starting at 1 ##and we have values ranging over 0-11 ##so we have to add one to the index scale_freqcount[note+1]=scale_freqcount[note+1]+1; ##uncomment this to see the array get filled #print(scale_freqcount) } ##print out the frequency count print(scale_freqcount) ##and do the graph par(mfrow=c(2,1)) plot(scale_pitches) ##for display purposes, we need to specify the x axis as 0:11 plot(0:11,scale_freqcount) ##we don't need to play these here