zl程序教程

您现在的位置是:首页 >  Python

当前栏目

小白学Pytorch系列--Torch API (8)

2023-04-18 16:45:14 时间

小白学Pytorch系列–Torch API (9)

Spectral Ops

stft

短时傅立叶变换 (STFT)。
STFT就是以滑动窗口的形式,在不同窗口内部做FFT。
得到结果为spectrum,横坐标时间,纵坐标频率,数值大小表示能量深度。


hop_length: 帧移(160)
winlength: 窗口长度(如果小于n_fft,那么按中间对称进行相乘,e.g. fft:[1,2,3,4], win:[1,1],input:[1,2,3,4]*[0,1,1,0])
center: False(直接做fft,droplast) 如果是True:按照对称性补对称的数,

x = torch.Tensor([[1,6,8,5,7,9,11]])
print(x.shape, x)

y = torch.stft(x,n_fft=4,hop_length=2,win_length=4,window=torch.Tensor([1,1,0,0]),center=True)
print(y, y.shape)
import torch
from torch.autograd import Variable
from torch.nn.functional import conv1d
import matplotlib.pyplot as plt
from scipy.signal.windows import hann
import numpy as np

stride = 512

y = np.sin(2*np.pi*50*np.linspace(0,10,2048))+np.sin(2*np.pi*20*np.linspace(0,10,2048)) + np.random.normal(scale=1,size=2048)
 
def create_filters(d,k,low=50,high=6000):
    x = np.arange(0, d, 1)
    wsin = np.empty((k,1,d), dtype=np.float32)
    wcos = np.empty((k,1,d), dtype=np.float32)
    start_freq = low
    end_freq = high
    # num_cycles = start_freq*d/44000.
    # scaling_ind = np.log(end_freq/start_freq)/k

    window_mask = hann(2048, sym=False) # same as 0.5-0.5*np.cos(2*np.pi*x/(k))
    for ind in range(k):
        wsin[ind,0,:] = window_mask*np.sin(2*np.pi*ind/k*x)
        wcos[ind,0,:] = window_mask*np.cos(2*np.pi*ind/k*x)

    return wsin,wcos

wsin, wcos = create_filters(2048,2048)

wsin_var = Variable(torch.from_numpy(wsin), requires_grad=False)
wcos_var = Variable(torch.from_numpy(wcos),requires_grad=False)

network_input = torch.from_numpy(y).float()
network_input = network_input.reshape(1,-1)

zx = np.sqrt(conv1d(network_input[:,None,:], wsin_var, stride=stride).pow(2)+conv1d(network_input[:,None,:], wcos_var, stride=stride).pow(2))
pytorch_Xs = zx.cpu().numpy()
plt.plot(pytorch_Xs[0,:1025,0])

istft

短时间傅里叶逆变换。这应该是stft()的逆函数。

bartlett_window

Bartlett窗口功能。

blackman_window

blackman窗口功能。

hamming_window

Hamming窗口功能

hann_window

Hann窗口功能

kaiser_window

使用窗口长度window_length和形状参数beta计算Kaiser窗口。