私は多かれ少なかれPythonの初心者で、このevolutionary Mona Lisaの実験のオーディオに取り組んでいます。 numpyの配列に与えられた.wavファイルを読む数値が「シャッフルされた」NumPy配列
- :
以下のコードはに意図されます。
- 波形の 'ゼロクロス'を検出します。つまり、配列要素が符号を変更したときです。これらのポイントで、アレイを波形の「チャンク」のネストされたリストに分割します。
- 正のチャンクを分離してから、これらのチャンクをシャッフルし、それらをNumPyアレイに再結合します。リストには2000以上の要素があるので、random.shuffle()は使用できません。
- シャッフルされた配列 の 'fitness'を、シャッフルされた配列と元のサンプルの差の2乗として定義された元のサンプルと比較してください。
最後に、私は複製、突然変異、および選択を追加しますが、今は自分の適応機能に問題があります。分割は、シャッフル、および再結合配列は、次のエラーが発生し、元の入力とは異なる寸法のものである:二番目の配列の
$ ValueError: operands could not be broadcast together with shapes (1273382) (1138213)
寸法は、私がプログラムを実行するたびに変化するが、周りに常に1138000- 1145000。分割、シャッフル、再結合のステップでいくつかのチャンクを失ったように見えますが、ステップ3のどこかでリストの理解度を間違って使用していると思われますが、どこか理由が分かりません。何が間違っていますか?
# Import scipy audio tools, numpy, and randomization tools
import scipy
from scipy.io import wavfile
import numpy
from random import shuffle, randint
# Read a wav file data array, detect zero crossings, split at zero crossings, and return a nested list.
def process_wav(input):
# Assign the wavefile data array to a variable.
wavdata = input[1]
# Detect zero crossings, i.e. changes in sign in the waveform data. The line below returns an array of the indices of elements after which a zero crossing occurs.
zerocrossings = numpy.where(numpy.diff(numpy.sign(wavdata)))[0]
# Increment each element in the array by one. Otherwise, the indices are off.
zerocrossings = numpy.add(numpy.ones(zerocrossings.size, zerocrossings.dtype), zerocrossings)
wavdatalist = wavdata.tolist()
zerocrossingslist = zerocrossings.tolist()
# Split the list at zero crossings. The function below splits a list at the given indices.
def partition(alist, indices):
return [alist[i:j] for i, j in zip([0]+indices, indices+[None])]
return partition(wavdatalist, zerocrossingslist)
# Accept a list as input, separate into positive and negative chunks, shuffle, and return a shuffled nested list
def shuffle_wav(list):
# Separate waveform chunks into positive and negative lists.
positivechunks = []
negativechunks = []
for chunk in list:
if chunk[0] < 0:
negativechunks.append(chunk)
elif chunk[0] > 0:
positivechunks.append(chunk)
elif chunk[0] == 0:
positivechunks.append(chunk)
# Shuffle the chunks and append them to a list, alternating positive with negative.
shuffledchunks = []
while len(positivechunks) >= 0 and len(negativechunks) > 0:
currentpositivechunk = positivechunks.pop(randint(0, len(positivechunks)-1))
shuffledchunks.append(currentpositivechunk)
currentnegativechunk = negativechunks.pop(randint(0, len(negativechunks)-1))
shuffledchunks.append(currentnegativechunk)
return [chunk for sublist in shuffledchunks for chunk in sublist]
def get_fitness(array, target):
return numpy.square(numpy.subtract(target, array))
# Read a sample wav file. The wavfile function returns a tuple of the file's sample rate and data as a numpy array, to be passed to the process_wav() function.
input = scipy.io.wavfile.read('sample.wav')
wavchunks = process_wav(input)
shuffledlist = shuffle_wav(wavchunks)
output = numpy.array(shuffledlist, dtype='int16')
print get_fitness(output, input[1])
scipy.io.wavfile.write('output.wav', 44100, output)
編集:ここでは、完全なトレースバックです:
Traceback (most recent call last):
File "evowav.py", line 64, in <module>
print get_fitness(output, input[1])
File "evowav.py", line 56, in get_fitness
return numpy.square(numpy.subtract(target, array))
ValueError: operands could not be broadcast together with shapes (1273382) (1136678)`
してくださいは、常にCOMPLETEトレースバックが含まれ、あるいは我々は助けることはできません。 –
お詫び申し上げます。私は上記の完全なトレースバックを追加しました。 – ecmendenhall
使用している.wavファイルへのリンクがあるので、再現できますか? –