Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

­ ­ ­ 
­ ­ ­ 
­ ­ ­ 
­ ­ ­ 
­ ­ ­ 
Posted
Updated 9-Jan-22 2:29am
Comments
Richard MacCutchan 9-Jan-22 7:52am    
You need to explain exactly what that means and why that code is not working.

1 solution

 
Share this answer
 
v2
Comments
Florentin lemarchand 9-Jan-22 10:05am    
Thanks, but i'am using python not matlab
Richard MacCutchan 9-Jan-22 10:13am    
See modified answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900