librosa 模块正在导入

huangapple go评论56阅读模式
英文:

librosa I am getting module

问题

import numpy as np
import librosa
from scipy.io import wavfile
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix, accuracy_score, roc_curve, auc
from sklearn.metrics import accuracy_score
import tensorflow as tf
import librosa
import librosa.display

sr = 16000

signal = np.random.randn(sr*5)

file_name = 'file1.wav'

signal_int = np.int16(signal/np.max(np.abs(signal)) * 32767)

wavfile.write(file_name, sr, signal_int)

audio_files = ['file1.wav', 'file2.wav', 'file3.wav']

y, sr = librosa.load(audio_files[0], sr=16000)
y = librosa.util.normalize(y)
mel_spectrogram = librosa.feature.melspectrogram(signal, sr=sr, n_mels=128, fmax=8000)
log_mel_spectrogram = librosa.power_to_db(mel_spectrogram, ref=np.max)

def add_noise(data):
    noise = np.random.randn(len(data))
    data_noise = data + 0.005 * noise
    return data_noise

def shift(data):
    return np.roll(data, int(len(data)/10))

def stretch(data, rate=0.8):
    return librosa.effects.time_stretch(data, rate)

X = np.empty((0, 128, 44))
y = np.empty((0, 10))
label = np.array([1, 0, 0])

for file in audio_files:
    signal, sr = librosa.load(file, sr=16000)
    signal = librosa.util.normalize(signal)
    mel_spectrogram = librosa.feature.melspectrogram(signal, sr=sr, n_mels=128, fmax=8000)
    log_mel_spectrogram = librosa.power_to_db(mel_spectrogram, ref=np.max)
    
    signal_noise = add_noise(signal)
    signal_shift = shift(signal)
    signal_stretch = stretch(signal) 
    
    log_mel_spectrogram_noise = librosa.feature.melspectrogram(signal_noise, sr=sr, n_mels=128, fmax=8000)
    log_mel_spectrogram_shift = librosa.feature.melspectrogram(signal_shift, sr=sr, n_mels=128, fmax=8000)
    log_mel_spectrogram_stretch = librosa.feature.melspectrogram(signal_stretch, sr=sr, n_mels=128, fmax=8000)
    
    log_mel_spectrogram = np.expand_dims(log_mel_spectrogram, axis=-1)
    log_mel_spectrogram_noise = np.expand_dims(log_mel_spectrogram_noise, axis=-1)
    log_mel_spectrogram_shift = np.expand_dims(log_mel_spectrogram_shift, axis=-1)
    log_mel_spectrogram_stretch = np.expand_dims(log_mel_spectrogram_stretch, axis=-1)
    
    X = np.concatenate((X, log_mel_spectrogram, log_mel_spectrogram_noise, log_mel_spectrogram_shift, log_mel_spectrogram_stretch), axis=0)
    y = np.concatenate((y, label), axis=0)
英文:
import numpy as np
import librosa
from scipy.io import wavfile
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix, accuracy_score, roc_curve, auc
from sklearn.metrics import accuracy_score
import tensorflow as tf
import librosa
import librosa.display

sr = 16000

signal = np.random.randn(sr*5)

file_name = 'file1.wav'

signal_int = np.int16(signal/np.max(np.abs(signal)) * 32767)

wavfile.write(file_name, sr, signal_int)

audio_files = ['file1.wav', 'file2.wav', 'file3.wav']

y, sr = librosa.load(audio_files[0], sr=16000)
y = librosa.util.normalize(y)
mel_spectrogram = librosa.feature.melspectrogram(signal, sr=sr, n_mels=128, fmax=8000)
log_mel_spectrogram = librosa.power_to_db(mel_spectrogram, ref=np.max)




def add_noise(data):
    noise = np.random.randn(len(data))
    data_noise = data + 0.005 * noise
    return data_noise

def shift(data):
    return np.roll(data, int(len(data)/10))

def stretch(data, rate=0.8):
    return librosa.effects.time_stretch(data, rate)


X = np.empty((0, 128, 44))
y = np.empty((0, 10))
label = np.array([1, 0, 0])

for file in audio_files:
    signal, sr = librosa.load(file, sr=16000)
    signal = librosa.util.normalize(signal)
    mel_spectrogram = librosa.feature.melspectrogram(signal, sr=sr, n_mels=128, fmax=8000)
    log_mel_spectrogram = librosa.power_to_db(mel_spectrogram, ref=np.max)
    
    signal_noise = add_noise(signal)
    signal_shift = shift(signal)
    signal_stretch = stretch(signal) 
    
    log_mel_spectrogram_noise = librosa.feature.melspectrogram(signal_noise, sr=sr, n_mels=128, fmax=8000)
    log_mel_spectrogram_shift = librosa.feature.melspectrogram(signal_shift, sr=sr, n_mels=128, fmax=8000)
    log_mel_spectrogram_stretch = librosa.feature.melspectrogram(signal_stretch, sr=sr, n_mels=128, fmax=8000)
    
    log_mel_spectrogram = np.expand_dims(log_mel_spectrogram, axis=-1)
    log_mel_spectrogram_noise = np.expand_dims(log_mel_spectrogram_noise, axis=-1)
    log_mel_spectrogram_shift = np.expand_dims(log_mel_spectrogram_shift, axis=-1)
    log_mel_spectrogram_stretch = np.expand_dims(log_mel_spectrogram_stretch, axis=-1)
    
    X = np.concatenate((X, log_mel_spectrogram, log_mel_spectrogram_noise, log_mel_spectrogram_shift, log_mel_spectrogram_stretch), axis=0)
    y = np.concatenate((y, label), axis=0)

I'm getting this error I don't know what to do, can you help me?

Traceback (most recent call last):

> mel_spectrogram = librosa.feature.melspectrogram(signal, sr=sr,
> n_mels=128, fmax=8000) TypeError: melspectrogram() takes 0 positional
> arguments but 1 positional argument (and 1 keyword-only argument)

答案1

得分: 1

不要使用位置参数来调用melspectrogram。请尝试像这样调用:

mel_spectrogram = librosa.feature.melspectrogram(y=signal, sr=sr, n_mels=128, fmax=8000)
英文:

You should not use positional arguments when calling melspectrogram. Try like this instead:

mel_spectrogram = librosa.feature.melspectrogram(y=signal, sr=sr, n_mels=128, fmax=8000)

huangapple
  • 本文由 发表于 2023年2月27日 06:48:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定