feature extraction using librosa












0















I am using following code obtain from Github. This code extract mfccs,chroma, melspectrogram, tonnetz and spectral contrast features give output in form of feat.np. I want to extract some other features like rmse, zerocross but when i add the relevent code i give error while concatenation.



# coding= UTF-8
#
# Author: Fing
# Date : 2017-12-03
#

import glob
import os
import librosa
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import specgram
import soundfile as sf

def extract_feature(file_name):
X, sample_rate = sf.read(file_name, dtype='float32')
if X.ndim > 1:
X = X[:,0]
X = X.T

# short term fourier transform
stft = np.abs(librosa.stft(X))

# mfcc
mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T,axis=0)

# chroma
chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T,axis=0)

# melspectrogram
mel = np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T,axis=0)

# spectral contrast
contrast = np.mean(librosa.feature.spectral_contrast(S=stft, sr=sample_rate).T,axis=0)

tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(X), sr=sample_rate).T,axis=0)

return mfccs,chroma,mel,contrast,tonnetz

def parse_audio_files(parent_dir,sub_dirs,file_ext='*.wav'):
features, labels = np.empty((0,193)), np.empty(0)
for label, sub_dir in enumerate(sub_dirs):
for fn in glob.glob(os.path.join(parent_dir, sub_dir, file_ext)):
try:
mfccs, chroma, mel, contrast,tonnetz = extract_feature(fn)
except Exception as e:
print("[Error] extract feature error. %s" % (e))
continue
ext_features = np.hstack([mfccs,chroma,mel,contrast,tonnetz])
print(ext_features)
print(features)
features = np.vstack([features,ext_features])
# labels = np.append(labels, fn.split('/')[1])
labels = np.append(labels, label)
print("extract %s features done" % (sub_dir))
return np.array(features), np.array(labels, dtype = np.int)

def one_hot_encode(labels):
n_labels = len(labels)
n_unique_labels = len(np.unique(labels))
one_hot_encode = np.zeros((n_labels,n_unique_labels))
one_hot_encode[np.arange(n_labels), labels] = 1
return one_hot_encode

# Get features and labels
r = os.listdir("data/")
r.sort()
features, labels = parse_audio_files('data', r)
np.save('feat.npy', features)
np.save('label.npy', labels)
`


This code work fine but when i want to extract other features also like rmse,zero crossing rate etc . when i add



#rmse=np.mean(librosa.feature.rmse(y=X).T,axis=0)


i got following error



File "C:UsersHPAnaconda2libsite-packagesnumpycoreshape_base.py", line 237, in vstack
return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)

ValueError: all the input array dimensions except for the concatenation axis must match exactly


how i can extract other features and concatenate also.










share|improve this question



























    0















    I am using following code obtain from Github. This code extract mfccs,chroma, melspectrogram, tonnetz and spectral contrast features give output in form of feat.np. I want to extract some other features like rmse, zerocross but when i add the relevent code i give error while concatenation.



    # coding= UTF-8
    #
    # Author: Fing
    # Date : 2017-12-03
    #

    import glob
    import os
    import librosa
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.pyplot import specgram
    import soundfile as sf

    def extract_feature(file_name):
    X, sample_rate = sf.read(file_name, dtype='float32')
    if X.ndim > 1:
    X = X[:,0]
    X = X.T

    # short term fourier transform
    stft = np.abs(librosa.stft(X))

    # mfcc
    mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T,axis=0)

    # chroma
    chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T,axis=0)

    # melspectrogram
    mel = np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T,axis=0)

    # spectral contrast
    contrast = np.mean(librosa.feature.spectral_contrast(S=stft, sr=sample_rate).T,axis=0)

    tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(X), sr=sample_rate).T,axis=0)

    return mfccs,chroma,mel,contrast,tonnetz

    def parse_audio_files(parent_dir,sub_dirs,file_ext='*.wav'):
    features, labels = np.empty((0,193)), np.empty(0)
    for label, sub_dir in enumerate(sub_dirs):
    for fn in glob.glob(os.path.join(parent_dir, sub_dir, file_ext)):
    try:
    mfccs, chroma, mel, contrast,tonnetz = extract_feature(fn)
    except Exception as e:
    print("[Error] extract feature error. %s" % (e))
    continue
    ext_features = np.hstack([mfccs,chroma,mel,contrast,tonnetz])
    print(ext_features)
    print(features)
    features = np.vstack([features,ext_features])
    # labels = np.append(labels, fn.split('/')[1])
    labels = np.append(labels, label)
    print("extract %s features done" % (sub_dir))
    return np.array(features), np.array(labels, dtype = np.int)

    def one_hot_encode(labels):
    n_labels = len(labels)
    n_unique_labels = len(np.unique(labels))
    one_hot_encode = np.zeros((n_labels,n_unique_labels))
    one_hot_encode[np.arange(n_labels), labels] = 1
    return one_hot_encode

    # Get features and labels
    r = os.listdir("data/")
    r.sort()
    features, labels = parse_audio_files('data', r)
    np.save('feat.npy', features)
    np.save('label.npy', labels)
    `


    This code work fine but when i want to extract other features also like rmse,zero crossing rate etc . when i add



    #rmse=np.mean(librosa.feature.rmse(y=X).T,axis=0)


    i got following error



    File "C:UsersHPAnaconda2libsite-packagesnumpycoreshape_base.py", line 237, in vstack
    return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)

    ValueError: all the input array dimensions except for the concatenation axis must match exactly


    how i can extract other features and concatenate also.










    share|improve this question

























      0












      0








      0








      I am using following code obtain from Github. This code extract mfccs,chroma, melspectrogram, tonnetz and spectral contrast features give output in form of feat.np. I want to extract some other features like rmse, zerocross but when i add the relevent code i give error while concatenation.



      # coding= UTF-8
      #
      # Author: Fing
      # Date : 2017-12-03
      #

      import glob
      import os
      import librosa
      import numpy as np
      import matplotlib.pyplot as plt
      from matplotlib.pyplot import specgram
      import soundfile as sf

      def extract_feature(file_name):
      X, sample_rate = sf.read(file_name, dtype='float32')
      if X.ndim > 1:
      X = X[:,0]
      X = X.T

      # short term fourier transform
      stft = np.abs(librosa.stft(X))

      # mfcc
      mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T,axis=0)

      # chroma
      chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T,axis=0)

      # melspectrogram
      mel = np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T,axis=0)

      # spectral contrast
      contrast = np.mean(librosa.feature.spectral_contrast(S=stft, sr=sample_rate).T,axis=0)

      tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(X), sr=sample_rate).T,axis=0)

      return mfccs,chroma,mel,contrast,tonnetz

      def parse_audio_files(parent_dir,sub_dirs,file_ext='*.wav'):
      features, labels = np.empty((0,193)), np.empty(0)
      for label, sub_dir in enumerate(sub_dirs):
      for fn in glob.glob(os.path.join(parent_dir, sub_dir, file_ext)):
      try:
      mfccs, chroma, mel, contrast,tonnetz = extract_feature(fn)
      except Exception as e:
      print("[Error] extract feature error. %s" % (e))
      continue
      ext_features = np.hstack([mfccs,chroma,mel,contrast,tonnetz])
      print(ext_features)
      print(features)
      features = np.vstack([features,ext_features])
      # labels = np.append(labels, fn.split('/')[1])
      labels = np.append(labels, label)
      print("extract %s features done" % (sub_dir))
      return np.array(features), np.array(labels, dtype = np.int)

      def one_hot_encode(labels):
      n_labels = len(labels)
      n_unique_labels = len(np.unique(labels))
      one_hot_encode = np.zeros((n_labels,n_unique_labels))
      one_hot_encode[np.arange(n_labels), labels] = 1
      return one_hot_encode

      # Get features and labels
      r = os.listdir("data/")
      r.sort()
      features, labels = parse_audio_files('data', r)
      np.save('feat.npy', features)
      np.save('label.npy', labels)
      `


      This code work fine but when i want to extract other features also like rmse,zero crossing rate etc . when i add



      #rmse=np.mean(librosa.feature.rmse(y=X).T,axis=0)


      i got following error



      File "C:UsersHPAnaconda2libsite-packagesnumpycoreshape_base.py", line 237, in vstack
      return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)

      ValueError: all the input array dimensions except for the concatenation axis must match exactly


      how i can extract other features and concatenate also.










      share|improve this question














      I am using following code obtain from Github. This code extract mfccs,chroma, melspectrogram, tonnetz and spectral contrast features give output in form of feat.np. I want to extract some other features like rmse, zerocross but when i add the relevent code i give error while concatenation.



      # coding= UTF-8
      #
      # Author: Fing
      # Date : 2017-12-03
      #

      import glob
      import os
      import librosa
      import numpy as np
      import matplotlib.pyplot as plt
      from matplotlib.pyplot import specgram
      import soundfile as sf

      def extract_feature(file_name):
      X, sample_rate = sf.read(file_name, dtype='float32')
      if X.ndim > 1:
      X = X[:,0]
      X = X.T

      # short term fourier transform
      stft = np.abs(librosa.stft(X))

      # mfcc
      mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T,axis=0)

      # chroma
      chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T,axis=0)

      # melspectrogram
      mel = np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T,axis=0)

      # spectral contrast
      contrast = np.mean(librosa.feature.spectral_contrast(S=stft, sr=sample_rate).T,axis=0)

      tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(X), sr=sample_rate).T,axis=0)

      return mfccs,chroma,mel,contrast,tonnetz

      def parse_audio_files(parent_dir,sub_dirs,file_ext='*.wav'):
      features, labels = np.empty((0,193)), np.empty(0)
      for label, sub_dir in enumerate(sub_dirs):
      for fn in glob.glob(os.path.join(parent_dir, sub_dir, file_ext)):
      try:
      mfccs, chroma, mel, contrast,tonnetz = extract_feature(fn)
      except Exception as e:
      print("[Error] extract feature error. %s" % (e))
      continue
      ext_features = np.hstack([mfccs,chroma,mel,contrast,tonnetz])
      print(ext_features)
      print(features)
      features = np.vstack([features,ext_features])
      # labels = np.append(labels, fn.split('/')[1])
      labels = np.append(labels, label)
      print("extract %s features done" % (sub_dir))
      return np.array(features), np.array(labels, dtype = np.int)

      def one_hot_encode(labels):
      n_labels = len(labels)
      n_unique_labels = len(np.unique(labels))
      one_hot_encode = np.zeros((n_labels,n_unique_labels))
      one_hot_encode[np.arange(n_labels), labels] = 1
      return one_hot_encode

      # Get features and labels
      r = os.listdir("data/")
      r.sort()
      features, labels = parse_audio_files('data', r)
      np.save('feat.npy', features)
      np.save('label.npy', labels)
      `


      This code work fine but when i want to extract other features also like rmse,zero crossing rate etc . when i add



      #rmse=np.mean(librosa.feature.rmse(y=X).T,axis=0)


      i got following error



      File "C:UsersHPAnaconda2libsite-packagesnumpycoreshape_base.py", line 237, in vstack
      return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)

      ValueError: all the input array dimensions except for the concatenation axis must match exactly


      how i can extract other features and concatenate also.







      python librosa






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 22 '18 at 10:07









      M. ZayyanM. Zayyan

      84




      84
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The shape of STFT and RMSE are different than MFCC and other features. STFT and MFCC are 2 dimensional while the others are one dimensional features.






          share|improve this answer


























          • maybe you should give python outputs to emphasize your answer. It would be even better if it is given using original question code.

            – LoneWanderer
            Nov 23 '18 at 22:33













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51464021%2ffeature-extraction-using-librosa%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The shape of STFT and RMSE are different than MFCC and other features. STFT and MFCC are 2 dimensional while the others are one dimensional features.






          share|improve this answer


























          • maybe you should give python outputs to emphasize your answer. It would be even better if it is given using original question code.

            – LoneWanderer
            Nov 23 '18 at 22:33


















          0














          The shape of STFT and RMSE are different than MFCC and other features. STFT and MFCC are 2 dimensional while the others are one dimensional features.






          share|improve this answer


























          • maybe you should give python outputs to emphasize your answer. It would be even better if it is given using original question code.

            – LoneWanderer
            Nov 23 '18 at 22:33
















          0












          0








          0







          The shape of STFT and RMSE are different than MFCC and other features. STFT and MFCC are 2 dimensional while the others are one dimensional features.






          share|improve this answer















          The shape of STFT and RMSE are different than MFCC and other features. STFT and MFCC are 2 dimensional while the others are one dimensional features.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 24 '18 at 0:37









          Berthur

          709212




          709212










          answered Nov 23 '18 at 22:02









          Rohan LeekhaRohan Leekha

          1




          1













          • maybe you should give python outputs to emphasize your answer. It would be even better if it is given using original question code.

            – LoneWanderer
            Nov 23 '18 at 22:33





















          • maybe you should give python outputs to emphasize your answer. It would be even better if it is given using original question code.

            – LoneWanderer
            Nov 23 '18 at 22:33



















          maybe you should give python outputs to emphasize your answer. It would be even better if it is given using original question code.

          – LoneWanderer
          Nov 23 '18 at 22:33







          maybe you should give python outputs to emphasize your answer. It would be even better if it is given using original question code.

          – LoneWanderer
          Nov 23 '18 at 22:33






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51464021%2ffeature-extraction-using-librosa%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'