2017-12-28 6 views
1

사운드 파일의 MFCC를 사용하여 .wav 파일에서 기능을 추출하려고합니다. MFCC 목록을 수십 개의 배열로 변환하려고하면 오류가 발생합니다. 목록에 여러 모양의 MFCC 값이 포함되어 있기 때문에이 오류가 발생하고 있음을 확신합니다 (그러나 문제를 해결하는 방법은 확실하지 않습니다).ValueError : 입력 배열을 모양 (20,590)에서 모양 (20)으로 방송 할 수 없습니다.

내가 다른 2 개의 stackoverflow 게시물을 보았지만 특정 태스크에 너무 구체적이기 때문에 내 문제가 해결되지 않습니다.

ValueError: could not broadcast input array from shape (128,128,3) into shape (128,128)

Value Error: could not broadcast input array from shape (857,3) into shape (857)

전체 오류 메시지 :

Traceback (most recent call last): File "/..../.../...../Batch_MFCC_Data.py", line 68, in X = np.array(MFCCs) ValueError: could not broadcast input array from shape (20,590) into shape (20)

코드 예 :

여기
all_wav_paths = glob.glob('directory_of_wav_files/**/*.wav', recursive=True) 
np.random.shuffle(all_wav_paths) 

MFCCs = [] #array to hold all MFCC's 
labels = [] #array to hold all labels 

for i, wav_path in enumerate(all_wav_paths): 

    individual_MFCC = MFCC_from_wav(wav_path) 
    #MFCC_from_wav() -> returns the MFCC coefficients 

    label = get_class(wav_path) 
    #get_class() -> returns the label of the wav file either 0 or 1 

    #add features and label to the array 
    MFCCs.append(individual_MFCC) 
    labels.append(label) 

#Must convert the training data to a Numpy Array for 
#train_test_split and saving to local drive 

X = np.array(MFCCs) #THIS LINE CRASHES WITH ABOVE ERROR 

# binary encode labels 
onehot_encoder = OneHotEncoder(sparse=False) 
Y = onehot_encoder.fit_transform(labels) 

#create train/test data 
from sklearn.cross_validation import train_test_split 
X_train, X_test, y_train, y_test = train_test_split(MFCCs, Y, test_size=0.25, random_state=0) 

#saving data to local drive 
np.save("LABEL_SAVE_PATH", Y) 
np.save("TRAINING_DATA_SAVE_PATH", X) 

형상 MFCC의 (에서의의 스냅 샷입니다. wav 파일) 나는 n은 MFCC를 배열

배열은 다음과 같은 형태로 포함 된 MFCC : 당신이 볼 수 있듯이

...More above... 
(20, 423) #shape of returned MFCC from one of the .wav files 
(20, 457) 
(20, 1757) 
(20, 345) 
(20, 835) 
(20, 345) 
(20, 687) 
(20, 774) 
(20, 597) 
(20, 719) 
(20, 1195) 
(20, 433) 
(20, 728) 
(20, 939) 
(20, 345) 
(20, 1112) 
(20, 345) 
(20, 591) 
(20, 936) 
(20, 1161) 
....More below.... 

의 MFCC 년대의 MFCC의 배열이 모든하지 않습니다 같은 모양을 가지고 있고,이 녹음 때문이다 모두 같은 시간 길이는 아닙니다. 이것이 내가 배열을 열심히 배열로 변환 할 수없는 이유입니까? 이것이 문제인 경우 MFCC 배열 전체에서 같은 모양을 유지하도록이 문제를 해결하려면 어떻게해야합니까?

이 내용과 조언을 얻기위한 모든 코드 스 니펫을 매우 높이 평가할 것입니다.

감사합니다.

+0

그래서이 MFCCs = []는 모양이나 실제 배열 자체만을 포함하고 있습니까? 내 말은'type (individual_MFCC)'이 배열인지 형태 정보인지를 뜻합니다. – kmario23

+0

혼란을 드려 죄송합니다. (individual_MFCC) =

+0

네, 언급 한 것처럼 서로 다른 배열 사이의 모양이 일치하지 않기 때문입니다. 두 가지 질문이 있습니다 : 1) 첫 번째 차원에는 항상 모양이 있습니다 (20, ...), 2) 배열의 최대 크기를 알고 있습니까? – kmario23

답변

1

min_shape

min_shape = (20, 345) 
MFCCs = [arr1, arr2, arr3, ...]  

for idx, arr in enumerate(MFCCs): 
    MFCCs[idx] = arr[:, :min_shape[1]] 

batch_arr = np.array(MFCCs) 

에 큰 배열을 줄이기 즉 min_shape에 배열을 다운 샘플링하기 위해 다음과 같은 논리를 사용하여 그리고 당신은 아래의 최소한의 예에서와 같이 배치 배열이 배열을 쌓을 수 :

In [33]: a1 = np.random.randn(2, 3)  
In [34]: a2 = np.random.randn(2, 5)  
In [35]: a3 = np.random.randn(2, 10) 

In [36]: MFCCs = [a1, a2, a3] 

In [37]: min_shape = (2, 2) 

In [38]: for idx, arr in enumerate(MFCCs): 
    ...:  MFCCs[idx] = arr[:, :min_shape[1]] 
    ...:  

In [42]: batch_arr = np.array(MFCCs) 

In [43]: batch_arr.shape 
Out[43]: (3, 2, 2) 

두 번째 전략의 경우 더 작은 어레이를 max_shape으로 업 샘플링하려면 비슷한 로직을 따르되 채우기 누락 된 값은 0으로 0보다 좋음 또는 nan 값입니다.

그런 다음 어레이 배열 모양을 일괄 배열 (num_arrays, dim1, dim2)으로 쌓을 수 있습니다. 그래서, 당신의 경우 모양은 (num_wav_files, 20, max_column이어야합니다.

관련 문제