LoginSignup
0
1

More than 3 years have passed since last update.

大きさが奇数の時の numpy, scipy の irfft

Posted at

scipy.fft.rfftn が実数値のn次元フーリエ変換 (FFT)、scipy.fft.irfftn はその逆変換なので、普通

irfftn(rfftn(a)) == a

を浮動小数点数の精度の範囲内で期待するけど、aの大きさが奇数の時そうはならない。

import numpy as np
from scipy.fft import rfft2, irfft2

a = np.eye(5)            # 5 x 5 
irfft2(rfft2(a)).shape   # => (5, 4)

rfft2(a) は複素数の shape=(5,3) になるけど、これが shape=(5, 5) の実数から来たという情報が失われているので元の大きさを irfftn に渡さないといけない:

a = np.eye(5)            # 5 x 5 
aa = irfft2(rfft2(a), a.shape)  # => a == aa

これで期待通り a == aa になる(もちろん浮動小数点数の範囲内で)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1