Skip to content

astromancer/tsa

Repository files navigation

tsa: Time Series Analysis Tools

This project is for Time Series Analysis and Frequency Spectral Estimation. It allows for convenient computation of periodograms and spectrograms (aka Dynamic Power Spectra) as well as enabling plotting of multivariate time series and interactive Time-Frequency Representations of data.

Install

pip install https://github.com/astromancer/tsa

Use

Time Series

As an example, let's generate a harmonic signal:

import numpy as np
from tsa import TimeSeries


# generate data
np.random.seed(54321)
n = 1000                                        # number of points
A = 5                                           # amplitude
ω = 1.35  * 2 * np.pi                           # angular frequency [radians/s]
t = np.linspace(0, 6, n)                        # time
signal = A * np.sin(ω * t) + np.random.randn(n)
errors = np.random.rand(n)                      # uniformly distributed uncertainties

# create time series
ts = TimeSeries(t, signal, errors)
tsp = ts.plot()

Time Series Plot

Periodogram

As an example, we generate a multi-tone harmonic signal using the built in Harmonic signal generator. We compute the periodogram using the TimeSeries.periodogram method, which returns a plottable Periodogram object.

import matplotlib.pyplot as plt
from tsa.ts.generate import Harmonic

# generate the signal
harmonic = Harmonic(amplitudes=[5, 4.3, 2.7],
                    frequencies=[1.35, 20.27, 51.3])
ts = TimeSeries(t, harmonic(t))
# compute the periodogram
pg = ts.periodogram(normalize='rms')
# plot
fig, (ax0, ax1) = plt.subplots(2, 1)
ts.plot(ax=ax0)
pg.plot(ax=ax1)

Periodogram Plot

Spectrogram and Time-Frequency Representations

To demonstrate the spectrogram, we generate a multi-component signal consisting of two superposed time series:

  • A harmonic signal with constant tone at 10 Hz
  • An amplitude- and frequency modulated signal. We compute the spectrogram using TimeSeries.spectrogram, and plot a Time-Frequency Representation of the data.
fs = 100                                            # sampling frequency
fc = 25                                             # carier signal
fm = 0.1                                            # modulation frequency
Δf = 10                                             # frequency deviation
duration = 60
t = np.linspace(0, duration, duration * fs)
a = Harmonic(5, 0.05, np.pi / 4)(t)                 # amplitude (modulated)
signalA = Harmonic(2, 10)(t)
signalB =  a * np.cos(2 * np.pi * fc * t + (Δf / fm) * np.sin(2 * np.pi * fm * t))

ts = TimeSeries(t, signalA + signalB)
sg = ts.spectrogram(nwindow=128, noverlap='50%', normalize='rms')
tfr = sg.plot()

Time Frequency Map

Interactive features

To activate the interactive features of the map:

tfr.connect()  

Contribute

Contributions are welcome!

  1. Fork it!
  2. Create your feature branch
    git checkout -b feature/rad
  3. Commit your changes
    git commit -am 'Add some cool feature 😎'
  4. Push to the branch
    git push origin feature/rad
  5. Create a new Pull Request

Contact

License

About

Time series analysis tools

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages