From bd424520a4713a06764a4351bf82f09fb7f3feb6 Mon Sep 17 00:00:00 2001 From: Peter M C Harrison Date: Tue, 26 Feb 2019 10:57:22 +0000 Subject: [PATCH 1/6] Declaring capture.output import from utils --- R/vec.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/vec.R b/R/vec.R index f684aa5..0f27cc2 100644 --- a/R/vec.R +++ b/R/vec.R @@ -71,7 +71,7 @@ is.coded.vec <- function(x) FALSE print.vec <- function(x, detail = FALSE, ...) { if (detail) { for (i in seq_along(x)) { - cat("[[", i, "]] ", capture.output(x[[i]]), "\n", sep = "") + cat("[[", i, "]] ", utils::capture.output(x[[i]]), "\n", sep = "") } } else { cat("Vector of type '", type(x), From 74894d7ce06c885c3b716f6ba1ed78b21e319bf7 Mon Sep 17 00:00:00 2001 From: Peter M C Harrison Date: Tue, 26 Feb 2019 10:58:46 +0000 Subject: [PATCH 2/6] Renaming --- R/{pc-milne-spectrum.R => milne-pc-spectrum.R} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename R/{pc-milne-spectrum.R => milne-pc-spectrum.R} (100%) diff --git a/R/pc-milne-spectrum.R b/R/milne-pc-spectrum.R similarity index 100% rename from R/pc-milne-spectrum.R rename to R/milne-pc-spectrum.R From bdaa5d94e7af565ca3d12d106e342d59f16a982a Mon Sep 17 00:00:00 2001 From: Peter M C Harrison Date: Tue, 26 Feb 2019 11:11:58 +0000 Subject: [PATCH 3/6] Renaming Milne pitch-class spectrum --- R/milne-pc-spectrum.R | 8 ++++---- man/dot-milne_pc_spectrum.Rd | 4 ++-- man/is.milne_pc_spectrum.Rd | 2 +- man/milne_pc_spectrum.Rd | 8 ++++---- man/new_complex_tone.Rd | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/R/milne-pc-spectrum.R b/R/milne-pc-spectrum.R index e791b2e..3a94ce0 100644 --- a/R/milne-pc-spectrum.R +++ b/R/milne-pc-spectrum.R @@ -1,4 +1,4 @@ -#' Constructor function for pitch-class Milne spectrum +#' Constructor function for Milne pitch-class spectrum #' #' This function constructs a "milne_pc_spectrum" object. #' @param x A numeric vector of pitch-class weights, @@ -23,11 +23,11 @@ y } -#' Pitch-class Milne spectrum +#' Milne pitch-class spectrum #' #' This function represents an input object as a -#' 'pitch-class Milne spectrum'. -#' A pitch-class Milne spectrum defines 'perceptual weight' +#' 'Milne pitch-class spectrum'. +#' A Milne pitch-class spectrum defines 'perceptual weight' #' as a continuous function of 'pitch class'. #' @details #' This spectrum is typically constructed from musical chords diff --git a/man/dot-milne_pc_spectrum.Rd b/man/dot-milne_pc_spectrum.Rd index b7edc45..8433bc2 100644 --- a/man/dot-milne_pc_spectrum.Rd +++ b/man/dot-milne_pc_spectrum.Rd @@ -1,8 +1,8 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/pc-milne-spectrum.R +% Please edit documentation in R/milne-pc-spectrum.R \name{.milne_pc_spectrum} \alias{.milne_pc_spectrum} -\title{Constructor function for pitch-class Milne spectrum} +\title{Constructor function for Milne pitch-class spectrum} \usage{ .milne_pc_spectrum(x) } diff --git a/man/is.milne_pc_spectrum.Rd b/man/is.milne_pc_spectrum.Rd index c3e3083..4163fbc 100644 --- a/man/is.milne_pc_spectrum.Rd +++ b/man/is.milne_pc_spectrum.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/pc-milne-spectrum.R +% Please edit documentation in R/milne-pc-spectrum.R \name{is.milne_pc_spectrum} \alias{is.milne_pc_spectrum} \title{Check for class "milne_pc_spectrum"} diff --git a/man/milne_pc_spectrum.Rd b/man/milne_pc_spectrum.Rd index 1158cf3..c670548 100644 --- a/man/milne_pc_spectrum.Rd +++ b/man/milne_pc_spectrum.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/pc-milne-spectrum.R +% Please edit documentation in R/milne-pc-spectrum.R \name{milne_pc_spectrum} \alias{milne_pc_spectrum} \alias{milne_pc_spectrum.pc_set} \alias{milne_pc_spectrum.default} -\title{Pitch-class Milne spectrum} +\title{Milne pitch-class spectrum} \usage{ milne_pc_spectrum(x, ...) @@ -49,8 +49,8 @@ An object of class \code{milne_pc_spectrum}. } \description{ This function represents an input object as a -'pitch-class Milne spectrum'. -A pitch-class Milne spectrum defines 'perceptual weight' +'Milne pitch-class spectrum'. +A Milne pitch-class spectrum defines 'perceptual weight' as a continuous function of 'pitch class'. } \details{ diff --git a/man/new_complex_tone.Rd b/man/new_complex_tone.Rd index 131fcaf..0159926 100644 --- a/man/new_complex_tone.Rd +++ b/man/new_complex_tone.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/pc-milne-spectrum.R +% Please edit documentation in R/milne-pc-spectrum.R \name{new_complex_tone} \alias{new_complex_tone} \title{Make complex tone} From c824f32bd3e29db4e720e039c30c736e6cde818c Mon Sep 17 00:00:00 2001 From: Peter M C Harrison Date: Tue, 26 Feb 2019 11:30:49 +0000 Subject: [PATCH 4/6] Adding milne_pc_spec_dist --- NAMESPACE | 2 + R/cosine-similarity.R | 14 ++++ R/milne-pc-spec-dist.R | 24 ++++++ man/cosine_similarity.Rd | 19 +++++ man/milne_pc_spec_dist.Rd | 27 +++++++ tests/testthat/test-milne_pc_spec_dist.R | 95 +++++++++++++++++++++++ tests/testthat/test-r-cosine-similarity.R | 25 ++++++ 7 files changed, 206 insertions(+) create mode 100644 R/cosine-similarity.R create mode 100644 R/milne-pc-spec-dist.R create mode 100644 man/cosine_similarity.Rd create mode 100644 man/milne_pc_spec_dist.Rd create mode 100644 tests/testthat/test-milne_pc_spec_dist.R create mode 100644 tests/testthat/test-r-cosine-similarity.R diff --git a/NAMESPACE b/NAMESPACE index 2a010bd..484f97c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -203,6 +203,7 @@ export(amplitude_to_dB) export(ascending_pc_dist) export(coded_vec) export(corpus) +export(cosine_similarity) export(dB_to_amplitude) export(decode) export(encode) @@ -232,6 +233,7 @@ export(lower) export(map_pc_chord_id_to_pc_set_id) export(metadata) export(midi_to_freq) +export(milne_pc_spec_dist) export(milne_pc_spectrum) export(num_elements) export(num_sequences) diff --git a/R/cosine-similarity.R b/R/cosine-similarity.R new file mode 100644 index 0000000..656dffa --- /dev/null +++ b/R/cosine-similarity.R @@ -0,0 +1,14 @@ +#' Cosine similarity +#' +#' Computes the cosine similarity between two numeric vectors. +#' @param x Numeric vector 1. +#' @param y Numeric vector 2. +#' @return Cosine similarity, as a numeric scalar. +#' @export +cosine_similarity <- function(x, y) { + numerator <- sum(x * y) + denominator <- + sqrt(sum(x ^ 2)) * + sqrt(sum(y ^ 2)) + numerator / denominator +} diff --git a/R/milne-pc-spec-dist.R b/R/milne-pc-spec-dist.R new file mode 100644 index 0000000..80d1dea --- /dev/null +++ b/R/milne-pc-spec-dist.R @@ -0,0 +1,24 @@ +#' Milne's pitch-class spectral distance +#' +#' Computes Milne's pitch-class spectral distance measure between two sonorities. +#' +#' @param x First sonority, passed to \code{\link{milne_pc_spectrum}}. +#' @param y Second sonority, passed to \code{\link{milne_pc_spectrum}}. +#' @param ... Further arguments passed to \code{\link{milne_pc_spectrum}}. +#' +#' @return +#' A numeric scalar where greater values indicate greater distance. +#' 0 is the minimum possible distance. +#' 1 corresponds to two uncorrelated spectra; +#' values greater than 1 are possible if the spectra are inversely correlated. +#' +#' @details +#' See \insertCite{Milne2016;textual}{hrep} for details. +#' +#' @export +milne_pc_spec_dist <- function(x, y, ...) { + 1 - cosine_similarity( + milne_pc_spectrum(x, ...), + milne_pc_spectrum(y, ...) + ) +} diff --git a/man/cosine_similarity.Rd b/man/cosine_similarity.Rd new file mode 100644 index 0000000..277818f --- /dev/null +++ b/man/cosine_similarity.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/cosine-similarity.R +\name{cosine_similarity} +\alias{cosine_similarity} +\title{Cosine similarity} +\usage{ +cosine_similarity(x, y) +} +\arguments{ +\item{x}{Numeric vector 1.} + +\item{y}{Numeric vector 2.} +} +\value{ +Cosine similarity, as a numeric scalar. +} +\description{ +Computes the cosine similarity between two numeric vectors. +} diff --git a/man/milne_pc_spec_dist.Rd b/man/milne_pc_spec_dist.Rd new file mode 100644 index 0000000..60105ed --- /dev/null +++ b/man/milne_pc_spec_dist.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/milne-pc-spec-dist.R +\name{milne_pc_spec_dist} +\alias{milne_pc_spec_dist} +\title{Milne's pitch-class spectral distance} +\usage{ +milne_pc_spec_dist(x, y, ...) +} +\arguments{ +\item{x}{First sonority, passed to \code{\link{milne_pc_spectrum}}.} + +\item{y}{Second sonority, passed to \code{\link{milne_pc_spectrum}}.} + +\item{...}{Further arguments passed to \code{\link{milne_pc_spectrum}}.} +} +\value{ +A numeric scalar where greater values indicate greater distance. +0 is the minimum possible distance. +1 corresponds to two uncorrelated spectra; +values greater than 1 are possible if the spectra are inversely correlated. +} +\description{ +Computes Milne's pitch-class spectral distance measure between two sonorities. +} +\details{ +See \insertCite{Milne2016;textual}{hrep} for details. +} diff --git a/tests/testthat/test-milne_pc_spec_dist.R b/tests/testthat/test-milne_pc_spec_dist.R new file mode 100644 index 0000000..076f516 --- /dev/null +++ b/tests/testthat/test-milne_pc_spec_dist.R @@ -0,0 +1,95 @@ +context("test-milne_pc_spec_dist") + +test_that("examples", { + approx_equal <- function(x, y, digits = 4) { + all.equal( + round(x, digits = digits), + round(y, digits = digits) + ) + } + + expect_true( + approx_equal( + milne_pc_spec_dist( + c(0, 4, 7), + c(0, 4, 7) + ), + 0 + ) + ) + expect_true( + approx_equal( + milne_pc_spec_dist( + c(0, 4, 7), + c(0, 3, 7) + ), + 0.2597 + ) + ) + expect_true( + approx_equal( + milne_pc_spec_dist( + c(0, 4, 7), + c(0, 2, 7) + ), + 0.2201 + ) + ) + expect_true( + approx_equal( + milne_pc_spec_dist( + c(0, 4, 7), + c(1, 5, 8) + ), + 0.8527 + ) + ) + expect_true( + approx_equal( + milne_pc_spec_dist( + c(0, 4, 7), + c(2, 7, 11) + ), + 0.4155 + ) + ) + expect_true( + approx_equal( + milne_pc_spec_dist( + c(0, 4, 7), + c(0, 5, 9) + ), + 0.4155 + ) + ) + expect_true( + approx_equal( + milne_pc_spec_dist( + c(2, 7, 8), + c(3, 4, 8, 9) + ), + 0.5619 + ) + ) + expect_true( + approx_equal( + milne_pc_spec_dist( + c(2, 6, 8, 9), + c(1, 4) + ), + 0.5940 + ) + ) +}) + +test_that("coercion", { + expect_equal( + milne_pc_spec_dist(c(0, 4, 7), c(0, 3, 7)), + milne_pc_spec_dist(c(60, 64, 67), c(60, 63, 67)) + ) + + expect_equal( + milne_pc_spec_dist(c(0, 4, 7), c(0, 3, 7)), + milne_pc_spec_dist(pi_chord(c(60, 64, 67)), pi_chord(c(60, 63, 67))) + ) +}) diff --git a/tests/testthat/test-r-cosine-similarity.R b/tests/testthat/test-r-cosine-similarity.R new file mode 100644 index 0000000..b43c5fd --- /dev/null +++ b/tests/testthat/test-r-cosine-similarity.R @@ -0,0 +1,25 @@ +context("test-r-cosine-similarity") + +test_that("examples", { + expect_equal(cosine_similarity(rep(1, times = 10), + rep(2, times = 10)), + 1) + expect_equal(cosine_similarity(rep(1, times = 10), + rep(-2, times = 10)), + -1) + expect_equal( + cosine_similarity( + c(0, 3, 0, 0, 2, 0, 0, 2, 0, 5), + c(1, 2, 0, 0, 1, 1, 0, 1, 0, 3) + ), + 0.94, + tolerance = 1e-2 + ) + expect_equal( + cosine_similarity( + c(0, 3, 0, 0, 2, 0, 0, 2, 0, 5), + c(1, 2, 0, 0, 1, 1, 0, 1, 0, 3) + ), + 0.9356015 + ) +}) From 5c7d7a3a74189bc5a721baee70ce8bb9268a12ff Mon Sep 17 00:00:00 2001 From: Peter M C Harrison Date: Tue, 26 Feb 2019 11:35:46 +0000 Subject: [PATCH 5/6] Added NEWS.md --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 NEWS.md diff --git a/NEWS.md b/NEWS.md new file mode 100644 index 0000000..9501360 --- /dev/null +++ b/NEWS.md @@ -0,0 +1,2 @@ +- Added a `NEWS.md` file to track changes to the package. +- Added `milne_pc_spec_dist` for computing spectral distances between sonorities. From a5453dee33a35f80d74afd2766de916f5a91c95c Mon Sep 17 00:00:00 2001 From: Peter M C Harrison Date: Tue, 26 Feb 2019 11:36:00 +0000 Subject: [PATCH 6/6] Increment version number --- DESCRIPTION | 2 +- NEWS.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 2c0ce46..b7663fd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: hrep Title: Harmony Representations -Version: 0.5.1 +Version: 0.6.0 Authors@R: person("Peter", "Harrison", email = "pmc.harrison@gmail.com", role = c("aut", "cre")) Description: This package provides methods for representing and manipulating chord sequences. Depends: R (>= 3.4.0) diff --git a/NEWS.md b/NEWS.md index 9501360..27d1058 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,2 +1,4 @@ +# hrep 0.6.0 + - Added a `NEWS.md` file to track changes to the package. - Added `milne_pc_spec_dist` for computing spectral distances between sonorities.