반전 STFT 및 ISTFT 에서는 파이썬
-
20-09-2019 - |
문제
가 없는 일반적인 형태의 짧은 시간 푸리에 변환 해당 역 변환로 건설 SciPy 또는 NumPy 거나 있을 수 있습니다.
가 pyplot specgram
기능 matplotlib 는 전화 ax.specgram()
, 는 전화 mlab.specgram()
, 는 전화 _spectral_helper()
:
#The checks for if y is x are so that we can use the same function to #implement the core of psd(), csd(), and spectrogram() without doing #extra calculations. We return the unaveraged Pxy, freqs, and t.
지
이것은 도우미 기능을 구현하는 공통점이 204#psd,csd,그리고 스펙트로.그 지 사용되는 것을 의미한 외부의 mlab
나는 확실하지 않는 경우에 이를 수행하는 데 사용할 수 있는 STFT 및 ISTFT,하지만.은 거기에 다른 것,또는 나를 번역 같은 뭔가 이러한 기능을 MATLAB?
가 작성하는 방법을 알고 내 자신의 ad-hoc 구현나는 뭔가를 찾고하는 전기능,처리 할 수 있는 다른 윈도우 기능(지만 정본),은 완전히 반전과 콜라(windowsistft(stft(x))==x
),에 의해 테스트를 여러 사람,no off-by-one 오류,처리 종료되고 제로 패딩이론,빠른 RFFT 구현을 위해 실제 입력,등등.
해결책
저는 조금 늦게 이지만,실 scipy inbuilt istft 기능으로의 0.19.0
다른 팁
여기에는 나이지,간 이에 대한 답변:
import scipy, pylab
def stft(x, fs, framesz, hop):
framesamp = int(framesz*fs)
hopsamp = int(hop*fs)
w = scipy.hanning(framesamp)
X = scipy.array([scipy.fft(w*x[i:i+framesamp])
for i in range(0, len(x)-framesamp, hopsamp)])
return X
def istft(X, fs, T, hop):
x = scipy.zeros(T*fs)
framesamp = X.shape[1]
hopsamp = int(hop*fs)
for n,i in enumerate(range(0, len(x)-framesamp, hopsamp)):
x[i:i+framesamp] += scipy.real(scipy.ifft(X[n]))
return x
Notes:
- 이 목록 이해 작은 나 같은 사용하는 시뮬레이션하 블록의 처리에서 신호를 numpy/scipy.그
blkproc
in Matlab.대for
루프,내가 적용되는 명령(예를 들어,fft
는)각 프레임의 신호의 내부 목록을 이해,그리고 다음scipy.array
캐스트는 그것을 2D-배열입니다.내가 이것을 사용하는 스펙트로,chromagrams,MFCC-그램,그리고 훨씬 더 많은. - 이 예를 들어,내가 사용하여 순진 겹치 추가 방법
istft
.을 재구성하기 위해 원본 신호 합의 순차적 창 함수해야 될 일정한,동일한 것이 바람직하 unity(1.0).이 경우 선택 한다(또는hanning
)창고 50%겹치는 완벽하게 작동합니다.보 이 논 자세한 내용은. - 아마 거기에 더 많은 원칙적인 방법을 컴퓨팅 ISTFT.이 예제는 주로 될 것을 의미한 교육입니다.
테스트:
if __name__ == '__main__':
f0 = 440 # Compute the STFT of a 440 Hz sinusoid
fs = 8000 # sampled at 8 kHz
T = 5 # lasting 5 seconds
framesz = 0.050 # with a frame size of 50 milliseconds
hop = 0.025 # and hop size of 25 milliseconds.
# Create test signal and STFT.
t = scipy.linspace(0, T, T*fs, endpoint=False)
x = scipy.sin(2*scipy.pi*f0*t)
X = stft(x, fs, framesz, hop)
# Plot the magnitude spectrogram.
pylab.figure()
pylab.imshow(scipy.absolute(X.T), origin='lower', aspect='auto',
interpolation='nearest')
pylab.xlabel('Time')
pylab.ylabel('Frequency')
pylab.show()
# Compute the ISTFT.
xhat = istft(X, fs, T, hop)
# Plot the input and output signals over 0.1 seconds.
T1 = int(0.1*fs)
pylab.figure()
pylab.plot(t[:T1], x[:T1], t[:T1], xhat[:T1])
pylab.xlabel('Time (seconds)')
pylab.figure()
pylab.plot(t[-T1:], x[-T1:], t[-T1:], xhat[-T1:])
pylab.xlabel('Time (seconds)')
여기에 STFT 코드를 사용하는.STFT+ISTFT 여기에 제공 완벽한 재건 (도에 대한 첫번째 프레임).나는 약간 수정 코드가 여기에 주어진에 의해 스티브 Tjoa:여기에는 규모의 재구성 신호와 같은 그의 입력 신호입니다.
import scipy, numpy as np
def stft(x, fftsize=1024, overlap=4):
hop = fftsize / overlap
w = scipy.hanning(fftsize+1)[:-1] # better reconstruction with this trick +1)[:-1]
return np.array([np.fft.rfft(w*x[i:i+fftsize]) for i in range(0, len(x)-fftsize, hop)])
def istft(X, overlap=4):
fftsize=(X.shape[1]-1)*2
hop = fftsize / overlap
w = scipy.hanning(fftsize+1)[:-1]
x = scipy.zeros(X.shape[0]*hop)
wsum = scipy.zeros(X.shape[0]*hop)
for n,i in enumerate(range(0, len(x)-fftsize, hop)):
x[i:i+fftsize] += scipy.real(np.fft.irfft(X[n])) * w # overlap-add
wsum[i:i+fftsize] += w ** 2.
pos = wsum != 0
x[pos] /= wsum[pos]
return x
librosa.core.stft
고 istft
보 매우 유사하는 내가 무엇을 찾고 있었지만,그들은 존재하지 않는 시간에:
librosa.core.stft(y, n_fft=2048, hop_length=None, win_length=None, window=None, center=True, dtype=<type 'numpy.complex64'>)
그들은하지 않는 반전을 정확하게,하지만;끝이 가늘게 합니다.
을 발견 또 다른 STFT 없지만,해당 역함수:
http://code.google.com/p/pytfd/source/browse/trunk/pytfd/stft.py
def stft(x, w, L=None):
...
return X_stft
- w 은 윈도우 기능으로 배열
- L 이 겹치에서 샘플
도 위의 답변을 잘 OOTB 습니다.그래서 나는 수정된 스티브 Tjoa 습니다.
import scipy, pylab
import numpy as np
def stft(x, fs, framesz, hop):
"""
x - signal
fs - sample rate
framesz - frame size
hop - hop size (frame size = overlap + hop size)
"""
framesamp = int(framesz*fs)
hopsamp = int(hop*fs)
w = scipy.hamming(framesamp)
X = scipy.array([scipy.fft(w*x[i:i+framesamp])
for i in range(0, len(x)-framesamp, hopsamp)])
return X
def istft(X, fs, T, hop):
""" T - signal length """
length = T*fs
x = scipy.zeros(T*fs)
framesamp = X.shape[1]
hopsamp = int(hop*fs)
for n,i in enumerate(range(0, len(x)-framesamp, hopsamp)):
x[i:i+framesamp] += scipy.real(scipy.ifft(X[n]))
# calculate the inverse envelope to scale results at the ends.
env = scipy.zeros(T*fs)
w = scipy.hamming(framesamp)
for i in range(0, len(x)-framesamp, hopsamp):
env[i:i+framesamp] += w
env[-(length%hopsamp):] += w[-(length%hopsamp):]
env = np.maximum(env, .01)
return x/env # right side is still a little messed up...
저 또한 이것을 발견 GitHub 에서지만,그것은 보인다에서 작동하는 파이프라인을 대신 정상적인 배열:
http://github.com/ronw/frontend/blob/master/basic.py#LID281
def STFT(nfft, nwin=None, nhop=None, winfun=np.hanning):
...
return dataprocessor.Pipeline(Framer(nwin, nhop), Window(winfun),
RFFT(nfft))
def ISTFT(nfft, nwin=None, nhop=None, winfun=np.hanning):
...
return dataprocessor.Pipeline(IRFFT(nfft), Window(winfun),
OverlapAdd(nwin, nhop))
나는 생각 scipy.신호는 당신이 무엇을 찾고 있습니다.그것은 합리적인 기본값으로 지원하는 여러 창의 종류,등등....
http://docs.scipy.org/doc/scipy-0.17.0/reference/generated/scipy.signal.spectrogram.html
from scipy.signal import spectrogram
freq, time, Spec = spectrogram(signal)
고정된 버전의 basj 의 대답이다.
import scipy, numpy as np
import matplotlib.pyplot as plt
def stft(x, fftsize=1024, overlap=4):
hop=fftsize//overlap
w = scipy.hanning(fftsize+1)[:-1] # better reconstruction with this trick +1)[:-1]
return np.vstack([np.fft.rfft(w*x[i:i+fftsize]) for i in range(0, len(x)-fftsize, hop)])
def istft(X, overlap=4):
fftsize=(X.shape[1]-1)*2
hop=fftsize//overlap
w=scipy.hanning(fftsize+1)[:-1]
rcs=int(np.ceil(float(X.shape[0])/float(overlap)))*fftsize
print(rcs)
x=np.zeros(rcs)
wsum=np.zeros(rcs)
for n,i in zip(X,range(0,len(X)*hop,hop)):
l=len(x[i:i+fftsize])
x[i:i+fftsize] += np.fft.irfft(n).real[:l] # overlap-add
wsum[i:i+fftsize] += w[:l]
pos = wsum != 0
x[pos] /= wsum[pos]
return x
a=np.random.random((65536))
b=istft(stft(a))
plt.plot(range(len(a)),a,range(len(b)),b)
plt.show()
액세스 권한이 있는 경우에는 C 이진 라이브러리는 당신이 원하는 무엇을,다음 사용 http://code.google.com/p/ctypesgen/ 를 생성하는 파이썬 인터페이스는 라이브러리입니다.