Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The principle of FFT Spectrum Analysis and the realization of python

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "the principle of FFT spectrum analysis and the implementation of python". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Click "brain-computer Interface Community" above to follow us.

Frequency spectrum analysis

The following is a set of common quantities used to describe and interpret signal properties (a common form of matlab, as is the common form in python):

X: sampled data

N=length (x): number of samples

Fs: sampling frequency (number of samples per unit of time or space) (commonly used: Hertz Hz)

Dt=1/fs: the time or space increment of each sample (if it is a time increment, it is also called: sampling interval or sampling step size, commonly used units: s)

T = (0:n-1) / fs: the time or space range of the data

Y=fft (x): discrete Fourier transform (DFT) of data

Abs (y): amplitude of DFT

(abs (y). ^ 2) / n: the power of DFT

Fs/n: frequency increment

F = (0:n-1) * (fs/n): frequency range

Fs/2: Nyquist frequency (midpoint of the frequency range)

Spectrum analysis is a technique that decomposes complex noise signals into simpler signals. Signals in the real world may be made up of a variety of simple signals. The way to find out the information of a signal at different frequencies (which may be amplitude, power, intensity or phase, etc.) is spectrum analysis.

Sampling theorem: the sampling frequency should be more than twice the signal frequency.

The complex FFT results of N sampling points were obtained after FFT transformation.

Suppose the sampling frequency is Fs and the number of sampling points is N. Then the result of the FFT operation is N complex numbers (or N points), each complex corresponding to a frequency value and the amplitude and phase of the frequency signal.

The frequency of the first point is 0Hz (that is, DC component), and the next point of the last point N corresponds to the sampling frequency Fs. The signal frequency represented by any of the sampling points n:

This shows that the frequency of the signal obtained by spectrum analysis is the largest (Nmur1) * Fs/N, and the resolving power of the frequency is Fs/N. Sampling frequency and sampling time restrict the upper limit of signal frequency which can be analyzed by FFT operation, and also limit the resolution of signal frequency obtained by analysis.

The specific quantitative relationship between the modulus value of each complex number and the amplitude characteristic of the frequency value corresponding to the corresponding point is as follows:

Suppose the signal is superimposed by the original signals of the following periods:

Then, the modulus of the first point obtained after FFT analysis is N times that of A1, and only when the corresponding frequency of the FFT result point is ω 2, ω 3, the modulus is obviously magnified, and at other frequency points, the modulus is close to 0. In the points where the modulus is obviously magnified, the modulus of the point except the first point is 2 times of the amplitude of the corresponding signal.

The phase of each complex number is the phase of the signal at this frequency value: φ 2, φ 3.

FFT results are symmetrical, and usually we only use the results of the first half, that is, results that are less than half the sampling frequency. At the same time, only the signal frequency with a certain amplitude and less than half of the sampling frequency is the real signal frequency.

The following is illustrated by the case of python

Case 1

Import numpy as npimport pylab as plimport math# sampling frequency fs=1048# sampling step t = [xmax 1048.0 for x in range (1048)] "the designed sampling value assumes that the signal y is superimposed by four periodic signals. "" y = [3. 0 * np.cos (2. 0 * np.pi * 50 * t0-np.pi * 30 np.pi 180) + 1. 5 * np.cos (2. 0 * np.pi * 75 * t0 + np.pi * 90 np.pi 180) + 1. 0 * np.cos (2. 0 * np.pi * 150 * t0 + np.pi * 120 max 180) + 2. 0 * np.cos (2. 0 * np.pi * 220) * t0 + np.pi * 30amp 180) for t0 in t] pl.plot (t Y) pl.xlabel ('time (s)') pl.title ("original signal") pl.show ()

The frequency spectrum of the above signal y is now analyzed in 0-1 second. In this case, the sampling frequency is 1048Hz, that is, the number of sampling points per unit time is 1048 "" # sampling points N=len (t) # sampling frequency fs=1048.0# resolution df = fs/ (NMur1) # build frequency array f = [df*n for n in range (0) N)] Y = np.fft.fft (y) * 2CMN # * 2CMN reflects the relationship between the result of FFT transform and the amplitude of the actual signal absY = [np.abs (x) for x in Y] # find the modulus of Fourier transform result pl.plot (freq (Hz) pl.xlabel ('freq (Hz)') pl.title ("fft") pl.show ()

Case 2

From scipy.fftpack import fft, fftshift, ifftfrom scipy.fftpack import fftfreqimport numpy as npimport matplotlib.pyplot as plt%matplotlib inline "" tweets: sampling period t_start: start time t_end: end time "" tweets = 0.01t_start = 0.5t_end = 5t = np.arange (t_start, t_end, tweets) f0 = 5f1 = 2 charting plt.figure (figsize= (10) 12)) # construct the original signal sequence y = 1.5*np.sin (2*np.pi*f0*t) + 3*np.sin (2*np.pi*20*t) + np.random.randn (t.size) ax=plt.subplot (511) ax.set_title ('original signal') plt.tight_layout () plt.plot (y) "" FFT (Fast Fourier Transformation) Fast Fourier transform "" Y = fft (y) ax=plt.subplot ( 512) ax.set_title ('fft transform') plt.plot (np.abs (Y)) "Y = fftshift (X) by moving the zero frequency component to the center of the array Rearrange the Fourier transform X. Shift_Y = fftshift (Y) ax=plt.subplot (513) ax.set_title ('shift fft transform') plt.plot (np.abs (shift_Y)) "" get the positive frequency part "pos_Y_from_fft = Y [: Y.size//2] ax=plt.subplot (514) ax.set_title (' fft transform') plt.tight_layout () plt.plot (np.abs (pos_Y_from_fft))"directly intercept shift fft The first half of the result pos_Y_from_shift = shift_ Y [shift _ Y.size//2:] ax=plt.subplot (515) ax.set_title ('shift fft cut') plt.plot (np.abs (pos_Y_from_shift)) plt.show ()

As we all know, the fast algorithm of Fourier transform FFT can be used to analyze the frequency domain characteristics of signals. However, FFT can only be used to analyze stationary signals. For non-stationary signals, short-time Fourier transform (STFT) is needed.

For non-stationary signals, the strategy of short-time Fourier transform is to add windows on the signals, usually hamming windows, of course, it can also be other types of window functions. The windowed signals are divided into a group of short-length subsequences, which can be approximately regarded as stationary sequences and can be analyzed by Fourier transform, which is the essence of short-time Fourier transform.

There are generally two ideas for EEG analysis with STFT. The first is to use STFT to separate the bands of EEG signals, so as to obtain the energy of each band as features (alpha, beta, theta, gamma, deta). The second is to use STFT to calculate the power spectral density as a feature, and the power spectral density (PSD) feature can be calculated for the whole signal subsequence or for a specific band in the subsequence. Of these two ways of thinking, the second one is widely used, which is explained below.

The function for STFT in matlab is spectrogram, and the following format is used to calculate the power spectral density (PSD):

] = spectrogram (x moment, windowbook, noverlapre, nfftJournal fs)

Where S is the short-time Fourier transform of the input signal x, F is the frequency vector, T is the time vector, P is the power spectral density matrix, x is the input signal, window is the time window, noverlap is the number of points of overlap, if 0 is the number of points without overlap,nfft as DFT, and fs is the sampling frequency. Among them, the dimension of F vector is the same as the number of rows of P, so the PSD of specific band can be selected according to F vector. The bands of alpha, beta, theta, gamma and deta can be divided into several narrow bands to extract narrowband PSD.

This is the end of the introduction of "the principle of FFT Spectrum Analysis and the implementation of python". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report