赞
踩
clc; clear; close all; %% 窗函数测试 https://ww2.mathworks.cn/help/signal/ug/windows.html M = 128; %% 平顶窗 flattop % w1 = flattopwin(M,'periodic'); % w2 = win_flattop(M,'periodic'); %% 布莱克曼窗 blackman % w1= blackman(M,'periodic'); % w2 = win_blackman(M,'periodic'); %% 凯塞窗 kaiser % w1= kaiser(M,2.5); % w2 = win_kaiser(M,2.5); %% 汉明窗 hamming % w1= hamming(M); % w2 = win_hamming(M); %% 海宁窗 hann % w1= hann(M); % w2 = win_hann(M); %% 高斯窗 gausswin X % w1= gausswin(M,2.5); % w2 = win_gauss(M,2.5); %% 矩形窗 rectwin https://ww2.mathworks.cn/help/signal/ref/rectwin.html#d124e4061 % w1 = rectwin(M); % w2 = ones(M,1); %% 三角窗 triang % w1 = triang(M); % w2 = win_triang(M); %% 类三角窗 bartlett % w1 = bartlett(M-1); % w2 = win_bartlett(M-1); %% 锥形窗 tukeywin % w1 = tukeywin(M); % w2 = win_tukey(M); %% nuttallwin % w1 = nuttallwin(M,'symmetric'); % w2 = win_nuttall(M,'symmetric'); %% blackmanharris % w1 = blackmanharris(M,'symmetric'); % w2 = win_blackmanharris(M,'symmetric'); %% barthann % w1 = barthannwin(M); % w2 = win_barthann(M); %% bohmanwin % w1 = bohmanwin(M); % w2 = win_bohmanwin(M); %% parzenwin w1 = parzenwin(M); w2 = win_parzen(M); %% wvtool(w1); wvtool(w2); plot(1:M,w1,'r',1:M,w2,'g'); % fileID = fopen("xxx.bin",'w');fwrite(fileID,w1,'float');fclose(fileID); % fileID = fopen("xxx.bin");plot(fread(fileID,'float'));fclose(fileID);
w = ones(L,1);
function w = win_triang(L) %% ref triang https://ww2.mathworks.cn/help/signal/ref/triang.html w = zeros(L,1); if rem(L,2) % 奇数 for n = 1 : (L+1)/2 w(n) = 2*n/(L+1); end for n = (L+1)/2+1 : L w(n) = 2 - 2*n/(L+1); end else for n = 1 : L/2 w(n) = (2*n-1)/L; end for n = L/2 + 1 : L w(n) = 2 - (2*n-1)/L; end end end
function w = win_bartlett(L)
%% ref bartleet.m https://ww2.mathworks.cn/help/signal/ref/bartlett.html
narginchk(1,1);
w = 2*(0:(L-1)/2)/(L-1);
if rem(L,2)
% It's an odd length sequence
w = [w w((L-1)/2:-1:1)]';
else
% It's even
w = [w w(L/2:-1:1)]';
end
end
function [w] = win_hann(L)
%% ref hann https://ww2.mathworks.cn/help/signal/ref/hann.html
w = zeros(L,1);
for n = 0:L-1
w(n+1) = 0.5*(1-cos(2*pi*n/(L-1)));
end
end
function [w] = win_hamming(L)
%% ref hamming https://ww2.mathworks.cn/help/signal/ref/hamming.html
w = zeros(L,1);
for n = 0 : L-1
w(n+1) = 0.54 - 0.46*cos(2*pi*n/(L-1));
end
end
function [w] = win_kaiser(L,beta)
%% ref Kaiser https://ww2.mathworks.cn/help/signal/ref/kaiser.html
% besseli(0,beta*sqrt(1-(((0:L-1)-(L-1)/2)/((L-1)/2)).^2))/besseli(0,beta)
w = zeros(L,1);
for n = 0 : L-1
w(n+1) = besseli(0,beta*sqrt(1-((n-(L-1)/2)/((L-1)/2)).^2))/besseli(0,beta);
end
function [w] = win_flattop(L)
%% ref flattopwin https://ww2.mathworks.cn/help/signal/ref/flattopwin.html
x = 0;
L = L+x;
a0 = 0.21557895;
a1 = 0.41663158;
a2 = 0.277263158;
a3 = 0.083578947;
a4 = 0.006947368;
w = zeros(L,1);
for n = 0:1: L-1
w(n+1) = a0 - a1*cos(2*pi*n/(L-1)) + a2*cos(4*pi*n/(L-1)) - a3*cos(6*pi*n/(L-1)) +a4*cos(8*pi*n/(L-1));
end
w = w(1:L-x);
end
function [w] = win_blackman(L)
%% ref blackman https://ww2.mathworks.cn/help/signal/ref/blackman.html
w = zeros(L,1);
for n = 0:L-1
w(n+1) = 0.42 - 0.5*cos(2*pi*n/(L-1)) + 0.08*cos(4*pi*n/(L-1));
end
end
function [w] = win_gauss(L,alpha) %% ref gausswin https://ww2.mathworks.cn/help/signal/ref/gausswin.html % 限制参数数量 narginchk(1,2); % 设置默认参数 if nargin < 2 || isempty(alpha) alpha = 0.500; end w = zeros(L,1); index = 1; for n = -(L-1)/2 : (L-1)/2 % w(index) = exp(-0.5*(2*alpha*n/(L-1))^2); w(index) = exp(-0.5*(n/((L-1)/(2*alpha)))^2); index = index + 1; end end
function w = win_nuttall(L,sflag) %% ref nuttallwin https://ww2.mathworks.cn/help/signal/ref/nuttallwin.html narginchk(1,2); if nargin < 2 || isempty(sflag) sFlag = "symmetric"; else if isstring(sflag) && strlength(sflag) == 0 sFlag = "symmetric"; else sflagOpts = {'symmetric','periodic'}; sFlag = convertCharsToStrings(validatestring(sflag,sflagOpts,'win_nuttall','sflag')); end end % window w = window(L,sFlag); end function w = window(L,sFlag) w = zeros(L,1); a = [0.3635819 0.4891775 0.1365995 0.0106411]; if sFlag == "periodic" for n = 0:L-1 w(n+1) = a(1) - a(2)*cos(2*pi*n/L) + a(3)*cos(4*pi*n/L) - a(4)*cos(6*pi*n/L); end else for n = 0:L-1 w(n+1) = a(1) - a(2)*cos(2*pi*n/(L-1)) + a(3)*cos(4*pi*n/(L-1)) - a(4)*cos(6*pi*n/(L-1)); end end end
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。