def __new__(cls, spectrogram, filterbank=PCP, num_classes=PCP.CLASSES, fmin=PCP.FMIN, fmax=PCP.FMAX, fref=A4, **kwargs): # check spectrogram type if not isinstance(spectrogram, Spectrogram): spectrogram = Spectrogram(spectrogram, **kwargs) # the spectrogram must not be filtered if spectrogram.filterbank is not None: import warnings warnings.warn('Spectrogram should not be filtered.') # reference frequency for the filterbank if fref is None: fref = spectrogram.tuning_frequency() # set filterbank if issubclass(filterbank, Filterbank): filterbank = filterbank(spectrogram.bin_frequencies, num_classes=num_classes, fmin=fmin, fmax=fmax, fref=fref) if not isinstance(filterbank, Filterbank): raise ValueError('not a Filterbank type or instance: %s' % filterbank) # filter the spectrogram data = np.dot(spectrogram, filterbank) # cast as PitchClassProfile obj = np.asarray(data).view(cls) # save additional attributes obj.filterbank = filterbank obj.spectrogram = spectrogram # return the object return obj
def __new__(cls, spectrogram, filterbank=PCP, num_classes=PCP.CLASSES, fmin=PCP.FMIN, fmax=PCP.FMAX, fref=A4, **kwargs): # check spectrogram type if not isinstance(spectrogram, Spectrogram): spectrogram = Spectrogram(spectrogram, **kwargs) # spectrogram should not be filtered if hasattr(spectrogram, 'filterbank'): warnings.warn('Spectrogram should not be filtered.', RuntimeWarning) # reference frequency for the filterbank if fref is None: fref = spectrogram.tuning_frequency() # set filterbank if issubclass(filterbank, Filterbank): filterbank = filterbank(spectrogram.bin_frequencies, num_classes=num_classes, fmin=fmin, fmax=fmax, fref=fref) if not isinstance(filterbank, Filterbank): raise ValueError('not a Filterbank type or instance: %s' % filterbank) # filter the spectrogram data = np.dot(spectrogram, filterbank) # cast as PitchClassProfile obj = np.asarray(data).view(cls) # save additional attributes obj.filterbank = filterbank obj.spectrogram = spectrogram # return the object return obj
def getPCPHistogram(filename, fs=8192, show=False): res = {} sig = Signal(filename, num_channels=1) fsig = FramedSignal(sig, frame_size=fs) stft = ShortTimeFourierTransform(fsig) spec = Spectrogram(stft) chroma = PitchClassProfile(spec, num_classes=12) hist = [0 for i in range(12)] hist_f = [0 for i in range(12)] for f in range(len(chroma)): wf = chroma[f] hist = map(sum, zip(hist, wf)) f = flatness(wf) hist_f = map(sum, zip(hist_f, [w * f for w in wf])) s = sum(hist) hist = map(lambda x: x / s, hist) C_hist = [hist[i - 9] for i in range(12)] res['standard'] = C_hist s_f = sum(hist_f) hist_f = map(lambda x: x / s_f, hist_f) C_hist_f = [hist_f[i - 9] for i in range(12)] res['standard_f'] = C_hist_f hpss = HarmonicPercussiveSourceSeparation() h, _ = hpss.process(spec) chroma = PitchClassProfile(h, num_classes=12) hist = [0 for i in range(12)] hist_f = [0 for i in range(12)] for f in range(len(chroma)): wf = chroma[f] hist = map(sum, zip(hist, wf)) f = flatness(wf) hist_f = map(sum, zip(hist_f, [w * f for w in wf])) s = sum(hist) hist = map(lambda x: x / s, hist) C_hist = [hist[i - 9] for i in range(12)] res['hpss'] = C_hist s_f = sum(hist_f) hist_f = map(lambda x: x / s_f, hist_f) C_hist_f = [hist_f[i - 9] for i in range(12)] res['hpss_f'] = C_hist_f dcp = DeepChromaProcessor() deepchroma = dcp(filename) hist = [0 for i in range(12)] hist_f = [0 for i in range(12)] for f in range(len(deepchroma)): wf = deepchroma[f] hist = map(sum, zip(hist, wf)) f = flatness(wf) hist_f = map(sum, zip(hist_f, [w * f for w in wf])) s = sum(hist) hist = map(lambda x: x / s, hist) res['deep'] = hist s_f = sum(hist_f) hist_f = map(lambda x: x / s_f, hist_f) res['deep_f'] = hist_f if show: plt.subplot(131) plt.barh(range(12), res['standard']) plt.subplot(132) plt.barh(range(12), res['hpss']) plt.subplot(133) plt.barh(range(12), res['deep']) plt.show() return res
from os.path import join as pj from . import AUDIO_PATH, ACTIVATIONS_PATH from madmom.audio.signal import SignalProcessor, FramedSignalProcessor from madmom.audio.filters import LogarithmicFilterbank from madmom.audio.stft import ShortTimeFourierTransformProcessor from madmom.audio.spectrogram import (Spectrogram, SpectrogramProcessor, FilteredSpectrogramProcessor, LogarithmicFilteredSpectrogram, LogarithmicSpectrogramProcessor) from madmom.features import Activations from madmom.features.onsets import * sample_file = pj(AUDIO_PATH, 'sample.wav') sample_spec = Spectrogram(sample_file, circular_shift=True) sample_log_filt_spec = LogarithmicFilteredSpectrogram( sample_spec, num_bands=24, mul=1, add=1) sample_cnn_act = Activations(pj(ACTIVATIONS_PATH, 'sample.onsets_cnn.npz')) sample_rnn_act = Activations(pj(ACTIVATIONS_PATH, 'sample.onsets_rnn.npz')) sample_brnn_act = Activations(pj(ACTIVATIONS_PATH, 'sample.onsets_brnn.npz')) sample_superflux_act = Activations(pj(ACTIVATIONS_PATH, 'sample.super_flux.npz')) class TestHighFrequencyContentFunction(unittest.TestCase): def test_values(self): odf = high_frequency_content(sample_log_filt_spec) self.assertTrue(np.allclose(odf[:6], [8.97001563, 9.36399107, 8.64144536, 8.34977449,