The principle of short-time Fourier transform (Fourier Transform) and Python implementation

Source: Internet
Author: User

principle

Short-time Fourier transform (Fourier Transform, STFT) is a common tool for speech signal processing. It defines a very useful time and frequency distribution class that specifies the complex amplitude of any signal that varies with time and frequency. In fact, the process of calculating a short-time Fourier transform is to divide a longer time signal into shorter segments of the same length, and to calculate the Fourier transform, the Fourier spectrum, on each shorter segment.

The usual mathematical definitions for short-time Fourier transforms are as follows:

which

DTFT (decrete time Fourier Transform) is a discrete Fourier transform. Its mathematical formula is as follows:

where x (n) is the amplitude of the signal at the sample number N. Ω~ is defined as follows:

When implemented, the short-time Fourier transform is computed as a series of fast Fourier transforms (fast Fourier Transform, FFT) with windowing data frames, where the window is "sliding" (slide) or jumping (hop) over time.

Python Implementation

In the program, the frame_size is the size of the signal into a shorter frame, and in speech processing, the frame size is usually between 20ms and 40ms. This is set to 25ms, i.e. frame_size = 0.025;

  Frame_stride is the sliding size or jump size of the adjacent frame, usually the sliding size of the frame is between 10ms and 20ms, which is set to 10ms, that is, frame_stride = 0.01. At this point, the overlapping size of the adjacent frames is 15ms;

The Window function uses Hamming window functions (Hamming function);

At each frame, a 512-point fast Fourier transform, i.e. Nfft =. The specific procedures are as follows:

#-*-Coding:utf8-*-ImportNumPy as NPdefCALC_STFT (signal, sample_rate=16000, frame_size=0.025, frame_stride=0.01, winfunc=np.hamming, NFFT=512):    #Calculate The number of frames from the signalFrame_length = Frame_size *sample_rate Frame_step= Frame_stride *sample_rate signal_length=len (signal) Frame_length=Int (round (frame_length)) Frame_step=Int (round (frame_step)) Num_frames= 1 + int (Np.ceil (float (np.abs (signal_length-frame_length))/frame_step)) #Zero paddingPad_signal_length = num_frames * Frame_step +frame_length z= Np.zeros ((Pad_signal_length-signal_length)) #Pad signal to make sure, all frames has equal number of samples    #without truncating any samples from the original signalPad_signal =np.append (signal, z)#Slice the signal into frames from indicesindices = np.tile (Np.arange (0, Frame_length), (Num_frames, 1)) +np.tile (np.arange (0, Num_frames* Frame_step, Frame_step), (Frame_length, 1)). T Frames= Pad_signal[indices.astype (Np.int32, copy=False)] #Get Windowed FramesFrames *=Winfunc (frame_length)#Compute The one-dimensional n-point discrete Fourier Transform (DFT) of    #a real-valued array by means of a efficient algorithm called Fast Fourier Transform (FFT)Mag_frames =Np.absolute (Np.fft.rfft (frames, nfft))#Compute Power SpectrumPow_frames = (1.0/NFFT) * ((mag_frames) * * 2)    returnPow_framesif __name__=='__main__':    ImportScipy.io.wavfileImportMatplotlib.pyplot as Plt#Read wav file    #"Osr_us_000_0010_8k.wav" is downloaded from http://www.voiptroubleshooter.com/open_speech/american.htmlSample_rate, signal = Scipy.io.wavfile.read ("Osr_us_000_0010_8k.wav")    #Get Speech Data in the first 2 secondsSignal = Signal[0:int (2. *Sample_rate)] #Calculate the short time Fourier transformPow_spec =Calc_stft (signal, sample_rate) plt.imshow (Pow_spec) plt.tight_layout () plt.show ()

References

1. Discrete time FOURIER TRANSFORM (DTFT). Https://www.dsprelated.com/freebooks/mdft/Discrete_Time_Fourier_Transform.html

2. The Short-time FOURIER TRANSFORM. Https://www.dsprelated.com/freebooks/sasp/Short_Time_Fourier_Transform.html

3. Short-time Fourier transform. Https://en.wikipedia.org/wiki/Short-time_Fourier_transform

4. Speech processing for machine learning:filter banks, mel-frequency cepstral coefficients (MFCCS) and what ' s in-between . Https://haythamfayek.com/2016/04/21/speech-processing-for-machine-learning.html

The principle of short-time Fourier transform (Fourier Transform) and Python implementation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.