Skip to content

Instantly share code, notes, and snippets.

@mFoxRU
Last active September 8, 2015 05:22
Show Gist options
  • Save mFoxRU/dbf58d13b4e33dd8cd6b to your computer and use it in GitHub Desktop.
Save mFoxRU/dbf58d13b4e33dd8cd6b to your computer and use it in GitHub Desktop.
from __future__ import division
import numpy as np
def periodogram_fft(x, fs=1.0, window=None, nfft=256):
"""
Calculate a PSD estimate using FFT
:param x: 1-d iterable of values
:param fs: Sampling frequency
:param window: Window
:param nfft: Number of DFT points
:return: PSD estimate
:rtype : ndarray
"""
if window is None:
window = []
x = np.asarray(x)
x_size = np.shape(x)
x_len = x_size[0]
if len(x_size) > 1:
if x_size[0] == 1:
x_len = x_size[1]
if x_len > nfft:
overlap = x_len % nfft
if overlap:
x = np.append(x, np.zeros((1, nfft-overlap)))
x = np.sum(np.reshape(x, (-1, nfft)), axis=0)
n = x_len
if len(window):
n = sum(x*x for x in window)
Pxx = np.abs(np.fft.fft(x, nfft)) ** 2/n
Pxx /= fs
Pxx[1:nfft/2] += Pxx[-1:nfft/2:-1]
return Pxx[0:nfft/2+1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment