<reponame>mattkjames7/wavespec
import numpy as np
from .. import Fourier
from .. import LombScargle
from .. import CrossPhase

def Spectrogram(t,v,wind,slip,Method='FFT',**kwargs):
	'''
	Creates a spectogram using a sliding window.
	
	Inputs
	======
		t : time array in seconds
		v : array of values the same length as t. If using crossphase,
			this should be a list or tuple containing two arrays.
	 wind : sliding window length in seconds
	 slip : difference in time between the start of one window and the 
			next - when slip < wind, each window will have an overlap,
			when slip > wind, there will be gaps where some data will be 
			unused and when slip == wind, each window is adjacent in time.
	 Freq : a list of frequencies (Hz) to solve for - only does anything
			when using L-S
	Method : Currently either 'FFT' or 'LS'
	WindowFunction : Select a window function to apply to the data before 
			the transform, the options are: 'none','cosine-bell','hamming',
			'triangle','welch','blackman','nuttall','blackman-nuttall',
			'flat-top','cosine','gaussian'			
	Param : This parameter is used to alter some of the window functions
			(see WindowFunctions.py).
	Detrend : This will linearly detrend each time window before the 
			transform.
	FindGaps : This tells the routine to scan for data gaps, when set
			to False - the data are assumed to be perfect.
	GoodData : This can be set to a boolean array which tells the DetectGaps
			function which data points are good (True) or bad (False),
			if set to None, then any non-finite data is assumed to be bad.
	Quiet : When set to True, the function produces no stdout output; when
			False, stdout shows the progress.
	LenW : This can be set to an integer value in order to force a specific
			window length (the number of elements, as opposed to the length
			in time defined using wind)
	Threshold:	If set to a value above 0, then all values which 
			correspond to frequencies where the amplitude is less than
			Threshold are set to 0, effectively removing noise from the
			spectra.
	Fudge:	(LS Only!)
			This applies a fudge for when f == Nyquist frequency, because
			small floating point numbers have relatively large errors.
			This should only be needed if intending to reproduce a
			two-sided FFT (also, if doing this then divide A by 2 and P 
			by 4).
	OneSided: (FFT Only!)
			This should be set to remove the negative frequencies in
			the second half of the spectra. In doing so, the amplitudes
			are doubled and the powers are quadrupled.
	Tax :	(LS only)
			An array of times at the centre of each bin.
									
	Returns
	=======
	Nw : Total number of time windows in the output array
	LenW : Length of a time window (number of elements)
	Freq : Array of frequencies in Hz.
	numpy.recarray : 
			Stores the output of the transform under the following fields:
				Tspec : Time in seconds of the middle of each time window
				Pow : Power at each frequency in each window, shape (Nw,LenW)
				Pha : Phase at each frequency in each window, shape (Nw,LenW)
				Amp : Amplitude at each frequency in each window, shape (Nw,LenW)
				Real : Real component at each frequency in each window, shape (Nw,LenW)
				Imag : Imaginary component at each frequency in each window, shape (Nw,LenW)
	'''
	if Method == 'FFT':
		return Fourier.Spectrogram(t,v,wind,slip,**kwargs)
	elif Method == 'LS':
		return LombScargle.Spectrogram(t,v,wind,slip,**kwargs)
	elif Method == 'CP-FFT':
		return CrossPhase.SpectrogramFFT(t,v,wind,slip,**kwargs)
	elif Method == 'CP-LS':
		return CrossPhase.SpectrogramLS(t,v,wind,slip,**kwargs)
	else:
		print('Method not supported')
		return None

import numpy as np
from .. import Fourier
from .. import LombScargle
from .. import CrossPhase

def Spectrogram(t,v,wind,slip,Method='FFT',**kwargs):
	'''
	Creates a spectogram using a sliding window.
	
	Inputs
	======
		t : time array in seconds
		v : array of values the same length as t. If using crossphase,
			this should be a list or tuple containing two arrays.
	 wind : sliding window length in seconds
	 slip : difference in time between the start of one window and the 
			next - when slip < wind, each window will have an overlap,
			when slip > wind, there will be gaps where some data will be 
			unused and when slip == wind, each window is adjacent in time.
	 Freq : a list of frequencies (Hz) to solve for - only does anything
			when using L-S
	Method : Currently either 'FFT' or 'LS'
	WindowFunction : Select a window function to apply to the data before 
			the transform, the options are: 'none','cosine-bell','hamming',
			'triangle','welch','blackman','nuttall','blackman-nuttall',
			'flat-top','cosine','gaussian'			
	Param : This parameter is used to alter some of the window functions
			(see WindowFunctions.py).
	Detrend : This will linearly detrend each time window before the 
			transform.
	FindGaps : This tells the routine to scan for data gaps, when set
			to False - the data are assumed to be perfect.
	GoodData : This can be set to a boolean array which tells the DetectGaps
			function which data points are good (True) or bad (False),
			if set to None, then any non-finite data is assumed to be bad.
	Quiet : When set to True, the function produces no stdout output; when
			False, stdout shows the progress.
	LenW : This can be set to an integer value in order to force a specific
			window length (the number of elements, as opposed to the length
			in time defined using wind)
	Threshold:	If set to a value above 0, then all values which 
			correspond to frequencies where the amplitude is less than
			Threshold are set to 0, effectively removing noise from the
			spectra.
	Fudge:	(LS Only!)
			This applies a fudge for when f == Nyquist frequency, because
			small floating point numbers have relatively large errors.
			This should only be needed if intending to reproduce a
			two-sided FFT (also, if doing this then divide A by 2 and P 
			by 4).
	OneSided: (FFT Only!)
			This should be set to remove the negative frequencies in
			the second half of the spectra. In doing so, the amplitudes
			are doubled and the powers are quadrupled.
	Tax :	(LS only)
			An array of times at the centre of each bin.
									
	Returns
	=======
	Nw : Total number of time windows in the output array
	LenW : Length of a time window (number of elements)
	Freq : Array of frequencies in Hz.
	numpy.recarray : 
			Stores the output of the transform under the following fields:
				Tspec : Time in seconds of the middle of each time window
				Pow : Power at each frequency in each window, shape (Nw,LenW)
				Pha : Phase at each frequency in each window, shape (Nw,LenW)
				Amp : Amplitude at each frequency in each window, shape (Nw,LenW)
				Real : Real component at each frequency in each window, shape (Nw,LenW)
				Imag : Imaginary component at each frequency in each window, shape (Nw,LenW)
	'''
	if Method == 'FFT':
		return Fourier.Spectrogram(t,v,wind,slip,**kwargs)
	elif Method == 'LS':
		return LombScargle.Spectrogram(t,v,wind,slip,**kwargs)
	elif Method == 'CP-FFT':
		return CrossPhase.SpectrogramFFT(t,v,wind,slip,**kwargs)
	elif Method == 'CP-LS':
		return CrossPhase.SpectrogramLS(t,v,wind,slip,**kwargs)
	else:
		print('Method not supported')
		return None
Creates a spectogram using a sliding window. 	 	Inputs 	====== 		t : time array in seconds 		v : array of values the same length as t. If using crossphase, 			this should be a list or tuple containing two arrays. 	 wind : sliding window length in seconds 	 slip : difference in time between the start of one window and the  			next - when slip < wind, each window will have an overlap, 			when slip > wind, there will be gaps where some data will be  			unused and when slip == wind, each window is adjacent in time. 	 Freq : a list of frequencies (Hz) to solve for - only does anything 			when using L-S 	Method : Currently either 'FFT' or 'LS' 	WindowFunction : Select a window function to apply to the data before  			the transform, the options are: 'none','cosine-bell','hamming', 			'triangle','welch','blackman','nuttall','blackman-nuttall', 			'flat-top','cosine','gaussian'			 	Param : This parameter is used to alter some of the window functions 			(see WindowFunctions.py). 	Detrend : This will linearly detrend each time window before the  			transform. 	FindGaps : This tells the routine to scan for data gaps, when set 			to False - the data are assumed to be perfect. 	GoodData : This can be set to a boolean array which tells the DetectGaps 			function which data points are good (True) or bad (False), 			if set to None, then any non-finite data is assumed to be bad. 	Quiet : When set to True, the function produces no stdout output; when 			False, stdout shows the progress. 	LenW : This can be set to an integer value in order to force a specific 			window length (the number of elements, as opposed to the length 			in time defined using wind) 	Threshold:	If set to a value above 0, then all values which  			correspond to frequencies where the amplitude is less than 			Threshold are set to 0, effectively removing noise from the 			spectra. 	Fudge:	(LS Only!) 			This applies a fudge for when f == Nyquist frequency, because 			small floating point numbers have relatively large errors. 			This should only be needed if intending to reproduce a 			two-sided FFT (also, if doing this then divide A by 2 and P  			by 4). 	OneSided: (FFT Only!) 			This should be set to remove the negative frequencies in 			the second half of the spectra. In doing so, the amplitudes 			are doubled and the powers are quadrupled. 	Tax :	(LS only) 			An array of times at the centre of each bin. 									 	Returns 	======= 	Nw : Total number of time windows in the output array 	LenW : Length of a time window (number of elements) 	Freq : Array of frequencies in Hz. 	numpy.recarray :  			Stores the output of the transform under the following fields: 				Tspec : Time in seconds of the middle of each time window 				Pow : Power at each frequency in each window, shape (Nw,LenW) 				Pha : Phase at each frequency in each window, shape (Nw,LenW) 				Amp : Amplitude at each frequency in each window, shape (Nw,LenW) 				Real : Real component at each frequency in each window, shape (Nw,LenW) 				Imag : Imaginary component at each frequency in each window, shape (Nw,LenW)