如何在Matlab中实现低通巴特沃斯滤波器? [英] How can I implement a low-pass Butterworth filter in Matlab?

查看:2983
本文介绍了如何在Matlab中实现低通巴特沃斯滤波器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此答案中,我知道如何创建高通巴特沃斯滤波器.

From this answer, I know how to create a High-pass Butterworth filter.

来自此视频 ,我知道 lowpasskernel = 1 - highpasskernel .

From this video, I know that, lowpasskernel = 1 - highpasskernel.

因此,我创建了以下低通巴特沃斯滤波器,

So, I created the following Low-pass Butterworth Filter,

function [out, kernel] = butterworth_lp(I, Dl, n)
    height = size(I, 1);
    width  = size(I, 2);

    [u, v] = meshgrid(-floor(width/2):floor(width/2)-1,-floor(height/2):floor(height/2)-1);

    % lp_kernel = 1 - hp_kernel
    kernel = 1 - butter_hp_kernel(u, v, Dl, n);

    % fft the image
    I_fft_shifted = fftshift(fft2(double(I)));

    % apply lowpass filter
    I_fft_shift_filtered = I_fft_shifted .* kernel;

    % inverse FFT, get real components
    out = real(ifft2(ifftshift(I_fft_shift_filtered)));

    % normalize and cast
    out = (out - min(out(:))) / (max(out(:)) - min(out(:)));
    out = uint8(255*out);

function k = butter_hp_kernel(u, v, Dh, n)
    uv =  u.^2+v.^2;
    D = sqrt(uv);
    frac = Dh./D;
    p = 2*n;
    denom =  frac.^p;
    k = 1./denom;

输出

这不是低通滤波器的输出.

This isn't a low-pass filter output.

那么,我的代码有什么问题?

推荐答案

好的.我已经按照遵循以下公式(第8/48页)解决了问题)

输出

源代码

butter_lp_kernel.m

function f = butter_lp_f(u, v, Dl, n)
    uv = u.^2+v.^2;
    Duv = sqrt(uv);
    frac = Duv./Dl;
    denom = frac.^(2*n);
    f = 1./(1.+denom);

function k = butter_lp_kernel(I, Dl, 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_lp_f(u, v, Dl, n);

ifftshow.m

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

butterworth_lpf.m

function [out1, out2] = butterworth_lpf(I, Dl, n)

    Kernel = butter_lp_kernel(I, Dl, 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

这篇关于如何在Matlab中实现低通巴特沃斯滤波器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆