当前位置:网站首页>Spectrum analysis of ADC sampling sequence based on stm32
Spectrum analysis of ADC sampling sequence based on stm32
2022-07-05 22:37:00 【Xiao Qiu HUST】
This article mainly introduces the right ADC The collected digital sequence FFT Spectrum analysis .
Determine sampling rate
In addition to complying with Nyquist's sampling law, some problems need to be considered in determining the sampling rate . In a digital system , We can only do some finite discrete operations , For finite sequences , We can't do it DTFT, Can only do DFT. This requires Treat a finite sequence as a periodic sequence .
Normalized angular frequency
The known sampling rate is f s f_s fs, Then a frequency is f 0 f_0 f0( f 0 < f s / 2 f_0<f_s/2 f0<fs/2) After the ideal cosine signal of is sampled, the sequence should be :
x [ n ] = A cos ( 2 π f 0 ⋅ n T 0 ) = A cos ( 2 π f 0 ⋅ n f s ) n ∈ Z x[n] = A\cos \left( {2\pi {f_0} \cdot n{T_0} } \right) = A\cos \left( {2\pi {f_0} \cdot {n \over { {f_s} } } } \right)\;\;n \in {\rm{Z} } x[n]=Acos(2πf0⋅nT0)=Acos(2πf0⋅fsn)n∈Z
As can be seen from the formula above , The sequence we actually get is independent of the specific sampling rate or signal frequency , But by their ratio f 0 / f s f_0/f_s f0/fs Decisive , It's like signal frequency f 0 f_0 f0 Sampling frequency f s f_s fs Normalized .
When the signal is continuous , The frequency of the signal f f f It can be any real number . And in this discrete case , f 0 / f s f_0/f_s f0/fs The frequency has been limited to 0~1 Between , The corresponding angular frequency is 0~ 2 π 2\pi 2π.
Combined with the periodicity of the spectrum, we can know π \pi π~ 2 π 2\pi 2π Spectrum and − π -\pi −π~ 0 0 0 The spectrum of is the same .
For a real sequence ,0~ π \pi π The spectrum of corresponds to the frequency 0~ f s / 2 f_s/2 fs/2 Complex exponential sequence of , And the rest of the π \pi π~ 2 π 2\pi 2π The spectrum of is the complex conjugate of the former . The real sequence is formed by the superposition of two conjugate complex exponential sequences . According to this characteristic , In computing a real sequence DFT when , It's only half the length , Because the other half just takes complex conjugation .
Finite length sequence
The previous analysis is all in “ Ideal ” Under the condition of ,“ Ideal ” It means that the cosine signal must always exist , From the beginning of the birth of the universe to the distant future, it always exists . Such a signal is not available in real life , The actual signal always has a beginning and an end .
Rectangular window
Directly intercept several periodic points from the ideal sequence to form a finite length sequence , So the ideal sequence is multiplied by a rectangular window in the time domain , That is, their convolution in the frequency domain . The frequency domain representation of the ideal cosine sequence has been analyzed above , All that remains is to focus on the spectrum of the window function .
Calculate the rectangular window DTFT, Direct use DTFT Analytical formula of :
X ( e j ω ) = ∑ n = 0 N − 1 e j ω n = 1 − e j ω N 1 − e j ω X\left( { {e^{j\omega } } } \right) = \sum\nolimits_{n = 0}^{N - 1} { {e^{j\omega n} } } = { {1 - {e^{j\omega N} } } \over {1 - {e^{j\omega } } } } X(ejω)=∑n=0N−1ejωn=1−ejω1−ejωN
Then we can use the following formula to further simplify ,
1 − e j 2 θ = 1 − ( cos ( 2 θ ) + j sin ( 2 θ ) ) = − j 2 sin ( θ ) e j θ 1 - {e^{j2\theta } } = 1 - \left( {\cos \left( {2\theta } \right) + j\sin \left( {2\theta } \right)} \right) = - j2\sin \left( \theta \right){e^{j\theta } } 1−ej2θ=1−(cos(2θ)+jsin(2θ))=−j2sin(θ)ejθ
Finally get :
X ( e j ω ) = e − j ω ( N − 1 ) / 2 sin ( ω N / 2 ) sin ( ω / 2 ) X\left( { {e^{j\omega } } } \right) = {e^{ - j\omega (N - 1)/2} }{ {\sin \left( {\omega N/2} \right)} \over {\sin \left( {\omega /2} \right)} } X(ejω)=e−jω(N−1)/2sin(ω/2)sin(ωN/2)
stay ω = 0 \omega=0 ω=0 The location of , X ( e j ω ) = 1 X\left( { {e^{j\omega } } } \right) = 1 X(ejω)=1, And in the ω \omega ω yes 2 π / N 2\pi/N 2π/N The integral times of , X ( e j ω ) = 0 X\left( { {e^{j\omega } } } \right)=0 X(ejω)=0. So when the period of the signal is exactly N N N When , There will be no spectrum leakage ( Please supplement the convolution process by yourself ).
Rising cosine window (Hann window )
Rising cosine window is also called Hann window , It can be expressed as :
w [ n ] = 1 2 ( 1 − cos ( 2 π n N ) ) , n ∈ [ 0 , N − 1 ] w[n] = {1 \over 2}\left( {1 - \cos \left( {2\pi {n \over N} } \right)} \right),\;\;\;\;n \in \left[ {0,N - 1} \right] w[n]=21(1−cos(2πNn)),n∈[0,N−1]
Calculate its DTFT:
X ( e j ω ) = 1 2 ∑ n = 0 N − 1 ( 1 − cos ( 2 π n / N ) ) e − j ω n X\left( { {e^{j\omega } } } \right) = {1 \over 2}\sum\nolimits_{n = 0}^{N - 1} {\left( {1 - \cos \left( {2\pi n/N} \right)} \right){e^{ - j\omega n} } } X(ejω)=21∑n=0N−1(1−cos(2πn/N))e−jωn
I can't find the solution explicitly , But you can substitute some special values to observe . When ω = 0 \omega=0 ω=0 when , X ( e j ω ) = 1 / 2 X\left( { {e^{j\omega } } } \right) = 1/2 X(ejω)=1/2, And when ω = ± 2 π / N \omega=\pm2\pi/N ω=±2π/N when , X ( e j ω ) = 1 / 4 X\left( { {e^{j\omega } } } \right) = 1/4 X(ejω)=1/4.
X ( e j ω ) X\left( { {e^{j\omega } } } \right) X(ejω) The result of convolution with the spectrum of ideal sequence will show spectrum leakage . For single frequency signals , Two peaks will be generated on both sides of the original single peak 1 / 2 1/2 1/2 Peak amplitude peak , At the same time, the peak amplitude also attenuates to the original 1 / 2 1/2 1/2.
Although it is called “ Spectrum leakage ”, But it doesn't really leak out , But by convolution . Therefore, in general, adding up the leaked spectrum directly is only an approximation .
Different window functions have different spectral characteristics , The side lobe of the rectangular window is relatively high , Once the spectrum leakage occurs, it will introduce a large error to the amplitude measurement of a specific frequency , But its frequency resolution is also the highest .Hann Although there will be spectrum leakage in the window , But its side lobe decays fast .
Sampling rate and sequence length
DFT The minimum frequency that can be resolved by the obtained spectrum is called frequency resolution ,N Point sequence to do DFT Can get N Spectrum of points , Therefore, the frequency resolution is f s / N f_s/N fs/N. It is best to choose the appropriate sampling rate and sequence length so that the frequency of the signal to be measured is exactly an integral multiple of the frequency resolution . If the frequency of the signal to be measured is exactly an integral multiple of the frequency resolution , Then you can use rectangular windows directly ; conversely , If the frequency of the signal to be measured is not an integral multiple of frequency resolution , Then you need to add windows according to your needs .
Analog front end
In determining ADC After the sampling rate of ,ADC The cut-off frequency of the previous anti aliasing filter should also be set at f s / 2 f_s/2 fs/2 within . But it should also be greater than the frequency of the signal you care about .
This oversampling method is generally used to sample some low-frequency signals ,ADC The previous anti aliasing filter is a low-pass filter , The sampling rate is greater than twice the maximum frequency of the signal . But the sampling theorem tells us that as long as the sampling rate is greater than Signal bandwidth The sampled signal can be recovered by twice . So there is also a bandpass system ,ADC The former is a bandpass filter , Using the characteristics of spectrum periodic extension of digital sequence , Use low-frequency sampling rate to collect high-frequency signals .
Simulated signal
stay ADC It hasn't been adjusted yet , Or sometimes I want to generate a single frequency simulation sequence by myself , You can write directly at the sampling rate f s f_s fs Next , frequency f 0 f_0 f0 Cosine sequence of :
x [ n ] = cos ( 2 π f 0 f s n ) x[n] = \cos \left( {2\pi { { {f_0} } \over { {f_s} } }n} \right) x[n]=cos(2πfsf0n)
DSP Library function
In the previous one article in , I mentioned using CubeMX After project generation , stay Drivers/CMSIS/Lib It's in the catalog DSP The library files , So I don't need to import the library from the outside DSP The library .
But these two library files are a little different , I am using CFFT I found that only the library files imported from the outside can be calculated correctly . If you use the existing library file , The program is in link Then it seems that something strange has covered the entry of the reset address , Come up here hardfault. Here, so it's better to check this again DSP library .
CMSIS Of DSP The library provides two kinds of FFT Function of , The set of real Numbers FFT And the plural FFT, Both have their own characteristics . Can be used to calculate by ADC Of the sampled sequence FFT.
The set of real Numbers FFT
The set of real Numbers FFT Fast calculation , It's our first choice ! It can handle a length of 32, 64, 128, 256, 512, 1024, 2048, 4096 The real number FFT.
The input sequence is a continuous real number , I thought the input was also to be plural FFT equally , The real part and the imaginary part are stored at intervals . But in fact, what it needs is continuous real numbers .
Not the same address operation , Input and output need to be stored in different places . The output sequence is a complex sequence , The length is half of the input sequence , Because the results of the other half and the first half are conjugate , There is no need to double count .
Because the complex number is stored in real part and imaginary part at intervals , A complex number takes up twice the storage space of a real number . So the actual physical space occupation of input and output is the same .
For example 1024 Dot floating point RFFT, The input is a piece of length 1024 The first address of the address space of floating-point numbers , The output also needs a space of the same size , But the storage will be 512 Plural .
Output storage is not all plural , A sequence of real numbers x [ n ] x[n] x[n] do FFT And then get X [ k ] X[k] X[k], X [ 0 ] X[0] X[0] It must be a real number , X [ N / 2 ] X[N/2] X[N/2] It must also be a real number , These two real numbers exist together in the position of the first complex number .
Sample code :
arm_rfft_fast_instance_f32 xInstFFT;
arm_rfft_fast_init_f32(&xInstFFT, 1024);
arm_rfft_fast_f32(&xInstFFT, pSrc, pDst, 0);
Calling RFFT Before , You need to initialize an instance first , It mainly sets the lookup table of rotation factor .RFFT All the time bit reverse, In this way, the input and output are in the order we are used to , and CFFT Yes, you can choose whether to bit reverse.arm_rfft_fast_f32 The last parameter in is choose to FFT still IFFT.
The plural FFT
#include "arm_const_structs.h"
arm_cfft_f32(arm_cfft_sR_f32_len1024, pData, 0, 1);
The plural FFT The data required to be input is stored alternately in real part and imaginary part , And it is the same address operation , The output results will overwrite the input data . Compared with RFFT, It doesn't need to be initialized , It only needs include arm_const_structs.h This header file , You can find ready-made examples .
This header file is in CMSIS Of DSP/Include Under the path , Although we can't use what is provided here DSP The library files , But the header file can still be used . The penultimate parameter is selection FFT still IFFT; The last parameter is whether bit reverse.
Hann window
Hann The coefficient of the window can be used Matlab Calculation , These coefficients can be stored as constants , It is convenient to call directly when adding windows .
open Matlab Window designer , choice Hann window , Then select “ periodic ”. Save the coefficients to the workspace , Then find a way to write C/C++ Just go to the source file of .
function [w] = genHann()
w = hann(1024, 'periodic');
end
I use Matlab Coder Turn the above function into C Code , But it's also troublesome , It's better to copy it directly from the workspace and write it to the source file .
边栏推荐
- Distributed resource management and task scheduling framework yarn
- 【无标题】
- How can easycvr cluster deployment solve the massive video access and concurrency requirements in the project?
- [groovy] mop meta object protocol and meta programming (execute groovy methods through metamethod invoke)
- Nacos 的安装与服务的注册
- Leetcode simple question check whether all characters appear the same number of times
- 344. Reverse String. Sol
- Technology cloud report: how many hurdles does the computing power network need to cross?
- When the industrial Internet era is truly mature, we will look at the emergence of a series of new industrial giants
- 2022 Software Test Engineer salary increase strategy, how to reach 30K in three years
猜你喜欢

Practice: fabric user certificate revocation operation process

Postman核心功能解析-参数化和测试报告

Three "factions" in the metauniverse

Win11 runs CMD to prompt the solution of "the requested operation needs to be promoted"

Search: Future Vision (moving sword)

基于STM32的ADC采样序列频谱分析

boundary IoU 的计算方式

Arduino 测量交流电流

Metaverse Ape上线倒计时,推荐活动火爆进行

A trip to Suzhou during the Dragon Boat Festival holiday
随机推荐
Assign the output of a command to a variable [repeat] - assigning the output of a command to a variable [duplicate]
Leetcode simple question: the minimum cost of buying candy at a discount
How to quickly experience oneos
Lesson 1: serpentine matrix
抖音__ac_signature
Unity Max and min constraint adjustment
700. Search in a Binary Search Tree. Sol
Metaverse ape ape community was invited to attend the 2022 Guangdong Hong Kong Macao Great Bay metauniverse and Web3.0 theme summit to share the evolution of ape community civilization from technology
A trip to Suzhou during the Dragon Boat Festival holiday
My experience and summary of the new Zhongtai model
What if win11 is missing a DLL file? Win11 system cannot find DLL file repair method
航海日答题小程序之航海知识竞赛初赛
[error record] groovy function parameter dynamic type error (guess: groovy.lang.missingmethodexception: no signature of method)
Draw a red lantern with MATLAB
Cobaltstrike builds an intranet tunnel
Postman core function analysis - parameterization and test report
【无标题】
2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth
Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
Google Maps case