"Algorithm Notes": Application and Code Implementation of Power Spectrum

1. Background:

Earlier, I took over a small project, using vibration sensors to monitor the switch of the fan.

There is no more explanation about the use of the single-chip microcomputer and the data collection of the acceleration sensor.

Data preparation: Set the vibration sampling frequency to 400Hz. The sampling points are 512 points.

According to the discrete point FFT, the frequency range that can be actually monitored (400/2=200Hz).

 

2. Expected results

From the discrete 512 data points, the vibration frequency and power are calculated.

The actual frequency F is proportional to the abscissa x of the data point:

F = x (400/512)=0.78125 x

3. Raw data points

The 512 data points are continuously collected by the sensor. They are the acceleration values ​​perpendicular to the vibration plane (Z axis). The acceleration value is used to describe the vibration force at a certain moment.

According to the configuration of the accelerometer chip range, the value of the original data point will be different.

1. As shown in the figure below: Take half of the points (256) to display. Configure the sensor range as plus or minus 16G. When the sensor is placed horizontally, the value of the Z axis represents the gravity G of the earth. The figure is around 2100.

2. Shake evenly by hand, the display is as follows:

The following figure has a total of 256 points, and the sensor outputs 400 points in 1 second, which means that the 3.5 waves in the figure actually use 256/400 = 0.64 s.

The frequency of the hand crank is 3.5/0.64 = 5.46875 Hz, which means that the hand crank is 5.5 times a second. (Shake it really fast)

3. Place it on the side of the main computer and measure the vibration of the main computer. The original waveforms observed are as follows:

Because it is placed on the side, the Z axis becomes smaller under the influence of the earth's gravity (there is about 40 left, there is no correction, but it does not affect the frequency calculation).

4. FFT Algorithm

4.1 Basic understanding

To obtain the frequency of discrete points, the first thing that comes to mind is the FFT algorithm, which is the fast Fourier transform. There are detailed theoretical explanations in advanced mathematics and integral transformation. There are also many predecessors on the Internet who have done an excellent job of sorting out. I won’t repeat it here, and give the link to the article I directly cited at the time:

https://blog.csdn.net/yga_airspace/article/details/86688278

There are encapsulated code in the article, and the description of the parameters is also very detailed.

 

4.2 Sorting out ideas

Pass 512 original data points to the FFT function to get the new changed array. The number of arrays remains unchanged, and the data changes from the time domain to the frequency domain.

4.3 Code implementation

The original author of the function here used the old format when defining the formal parameters. So don't be surprised. Remember to include the header file "math.h".

Among them, the pr[512] array stores 512 original data. After the function ends, the data in pr becomes the modulus of the transformed frequency domain data (used for spectrum display).

/*******************************************************************\
		double pr[n]	存放n个采样输入的实部,返回离散傅里叶变换的摸
		double pi[n]	存放n个采样输入的虚部
		double fr[n]	返回离散傅里叶变换的n个实部
		double fi[n]	返回离散傅里叶变换的n个虚部
		int n	采样点数
		int k	满足n=2^k
\*******************************************************************/
  void kfft(pr,pi,n,k,fr,fi)
  int n,k;
  double pr[],pi[],fr[],fi[];
  { 
		int it,m,is,i,j,nv,l0;
    double p,q,s,vr,vi,poddr,poddi;
    for (it=0; it<=n-1; it++)  //将pr[0]和pi[0]循环赋值给fr[]和fi[]
    { 
		m=it; 
		is=0;
		for(i=0; i<=k-1; i++)
        { 
			j=m/2; 
			is=2*is+(m-2*j); 
			m=j;
		}
        fr[it]=pr[is]; 
        fi[it]=pi[is];
    }
    pr[0]=1.0; 
    pi[0]=0.0;
    p=6.283185306/(1.0*n);
    pr[1]=cos(p); //将w=e^-j2pi/n用欧拉公式表示
    pi[1]=-sin(p);

    for (i=2; i<=n-1; i++)  //计算pr[]
    {  
		p=pr[i-1]*pr[1]; 
		q=pi[i-1]*pi[1];
		s=(pr[i-1]+pi[i-1])*(pr[1]+pi[1]);
		pr[i]=p-q; pi[i]=s-p-q;
    }
    for (it=0; it<=n-2; it=it+2)  
    {
		vr=fr[it];
		vi=fi[it];
		fr[it]=vr+fr[it+1]; 
		fi[it]=vi+fi[it+1];
		fr[it+1]=vr-fr[it+1]; 
		fi[it+1]=vi-fi[it+1];
    }
	m=n/2; 
	nv=2;
    for (l0=k-2; l0>=0; l0--) //蝴蝶操作
    { 
		m=m/2; 
		nv=2*nv;
        for (it=0; it<=(m-1)*nv; it=it+nv)
          for (j=0; j<=(nv/2)-1; j++)
            { 
				p=pr[m*j]*fr[it+j+nv/2];
				q=pi[m*j]*fi[it+j+nv/2];
				s=pr[m*j]+pi[m*j];
				s=s*(fr[it+j+nv/2]+fi[it+j+nv/2]);
				poddr=p-q; 
				poddi=s-p-q;
				fr[it+j+nv/2]=fr[it+j]-poddr;
				fi[it+j+nv/2]=fi[it+j]-poddi;
				fr[it+j]=fr[it+j]+poddr;
				fi[it+j]=fi[it+j]+poddi;
            }
    }
    for (i=0; i<=n-1; i++)
       { 
		  pr[i]=sqrt(fr[i]*fr[i]+fi[i]*fi[i]);  //幅值计算
       }
    return;
  }

4.4 Effect

Place the sensor on the side of the computer host to measure the frequency. The hard disk speed of the host computer is 7200 rpm, corresponding to a frequency of 120 Hz.

It can be seen that the frequency spectrum obtained by the FFT transform can extract 120 Hz vibration. But the influence of low frequency noise is very serious. This will undoubtedly increase the trouble for later data processing.

 

4.5 Advantages and disadvantages

Advantages: fast speed, calculation can be completed within 1s.

Disadvantages: The randomness changes too much, which is not conducive to data processing.

 

5. Power spectrum

5.1 How to find

To be honest, I have also seen many methods for spectrum signal processing, and compared the stability and ease of implementation of various methods. Choosing to use the power spectrum means that the power spectrum can remove the randomness in the FFT. The image of the power spectrum is more stable.

The predecessors on the Internet have given various ways to obtain the power spectrum:

(1) Direct method to obtain power spectrum (2) Correlation function method (3) Correlation AR model method (4) BURG method

You can baiDu by yourself for related knowledge.

I chose the correlation function method.

5.2 Correlation function method

First find the autocorrelation function, and then do the frequency domain transformation.

5.3 Code implementation

1. In the process of implementing the code, it was discovered that the original value needs to be preprocessed first.

Find the average of 512 original points, and subtract the average from each original value. The purpose of this is to make the original value array distributed above and below the zero line, eliminate the influence of the DC component, and focus on the change of the value.

If this operation is not performed, the overall power spectrum image data obtained will be large, and the degree of influence of frequency changes will be weak on the image. It looks like a logarithmic curve without waves.

2. Find the autocorrelation function

Here is the C language to implement the xcorr function in matlab.

int xcorr(double *corr, double *x, double *y, int iDataN, int iSyncLength)
{
	double r =0.0;
	int i=0, j=0;
	for (i = 0; i < iDataN- iSyncLength+1; i++)
	{
		r=0;
		for(j=0; j < iSyncLength; j++)
		{
			r+=x[i+j]*y[j];
			
		}
		corr[i]=r;
	}
 
	for (i = iDataN- iSyncLength+1; i < iDataN; i++)
	{
		r=0;
		for(j=0; j <iDataN- i; j++)
		{		
			r+=x[i+j]*y[j];
			
		}
		corr[i]=r;
	}
	return 0;
 
}

3. Find the power spectrum

 

void PowerSpectrum()
{
      //求自相关函数
      xcorr(fr,pr,pr,NUM,NUM);
      memcpy(pr,fr,NUM*sizeof(double));
      //快速傅里叶变换
      kfft(pr,pi,NUM,9,fr,fi);
      //对数变换
      {
        uint16_t i;
        for(i=0;i<NUM/2;i++)
        {
              pr[i]=10*log(fabs(pr[i+1]));
        }
      }
 }

5.4 Effect

1. Place the sensor on a stationary table. It can be found that the power spectrum has no obvious characteristics.

The power spectrum is as follows:

2. Put the hardware sensor on the surface of my notebook, and the hard drive speed is also 7200 rpm (frequency is 120Hz).

The power spectrum is as follows:

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/Kshine2017/article/details/102914108