6
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

numpyとscipyでFFT処理してローパスフィルター

Posted at

FFT処理でnumpyとscipyを使った方法をまとめておきます。
このページでは処理時間を比較しています。

以下のページを参考にさせていただきました。
■Python NumPy SciPy : FFT 処理による波形整形(スムーザ)
https://org-technology.com/posts/smoother.html

まず、処理するサンプルデータを作成します。

import numpy as np
from scipy import fftpack
import matplotlib.pyplot as plt

# サンプルデータ作成
n = 512                         # データ数
dt = 0.01                       # サンプリング間隔
f = 1                           # 周波数
t = np.linspace(1, n, n)*dt-dt
y = np.sin(2*np.pi*f*t)+0.5*np.random.randn(t.size)

# FFT 処理と周波数軸の作成
yf = fftpack.fft(y)/(n/2)
freq = fftpack.fftfreq(n, dt)

①scipyのfftpackを使う方法(fft, ifft)

yf = fftpack.fft(y)/(n/2)
freq = fftpack.fftfreq(n, dt)

fs = 2
yf2 = np.copy(yf)
yf2[(freq > fs)] = 0
yf2[(freq < 0)] = 0

y2 = np.real(fftpack.ifft(yf2)*n)

計算時間:48.2 µs ± 869 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

②scipyのfftpackを使う方法(rfft, irfft)

yf = fftpack.rfft(y)/(n/2)
freq = fftpack.fftfreq(n, dt)

fs = 2
yf2 = np.copy(yf)
yf2[(freq > fs)] = 0
yf2[(freq < 0)] = 0

y2 = np.real(fftpack.irfft(yf2)*(n/2))

計算時間:38.7 µs ± 723 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

③numpyのfftを使う方法

yf = np.fft.fft(y)/(n/2)
freq = np.fft.fftfreq(n, d=dt)

fs = 2
yf2 = np.copy(yf)
yf2[(freq > fs)] = 0
yf2[(freq < 0)] = 0

y2 = np.real(np.fft.ifft(yf2)*n)

計算時間:41.3 µs ± 2.3 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

結果は
1位 38.7us ②scipyのfftpackを使う方法(rfft, irfft)
2位 41.3us ③numpyのfftを使う方法
3位 48.2us ①scipyのfftpackを使う方法(fft, ifft)

以上です。

6
9
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
6
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?