Skip to content

Commit

Permalink
Merge pull request #6 from pmcharrison/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
pmcharrison committed Feb 26, 2019
2 parents eacf646 + a5453de commit fc99884
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 14 deletions.
2 changes: 1 addition & 1 deletion 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)
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
@@ -0,0 +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.
14 changes: 14 additions & 0 deletions 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
}
24 changes: 24 additions & 0 deletions 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, ...)
)
}
8 changes: 4 additions & 4 deletions R/pc-milne-spectrum.R → 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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion R/vec.R
Expand Up @@ -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),
Expand Down
19 changes: 19 additions & 0 deletions man/cosine_similarity.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/dot-milne_pc_spectrum.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/is.milne_pc_spectrum.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions man/milne_pc_spec_dist.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/milne_pc_spectrum.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/new_complex_tone.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 95 additions & 0 deletions 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)))
)
})
25 changes: 25 additions & 0 deletions 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
)
})

0 comments on commit fc99884

Please sign in to comment.