Spectrum scale setting problem after signal Fourier transform - with Matlab code

I. Overview

The frequency spectrum is obtained after the time-domain signal is transformed by FFT, and the correct frequency scale must be set when drawing the spectrum diagram, so that the correct result can be obtained from the diagram.

Two, example analysis

There is a cosine signal, the signal frequency is 30Hz, the sampling frequency is 128Hz, and the signal length is 128. The original signal is shown in the figure below: The
Matlab code is as follows:

fs=128;                         % 采样频率
N=128;                          % 信号长度
t=(0:N-1)/fs;                   % 时间序列
y=cos(2*pi*30*t);               % 余弦信号
figure,plot(y,'r'); xlabel('样点'); ylabel('幅值');

insert image description here

Figure 1 Original signal

If you do not process the spectrum scale, but directly plot the spectrum, then the obtained spectrum value will be deviated, as shown in the figure below: It can be seen that the signal spectrum is 30Hz, but the actually obtained spectrum is 31Hz. The reason for this error It is a setting error of the frequency scale.

y=fft(y,N);                     % FFT
figure,stem(abs(y),'r');title('频谱')

insert image description here

Figure 2 Spectrum diagram

3. Spectrum scale setting method

The signal length is N, the sampling frequency is fs, the frequency of the signal after DFT (FFT) is between -fs/2 and fs/2, and the frequency interval between spectral lines is:

Δ f = f s N = 1 N T s \Delta f=\frac{ { {f}_{s}}}{N}=\frac{1}{N{ {T}_{s}}} f _=Nfs=NTs1

Where: T s { {T}_{s}}Tsis the sampling period. The frequency scale starts from 0, and the maximum frequency is fs 2 \frac{ { {f}_{s}}}{2}2fs. Therefore, the simple setting of the spectrum scale is as follows:

f r e q = ( 0 : N − 1 ) × f s N freq=(0:N-1)\times \frac{ { {f}_{s}}}{N} freq=(0:N1)×Nfs

The given frequency scale is 0, △f,..., fs 2 \frac{ { {f}_{s}}}{2}2fs, fs-△f, but actually there is no larger than fs 2 \frac{ { {f}_{s}}}{2}2fsThe frequency component of is greater than fs 2 \frac{ { {f}_{s}}}{2}2fsThe frequency components of are actually negative frequency components. As shown in Figure 3.

To give the full range of components from negative to positive frequencies, the frequency scale should be

f r e q = ( 0 : N − 1 ) × f s N − f s 2 freq=(0:N-1)\times \frac{ { {f}_{s}}}{N}-\frac{ { {f}_{s}}}{2} freq=(0:N1)×Nfs2fs

Determine the volatility function: − fs 2 − fs 2 + Δ f , ⋅ ⋅ ⋅ , 0 , Δ f , ⋅ ⋅ ⋅ , fs 2 − Δ f -\frac{ { {f } _{s}}} {2}-\frac{ { {f}_{s}}}{2}+\Delta f,\centerdot \centerdot \centerdot ,0,\Delta f,\centerdot \centerdot \centerdot ,\frac{ { { f}_{s}}}{2}-\Delta f2fs2fs+f , _,0,f , _,2fsf _

insert image description here

Figure 3 Comparison of positive and negative frequencies

3.1 Positive and negative spectrum scale settings

According to the above theoretical settings, the corresponding matlab code is as follows:

%% 频谱显示1(正负频谱都显示)
freq0=(0:N-1)*fs/N;              % 频率间隔
figure,plot(freq0,abs(y),'r')    % 频谱

insert image description here

Figure 4 Spectrum scale setting (positive and negative frequencies are displayed simultaneously)

3.2 Positive Spectrum Scale Setting

It is also possible to display only positive frequencies through the above formula: the corresponding Matlab code is as follows:

%% 频谱显示2(正频谱)
freq=(0:N/2)*fs/N;              % 设置正频率刻度 
figure,plot(freq,abs(y(1:N/2+1)),'r');title('只有正频率刻度')
figure,stem(freq,abs(y(1:N/2+1)),'r');title(' 只有正频率刻度')

insert image description here

Figure 5 Spectrum scale setting (positive frequency)

3.3 Set the frequency scale through the linspace function (both positive and negative frequencies are displayed)

You can also set the frequency scale through the linspace function, and the corresponding Matlab code is as follows:

freq3=linspace(0,fs,N+1)-fs/2;     % linspace 函数设置正负频率
freq3=freq3(1:N);
figure,plot(freq3,abs(y),'r')    % 频谱

insert image description here

Figure 6 Spectrum scale setting (positive and negative frequencies are displayed simultaneously)

3.4 Set the frequency scale (positive frequency) through the linspace function

Or set the frequency scale to display positive frequencies through the linspace function

%% 通过linspace函数设置频率刻度(正频率)
freq4=linspace(0,fs/2,N/2+1);     % linspace 函数设置正负频率
figure,stem(freq4,abs(y(1:N/2+1)),'r');title('正频率刻度')

insert image description here

Figure 7 Spectrum scale setting (positive frequency)

4. Matlab program acquisition and verification

Code is also available at the following link:

Frequency scale settings in spectrogram

Targeted verification experiments can be carried out , please private message the blogger.

Guess you like

Origin blog.csdn.net/m0_70745318/article/details/128225108