Created
September 7, 2017 19:02
-
-
Save bmcfee/1f66825cef2eb34c839b42dddbad49fd to your computer and use it in GitHub Desktop.
Krumhansl-Schmuckler key estimation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import scipy.linalg | |
import scipy.stats | |
def ks_key(X): | |
'''Estimate the key from a pitch class distribution | |
Parameters | |
---------- | |
X : np.ndarray, shape=(12,) | |
Pitch-class energy distribution. Need not be normalized | |
Returns | |
------- | |
major : np.ndarray, shape=(12,) | |
minor : np.ndarray, shape=(12,) | |
For each key (C:maj, ..., B:maj) and (C:min, ..., B:min), | |
the correlation score for `X` against that key. | |
''' | |
X = scipy.stats.zscore(X) | |
# Coefficients from Kumhansl and Schmuckler | |
# as reported here: http://rnhart.net/articles/key-finding/ | |
major = np.asarray([6.35, 2.23, 3.48, 2.33, 4.38, 4.09, 2.52, 5.19, 2.39, 3.66, 2.29, 2.88]) | |
major = scipy.stats.zscore(major) | |
minor = np.asarray([6.33, 2.68, 3.52, 5.38, 2.60, 3.53, 2.54, 4.75, 3.98, 2.69, 3.34, 3.17]) | |
minor = scipy.stats.zscore(minor) | |
# Generate all rotations of major | |
major = scipy.linalg.circulant(major) | |
minor = scipy.linalg.circulant(minor) | |
return major.T.dot(X), minor.T.dot(X) |
Thanks. Don't forget the:
from dataclasses import dataclass
Thanks. Don't forget the:
from dataclasses import dataclass
You are right! I've updated my snippet :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've slightly adapted this code and fixed "greater than 1" issue mentioned by @devanshugupta.