C语言实现序列幅度估计的两种方法
1 MATAB实现
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 接收信号幅度估计
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all;
clear all;
clc;
%--------------参数定义
count=4; % 采样点数
y1=[5+2i,6+4i,5+6i,7+8i];
%------找到波束1,2 接收到的信号的幅度
% 法二 利用FFT
% FFT的结果取abs,得到的就是幅度谱,在幅度谱中找到信号频点对应的谱峰。
% 把谱峰的值除以序列的长度,就得到了序列的幅度。
y1_FFT=fft(y1,count); % 傅里叶变换
L=length(y1);
P1=abs(y1_FFT)/L; % 幅度谱
u1=max(P1); % 得到波束1接收到的信号的幅度值
fprintf('得到波束1接收到的信号的幅度值为%g\n',u1);
2 C语言实现
开发环境为VIVADO的SDK,不过本质一样的
2.1 mian函数
#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "math.h"
// 子函数定义
#include "amp_est.h"
#define PI 3.1415926535897931
//将相控阵天线接收到的数据放在这里面
#define count 4 //输入序列的长度,只限2的N次方 count个复数
complex array1[2*count] = {5.0,2.0, 6,4, 5.0,6.0, 7,8}; // 1+2i,3+4i,5+6i,7+8i 所有的数据按照 实部、虚部 依次放入数组中
double number[2*count]= {5.0,2.0, 6,4, 5.0,6.0, 7,8};
complex array_1[2*count] = {0};
double u1,u_1;
int main()
{
int i;
init_platform();
printf("begin \n\r");
//----------第一种方法
u1=amp_est(count,array1);// 得到序列1的幅度
printf("Amplitude of beam one is %.4f \n" , u1);
//-----------第二种方法
for (i=0; i<count; i++)
{
array_1[i].real=number[2*i];
array_1[i].img=number[2*i+1];
//printf("real is %.4f \n" , array_1[i].real);
//printf("img is %.4f \n" , array_1[i].img);
}
u_1=amp_est(count,array_1);// 得到序列1的幅度
printf("Amplitude of beam one is %.4f \n" , u_1);
cleanup_platform();
return 0;
}
2.2 amp_est.c
#include "amp_est.h"
#include "stdio.h"
#define PI 3.1415926535897931
complex *W; //输出序列的值
/*****************************
子函数
*****************************/
void add(complex ,complex ,complex *); //复数加法
void mul(complex ,complex ,complex *); //复数乘法
void sub(complex ,complex ,complex *); //复数减法
void divi(complex ,complex ,complex *); //复数除法
/*****************************
复数加法
*****************************/
void add(complex a,complex b,complex *c)
{
c->real=a.real+b.real;
c->img=a.img+b.img;
}
/*****************************
复数乘法
*****************************/
void mul(complex a,complex b,complex *c)
{
c->real=a.real*b.real - a.img*b.img;
c->img=a.real*b.img + a.img*b.real;
}
/*****************************
复数减法
*****************************/
void sub(complex a,complex b,complex *c)
{
c->real=a.real-b.real;
c->img=a.img-b.img;
}
/*****************************
复数除法
*****************************/
void divi(complex a,complex b,complex *c)
{
c->real=( a.real*b.real+a.img*b.img )/( b.real*b.real+b.img*b.img);
c->img=( a.img*b.real-a.real*b.img)/(b.real*b.real+b.img*b.img);
}
/******************************
函数功能:通过FFT估计信号的幅度
输入参数:复数序列 数组
输出参数:波束接收到的信号幅度u
******************************/
double amp_est(int count,complex array[])
{
int i=0,j=0,k=0,l=0;
complex up,down,product;
complex temp;
double t;
double u=0.0;
double sum[count];
W=(complex *)malloc(sizeof(complex) * count);
for(i=0;i<count;i++)
{
W[i].real=cos(2*PI/count*i);
W[i].img=-1*sin(2*PI/count*i);
}
for(i=0;i<count;i++)
{
k=i;j=0;
t=(log(count)/log(2));
while( (t--)>0 )
{
j=j<<1;
j|=(k & 1);
k=k>>1;
}
if(j>i)
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
for(i=0;i<log(count)/log(2) ;i++)
{
l=1<<i;
for(j=0;j<count;j+=2*l)
{
for(k=0;k<l;k++)
{
mul(array[j+k+l],W[count*k/2/l],&product);
add(array[j+k],product,&up);
sub(array[j+k],product,&down);
array[j+k]=up;
array[j+k+l]=down;
}
}
}
for(i=0;i<count;i++)
{
sum[i]=sqrt(array[i].real * array[i].real+array[i].img * array[i].img)/count;//幅度谱 P
// printf("%.4f\n",sum[i]);
}
//找幅度谱的最大值的最大值
for(i=0; i<count; i++)
{
if(sum[i]>u)
{
u = sum[i];
}
}
return u;
}
2.3 amp_est.h
#ifndef SRC_AMP_EST_H_
#define SRC_AMP_EST_H_
typedef struct//结构体
{
double real;
double img;
}complex;
double amp_est(int count,complex array[]); //估计波束1接收到的信号幅度
#endif /* SRC_AMP_EST_H_ */