MATLAB - FFT (Fast Fourier Transform)

Basic knowledge
FFT is the fast Fourier transform, which reduces the amount of calculation of DFT by using periodicity and reducibility. The common ones are the radix-2 algorithm extracted by time (DIT-FFT) and the radix-2 algorithm extracted by frequency (DIF-FFT).

1. Use the built-in function fft to perform fast Fourier transform
If the known sequence x = [ 4 , 3 , 2 , 6 , 7 , 8 , 9 , 0 ] x=[4,3,2,6,7,8 ,9,0]x=[4,3,2,6,7,8,9,0 ] , findX ( k ) = DFT [ x ( n ) ] X(k)=DFT[x(n)]X(k)=D FT [ x ( n ) ]
code is very simple, only two lines

x=[4,3,2,6,7,8,9,0];
xk=fft(x)

insert image description here
Generally, for MATLAB, if you want it to display the result, do not add a semicolon to the calculation part.
2. Draw the amplitude-frequency diagram of 128-point DFT.
The known signal is composed of a 15Hz sinusoidal signal with an amplitude of 0.5 and a 40Hz sinusoidal signal with an amplitude of 2. The data sampling frequency is 100Hz. Try to draw an amplitude-frequency diagram of N=128-point DFT.
Explanation about the following codes

f=(0:N-1)'*fs/N;

Among them, (0:N-1)' is a row vector obtained by generating a column vector whose length is N and whose interval is 1. fs/N refers to the frequency interval in the frequency domain.
If the N-point sequence x(n) (n=0,1,...,N-1) is obtained at the sampling frequency fs(Hz). Its FFT is also a sequence of N points, that is, X(k) (k=0,1,...,N-1), then the actual frequency value corresponding to the Kth point is: f=k*fs/
N

clc;
fs=100;
Ts=1/fs;%采样时间间隔
N=128;
n=0:N-1;
t=n*Ts;    %x不是直接关于n的函数,因为是固定的采样时间点
x=0.5*sin(2*pi*15*t)+2*sin(2*pi*40*t);

y=fft(x,N);
f=(0:N-1)'*fs/N;
stem(f,abs(y));

Running Results
insert image description here
It can also be seen that the amplitude spectrum is symmetrical about fN.
3. Noise analysis of power spectrum using FFT
It is known that the signal with measurement noise x ( t ) = sin ( 2 π f 1 t ) + sin ( 2 π f 2 t ) + 2 w ( t ) x(t)= sin(2πf1t)+sin(2πf2t)+2w(t)x(t)=sin(2πf1t)+s in ( 2 π f 2 t )+2 w ( t ) where f1=50Hz, f2=120Hz, w(t) is a random signal with mean value zero and variance 1, the sampling frequency is 1000Hz, and the number of data points N=512. Try plotting the power spectrum of a signal.
The following introduces some basic functions and knowledge:
conj(Y)
conj(Y) is a function in MATLAB, which means to take the conjugate complex number of each element in Y. If Y is a real array, returns itself. In signal processing, complex conjugate numbers are often used for frequency-domain transform processing.
Find the power
P=Y.*conj(Y)/512;
note that this is point multiplication.
In signal processing, power can be expressed as the average energy of a signal. For a discrete-time signal, its energy can be expressed as the sum of its amplitude squares, namely:
E = ∑ n = 0 N − 1 ∣ x [ n ] ∣ 2 E = \sum_{n=0}^{N-1} |x[n]|^2E=n=0N1x[n]2
where,NNN is the number of sampling points of the signal,x [ n ] x[n]x [ n ] is the signal at timennThe sampled value of n . Here∣ x [ n ] ∣ 2 |x[n]|^2x[n]2 means pairx [ n ] x[n]x [ n ] takes the modulo length squared.
If you want to calculate the average power of the signal, you can divide its total energy by the number of sampling points, namely:
P = EN = 1 N ∑ n = 0 N − 1 ∣ x [ n ] ∣ 2 P = \frac{E}{N} = \frac{1}{N} \sum_{n=0}^{N-1} |x[n]|^2P=NE=N1n=0N1x[n]2 PP
hereP represents the power of the signal.
In the code,YYY represents the discrete Fourier transform of the signal, that is, the frequency domain representation, its magnitude∣ Y ∣ |Y|| Y | represents the contribution of the signal on each frequency component. In order to calculate the power spectrum of the signal,YYY transforms its magnitude squared, that is,∣ Y ∣ 2 |Y|^2Y2 . Thanks toYYY contains the information of positive frequency and negative frequency, so it needs to be conjugated, that is, take the conjugate complex number of the negative frequency part, and then multiply it with the positive frequency part, that is, Y ⋅ conj ⁡ ( Y ) Y \cdot \operatorname{conj}(Y)Yconj ( Y ) . Finally, divide the result by the number of sampling pointsNNN , the power spectrumPPP :
P = Y ⋅ conj ⁡ ( Y ) NP = \frac{Y \cdot \operatorname{conj}(Y)}{N}P=NYconj(Y)
PP hereP is a lengthNNA vector of N representing the power of the signal at each frequency component.
randn
andn is a function in MATLAB used to generate a standard normal distribution random number with mean 0 and variance 1. For example, a standard normal distribution random matrix of size 3x3 can be generated using the following code:
A = randn(3,3);
Full code

clc;
t=0:0.001:0.6;%设置步进与时间区间
x=sin(2*pi*50*t)+sin(2*pi*120*t);%根据已知写出信号的表达式
noise=randn(1,length(t));%生成均值为零、方差为1的随机信号,也就是噪声
y=x+noise;%带有噪声的信号
subplot(121);
plot(t,y);

fs=1000;
Y=fft(y,512);%512点的FFT
P=Y.*conj(Y)/512;%求功率
f=(0:255)*fs/256%由上面的分析可知,频谱关于奈奎斯特频率对称,所以取其中一半
subplot(122);
plot(f,P(1:256))%功率随频率的变化,即功率谱图,绘制出一半

Running results:
insert image description here
4. Effect of sequence length and FFT length on signal spectrum
Known signal x ( t ) = 0.5 sin ( 2 π f 1 t ) + 2 sin ( 2 π f 2 t ) x(t)=0.5sin (2πf1t)+2sin(2πf2t)x(t)=0.5sin(2πf1t)+2 s in ( 2 π f 2 t )
Among them, f1=15Hz, f2=40Hz, and the sampling frequency is 100Hz.
Draw its amplitude spectrum under the following conditions.
With the previous foundation, here is a relatively simple
code

nlength=32;
nfft1=32;
nfft2=128;
fs=100;
Ts=1/fs;
n=0:nlength-1;
t=n*Ts;

x=0.5*sin(2*pi*15*t)+2*sin(2*pi*40*t);

y1=fft(x,nfft1);
f1=(0:31)*fs/32;
subplot(211);
Y1=abs(y1);
plot(f1(1:16),Y1(1:16));

y2=fft(x,nfft2);
f2=(0:127)*fs/128;
subplot(212);
Y2=abs(y2);
plot(f2(1:64),Y2(1:64));

Running Results
insert image description here
Result Analysis
The more sampling points, the smoother the frequency spectrum.
Note
When drawing a graph, you must analyze whether it is a spectrum graph or a certain transformation. The purpose is to distinguish who is the dependent variable and who is the independent variable.

Guess you like

Origin blog.csdn.net/m0_46155417/article/details/129448371