<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Ambient Noise Waveforms Preprocessing Methods Examples
The following notebook contains examples for using the psprocess.py toolbox for preprocessing raw seismic waveforms to a point where they can be used for cross-correlations. Currently the script can only operate with MSEED formats, but additional support for other formats such as
Step1:  The Preprocess class requires many input parameters to function. Below is a list of examples.
Step2:  The following processing examples are in order from Bensen et al. (2007). The final example has all of them combined.
-    First, the trace has its instrument response removed. 
-    Second, the trace is trimmed, demeaned and detrended. 
-    Third, the trace is passed through a butterworth band-pass filter to remove high amplitude noise and event signals as much as possible.
-    Fourth, the trace is downsampled to allow for swifter processing. The closer to the original sample rate this is left, the longer overall processing will take!
-    Fifth, the trace is normalised. This can be specified as either time-normalised or one-bit normalised.
-    Sixth, the spectrum of the waveform is 'whitened'.
Take note that for the purposes of this example, the instrument response has been kept. This is because the metadata file for this waveform is having some technical difficulties. The resulting waveforms and techniques posed here are still valid for example purposes.
Step3:  Next, downsample the example_trace. The output downsampled trace is dictated by the variable PERIOD_RESAMPLE. The new sample rate is 1/PERIOD_RESAMPLE
Step4:  Normalise the trace, either with respect to time, or the one-bit normalisation procedure.
Step5:  Finally spectrally whiten the trace.
<ASSISTANT_TASK:>
Python Code:
from pysismo.pspreprocess import Preprocess
from obspy import read 
from obspy.core import Stream
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline 
Explanation: Ambient Noise Waveforms Preprocessing Methods Examples
The following notebook contains examples for using the psprocess.py toolbox for preprocessing raw seismic waveforms to a point where they can be used for cross-correlations. Currently the script can only operate with MSEED formats, but additional support for other formats such as: SAC, SEED and SUDS will likely be added in the future. The user specifies the input path to the raw waveform. This waveform should be only one trace for the purposes of this example, e.g. BHZ. A Preprocess object is created with this input, and the output is specified by one of the functions contained. 
The theory for the current workflow that this example follows for preprocessing is explained in depth in Bensen et al. (2007).
End of explanation
# list of example variables for Preprocess class
FREQMAX = 1./1                            # bandpass parameters
FREQMIN = 1/20.0
CORNERS = 2
ZEROPHASE = True 
ONEBIT_NORM = False                       # one-bit normalization
PERIOD_RESAMPLE = 0.02                     # resample period to decimate traces, after band-pass
FREQMIN_EARTHQUAKE = 1/75.0               # earthquakes periods band
FREQMAX_EARTHQUAKE = 1/25.0   
WINDOW_TIME = 0.5 * 1./FREQMAX_EARTHQUAKE # time window to calculate time-normalisation weights
WINDOW_FREQ = 0.0002                      # freq window (Hz) to smooth ampl spectrum
# here is a list of all of the functions and variables that the Preprocess class contains
help(Preprocess)
# set the path to the desired waveform, the example HOLS.mseed is provided. 
example_path = 'tools/examples/HOLS.mseed'
# import a trace from the example waveform
example_trace = read(example_path)[0]
# initialise the Preprocess class
PREPROCESS = Preprocess(FREQMIN, FREQMAX, FREQMIN_EARTHQUAKE, 
                        FREQMAX_EARTHQUAKE, CORNERS, ZEROPHASE, 
                        PERIOD_RESAMPLE, WINDOW_TIME, WINDOW_FREQ, 
                        ONEBIT_NORM)
Explanation: The Preprocess class requires many input parameters to function. Below is a list of examples.
End of explanation
# process the band-pass filtered trace
# the bands are set from the above freqmax and freqmin parameters entered when the class is initialised.
example_trace = PREPROCESS.bandpass_filt(example_trace)
st = Stream(traces=[example_trace])
st.plot()
Explanation: The following processing examples are in order from Bensen et al. (2007). The final example has all of them combined.
-    First, the trace has its instrument response removed. 
-    Second, the trace is trimmed, demeaned and detrended. 
-    Third, the trace is passed through a butterworth band-pass filter to remove high amplitude noise and event signals as much as possible.
-    Fourth, the trace is downsampled to allow for swifter processing. The closer to the original sample rate this is left, the longer overall processing will take!
-    Fifth, the trace is normalised. This can be specified as either time-normalised or one-bit normalised.
-    Sixth, the spectrum of the waveform is 'whitened'.
Take note that for the purposes of this example, the instrument response has been kept. This is because the metadata file for this waveform is having some technical difficulties. The resulting waveforms and techniques posed here are still valid for example purposes.
End of explanation
# Previous trace sample rate:
print 'Initial trace sample rate: ', example_trace.stats.sampling_rate
# Downsample trace
example_trace = PREPROCESS.trace_downsample(example_trace)
print 'Downsampled trace sample rate: ', example_trace.stats.sampling_rate
Explanation: Next, downsample the example_trace. The output downsampled trace is dictated by the variable PERIOD_RESAMPLE. The new sample rate is 1/PERIOD_RESAMPLE
End of explanation
example_trace_copy = example_trace
# one-bit normalization
example_trace.data = np.sign(example_trace.data)
st = Stream(traces=[example_trace])
# plot the one-bit normalised trace
st.plot()
# copy the trace for time normalisation
example_trace = example_trace_copy
# process for time normalisation
example_trace = PREPROCESS.time_norm(example_trace, example_trace_copy)
st = Stream(traces=[example_trace])
# plot the time normalised trace
st.plot()
Explanation: Normalise the trace, either with respect to time, or the one-bit normalisation procedure.
End of explanation
# process the whitened spectrum for the trace
example_trace = PREPROCESS.spectral_whitening(example_trace)
st = Stream(traces=[example_trace])
# plot the time normalised trace
st.plot()
Explanation: Finally spectrally whiten the trace.
End of explanation
<END_TASK>