Skip to content

Commit

Permalink
Merge pull request MAVENSDC#197 from nickssl/matplotlib-backend
Browse files Browse the repository at this point in the history
Added pwrspc
  • Loading branch information
nickssl authored Dec 14, 2023
2 parents 40d02aa + 384cfbd commit d30e8d8
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pytplot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def newlayout(self, layout):
#from pytplot.tplot_math import *
from .tplot_math import add_across, add, avg_res_data, clip, crop, deflag, degap, derive, divide, flatten
from .tplot_math import interp_nan, tinterp, join_vec, multiply, resample, spec_mult, split_vec, subtract, pwr_spec
from .tplot_math import clean_spikes, tsmooth, smooth, tdotp, tcrossp
from .tplot_math import clean_spikes, tsmooth, smooth, tdotp, tcrossp, pwrspc
#from .tplot_math import tdeflag
from .tplot_math import tnormalize, subtract_median, subtract_average, time_clip, tkm2re, makegap

Expand Down
6 changes: 1 addition & 5 deletions pytplot/tplot_math/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
# Copyright 2018 Regents of the University of Colorado. All Rights Reserved.
# Released under the MIT license.
# This software was developed at the University of Colorado's Laboratory for Atmospheric and Space Physics.
# Verify current version before use at: https://github.com/MAVENSDC/Pytplot

from .add_across import add_across
from .add import add
from .avg_res_data import avg_res_data
Expand Down Expand Up @@ -33,3 +28,4 @@
from .tkm2re import tkm2re
from .makegap import makegap
from .tdeflag import tdeflag
from .pwrspc import pwrspc
97 changes: 97 additions & 0 deletions pytplot/tplot_math/pwrspc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import logging
import numpy as np
from scipy.stats import linregress


def pwrspc(time, quantity, noline=False, nohanning=False, bin=3, notperhz=False):
"""
Compute the power spectrum of a given time series.
Parameters:
time (array):
The time array.
quantity (array):
The data array for which the power spectrum is to be computed.
noline (bool):
If True, straight line is not subtracted from the data.
nohanning (bool):
If True, no Hanning window is applied to the data.
bin (int):
Bin size for binning the data. Default is 3.
notperhz (bool):
If True, the output units are the square of the input units.
Returns:
tuple: Tuple containing:
- freq (array):
The frequency array.
- power (array):
The computed power spectrum.
Notes:
This is similar to IDL pwrspc.pro routine.
Example:
>>> # Compute the power spectrum of a given time series
>>> from pytplot import pwrspc
>>> time = [1, 2, 3, 4, 5]
>>> quantity = [1, 2, 3, 4, 5]
>>> freq, power = pwrspc(time, quantity)
"""

t = np.array(time, dtype=np.float64) - time[0]
x = np.array(quantity, dtype=np.float64)

if not noline:
slope, intercept, _, _, _ = linregress(t, x)
x -= (slope * t + intercept)

binsize = bin

window = 0.0
if not nohanning:
window = np.hanning(len(x))
x *= window

nt = len(t)
if nt % 2 != 0:
logging.info('needs an even number of data points, dropping last point...')
t = t[:-1]
x = x[:-1]
nt -= 1

xs2 = np.abs(np.fft.fft(x)) ** 2
dbign = float(nt)
logging.info('bign=', dbign)

k = np.arange(0, dbign // 2 + 1)
tres = np.median(np.diff(t))
fk = k / (dbign * tres)

pwr = np.zeros(nt // 2 + 1)
pwr[0] = xs2[0] / dbign**2
pwr[1:nt // 2] = (xs2[1:nt // 2] + xs2[nt:nt // 2:-1]) / dbign**2
pwr[-1] = xs2[-1] / dbign**2

if not nohanning:
wss = dbign * np.sum(window ** 2)
pwr = pwr*dbign**2/wss

dfreq = binsize * (fk[1] - fk[0])
npwr = len(pwr) - 1
nfinal = int(npwr / binsize)
iarray = np.arange(nfinal)
power = np.zeros(nfinal)

idx = (iarray + 0.5) * binsize + 1
freq = [fk[int(i)] for i in idx]

for i in range(binsize):
power += pwr[iarray * binsize + i + 1]

if not notperhz:
power /= dfreq

logging.info('dfreq=', dfreq)

return freq, power

0 comments on commit d30e8d8

Please sign in to comment.