wavfile.read
에 의해 반환 된 배열 data
은 정수 데이터 형식을 갖는 수십 개의 배열입니다. NumPy와 배열의 데이터 타입이 장소에서 변경 될 수 없기 때문에이 라인 :
data[i][0] = math.sin(data[i][0])
항상 대신 그 라인의 0
될 것 정수에 math.sin
의 결과를 캐스트 생성 계산 된 결과를 저장하는 새로운 부동 소수점 배열.
또는 한 번에 배열의 모든 요소의 사인 계산 numpy.sin
를 사용하여 추가 의견에서
import numpy as np
import scipy.io.wavfile
rate, data = scipy.io.wavfile.read('xenencounter_23.wav')
sin_data = np.sin(data)
print sin_data
는, 당신이 각 값의 사인을하고 그 결과를 기록 할 것으로 보인다 새 wav 파일로.
다음은 내가 원하는대로하는 예입니다. 여기서 'M1F1-int16-AFsp.wav'파일을 사용하겠습니다 : http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Samples.html. show_info
함수는 각 단계의 결과를 설명하기위한 편리한 방법입니다. 대화 형 셸을 사용하는 경우이 변수를 사용하여 변수와 해당 속성을 검사 할 수 있습니다.
import numpy as np
from scipy.io import wavfile
def show_info(aname, a):
print "Array", aname
print "shape:", a.shape
print "dtype:", a.dtype
print "min, max:", a.min(), a.max()
print
rate, data = wavfile.read('M1F1-int16-AFsp.wav')
show_info("data", data)
# Take the sine of each element in `data`.
# The np.sin function is "vectorized", so there is no need
# for a Python loop here.
sindata = np.sin(data)
show_info("sindata", sindata)
# Scale up the values to 16 bit integer range and round
# the value.
scaled = np.round(32767*sindata)
show_info("scaled", scaled)
# Cast `scaled` to an array with a 16 bit signed integer data type.
newdata = scaled.astype(np.int16)
show_info("newdata", newdata)
# Write the data to 'newname.wav'
wavfile.write('newname.wav', rate, newdata)
다음은 출력입니다. (초기 경고 scipy.io.wavfile.read
의해 이해되지 않는 파일의 일부 메타 데이터는, 아마도 의미있다.)는
<snip>/scipy/io/wavfile.py:147: WavFileWarning: Chunk (non-data) not understood, skipping it.
WavFileWarning)
Array 'data'
shape: (23493, 2)
dtype: int16
min, max: -7125 14325
Array 'sindata'
shape: (23493, 2)
dtype: float32
min, max: -0.999992 0.999991
Array 'scaled'
shape: (23493, 2)
dtype: float32
min, max: -32767.0 32767.0
Array 'newdata'
shape: (23493, 2)
dtype: int16
min, max: -32767 32767
새 파일 'newname.wav'는 부호 16 개 비트 값의 두 개의 채널을 포함한다.
+1은'np.sin (...) '을 제안합니다. 그건 분명히 갈 길이며, 더 길어질 것입니다. – nneonneo
감사합니다. 저는 사인 값을 다른 배열 (또는 기술적으로 목록)에 저장하려고 시도했지만, 쓰는 경우'AttributeError : 'list'객체에 'ndim'속성이 없습니다. – JVE999
@ Jamil, 나는 당신 자신을 익히고 싶을 것입니다. (예 : http://www.engr.ucsb.edu/~shell/che210d/numpy.pdf 또는 http://wiki.scipy.org/Tentative_NumPy_Tutorial) 또는 다른 수많은 numpy 자습서 저 밖에). 당신이 한 일을 보지 않고서는 그 문제를 해결할 수 없습니다. –