title: How can I implement a high-pass Butterworth filter in Matlab?
body: <p>According to Page#14 of <a href="http://mstrzel.eletel.p.lodz.pl/mstrzel/pattern_rec/filtering.pdf" rel="nofollow noreferrer">this link</a>, the equation for a high-pass Butterworth filter is,</p>
  
  <p><a href="https://i.stack.imgur.com/Tg2it.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Tg2it.png" alt="enter image description here"></a></p>
  
  <p>And, according to Page#17, the output should be something like the following,</p>
  
  <p><a href="https://i.stack.imgur.com/tpm9H.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tpm9H.png" alt="enter image description here"></a></p>
  
  <p>Now, I have looked at this <a href="https://stackoverflow.com/a/30279157/159072">answer in SO</a>, and has written the following Matlab code using the formula given in the linked pdf document.</p>
  
  <p>The output looks different than that of the one given above.</p>
  
  <p><a href="https://i.stack.imgur.com/QgQU0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QgQU0.png" alt="enter image description here"></a></p>
  
  <p><strong>What is the possible problem in my source code?</strong></p>
  
  <hr>
  
  <p><em>Source Code</em></p>
  
  <p><strong>main.m</strong></p>
  
  <pre><code>clear_all();
  
  I = gray_imread('cameraman.png');
  
  n = 1;
  Dh = 10;
  
  [J, Kernel] = butterworth_hp(I, Dh, n);
  
  imshowpair(I, J, 'montage');
  </code></pre>
  
  <p><strong>butterworth_hp.m</strong></p>
  
  <pre><code>function [out, kernel] = butterworth_hp(I, Dh, n)
  height = size(I, 1);
  width = size(I, 2);
  
  I_fft_shifted = fftshift(fft2(double(I)));
  
  [u, v] = meshgrid(-floor(width/2):floor(width/2)-1,-floor(height/2):floor(height/2)-1);
  kernel = butter_hp_kernel(u, v, Dh, n);
  
  I_fft_shift_filtered = I_fft_shifted .* kernel;
  
  out = real(ifft2(ifftshift(I_fft_shift_filtered)));
  
  out = (out - min(out(:))) / (max(out(:)) - min(out(:)));
  out = uint8(255*out);
  
  
  function k = butter_hp_kernel(u, v, D0, n)
  uv =  u.^2+v.^2;
  Duv = uv.^0.5;
  frac = D0./Duv;
  p = 2*n;
  denom =  frac.^p;
  A = 0.414;
  k = 1./(1+A*denom);
  </code></pre>