The usage of short-time Fourier transform spectrogram and stft in Matlab

The usage of short-time Fourier transform spectrogram and stft in Matlab

  In Matlab, the short-time Fourier transform requires the use of functions spectrogram, and in Matlab2019, a new function has been introduced stft. Let’s take a look at how these two functions are used.

  The basic principle of the short-time Fourier transform is to segment the data into windows, do fft, there will be overlap in the segmentation, so the short-time Fourier transform result of a vector is a matrix. Knowing this, the following functions and parameters will be easier to understand.

spectrogram

parameter list

  Let's look at the spectrogramfunction first . In earlier versions, the name of this function is specgram. Several common usages are as follows:

spectrogram(x)
s = spectrogram(x)
s = spectrogram(x, window)
s = spectrogram(x, window, noverlap)
s = spectrogram(x, window, noverlap, nfft)
s = spectrogram(x, window, noverlap, nfft, fs)
[s, f, t] = spectrogram(x, window, noverlap, nfft, fs)
[s, f, t] = spectrogram(x, window, noverlap, f, fs)
[s, f, t, p] = spectrogram(x, window, noverlap, f, fs)

among them,

  • xIndicates the input signal;
  • windowRepresents the window function. If windowthe value is an integer, then the length of each segment of x that is segmented is equal to window, and the default Hamming window is used; if window is a vector, then the length of each segment after segmentation is equal to length(window), and the input vector is the window function to be added;
  • overlapIndicates the number of overlap points between the two segments. The value of overlap must be less than the window length. If overlap is not specified, the default is half of the window length, that is, 50% overlap;
  • nfftRepresents the number of fft points, the number of fft points can be different from the window length. When this parameter is not specified, Matlab will take max(256, 2^(ceil(log2(length(window))))), which is the window length When it is less than 256, the number of fft points is 256; when the window length is greater than 256, the number of fft points is the smallest integer power of 2 greater than the window length;
  • fsRepresents the sampling rate, used for normalized display;
  • fIndicates the spectrum range of the display, f is a vector with the same length as the number of rows in s;
    • When x is a real signal and nfft is an even number, the number of rows of s is (nfft/2+1)
    • When x is a real signal and nfft is an odd number, the number of rows of s is (nfft+1)/2
    • When x is a complex signal, the number of rows in s is nfft
    • When f is specified in the input parameter list, the function will calculate the spectrogram at the frequency specified by f, and the returned f is the same as the input f;
  • tRepresents the displayed time range, which is a vector with the same length as the number of columns in s;
  • pRepresents the power spectral density. For a real signal, p is the single-sided period estimation of each segment of the PSD; for a complex signal, when the F frequency vector is specified, P is the two-sided PSD; how to calculate the PSD

Examples

  1. the generated signal is as follows, and the 4 dot frequency signals are spliced ​​together:

clc;clear all;close all;
fs = 10e6;
n = 10000;
f1 = 10e3; f2 = 50e3; f3 = 80e3; f4 = 100e3;
t = (0:n-1)'/fs;
sig1 = cos(2*pi*f1*t);
sig2 = cos(2*pi*f2*t);
sig3 = cos(2*pi*f3*t);
sig4 = cos(2*pi*f4*t);

sig = [sig1; sig2; sig3; sig4];

  The time-domain waveform of the signal is as follows:

Insert picture description here

  Call spectrogram(sig)it directly , and you can get the following results. The default horizontal axis in the figure is frequency, and the vertical axis is time.

Insert picture description here

  In order to make drawing more flexible, we do not use spectrogramdrawing directly , and safter finding out, we will sdraw a separate drawing. This time we specify windowa size of 256

s = spectrogram(sig, 256);
t = linspace(0, 4*n/fs, size(s,1));
f = linspace(0, fs/2, size(s,2));
figure;
imagesc(t, f, 20*log10((abs(s))));xlabel('Samples'); ylabel('Freqency');
colorbar;

Insert picture description here

noverlapThe default is 50%, now we set it to windowthe length minus 1, that is, each step is 1

s = spectrogram(sig, 256, 255);
t = linspace(0, 4*n/fs, size(s,1));
f = linspace(0, fs/2, size(s,2));
figure;
imagesc(t, f, 20*log10((abs(s))));xlabel('Samples'); ylabel('Freqency');
colorbar;

Insert picture description here

  Coupled with the nfftsum fsparameter, we specify the number of fft points to be the window length

s = spectrogram(sig, 256, 128, 256, fs);

  The graphics for this are the same as before, no more

  If the return value, increasing fand tso we will not need to redefine below fand tthe

[s, f, t] = spectrogram(sig, 256, 128, 256, fs);
figure;
imagesc(t, f, 20*log10((abs(s))));xlabel('Samples'); ylabel('Freqency');
colorbar;

  From the above figure, we can see that the frequencies of our 4 signals are relatively small, while the drawn figure shows a relatively large spectrum range, which leads to a large part of the information below that we actually don’t need. At this time, we can fcalculate the frequency spectrum through the specified interval. In order to display better, we also adjust other parameters

window = 2048;
noverlap = window/2;
f_len = window/2 + 1;
f = linspace(0, 150e3, f_len);
[s, f, t] = spectrogram(sig, window, noverlap, f, fs);
figure;
imagesc(t, f, 20*log10((abs(s))));xlabel('Samples'); ylabel('Freqency');
colorbar;

Insert picture description here

  Finally, add the return value of the power spectral density

[s, f, t, p] = spectrogram(sig, window, nfft, f, fs);
figure;
imagesc(t, f, p);xlabel('Samples'); ylabel('Freqency');
colorbar;

Insert picture description here

stft

  There are not many explanations of this function in Matlab. The example only writes two, but the usage is relatively simple:

window = 2048;
noverlap = window/2;
nfft = window;
[s, f, t, p] = spectrogram(sig, window, noverlap, nfft, fs);
figure;
imagesc(t, f, 20*log10((abs(s))));xlabel('Samples'); ylabel('Freqency');
title('Short-time Fourier transform graph drawn using spectrogram');
colorbar;

ss = stft(sig,fs,'Window',hamming(window),'OverlapLength',window/2,'FFTLength',nfft);
figure;
imagesc(t, f, 20*log10((abs(ss(1024:end,:)))));xlabel('Samples'); ylabel('Freqency');
title('Short-time Fourier transform graph drawn using stft');
colorbar;

Insert picture description here

Reference: https://cloud.tencent.com/developer/article/1652276 The usage of short-time Fourier transform spectrogram and stft in Matlab-Cloud + Community-Tencent Cloud