matlab taylorwin(n,nbar,sll)加窗函数 c/c++实现代码
时间: 2024-05-02 15:18:32 浏览: 83
阵列天线泰勒综合法降低副瓣电平的matlab源代码
MATLAB代码:
```matlab
function w = taylorwin(n, nbar, sll)
% TAYLORWIN returns a Taylor window of length n with sidelobe level sll
% nbar is the width of mainlobe
% Usage: w = taylorwin(n, nbar, sll)
%
% Ref: J. F. Kaiser, "Nonrecursive digital filter design using I0-sinh window function," Proc. IEEE Int. Symp. Circuits and Systems, pp. 20-23, Apr. 1974.
%
% Written by: Z. Shi, 2019/10/15
% Check inputs
if nargin < 3
sll = 30;
end
if nargin < 2
nbar = ceil(n/4);
end
if nargin < 1
error('Not enough input arguments');
end
if mod(n, 2) == 0
error('Window length must be odd');
end
% Normalize nbar
nbar = nbar/(n-1);
% Calculate beta parameter
beta = pi*sll/20;
k = 0:n-1;
a = nbar^2 - (k-n/2).^2;
b = sqrt(a)./nbar;
beta2 = beta^2;
temp = sqrt(1 - beta2*a)/beta;
w = sin(beta * b)./b .* temp;
% Set DC to 1
w((n+1)/2) = 1;
end
```
C/C++代码:
```c++
#include <iostream>
#include <cmath>
using namespace std;
double bessi0(double x)
{
double ax,ans,a;
double y;
if ((ax=fabs(x)) < 3.75) {
y=x/3.75;
y*=y;
ans=1.0+y*(3.5156229+y*(3.0899424+y*(1.2067492
+y*(0.2659732+y*(0.360768e-1+y*0.45813e-2)))));
} else {
y=3.75/ax;
ans=(exp(ax)/sqrt(ax))*(0.39894228+y*(0.1328592e-1
+y*(0.225319e-2+y*(-0.157565e-2+y*(0.916281e-2
+y*(-0.2057706e-1+y*(0.2635537e-1+y*(-0.1647633e-1
+y*0.392377e-2))))))));
}
return ans;
}
double taylorwin(double *w, int n, int nbar, double sll)
{
// Check inputs
if (n%2 == 0)
{
cout << "Window length must be odd." << endl;
return 0;
}
// Normalize nbar
double nbar_norm = (double)nbar/(double)(n-1);
// Calculate beta parameter
double beta = M_PI*sll/20.0;
for (int i = 0; i < n; i++)
{
double k = i - (n-1)/2.0;
double a = nbar_norm*nbar_norm - k*k;
double b = sqrt(a)/nbar_norm;
double beta2 = beta*beta;
double temp = sqrt(1 - beta2*a)/beta;
double bes = bessi0(beta*b);
w[i] = bes*temp;
}
// Set DC to 1
w[(n-1)/2] = 1;
return 1;
}
int main()
{
int n = 101;
int nbar = 20;
double sll = 30;
double w[n] = {0};
taylorwin(w, n, nbar, sll);
for (int i = 0; i < n; i++)
{
printf("%f\n", w[i]);
}
return 0;
}
```
阅读全文