Spectrum¶
- class pathsim.blocks.spectrum.Spectrum(freq=[], t_wait=0.0, alpha=0.0, labels=[])[source]¶
Bases:
BlockBlock for fourier spectrum analysis (spectrum analyzer).
Computes continuous time running fourier transform (RFT) of the incoming signal.
A time threshold can be set by ‘t_wait’ to start recording data only after the simulation time is larger then the specified waiting time, i.e. ‘t - t_wait > dt’. This is useful for recording the steady state after all the transients have settled.
An exponential forgetting factor ‘alpha’ can be specified for realtime spectral analysis. It biases the spectral components exponentially to the most recent signal values by applying a single sided exponential window like this:
\[\int_0^t u(\tau) \exp(\alpha (t-\tau)) \exp(-j \omega \tau)\ d \tau\]It is also known as the ‘exponentially forgetting transform’ (EFT) and a form of short time fourier transform (STFT). It is implemented as a 1st order statespace model
\[\dot{x} = - \alpha x + \exp(-j \omega t) u\]where ‘u’ is the input signal and ‘x’ is the state variable that represents the complex fourier coefficient to the frequency ‘omega’. The ODE is integrated using the numerical integration engine of the block.
Example
This is how to initialize it:
import numpy as np #linear frequencies (0Hz, DC -> 1kHz) sp1 = Spectrum( freq=np.linspace(0, 1e3, 100), labels=['x1', 'x2'] #labels for two inputs ) #log frequencies (1Hz -> 1kHz) sp2 = Spectrum( freq=np.logspace(0, 3, 100) ) #log frequencies including DC (0Hz, DC + 1Hz -> 1kHz) sp3 = Spectrum( freq=np.hstack([0.0, np.logspace(0, 3, 100)]) ) #arbitrary frequencies sp4 = Spectrum( freq=np.array([0, 0.5, 20, 1e3]) )
Note
This block is relatively slow! But it is valuable for long running simulations with few evaluation frequencies, where just FFT’ing the time series data wouldnt be efficient OR if only the evaluation at weirdly spaced frequencies is required. Otherwise its more efficient to just do an FFT on the time series recording after the simulation has finished.
- Parameters:
- input_port_labels = None¶
- output_port_labels = {}¶
- property t_wait¶
- property alpha¶
- property labels¶
- property freq¶
- reset()[source]¶
Reset the blocks inputs and outputs and also its internal solver, if the block has a solver instance.
- read()[source]¶
Read the recorded spectrum
Example
This is how to get the recorded spectrum:
import numpy as np #linear frequencies (0Hz, DC -> 1kHz) spc = Spectrum( freq=np.linspace(0, 1e3, 100), labels=['x1', 'x2'] #labels for two inputs ) #... run the simulation ... #read the complex spectra of x1 and x2 freq, [X1, X2] = spc.read()
- Returns:
freq (array[float]) – evaluation frequencies
spec (array[complex]) – complex spectrum
- collect()[source]¶
Yield (category, id, data) tuples for recording blocks to simplify
Deprecated since version 1.0.0: its against pathsims philosophy
global data collection from all recording blocks.
- to_checkpoint(prefix, recordings=False)[source]¶
Serialize Spectrum state including integration time.
- plot(*args, **kwargs)[source]¶
Directly create a plot of the recorded data for visualization.
The ‘fig’ and ‘ax’ objects are accessible as attributes of the ‘Spectrum’ instance from the outside for saving, or modification, etc.
- save(path='spectrum.csv')[source]¶
Save the recording of the spectrum to a csv file
- Parameters:
path (str) – path where to save the recording as a csv file
- class pathsim.blocks.spectrum.RealtimeSpectrum(freq=[], t_wait=0.0, alpha=0.0, labels=[])[source]¶
Bases:
SpectrumAn extension of the ‘Spectrum’ block that also initializes a realtime plotter.
Deprecated since version 1.0.0.
Creates an interactive plotting window while the simulation is running. Otherwise implements the same functionality as the regular ‘Spectrum’ block.
Note
Due to the plotting being relatively expensive, including this block slows down the simulation significantly but may still be valuable for debugging and testing.
- Parameters: