Matlab中的窗函数

Matlab中的窗函数

  • 常用窗函数计算方法
  • 为了方便用其它语言写,大部分使用for生成的(matlab数组从1开始所以有的循环是n+1)

Test

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);

    

Rectwin

  • 矩形窗
w = ones(L,1);

Triang

  • 三角窗
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

Bartlett

  • 类三角窗
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

Hann

  • 海宁窗
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

Hamming

  • 汉明窗
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

Kaiser

  • 凯塞窗
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

Flattop

  • 平顶窗
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

Blackman

  • 布莱克曼窗
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

Gauss

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

Nutall

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

参考

加窗法

  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值