Skip to content

Commit

Permalink
Compute frequency errors in cents #2
Browse files Browse the repository at this point in the history
  • Loading branch information
jurihock committed Jan 9, 2023
1 parent f3f6e6b commit 8ccdc06
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/python/qdft/chroma.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,27 @@ def __init__(self, samplerate, bandwidth=('A0', 'C#8'), concertpitch=440, decibe

def chroma(self, samples):

stash = { 'errors': None }
stash = { 'cents': None }

def analysis(dfts):
stash['errors'] = self.analyze(dfts)
def analysis(dfts, mode=None):
stash['cents'] = self.analyze(dfts, mode)

dfts = self.qdft.qdft(samples, analysis)
# TODO: analyze raw dfts
# dfts = self.qdft.qdft(samples, analysis)

# TODO: analyze windowed dfts
dfts = self.qdft.qdft(samples)
stash['cents'] = self.analyze(dfts, 'p')

magnis = numpy.abs(dfts)
errors = stash['errors']
cents = stash['cents']

if self.decibel:

with numpy.errstate(all='ignore'):
magnis = 20 * numpy.log10(magnis)

chromagram = magnis + 1j * errors
chromagram = magnis + 1j * cents

chromagram = chromagram[..., ::2]
assert chromagram.shape[-1] == self.frequencies.shape[-1]
Expand Down Expand Up @@ -107,4 +112,11 @@ def analyze(self, dfts, mode=None):
errors[..., 0] = 0
errors[..., -1] = 0

return errors
oldfreqs = self.qdft.frequencies
oldbins = numpy.arange(oldfreqs.size)
newbins = oldbins + errors
newfreqs = self.qdft.bandwidth[0] * numpy.power(2, newbins / self.qdft.resolution)

cents = 1200 * numpy.log2(newfreqs / oldfreqs)

return cents

0 comments on commit 8ccdc06

Please sign in to comment.