Readme

This python module calculates the fourier transformation of a numpy.ndarray (or pandas.DataFrame) and computes the power spectral density of the signal.

Setup

To install the module into the system simply execute setup.py

$ python setup.py install

Usage

Prepare the signal and important known information about the signal (like rate and avg):

import numpy as np
signal = np.random.randn(10240) # Normal distributed signal
rate = 8 # Hz
avg = len(signal)/1024

Then create an instance of the class SpectrumAnalyzer which analyzes the signal (the following are the default settings):

from spectrumanalyzer import SpectrumAnalyzer
spectrum = SpectrumAnalyzer(
                 timesignal=signal,
                 samplingrate=rate,
                 averages=avg,
                 shifting_method=False,
                 short_mode=1,
                 filter_type='lowpass',
                 passband_factor=1,
                 filter_order=6,
                 filter_analog=False,
                 first_spectra_highest_frequency=rate/8,
                 downsample=True,
                 )

To calculate the power spectral density, call the function cut_timesignal(num) and process the returned single spectra as shown:

all_spectra = []
for single_spectrum, \
        frequency_span_array, \
        k in spectrum.cut_timesignal(64): # cut into 64 Spectra
    all_spectra.append(single_spectrum)
averaged_spectrum = spectrum.first_spectrum_avg

Optional: Plot the signal and the PSD:

import matplotlib.pyplot as plt
fig, (ax, ax2) = plt.subplots(2)
ax.plot(np.arange(0, len(signal)/rate, 1/rate), signal)
ax.set_title('Signal')

ax2.loglog(frequency_span_array, avgeraged_spectrum)
ax2.set_title('PSD of Signal')
plt.show()

Calculation steps for the first spectrum

The following steps are done automatically by cut_timesignal(num):

  1. Cutting the signal into num parts.

  2. Apply a lowpass filter to each signal part.

  3. If downsample is True

    • use every gap = (RATE / first_spectra_highest_frequency) Point.

  4. Calculate the fourier transformation.

    • [if downsample and shifting_method is both True]: Take average of fourier transformed shifted points (shifted by gap).

  5. Calculate power spectral density (PSD) by taking squared absolute values.

  6. Normalize with normalization factor.

  7. Return a single PSD for the ``k``th part of the signal.

  8. When k == num the averaged spectrum is returned.

Licensing

Warning

SpectrumAnalyzer does not have a license yet and can not be published at this point.