Compute Fourier Spectra

The fourier_util module provides classes representing the Fourier spectrum of timeseries, and methods to search for peaks.

Basic usage, get parameters and time series:

>>>from postcactus.fourier_util import *
>>>t = linspace(0,30,400)
>>>y = sin(2*pi*1.3*t) + t - 3
>>>sp = spec(t,y)
>>>sp2 = spec(t,y,remove_mean=True, window=hanning)
>>>plot(sp.f, sp.amp, 'k-')
[<matplotlib.lines.Line2D at 0x4733f10>]
>>>plot(sp2.f, sp2.amp, 'g-')
[<matplotlib.lines.Line2D at 0x473ec50>]
>>>axvline(1.3)
<matplotlib.lines.Line2D at 0x4741ed0>
>>>pks = peaks(sp2.f, sp2.amp, 0.01)
>>>print pks
#               f / Hz             f_fit / Hz       amp. 
               1.29675        1.2992136857669     0.0709 
# Resolution = 0.016625 Hz
>>>axvline(pks.all[0].ff)
<matplotlib.lines.Line2D at 0x48e4c90>
>>>axvline(pks.all[0].f)
<matplotlib.lines.Line2D at 0x48f1e10>
class postcactus.fourier_util.spec(t, y, norm=True, window=None, remove_mean=False)

Class representing a Fourier spectrum.

Variables
  • f – Frequency

  • amp – Power spectral density

Parameters
  • t (1d numpy array) – sample times (can be irregular).

  • y (1d numpy array (real or complex)) – data samples.

  • norm (bool) – Whether to normalize the PSD to the largest peak.

  • window (Any function accepting length and returning numpy array of this length.) – Window function applied before Fourier transform.

  • remove_mean (bool) – Whether to subtract mean value before Fourier transform.

class postcactus.fourier_util.peak(f, ff, a)

Represents one peak of a Fourier spectrum.

Variables
  • f – Frequency of the frequency bin of the maximum.

  • ff – Frequency of the peak obtained by fitting a parabola through the maximum and its two neighbours.

  • a – Amplitude of the peak.

class postcactus.fourier_util.peaks(f, a0, cut)

This class represents the peaks of a Fourier spectrum. Each peak is characterized by the frequency of the corresponding frequency bin of the power spectrum, as well as the frequency of the maximum of a quadratic fit to the maximum and its left and right neighbour.

Variables

all – All peaks, sorted by amplitude (list of peak instances)

Searches for the peaks in a given Fourier spectrum.

Parameters
  • f (1d numpy array) – frequencies of the PSD.

  • a0 (1d numpy array) – PSD.

  • cut (float) – Ignore peaks smaller than this fraction of the largest one.

save(fname)

Save peaks to human readable file.

Parameters

fname (string) – The filename.