How to use FFT in MATLAB

Source: Internet
Author: User

I. Calling methods

X=fft ( x);
X=fft ( x, N);
x=ifft (X);
x=ifft (X,n)

When using MATLAB for spectral analysis, note:

(1) The data structure of the function FFT return valuehave symmetry。
Example: N=8;n=0:n-1;xn=[4 3 2 6 7 8 9 0]; Xk=fft (xn)

Output:

Xk =

39.0000-10.7782 + 6.2929i 0-5.0000i 4.7782-7.7071i 5.0000 4.7782 + 7.7071i 0 + 5. 0000i-10.7782-6.2929i

XK has the same number of dimensions as Xn, with a total of 8 elements.the first number of XK corresponds to the DC component, that is, the frequency value is 0.

(2) When doing FFT analysis,The magnitude of the amplitude is related to the number of FFT selection points, but does not affect the results of the analysis. The IFFT has been dealt with. To get the true magnitude of the amplitude, simply multiply the resulting transformed result by 2 divided by N.

two. Examples of FFT applications

Example 1:x=0.5*sin (2*pi*15*t) +2*sin (2*pi*40*t). The sampling frequency is fs=100hz, and the n=128 and 1024-point amplitude-frequency graphs are plotted respectively.
clf;fs=100; n=128;   % sampling frequency and data points n=0:n-1;t=n/fs;   % Time Series X=0.5*sin (2*pi*15*t) +2*sin (2*pi*40*t); % Signal Y=fft (x,n);    % Fast Fourier transformation of the signal mag=abs (y);     The amplitude f=n*fs/n after Fourier transformation is obtained;    % frequency sequence subplot (2,2,1), Plot (F,MAG);   % plots amplitude xlabel (' frequency/hz ') with frequency variation, ylabel (' amplitude '), title (' n=128 '), Grid on;subplot (2,2,2), Plot (f (1:N/2), Mag (1:N/2)); % plots amplitude xlabel (' frequency/hz ') with frequency variation before Nyquist frequency, ylabel (' amplitude '), title (' n=128 '), grid on;% processing fs=100 for signal sampling data of 1024 points; N=1024;n=0:n-1;t=n/fs;x=0.5*sin (2*pi*15*t) +2*sin (2*pi*40*t); % Signal Y=fft (x,n);   % Fast Fourier transformation of the signal mag=abs (y);   % the amplitude of the Fourier transform is obtained f=n*fs/n;subplot (2,2,3), Plot (F,MAG); % plots amplitude xlabel (' frequency/hz ') with frequency variation, ylabel (' amplitude '), title (' n=1024 '), Grid on;subplot (2,2,4) plot (f (1:N/2), Mag (1:N/2)); % plot amplitude xlabel (' frequency/hz ') with frequency before Nyquist frequency, ylabel (' amplitude '); title (' n=1024 '); grid on;
Results:

The

Fs=100hz,nyquist frequency is fs/2=50hz. The entire spectrum is based on the Nyquist frequency as the symmetric axis. And it can be clearly recognized that the signal contains two kinds of frequency components: 15Hz and 40Hz. So we can know the symmetry of FFT transform data. Therefore, using FFT to do spectral analysis of the signal, we only need to investigate the 0~nyquist frequency range of the FU frequency characteristics. If the sampling frequency and sampling interval are not given, the analysis is usually performed on the normalized frequency 0~1. In addition, the magnitude of the amplitude is related to the number of samples used, the amplitude of the same frequency using 128 points and 1024 points has different performance values, but in the same picture, the 40Hz and 15Hz vibration amplitude ratio of 4:1, and the true amplitude of 0.5:2 is consistent. to correspond to the true amplitude, multiply the transformed result by 2 by N.

Example 2:x=0.5*sin (2*pi*15*t) +2*sin (2*pi*40*t), fs=100hz, drawing:
(1) The number of data n=32,fft used by the sampling point nfft=32;
(2) n= 32,nfft=128;
(3) n=136,nfft=128;
(4) n=136,nfft=512.

clf;fs=100; % sampling frequency ndata=32; % data length n=32;   The data length of%fft is n=0:ndata-1;t=n/fs;   The% data corresponds to the time series X=0.5*sin (2*pi*15*t) +2*sin (2*pi*40*t);   % Time domain signal y=fft (x,n);    The Fourier transform of the% signal mag=abs (y); The amplitude f= (0:n-1) *fs/n is obtained; % true frequency subplot (2,2,1), Plot (f (1:N/2), mag (1:N/2) *2/n); % draws the amplitude xlabel (' frequency/hz ') before the Nyquist frequency, ylabel (' amplitude '); title (' Ndata=32 nfft=32 '); grid on;   ndata=32;     % data number n=128;   %fft the length of data used n=0:ndata-1;t=n/fs; % Time Series X=0.5*sin (2*pi*15*t) +2*sin (2*pi*40*t); Y=fft (x,n); Mag=abs (y); f= (0:n-1) *fs/n; % true frequency subplot (2,2,2), Plot (f (1:N/2), mag (1:N/2) *2/n); % draws the amplitude xlabel (' frequency/hz ') before the Nyquist frequency, ylabel (' amplitude '); title (' Ndata=32 nfft=128 '); grid on;   ndata=136;     % data number n=128; %fft the number of data used n=0:ndata-1;t=n/fs;   % Time Series X=0.5*sin (2*pi*15*t) +2*sin (2*pi*40*t); Y=fft (x,n); Mag=abs (y); f= (0:n-1) *fs/n; % true frequency subplot (2,2,3), Plot (f (1:N/2), mag (1:N/2) *2/n); % draws the amplitude xlabel (' frequency/hz ') before the Nyquist frequency, ylabel (' amplitude '); title (' ndata=136 nfft=128 '); grid on;    ndata=136;    % data number n=512; %fft the number of data used n=0:ndata-1;t=n/fs; % Time Series X=0.5*sin (2*pi*15*t) +2*sin (2*pi*40*t); Y=fft (x,n); Mag=abs (y); f= (0:N-1) *fs/n; % true frequency subplot (2,2,4), Plot (f (1:N/2), mag (1:N/2) *2/n); % draws the amplitude xlabel (' frequency/hz ') before the Nyquist frequency, ylabel (' amplitude '); title (' ndata=136 nfft=512 '); grid on;
Results:



Conclusion:
(1) When the number of data and the number of FFT data is 32 o'clock, frequency resolution is low, but not due to the addition of 0 of other frequency components.
(2) because the signal in the time domain plus 0, resulting in a lot of other components in the amplitude spectrum, which is caused by the addition of 0. Its amplitude is significantly reduced due to the addition of multiple zeros.
(3) The FFT program truncates the data, when the resolution is high.
(4) Also at the end of the data to fill 0, but because the number of data containing the signal is enough, the FFT amplitude spectrum is basically unaffected.

In the spectrum analysis of the signal, the data sample should have sufficient length, the data points used in the general FFT program are the same as those of the original signal data, so the spectral graph has high quality, which can reduce the effect of 0 or truncation.

Example 3:x=cos (2*pi*0.24*n) +cos (2*pi*0.26*n)

The code is a little, look directly at the result:



Conclusion:

(1) The data points are too small to be able to see the detailed information about the signal spectrum;
(2) The middle of the graph is the X (n) to fill 90 zeros, the amplitude spectrum of the data is quite dense, called high-density spectrum map. But it is difficult to see the spectral composition of the signal.
(3) The effective data of the signal is very long, can clearly see the frequency component of the signal, one is 0.24Hz, one is 0.26Hz, called the high resolution spectrum.
It can be seen that the sampled data is too small and the frequency component cannot be distinguished by FFT transform. Adding 0 can increase the number of data in the spectrum, the density of the spectrum increases, but still can not distinguish the frequency components, that is, the spectral resolution is not improved. The frequency component can be distinguished only by a sufficient number of data points.



Resources:

1. "Matlab realization of digital signal processing" Wan Yongge Editor

2. Blog: http://blog.163.com/fei_lai_feng/blog/static/9289962200971751114547/


How to use FFT in MATLAB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.