matlab taylorwin(n,nbar,sll)加窗函数 c/c++实现代码
时间: 2024-05-12 07:19:44 浏览: 190
以下是MATLAB和C/C++语言的taylorwin(n,nbar,sll)加窗函数的实现代码。
MATLAB代码:
```matlab
function w = taylorwin(n, nbar, sll)
% TAYLORWIN Returns a Taylor window of length n, with nbar sidelobe
% attenuation (in dB) and sll minimum stopband attenuation (in dB).
% The window is normalized to have a peak value of 1.
%
% Usage: w = taylorwin(n, nbar, sll)
%
% Arguments:
% n - the length of the window
% nbar - the desired sidelobe attenuation (in dB)
% sll - the minimum desired stopband attenuation (in dB)
%
% Returns:
% w - the Taylor window vector of length n
%
% Reference:
% Harris, F. J. (1978). On the use of windows for harmonic analysis with
% the discrete Fourier transform. Proceedings of the IEEE, 66(1), 51-83.
if n < 2
error('Window length must be greater than 1')
end
if nbar < 0
error('Sidelobe attenuation must be non-negative')
end
if sll < 0
error('Minimum stopband attenuation must be non-negative')
end
alpha = (n-1)/2;
w = zeros(n,1);
% Compute the weighting factor k
k = -20*log10(sqrt(10^(-nbar/10) - 10^(-sll/10))) / alpha;
% Compute the Taylor coefficients
tay = zeros(1,n);
tay(1) = 1;
for i = 2:n
tay(i) = tay(i-1) * (k^2 - ((i-1)/alpha)^2);
end
% Compute the window
for i = 1:n
w(i) = tay(i) / tay(1);
end
% Normalize the window to have a peak value of 1
w = w / max(w);
```
C/C++代码:
```c
#include <math.h>
void taylorwin(int n, int nbar, int sll, double *w)
{
int i;
double alpha = (n-1) / 2.0;
double k;
double tay[n];
double maxval = 0.0;
if (n < 2) {
return;
}
if (nbar < 0) {
return;
}
if (sll < 0) {
return;
}
// Compute the weighting factor k
k = -20*log10(sqrt(pow(10,-nbar/10) - pow(10,-sll/10))) / alpha;
// Compute the Taylor coefficients
tay[0] = 1;
for (i=1; i<n; i++) {
tay[i] = tay[i-1] * (pow(k,2) - pow((i-1)/alpha,2));
}
// Compute the window
for (i=0; i<n; i++) {
w[i] = tay[i] / tay[0];
if (w[i] > maxval) {
maxval = w[i];
}
}
// Normalize the window to have a peak value of 1
for (i=0; i<n; i++) {
w[i] = w[i] / maxval;
}
}
```
阅读全文