且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Matlab中实现高通巴特沃斯滤波器?

更新时间:2022-03-09 16:23:59

我已经解决了这个问题.

I have solved this problem.

解决此问题的关键是功能ifftshow().

The key to solving this problem was the function ifftshow().

源代码

main.m

clear_all();
I = gray_imread('cameraman.png');
Dh = 10;
n = 1;
[J, K] = butterworth_hpf(I, Dh, n);

imshowpair(I, K, 'montage');
%draw_multiple_images({I, J, K});

ifftshow.m

function out = ifftshow(f)
    f1 = abs(f);
    fm = max(f1(:));
    out = f1/fm;
end

butter_hp_kernel.m

function k = butter_hp_kernel(I, Dh, n) 
    Height = size(I,1); 
    Width = size(I,2); 

    [u, v] = meshgrid( ...
                    -floor(Width/2) :floor(Width-1)/2, ...
                    -floor(Height/2): floor(Height-1)/2 ...
                 ); 

    k = butter_hp_f(u, v, Dh, n);

function f = butter_hp_f(u, v, Dh, n)
    uv = u.^2+v.^2;
    Duv = sqrt(uv);
    frac = Dh./Duv;
    %denom = frac.^(2*n);
    A=0.414; denom = A.*(frac.^(2*n));    
    f = 1./(1.+denom);

butterworth_hpf.m

function [out1, out2] = butterworth_hpf(I, Dh, n)

    Kernel = butter_hp_kernel(I, Dh, n);

    I_ffted_shifted = fftshift(fft2(I));

    I_ffted_shifted_filtered = I_ffted_shifted.*Kernel;

    out1 = ifftshow(ifft2(I_ffted_shifted_filtered));

    out2 = ifft2(ifftshift(I_ffted_shifted_filtered));
end