I am new to python, and I would like to know how to draw the STFT of a signal with the following program:
import numpy as np
import scipy.signal as sg
import matplotlib.pyplot as plt
from scipy import fftpack
def _stft(signal, sfreq, fmax):
n_samp=signal.shape[-1]
tw = fftpack.fftfreq(n_samp, 1. / sfreq) / n_samp
tw = np.r_[tw[:1], tw[1:][::-1]]
Pas=int(fmax*n_samp/sfreq);
STFT = np.empty((n_samp, len(tw)), dtype=np.complex128)
Fx = fftpack.fft(signal)
XF = np.concatenate([Fx, Fx], axis=-1)
k = 0.2 # 1 for classical stowckwell transform
f_range = np.arange(0, Pas-1, 1)
windows = np.empty(n_samp)
for i_f in f_range:
if i_f == 0.:
window = np.ones(n_samp)
else:
f=i_f
window = ((f / (np.sqrt(2. * np.pi) * k)) *
np.exp(-0.5 * (1. / k ** 2.) * (f ** 2.) * tw ** 2.))
window /= window.sum() # normalisation
windows = fftpack.fft(window)
STFT[..., i_f, :] = fftpack.ifft(XF[..., i_f:i_f + n_samp] * window)
return STFT
• Calculate the STFT of a signal with a chirp :
t=np.arange(0,1,Te , dtype=np.float64`
F1 = 50; F2 = 100;
Fe= 1000
chirp1 = np.cos(2*np.pi*(F1*t+((F2-F1)/2)*t**2))
• Display the STFT matrix modulus using the function.
thanking you in advance
What I have tried: