当前位置:   article > 正文

Matlab中的窗函数_matlab窗函数

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

    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

Rectwin

  • 矩形窗
w = ones(L,1);
  • 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

参考

加窗法

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/287681
推荐阅读
相关标签
  

闽ICP备14008679号