Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Spectral Rolloff and Crest Factor

  • Loading branch information...
commit 8674c1b679823061aa6cf8b516953026a1104a23 1 parent b7419cb
@jsawruk authored
Showing with 39 additions and 6 deletions.
  1. +4 −0 README.md
  2. +35 −6 pymir/Spectrum.py
4 README.md
@@ -23,6 +23,8 @@ PyMIR is a Python library for common tasks in Music Information Retrieval (MIR)
* Spectral Flatness
* Spectral Moments (mean, variance, skewness, kurtosis)
* Spectral Spread
+ * Spectral Rolloff
+ * Spectral Crest Factor
* Chroma
* Inverse Discrete Cosine Transform
* Inverse FFT
@@ -76,6 +78,7 @@ The standard workflow for working with PyMIR is:
spectra = [f.spectrum() for f in fixedFrames]
spectra[0].centroid() # Spectral Centroid
spectra[0].chroma() # Chroma vector
+ spectra[0].crest() # Spectral Crest Factor
spectra[0].flatness() # Spectral Flatness
spectra[0].idct() # Inverse DCT
spectra[0].ifft() # Inverse FFT
@@ -83,6 +86,7 @@ The standard workflow for working with PyMIR is:
spectra[0].mean() # Spectral Mean
spectra[0].mfcc2() # MFCC (vectorized implementation)
spectra[0].plot() # Plot using matplotlib
+ spectra[0].rolloff() # Spectral Rolloff
spectra[0].skewness() # Spectral Skewness
spectra[0].spread() # Spectral Spread
spectra[0].variance() # Spectral Variance
View
41 pymir/Spectrum.py
@@ -96,6 +96,19 @@ def chroma(self):
"""
return Pitch.chroma(self)
+ def crest(self):
+ """
+ Compute the spectral crest factor, i.e. the ratio of the maximum of the spectrum to the
+ sum of the spectrum
+ """
+ absSpectrum = abs(self)
+ spectralSum = numpy.sum(absSpectrum)
+
+ maxFrequencyIndex = numpy.argmax(absSpectrum)
+ maxSpectrum = absSpectrum[maxFrequencyIndex]
+
+ return maxSpectrum / spectralSum
+
def flatness(self):
"""
Compute the spectral flatness (ratio between geometric and arithmetic means)
@@ -146,9 +159,29 @@ def plot(self):
Plot the spectrum using matplotlib
"""
plt.plot(abs(self))
- plt.xlim(0, len(self) / 2)
+ plt.xlim(0, len(self))
plt.show()
+ def rolloff(self):
+ """
+ Determine the spectral rolloff, i.e. the frequency below which 85% of the spectrum's energy
+ is located
+ """
+ absSpectrum = abs(self)
+ spectralSum = numpy.sum(absSpectrum)
+
+ rolloffSum = 0
+ rolloffIndex = 0
+ for i in range(0, len(self)):
+ rolloffSum = rolloffSum + absSpectrum[i]
+ if rolloffSum > (0.85 * spectralSum):
+ rolloffIndex = i
+ break
+
+ # Convert the index into a frequency
+ frequency = rolloffIndex * (self.sampleRate / 2.0) / len(self)
+ return frequency
+
def skewness(self):
"""
Compute the spectral skewness (third spectral moment)
@@ -182,8 +215,4 @@ def variance(self):
"""
Compute the spectral variance (second spectral moment)
"""
- return numpy.var(abs(self))
-
- # TODO
- # Crest
- # Rolloff
+ return numpy.var(abs(self))
Please sign in to comment.
Something went wrong with that request. Please try again.