<jupyter_start><jupyter_text>STFT<jupyter_code>"""
x : 入力信号
win : 窓関数
step : シフト幅
"""

def stft(x, win, step):
    l = len(x) # 入力信号の長さ
    N = len(win) # 窓幅
    M = int(ceil(float(l - N + step) / step))
    
    new_x = zeros(N + ((M - 1) * step))
    new_x[: l] = x 
    
    X = zeros([M, N], dtype = complex64) # スペクトログラムの初期化
    for m in range(M):
        start = step * m
        X[m, :] = fft(new_x[start : start + N] * win)
    return X<jupyter_output><empty_output><jupyter_text>iSTFT<jupyter_code>def istft(X, win, step):
    M, N = X.shape
    assert (len(win) == N), "FFT length and window length are different."

    l = (M - 1) * step + N
    x = zeros(l, dtype = float64)
    wsum = zeros(l, dtype = float64)
    for m in range(M):
        start = step * m
        
        x[start : start + N] = x[start : start + N] + ifft(X[m, :]).real * win
        wsum[start : start + N] += win ** 2 
    pos = (wsum != 0)
    x_pre = x.copy()
    
    ### 窓分のスケール合わせ
    x[pos] /= wsum[pos]
    return x

# 設定
fftlen = 512

dt = 0.01
f = 10
t = np.linspace(1, fftlen, fftlen) * dt - dt
y = np.sin(np.pi * f * t)

win = np.hamming(fftlen)
step = fftlen / 4
spectrogram = stft(y, win, step)
resyn_data = istft(spectrogram, win, step)
### plot
plot(y)
title("Input_signal", fontsize = 20)
xlim([0, len(y)])
show()

imshow(abs(spectrogram[:, : fftlen / 2 + 1].T), aspect = "auto", origin = "lower")
show()
plot(resyn_data)
xlim([0, len(resyn_data)])
title("Resynthesized signal", fontsize = 20)
show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17493472584856398, 'fraction_numerical': 0.018276762402088774, 'mean_word_length': 2.748166259168704, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '649808', 'license': 'MIT', 'n_tokens_mistral': 713, 'n_tokens_neox': 687, 'n_words': 165, 'path': 'STFT.ipynb', 'repo_name': 'Accent-/signal-bootcamp', 'size': '1532'}