function [st,t,f] = st(timeseries,minfreq,maxfreq,samplingrate,freqsamplingrate)
if verbose disp('No parameters inputted.'),end
if size(timeseries,2) > size(timeseries,1)
if size(timeseries,2) > 1
error('Please enter a *vector* of data, not matrix')
elseif (size(timeseries)==[1 1]) == 1
error('Please enter a *vector* of data, not a scalar')
maxfreq = fix(length(timeseries)/2);
maxfreq = fix(length(timeseries)/2);
[ minfreq,maxfreq,samplingrate,freqsamplingrate] = check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries);
[ minfreq,maxfreq,samplingrate,freqsamplingrate] = check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries);
[ minfreq,maxfreq,samplingrate,freqsamplingrate] = check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries);
[ minfreq,maxfreq,samplingrate,freqsamplingrate] = check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries);
if verbose disp('Error in input arguments: using defaults'),end
maxfreq = fix(length(timeseries)/2);
disp(sprintf('Minfreq = %d',minfreq))
disp(sprintf('Maxfreq = %d',maxfreq))
disp(sprintf('Sampling Rate (time domain) = %d',samplingrate))
disp(sprintf('Sampling Rate (freq. domain) = %d',freqsamplingrate))
disp(sprintf('The length of the timeseries is %d points',length(timeseries)))
t = (0:length(timeseries)-1)*samplingrate;
spe_nelements =ceil((maxfreq - minfreq+1)/freqsamplingrate) ;
f = (minfreq + [0:spe_nelements-1]*freqsamplingrate)/(samplingrate*length(timeseries));
if verbose disp(sprintf('The number of frequency voices is %d',spe_nelements)),end
st = strans(timeseries,minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,removeedge,analytic_signal,factor);
if verbose disp('Plotting pseudocolor image'),end
function st = strans(timeseries,minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,removeedge,analytic_signal,factor);
if verbose disp('Removing trend with polynomial fit'),end
r = polyfit(ind,timeseries,2);
timeseries = timeseries - fit;
if verbose disp('Removing edges with 5% hanning taper'),end
sh_len = floor(length(timeseries)/10);
sh_len=length(timeseries);
if size(wn,2) > size(wn,1)
timeseries(1:floor(sh_len/2),1) = timeseries(1:floor(sh_len/2),1).*wn(1:floor(sh_len/2),1);
timeseries(length(timeseries)-floor(sh_len/2):n,1) = timeseries(length(timeseries)-floor(sh_len/2):n,1).*wn(sh_len-floor(sh_len/2):sh_len,1);
if verbose disp('Calculating analytic signal (using Hilbert transform)'),end
ts_spe = fft(real(timeseries));
h = [1; 2*ones(fix((n-1)/2),1); ones(1-rem(n,2),1); zeros(fix((n-1)/2),1)];
ts_spe(:) = ts_spe.*h(:);
timeseries = ifft(ts_spe);
tic;vector_fft=fft(timeseries);tim_est=toc;
vector_fft=[vector_fft,vector_fft];
tim_est = tim_est*ceil((maxfreq - minfreq+1)/freqsamplingrate) ;
if verbose disp(sprintf('Estimated time is %f',tim_est)),end
st=zeros(ceil((maxfreq - minfreq+1)/freqsamplingrate),n);
if verbose disp('Calculating S transform...'),end
st(1,:) = mean(timeseries)*(1&[1:1:n]);
st(1,:)=ifft(vector_fft(minfreq+1:minfreq+n).*g_window(n,minfreq,factor));
for banana=freqsamplingrate:freqsamplingrate:(maxfreq-minfreq)
st(banana/freqsamplingrate+1,:)=ifft(vector_fft(minfreq+banana+1:minfreq+banana+n).*g_window(n,minfreq+banana,factor));
if verbose disp('Finished Calculation'),end
function gauss=g_window(length,freq,factor)
vector(1,:)=[0:length-1];
vector(2,:)=[-length:-1];
vector=vector*(-factor*2*pi^2/freq^2);
function [ minfreq,maxfreq,samplingrate,freqsamplingrate] = check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries)
if verbose disp('Array of inputs accepted.'),end
if l > 1 maxfreq = temp(2);,end;
if l > 2 samplingrate = temp(3);,end;
if l > 3 freqsamplingrate = temp(4);,end;
if verbose disp('Ignoring extra input parameters.'),end
if minfreq < 0 | minfreq > fix(length(timeseries)/2);
if verbose disp('Minfreq < 0 or > Nyquist. Setting minfreq = 0.'),end
if maxfreq > length(timeseries)/2 | maxfreq < 0
maxfreq = fix(length(timeseries)/2);
if verbose disp(sprintf('Maxfreq < 0 or > Nyquist. Setting maxfreq = %d',maxfreq)),end
if verbose disp('Swapping maxfreq <=> minfreq.'),end
samplingrate = abs(samplingrate);
if verbose disp('Samplingrate <0. Setting samplingrate to its absolute value.'),end
freqsamplingrate = abs(freqsamplingrate);
if verbose disp('Frequency Samplingrate negative, taking absolute value'),end
disp('st() HELP COMMAND')
disp('st() returns - 1 or an error message if it fails')
disp('USAGE:: [localspectra,timevector,freqvector] = st(timeseries)')
disp('NOTE:: The function st() sets default parameters then calls the function strans()')
disp('You can call strans() directly and pass the following parameters')
disp(' **** Warning! These inputs are not checked if strans() is called directly!! ****')
disp('USAGE:: localspectra = strans(timeseries,minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,removeedge,analytic_signal,factor) ')
disp('Default parameters (available in st.m)')
disp('VERBOSE - prints out informational messages throughout the function.')
disp('REMOVEEDGE - removes the edge with a 5% taper, and takes')
disp('FACTOR - the width factor of the localizing gaussian')
disp(' ie, a sinusoid of period 10 seconds has a ')
disp(' gaussian window of width factor*10 seconds.')
disp(' I usually use factor=1, but sometimes factor = 3')
disp(' to get better frequency resolution.')
disp('Default input variables')
disp('MINFREQ - the lowest frequency in the ST result(Default=0)')
disp('MAXFREQ - the highest frequency in the ST result (Default=nyquist')
disp('SAMPLINGRATE - the time interval between successive data points (Default = 1)')
disp('FREQSAMPLINGRATE - the number of frequencies between samples in the ST results')