Matlab implements FFT transformation

Matlab implements FFT transformation


In signal processing, Fast Fourier Transform (FFT) is a very common frequency domain analysis method. This article will introduce how to use Matlab to implement FFT transformation, and demonstrate the actual output results through Matlab code.

principle

FFT is a fast algorithm for computing the Discrete Fourier Transform (DFT). DFT converts a signal in the time domain to a signal in the frequency domain, which can be expressed by the following formula:

X k = ∑ n = 0 N − 1 x n e − i 2 π k n / N X_k=\sum_{n=0}^{N-1}x_n e^{-i2\pi kn/N} Xk=n=0N1xnei2πkn/N

Among them, xn x_nxnis the signal sequence in the time domain, X k X_kXkis the signal sequence in the frequency domain, kkk is the frequency number (0 ≤ k < N 0\leq k < N0k<N), N N N is the signal length.

The FFT algorithm reduces the computational complexity of the DFT algorithm from O ( N 2 ) O(N^2) through a divide-and-conquer strategyO ( N2 )Reduced toO( N log 2 N ) O(Nlog_2N)O ( Nl o g2N ) , thus achieving the purpose of quickly calculating DFT on a computer.

accomplish

Manual verification

Here, we will give a simple example to illustrate how to use Matlab for FFT transformation. We first generate a simple array:

x = [1, 2, 3, 4, 5, 6, 7, 8];

Next, we use Matlab's built-in fft function to perform FFT transformation on this array:

X = fft(x);

This operation returns an array of complex numbers with the same length as the input array. We can print out this array using Matlab's disp function:

disp(X);

The output of this example is as follows:

   36.0000 + 0.0000i
   -4.0000 + 9.6569i
   -4.0000 + 4.0000i
   -4.0000 + 1.6569i
   -4.0000 + 0.0000i
   -4.0000 - 1.6569i
   -4.0000 - 4.0000i
   -4.0000 - 9.6569i

As you can see, the output result is an array of complex numbers with a length of 8.

In order to verify the correctness of the FFT, we can manually calculate the FFT result of this input array, and then compare the result with the result calculated by Matlab. The calculation process of the FFT algorithm can be expressed by the following formula:

X k = ∑ n = 0 N − 1 x n e − i 2 π k n / N X_k=\sum_{n=0}^{N-1}x_n e^{-i2\pi kn/N} Xk=n=0N1xnei2πkn/N

Among them, xn x_nxnis the signal sequence in the time domain, X k X_kXkis the signal sequence in the frequency domain, kkk is the frequency number (0 ≤ k < N 0\leq k < N0k<N), N N N is the signal length.

For input array x = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] x=[1,2,3,4,5,6,7,8]x=[1,2,3,4,5,6,7,8 ] , we haveN = 8 N=8N=8 . Therefore,X 0 X_0X0The calculation formula is:

X 0 = ∑ n = 0 7 xne − i 2 π 0 n / 8 = 36 X_0 = \sum_{n=0}^{7}x_n e^{-i2\pi 0n/8}=36X0=n=07xnei2π0n/8=36

Next, we can calculate X 1 X_1X1

X 1 = ∑ n = 0 7 x n e − i 2 π 1 n / 8 = − 4 + 9.6569 i X_1 = \sum_{n=0}^{7}x_n e^{-i2\pi 1n/8}=-4+9.6569i X1=n=07xnei2π1n/8=4+9.6569 i

By analogy, we can calculate all X k X_kXk. The final result should be consistent with the result calculated by Matlab.

Simple fft transform and spectrum

The following is a simple Matlab code to implement FFT transformation:

% 生成测试信号
Fs = 1000;      % 采样频率
t = 0:1/Fs:1-1/Fs;   % 时间向量
x = 1*sin(2*pi*100*t); % 信号

% 绘制信号图
subplot(2,1,1);
plot(t,x);
title('信号');
xlabel('时间 (s)');
ylabel('幅度');

% 计算FFT
N = length(x);
X = fft(x);
f = Fs*(0:(N/2))/N;

% 绘制FFT图
subplot(2,1,2);
plot(f,abs(X(1:N/2+1)));
title('FFT');
xlabel('频率 (Hz)');
ylabel('幅度');

image-20230308202228276

In this example, we have generated a sinusoidal signal with a frequency of 100Hz. We compute the FFT using Matlab's fft function and plot the result as a magnitude spectrum. Note that when plotting the magnitude spectrum, we only plot the positive half of the frequency, because the output of the FFT algorithm is symmetric.

Find the power spectrum

The amplitude spectrum of the signal can be obtained through FFT transformation, but in order to better understand the characteristics of the signal, we usually need to obtain the power spectral density of the signal. Power spectral density describes the power distribution of a signal at different frequencies.

The method to obtain the power spectrum is to perform FFT transformation on the signal, divide the square of the amplitude at each frequency by the length of the signal, and multiply it by a coefficient to obtain the power spectral density. The specific formula is as follows:

P k = 2 ∣ X k ∣ 2 N P_k=\frac{2|X_k|^2}{N} Pk=N2∣Xk2

Among them, P k P_kPkis the frequency kkThe power spectral density of k , X k X_kXkis the frequency kkSignal amplitude for k , NNN is the signal length.

The following is a simple Matlab code to obtain the power spectrum:

% 生成测试信号
Fs = 1000;      % 采样频率
t = 0:1/Fs:1-1/Fs;   % 时间向量
x = 1*sin(2*pi*100*t); % 信号

% 绘制信号图
subplot(2,1,1);
plot(t,x);
title('信号');
xlabel('时间 (s)');
ylabel('幅度');

% 计算FFT
N = length(x);
X = fft(x);
f = Fs*(0:(N/2))/N;

% 计算功率谱
P = (2*abs(X(1:N/2+1)).^2)/N;

% 绘制功率谱图
subplot(2,1,2);
plot(f,P);
title('功率谱密度');
xlabel('频率 (Hz)');
ylabel('功率');

image-20230308201245085

Note that when we calculate the power spectrum, we use a coefficient 2 22 , this is because we are only plotting the positive half of the frequency, when in fact the power spectrum of the signal is symmetric.

in conclusion

This article introduces how to use Matlab to implement FFT transformation and calculate the power spectral density of the signal. Through FFT transformation, we can convert the signal from the time domain to the frequency domain to further understand the characteristics of the signal.

Guess you like

Origin blog.csdn.net/qq_34022877/article/details/129410817
FFT