출처: 다크프로그래머

“다크 프로그래머-Fourier Transform(푸리에 변환)의 이해와 활용” 직접 실험해보기

  • 푸리에 변환은 임의의 신호를 sine, cosine 주기 함수를 활용하여 분해하여 표현하는 것
  • 푸리에 변환된 값은 실수와 허수의 합으로 표현되며, 이를 활용하여 magnitude와 phase를 구함

  • 원본 이미지에서 가로, 세로, 대각선의 선을 추가하여 이에 따른 푸리에 변환 결과 비교

  • 4가지 input의 magnitude와 phase 계산하고 magnitude와 phase를 활용하여 다시 이미지를 reconstruct
  • magnitude에 2번 째는 가로선, 3번 째는 가로 세로선, 4번 째는 대각선 선이 1번 째에 비해 더 추가되어 있음
  • phase 또한 magnitude와 유사하게 원본에 추가적인 선이 보임
  • 펼쳐서 코드 확인
      for im in [im1, im2, im3, im4]:
          # 푸리에 변환
          spec1 = fft2(im)
          spec1 = np.roll(spec1 , 400, axis=(0,1))
    
          img  = spec1.imag
          real = spec1.real
            
          # magnitude & phase
          mag = np.sqrt(img**2 + real**2)
          pha = np.arctan2(img, real)
    
          plt.figure(figsize=(10,5))
          plt.subplot(1,2,1)
          plt.imshow(np.log(1+mag), cmap='gray')
          plt.title('mag')
    
          plt.subplot(1,2,2)
          plt.imshow(pha, cmap='gray')
          plt.title('phase')
          plt.show()
    
          # reconstruction
          y_num = ifft2(real + img * 1j)
          y_feat = ifft2(mag * np.exp(1j*pha))
    
          plt.figure(figsize=(10,5))
          plt.subplot(1,2,1)
          plt.imshow(np.abs(y_num), cmap='gray')
          plt.title('original')
    
          plt.subplot(1,2,2)
          plt.imshow((np.abs(y_feat)), cmap='gray')
          plt.title('reconst')
          plt.show()
    


  • magnitude의 일부를 가리고 (0으로 mask) 다시 reconstruct 했을 때, 올빼미에 선들이 사라짐

  • 소리의 푸리에 변환 (UrbanSound8K 데이터셋)

Tags:

Categories:

Updated: