# Make sure prepareData.R has run or prepareData.RData is loaded! # Show pre-normalized wait_next <- function() { print(readline('press for next graph')) } idx.wordpos_sequence_length <- function(word_pos, sequence_length) { !is.na(all_data_with_timing['word_pos']) & all_data_with_timing['word_pos'] == word_pos & !is.na(all_data_with_timing['sequence_length']) & all_data_with_timing['sequence_length'] == sequence_length } idx.wordname <- function(word_name) { !is.na(all_data_with_timing['word_name']) & all_data_with_timing['word_name'] == word_name } plot_selection <- function(curves, index, average = T, opacity = 0.1, xlab='x', ylab='y', mean_lty=2,mean_lwd=2,axes=T,href=T) { plot(curves[index,],lty=1,col=rgb(0,0,0,opacity),xlab=xlab,ylab=ylab,axes=axes,href=href) if (average) { lines(mean(curves[index, ]),lty=mean_lty,lwd=mean_lwd) } return(curves[index,]) } plot_list <- function(curves, average = T, opacity = 0.1, xlim=c(0,1), ylim=c(-10,10), xlab='x', ylab='y', mean_lty=2,mean_lwd=2,axes=T,href=T, mean_curve = NA,lty=1) { first <- T i <- 1 for (curve in curves) { if (first) { first <- F summed <- curve par(new=F) plot(curve, xlim=xlim, ylim=ylim, xlab=xlab, ylab=ylab, col=rgb(0,0,0,opacity),axes=axes, lty=lty[(i-1) %% length(lty) + 1]) } else { summed <- sum(curve, summed) plot(curve, xlim=xlim, ylim=ylim,axes=F, xlab='', ylab='', col=rgb(0,0,0,opacity), lty=lty[(i-1) %% length(lty) + 1]) } par(new=T) i <- i + 1 } if (any(is.na(mean_curve))) { mean_curve <- mean(summed) * (1/length(curves)) } if (average) { lines(mean_curve,lty=mean_lty,lwd=mean_lwd) } par(new=F) return(mean_curve) } # Plot the converted unnormalized bsplines. They are not combined and do not run on the same t and y range. # To account for this they must first all be selected and then afterwards plotted one-by-one. # Can be used on output of convert2bsplines e.g. amplitudes_bsplines and pitches_bsplines_hz plot_uncombined_selection <- function(bsplines, index, average = T, opacity = 1, xlab='x', ylab='y', mean_lty=2, mean_lwd=2,xlim=c(0, 1), ylim=c(-10, 10),axes=T) { first <- T evalsteps <- 40 evalpoints <- seq(xlim[1], xlim[2], (xlim[2] - xlim[1])/(evalsteps-1)) meanvalues <- matrix(NA, length(control_sample[control_sample==T]), evalsteps) i <- 1 for (row in (1:length(control_sample))[control_sample]) { participant <- as.character(all_data[[row, 'participant']]) sentence_pos <- as.numeric(as.character(all_data[row, 'sentence_pos'])) word_name <- as.character(all_data[row, 'word_name']) duration <- as.numeric(as.character(all_data_with_timing[row, 'duration'])) spline <- bsplines[[participant]]$waveforms[[sentence_pos]][[word_name]] if (first) { first <- F par(new=F) plot(spline, xlim=xlim, ylim=ylim, xlab=xlab, ylab=ylab, col=rgb(0,0,0,opacity),axes=axes) } else { plot(spline, xlim=xlim, ylim=ylim,axes=F, xlab='', ylab='', col=rgb(0,0,0,opacity)) } frange <- spline$fd$basis$range for (j in 1:length(evalpoints)) { jt <- evalpoints[j] if (jt >= frange[1] && jt <= frange[2]) { meanvalues[i, j] <- eval.fd(jt, spline$fd) } } i <- i+1 par(new=T) } ymean <- colMeans(meanvalues, na.rm=T) fmean <- convert_waveform2bspline(evalpoints[!is.na(ymean)], ymean[!is.na(ymean)]) plot(fmean, xlim=xlim, ylim=ylim,axes=F, xlab='', ylab='', col=1, lty=mean_lty,lwd=mean_lwd) par(new=F) } # Example amplitude control. control_sample <- (1:1107) %in% sample((1:1107)[idx.wordname('Venlo') == T], 10) # Unnormalized print('unnormalized') plot_uncombined_selection(amplitudes_bsplines, control_sample,ylim=c(0,0.05),xlim=c(0,0.7),xlab='t (s)', ylab='amplitude (Pa)',opacity=0.3) wait_next() # Normalized print('normalized') plot_selection(amplitudes_combined[[1]], control_sample, opacity=0.3, xlab='genormeerde tijd', ylab='intensiteit (Z-score)') wait_next() # Time-warp function plot_selection(amplitudes_landmarked_combined$warpfd, control_sample, opacity=0.3, xlab='bron t', ylab='doel t',axes=F,href=F) wait_next() # Landmarked print('combined') selection <- plot_selection(amplitudes_landmarked_combined[[1]], control_sample, opacity=0.2, xlab='genormeerde tijd', ylab='intensiteit (Z-score)') wait_next() # Registered subselection selection_registered <- register_landmarks_continuous(selection) # This will take a while plot_selection(selection_registered$regfd, 1:10 > 0, opacity=0.2, xlab='genormeerde tijd', ylab='intensiteit (Z-score)') # Example of pos 1 out of 5 print('pitches (1, 5)') plot_selection(pitches_registered, idx.wordpos_sequence_length(1,5)) wait_next() # Example of pos 5 out of 5 print('pitches (5, 5)') plot_selection(pitches_registered, idx.wordpos_sequence_length(5,5)) wait_next() # Show components pca1mean <- mean(pitches_registered_pca_varmx$scores[,1]) pca1sd <- sd(pitches_registered_pca_varmx$scores[,1]) pca2mean <- mean(pitches_registered_pca_varmx$scores[,2]) pca2sd <- sd(pitches_registered_pca_varmx$scores[,2]) prpv <- pitches_registered_pca_varmx plot(prpv) wait_next() plot_list(list(reconstruct(prpv, -pca1sd, 0), reconstruct(prpv, pca1sd, 0)), ylim=c(-2, 2), mean_curve=reconstruct(prpv, 0, 0), opacity=1,lty=c(3,1), ylab='intonatie (Z-score)', xlab ='genormeerde tijd') wait_next() plot_list(list(reconstruct(prpv, 0, -pca2sd), reconstruct(prpv, 0, pca2sd)), ylim=c(-2, 2), mean_curve=reconstruct(prpv, 0, 0), opacity=1,lty=c(3,1), ylab='intonatie (Z-score)', xlab ='genormeerde tijd') wait_next() plot_list(list(reconstruct(prpv, pca1sd, -pca2sd), reconstruct(prpv, pca1sd, pca2sd)), ylim=c(-2, 2), mean_curve=reconstruct(prpv, pca1sd, 0), opacity=1,lty=c(3,1), ylab='intonatie (Z-score)', xlab = 'genormeerde tijd') wait_next() plot_list(list(reconstruct(prpv, -pca1sd, -pca2sd), reconstruct(prpv, -pca1sd, pca2sd)), ylim=c(-2, 2), mean_curve=reconstruct(prpv, -pca1sd, 0), opacity=1,lty=c(3,1), ylab='intonatie (Z-score)', xlab ='genormeerde tijd') wait_next() data_by_wordform <- function(syllables, stress, column) { all_data_with_timing[all_data_with_timing[,'syllables']==syllables & all_data_with_timing[,'stress']==stress, column] } data_by_position <- function(sequence_length, word_pos, column) { all_data_with_timing[all_data_with_timing[,'sequence_length']==sequence_length & all_data_with_timing[,'word_pos']== word_pos, column] } print_position_data <- function(column) { print(column) for (i in 2:5) { for (j in 1:5) { if (j <= i) { print(c(i, j)) data <- data_by_position(i, j, column) print(c(mean(data), sd(data))) } } } } print_position_data('pitchv_PC1') print_position_data('pitchv_PC2') plot_wordform <- function(column, ylab, ylim) { func1 <- mean(data_by_wordform(1, 1, column)) func2 <- c(mean(data_by_wordform(2, 1, column)), mean(data_by_wordform(2, 2, column))) func3 <- c(mean(data_by_wordform(3, 1, column)), mean(data_by_wordform(3, 2, column))) xlim <- c(0.8, 2.2) plot(func1, xlim=xlim, ylim=ylim, xlab='klemtoon', ylab=ylab, axes=F, pch=1) axis(1, at=1:2, lab=c("1", "2")) axis(2, las=1, at=c(-0.4, -0.2, 0, 0.2, 0.4, 0.6)) box() par(new=T) plot(func2, type="o", pch=2,lty=2, xlim=xlim, ylim=ylim, xlab='', ylab='', axes=F) par(new=T) plot(func3, type="o", xlim=xlim, ylim=ylim, xlab='', ylab='', axes=F, pch=3) } plot_wordform('pitchv_PC1', ylab='PC1 intonatie', ylim=c(-0.3, 0.3)) wait_next() plot_wordform('pitchv_PC2', ylab='PC2 intonatie', ylim=c(-0.4, 0.6)) wait_next() # PC1 of word 1 by length plot_first <- function(column, ylab) { values <- unlist(lapply(2:5, function(i) mean(data_by_position(i, 1, column)))) plot(2:5, values, xlab='opsommingslengte', ylab=ylab, type='b') } plot_first('pitchv_PC1', 'PC1 intonatie van eerste woord') wait_next() plot_sequencepos <- function(column, ylab, ylim=c(-1, 1)) { mean_data <- function(sequence_length, word_pos) mean(data_by_position(sequence_length, word_pos, column)) first <- T for (sequence_length in 1:5) { values <- list() for (word_pos in 1:5) { if (word_pos > sequence_length) { values <- append(values, NA) } else { values <- append(values, mean_data(sequence_length, word_pos)) } } if (first) { plot(1:5, unlist(values), xlab='opsommingspositie', ylab=ylab, ylim=ylim, type='b', pch=sequence_length) first <- F } else { plot(1:5, unlist(values), xlab='', ylab='', ylim=ylim, type='b', axes=F, pch=sequence_length) } par(new=T) } par(new=F) } plot_sequencepos('pitchv_PC1', 'PC1 intonatie') wait_next() plot_sequencepos('pitchv_PC2', 'PC2 intonatie', ylim=c(-0.4, 0.4)) wait_next()