Plot a waveform (from a WAV file), IPA notation (Unicode), and two more data curves on the same plot. Uses R Basic Graphics only.

# construct a range in ms; this helps with the labels
maxms <- ceiling(nsamples/20/100)*100
# plot the waveform without axes
par(xpd=NA)
Plot1<-plot(wavData[1:length(wavData)], type = 'l',
     axes=F,ylab='',xlab='Time (ms)',
     ylim=c(-22000,50000),xlim=c(1,20*maxms+2000),col='slateblue')
# this is a sequence in ms, given 20 samples/ms at 20 kHz
msSeq = seq(0,maxms*20,200*20)
# put the x axis in
axis(1,at=msSeq,labels=c('0','200','400','600','800','1000','1200'))
# set some values that will be used a lot
yval=-20000
cexval = 1.1
posval = 3
# use Unicode values for IPA
# http://en.wikipedia.org/wiki/International_Phonetic_Alphabet)
text(msSeq[1]+500,yval,'\u0074',pos=posval,cex=cexval)   # t
text(msSeq[2]-500,yval,'\u0075',pos=posval,cex=cexval)   # oo
text(msSeq[3]-900,yval,'\u03b8',pos=posval,cex=cexval)   # th
text(msSeq[4]-500,yval,'\u0062',pos=posval,cex=cexval)   # b
text(msSeq[4]+600,yval,'\u0072',pos=posval,cex=cexval)   # r
text(msSeq[5]-1000,yval,'\u028c',pos=posval,cex=cexval)  # u
text(msSeq[6],yval,'\u0283',pos=posval,cex=cexval)       # sh

# now, add the loudness model data to the plot
# read up the sones/phons data from a CSV file
loud <- read.csv('toothtdh.csv',header=T)
# remove any values for long-term that are < 1
# this is to remove negative values of log10()
loud=loud[loud$lt_sones>=1,]

# extract the long/short-term sones data
# take the log10() and scale it to make the waveform cover more space in 
# the y dimension 
scale_sones = 10000
lt_sones<-data.frame(ms=loud$ms,lt_sones=log10(loud$lt_sones)*scale_sones)
st_sones<-data.frame(ms=loud$ms,st_sones=log10(loud$st_sones)*scale_sones)# display the long-term loudness model output (lt_sones)
yoffset = 15000
# Long-term loudness
text(-2000,yoffset+5000,'Long-term\nAvg',cex=.9)
# scale the data to 20 points per ms and offset it by yoffset to
# move it up the page
lines(lt_sones$ms*20,lt_sones$lt_sones+yoffset,lwd=2)
# put a line at the top value and the lowest value
lines(c(0,24750),c(min(lt_sones$lt_sones)+yoffset,min(lt_sones$lt_sones)+yoffset),lty=3)
lines(c(0,24750),c(max(lt_sones$lt_sones)+yoffset,max(lt_sones$lt_sones)+yoffset),lty=3)
# put the label next to the min/max value lines
# label has to come from the original sone values (not the log10 values)
text(24700,min(lt_sones$lt_sones)+yoffset,paste(min(loud$lt_sones),'sones'),pos=4,cex=.8)
text(24700,max(lt_sones$lt_sones)+yoffset,paste(max(loud$lt_sones),'sones'),pos=4,cex=.8)

# display the short-term loudness model output (st_sones)
yoffset = 40000
# Short-term loudness
text(-2000,yoffset+5000,'Short-term\nAvg',cex=.9)
# scale the data to 20 points per ms and offset it by yoffset to
# move it up the page
lines(st_sones$ms*20,st_sones$st_sones+yoffset,lwd=2)
# put a line at the top value and the lowest value
lines(c(0,24750),c(min(st_sones$st_sones)+yoffset,min(st_sones$st_sones)+yoffset),lty=3)
lines(c(0,24750),c(max(st_sones$st_sones)+yoffset,max(st_sones$st_sones)+yoffset),lty=3)
# put the label next to the min/max value lines
# label has to come from the original sone values (not the log10 values)
text(24700,min(st_sones$st_sones)+yoffset,paste(min(loud$st_sones),'sones'),pos=4,cex=.8)
text(24700,max(st_sones$st_sones)+yoffset,paste(max(loud$st_sones),'sones'),pos=4,cex=.8)