Short-time Fourier transform (STFT) and matlab

Notes~for personal use~

Basic theory of short-time Fourier transform

        Short-Time Fourier Transform (Short-Time Fourier Transform, STFT) is a time-frequency analysis method, which divides the signal into several short periods in the time domain, and performs window function windowing on each short period before performing Fu Lie transform to get the frequency components at each moment. Compared with Discrete Fourier Transform (DFT) and Continuous Fourier Transform (CFT), STFT has the advantages of high time and frequency resolution.

        In STFT, a sliding window (also called time window, analysis window) is used to divide the input signal into several short periods. For each epoch, its Fourier transform can be calculated. Due to the existence of the window function, the Fourier transform of each short period can only reflect the frequency information in the short period. The result of this process is a series of transformed spectrograms on the time axis .

        For example, suppose a time-frequency analysis is performed on an audio signal. First, the signal is divided into several short periods of length W, assuming that the window function is h(n), then the STFT of the kth short period can be expressed as:

Among them, x(n) is the original signal, n is the sampling point in the time domain, k represents the kth short period, and ω represents the frequency. The meaning of this formula is: After windowing the signal of the kth short period of time, perform Fourier transform to obtain the frequency component of the short period of time .

       Combining all short-term STFTs, the time-frequency spectrum of the entire signal can be obtained. Wherein, the horizontal axis represents the time axis, the vertical axis represents the frequency axis, and the color represents the magnitude of the frequency component at that moment .

       An obvious shortcoming of STFT - the length of the window function cannot be too short, otherwise the frequency information cannot be accurately reflected, and it cannot be too long, otherwise the time domain resolution will decrease. This problem can be solved by using some STFT-based variant methods, such as Continuous Wavelet Transform (CWT) and variable-length window short-time Fourier transform (Variable-Length Window STFT, VSTFT), etc... ...follow-up discussion

Matlab use and demonstration

STFT (Short-Time Fourier Transform) is a time-frequency analysis method that can represent signals in a matrix in the time domain and frequency domain. Specifically, STFT divides the entire signal into several small segments, and performs window function and DFT processing on each small segment signal in the time domain, and finally obtains the matrix expression of the signal in two dimensions of time and frequency.

The function to calculate STFT transformation in Matlab is spectrogram , the syntax is as follows:

[S,F,T,P] = spectrogram(x, window, noverlap, nfft, fs);

Among them, ' x ' represents the input signal, ' window ' represents the length of the window function (in sampling points), ' noverlap ' represents the overlapping length between two adjacent windows (in sampling points, the default is ' window *0.5 '),' nfft 'indicates the number of points used in DFT transformation (must be a power of 2, the default is 256),' fs 'indicates the sampling rate. Function return values ​​include:

- ' S ': STFT spectral matrix with 'nfft/2+1' rows and STFT segments as columns.
- ' F ': frequency vector, unit is Hz, length is 'nfft/2+1'.
- ' T ' : time vector in seconds, the length is the number of STFT segments.
- ' P ': power spectrum matrix, same as STFT spectrum matrix 'S'.

Now, let's take a look at the specific implementation process of the sample code:

% 生成一个包含两个正弦波的信号
Fs = 1000; % 采样率
t = 0:1/Fs:1; % 时间序列
x = sin(2*pi*50*t) + sin(2*pi*120*t);

% STFT 参数设置
winlen = 0.5; % 每个小段的长度
overlap = 0.25; % 相邻小段的重叠长度
nfft = 1024; % DFT 点数

% 计算 STFT
[S,F,T,P] = spectrogram(x, round(winlen*Fs), round((1-overlap)*winlen*Fs), nfft, Fs);

step1: First, generate a signal ' x ' containing two sine waves with a sampling rate of 1000 Hz and a duration of 1 second.

step2: Then, set the parameters of STFT. Among them, ' winlen ' indicates that the length of each segment is 0.5 seconds, and ' overlap ' indicates that there is an overlap of 0.25 seconds between two adjacent segments, that is, there is 0.25 seconds of repeated data between two segments. ' nfft ' indicates that 1024 points are used for Fourier transform when performing DFT transform.

step3: Finally, call the ' spectrogram ' function to calculate the STFT transformation of ' x '. Actually, the function also returns the power spectrum matrix ' P ', but it is not used here. ' S ', ' F ', and ' T ' returned by the function denote the STFT spectral matrix, frequency vector, and time vector, respectively.

In this example, the STFT spectral matrix (ie 'S' ) is passed to the ' imagesc ' function to visualize the STFT's spectrogram. as follows:

% 绘制 STFT 频谱图
imagesc(T, F, 20*log10(abs(S)));
axis xy;
xlabel('Time (s)');
ylabel('Frequency (Hz)');
title('STFT Spectrogram');
colorbar;

        Use the ' imagesc ' function to display the STFT as a spectrogram, where the input parameters ' T ' and ' F ' are time and frequency vectors , respectively , and the input parameter 'abs(S)' represents the modulus of the STFT spectral matrix (since the modulus is more It can reflect the strength of the signal at different frequencies). At the same time, use the '20*log10' function to take the logarithm of the STFT spectral matrix and multiply it by 20. The purpose is to convert the STFT spectral matrix from linear units (ie amplitude) to logarithmic units (ie decibels) to enhance its visualization. Finally, use 'axis xy' to make the horizontal axis represent time and the vertical axis to represent frequency, add a colorbar with ' colorbar ', and finally add axis labels and titles with the 'xlabel', 'ylabel' and 'title' functions. to get the image:

 You can also choose to normalize S. as follows:

S = abs(S)/ max(abs(S(:)));
imagesc(T, F, S);
axis xy;
xlabel('Time (s)');
ylabel('Frequency (Hz)');
title('STFT Spectrogram');
colorbar;

to get the image:

You can see the result very clearly~  

Guess you like

Origin blog.csdn.net/weixin_43681559/article/details/130338137