0

有一个代码:

import wave
import numpy as np
import math

wav = wave.open("music.wav", mode="r")
(nchannels, sampwidth, framerate, nframes, comptype, compname) = wav.getparams()

content = wav.readframes(nframes)
samples = np.fromstring(content, dtype=types[sampwidth])

for n in range(nchannels):
    channel = samples[n::nchannels]
    print channel

因此:

[0 0 0 ..., 0 0 8]
[0 0 0 ..., 0 0 0]

类型:

<type 'numpy.ndarray'>
<type 'numpy.ndarray'>

我不知道下一步该做什么......我会很高兴另一个解决方案:)

4

2 回答 2

1

不确定你的第二个问题,但对于第一个......

如果样本中有一个 nd numpy 数组:

samples
array([[   1,    3],
   [   2,    2],
   [   3,    4],
   [   4,    5],
   [   5,  100],
   [   6, 1000],
   [   7,    0],
   [   8,    1]]

mean1 = samples.mean(axis=1)
max1 = samples.max(axis=1)

outputWav = numpy.vstack((mean1,max1)).T

然后写出这个文件,注意从浮点数到整数的舍入问题。

于 2013-10-30T14:09:49.963 回答
0

解决方案:

# first channel
samples_o = samples[0::2]
# second channel
samples_c = samples[1::2]

# for 3 second 24000 = 8000*3
gr_size = len(samples_o) // gr_count 

lst = [lst[i:i+gr_size] for i in range(0, len(samples_o), gr_size)]

agr = []

for array in lst:
  max_el = np.argmax(array, axis=0)
  agr.append(max_el)

print np.mean(agr, axis=0) # avg max volume for first channel
于 2013-11-07T09:19:46.873 回答