前提・実現したいこと
Pythonを使用した音声解析としてウェーブレット変換をしたいです。
一つ目の表示である音声波形はうまく出るのですが、Fs=1/0.0001の行からエラーが出てしまいます。
解決方法を教えていただきたいです。
発生している問題・エラーメッセージ
Using memory-mapped arrays... Traceback (most recent call last): File "C:\Users\hiro2\Desktop\sound\wavelet.py", line 45, in <module> r=pycwt.cwt_f(y,freqs,Fs,pycwt.Morlet(omega0)) File "C:\Users\hiro2\AppData\Local\Programs\Python\Python39\lib\site-packages\swan\pycwt.py", line 234, in cwt_f return cwt_a(signal, scales, dt, wavelet, ppd, verbose=verbose) File "C:\Users\hiro2\AppData\Local\Programs\Python\Python39\lib\site-packages\swan\pycwt.py", line 219, in cwt_a W = memsafe_arr((len(scales), siglen), 'complex') File "C:\Users\hiro2\AppData\Local\Programs\Python\Python39\lib\site-packages\swan\pycwt.py", line 34, in memsafe_arr return np.memmap(_tmpfile, dtype=dtype, shape=shape) File "C:\Users\hiro2\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\core\memmap.py", line 254, in __new__ fid.write(b'\0') File "C:\Users\hiro2\AppData\Local\Programs\Python\Python39\lib\tempfile.py", line 474, in func_wrapper return func(*args, **kwargs) TypeError: write() argument must be str, not bytes
該当のソースコード
Python
from swan import pycwt import numpy as np import matplotlib.pyplot as plt import wave #import struct from scipy import fromstring, int16 #from pylab import * #from scipy import signal wavfile = '04-agarikata-kara-01-p6TAskMJUgg.wav' wr = wave.open(wavfile, "rb") ch = wr.getnchannels() width = wr.getsampwidth() fr = wr.getframerate() fn = wr.getnframes() fs = fn / fr print("オーディオチャンネル数(モノラル: 1 ステレオ:2 ) : ", ch) print("オーディオフレーム数 : ", fn) print("サンプリングレート : ",fr) print("記録時間(Sec) : ", fs, 'sec') print("サンプルサイズ(バイト数) : ", width) origin = wr.readframes(wr.getnframes()) data = origin[:fn] wr.close() amp = max(data) print(amp) print('len of origin', len(origin)) print('len of sampling: ', len(data)) # ステレオ前提 > monoral y = np.frombuffer(data, dtype="int16") /32768.0 x = np.linspace(0,fs, int(fn/2), endpoint=False) plt.plot(x, y) plt.show() print(len(y)) Fs = 1/0.0001 omega0 = 5 #0.2 #1 #2 #8 # (1) Freqを指定してcwt freqs=np.arange(10,2000,2.5) r=pycwt.cwt_f(y,freqs,Fs,pycwt.Morlet(omega0)) rr=np.abs(r) plt.rcParams['figure.figsize'] = (10, 6) fig = plt.figure() ax1 = fig.add_axes([0.1, 0.75, 0.7, 0.2]) ax2 = fig.add_axes([0.1, 0.1, 0.7, 0.60], sharex=ax1) ax3 = fig.add_axes([0.83, 0.1, 0.03, 0.6]) ax1.plot(x, y, 'k') img = ax2.imshow(np.flipud(rr), extent=[0, 3,10, 2000], aspect='auto') #, interpolation='nearest') twin_ax = ax2 twin_ax.set_yscale('log') twin_ax.set_xlim(0, 3) twin_ax.set_ylim(10, 2000) ax2.tick_params(which='both', labelleft=False, left=False) twin_ax.tick_params(which='both', labelleft=True, left=True, labelright=False) fig.colorbar(img, cax=ax3) plt.show() plt.plot(freqs,rr) plt.xscale('log') plt.show()
試したこと
他のサイトを訪れた結果コードがステレオではなくモノラル対応なのではないかという疑惑があります。
使用したサイト(FW/ツールのバージョンなど)
まだ回答がついていません
会員登録して回答してみよう