Skip to content
Snippets Groups Projects
Commit 349f33b4 authored by Junjie Zhang's avatar Junjie Zhang
Browse files

add instFrequency() method to timeSeries

parent 8c538bc7
No related branches found
No related tags found
No related merge requests found
import numpy as np import numpy as np
import cmath import cmath
from scipy.signal import hilbert, chirp
class TimeSeries: class TimeSeries:
"""Stores data from different simulation sources. """Stores data from different simulation sources.
...@@ -44,6 +45,15 @@ class TimeSeries: ...@@ -44,6 +45,15 @@ class TimeSeries:
ts_real = TimeSeries(self.name+'_real', self.time, _real) ts_real = TimeSeries(self.name+'_real', self.time, _real)
return ts_real return ts_real
def imag(self):
""" get the imaginary part of complex time series.
"""
_imag = []
for value in self.values:
_imag.append(np.imag(value))
ts_imag = TimeSeries(self.name+'_real', self.time, _imag)
return ts_imag
def phasor(self): def phasor(self):
"""Calculate phasors of complex time series """Calculate phasors of complex time series
and return dict with absolute value and phase. and return dict with absolute value and phase.
...@@ -199,8 +209,8 @@ class TimeSeries: ...@@ -199,8 +209,8 @@ class TimeSeries:
"""Returns relative difference between two time series objects to the first. """Returns relative difference between two time series objects to the first.
""" """
diff_val=TimeSeries.diff('diff', ts1, ts2).values diff_val=TimeSeries.diff('diff', ts1, ts2).values
# in case an element in ts1 is zero and the corresponding diff is non-zero # relative error to the max value of ts1
rel_diff_to_ts1=np.divide(diff_val, ts1.values, out=np.zeros_like(diff_val), where=ts1.values!=0) rel_diff_to_ts1 = diff_val/ts1.values.max()
ts_rel_diff_to_ts1 = TimeSeries(name, ts1.time, rel_diff_to_ts1) ts_rel_diff_to_ts1 = TimeSeries(name, ts1.time, rel_diff_to_ts1)
return ts_rel_diff_to_ts1 return ts_rel_diff_to_ts1
...@@ -222,5 +232,15 @@ class TimeSeries: ...@@ -222,5 +232,15 @@ class TimeSeries:
phasor_list = {} phasor_list = {}
for name, ts in timeseries_list.items(): for name, ts in timeseries_list.items():
phasor_list[name] = ts.phasor() phasor_list[name] = ts.phasor()
return phasor_list
return phasor_list @staticmethod
\ No newline at end of file def instFrequency(ts):
duration = ts.time.max()
samples = ts.time.size
sampling_freq = samples / duration
analytic_signal = hilbert(ts.abs().values)
instaneous_phase=np.unwrap(np.angle(analytic_signal))
instantaneous_frequency = (np.diff(instaneous_phase) / (2.0 * np.pi) * sampling_freq)
ts_instFreq=TimeSeries(str(ts.name + "instFreq"), ts.time[:-1], instantaneous_frequency)
return ts_instFreq
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment