import dascore as dc
from dascore.units import m, s
# Get a patch composed of sin waves whose correlation results
# can easily be checked.
= dc.get_example_patch(
patch "sin_wav",
=100,
sample_rate=range(10, 20),
frequency=5,
duration=10,
channel_count=0.5).set_units(distance='m')
).taper(time
# Example 1
# Calculate cc for all channels as receivers and
# the 10 m channel as the master channel. Squeeze the output
# so the returned patch is 2D.
= patch.correlate(distance = 10 * m).squeeze()
cc_patch
# Example 2
# Get cc within (-2,2) sec of lag for all channels as receivers
# and the 10 m channel as the master channel. The new patch has dimensions
# (lag_time, distance, source_distance)
= (
cc_patch = 10 * m)
patch.correlate(distance =(-2, 2))
.select(lag_time
)
# Example 3
# First remove every other distance channel (less memory usage)
# the use the new 2nd channel as the source.
= (
cc_patch =2, filter_type=None)
patch.decimate(distance=1, samples=True)
.correlate(distance
)
# Example 4
# Correlate along time dimension (perhaps for template matching
# applications)
= patch.correlate(time=100, samples=True)
cc_patch
# Example 5
# A pipeline of frequency domain correlation and an array of sources
= patch.pad(time="correlate") # pad to at least 2n + 1
padded_patch = patch.dft("time", real=True)
dft_patch # Any other pre-processing steps go here...
# ...
# Perform the correlation with 3 source channels
= dft_patch.correlate(distance=[1, 3, 7], samples=True)
cc_patch # Perform any post-processing here
# ...
# Convert back to time domain, apply `correlate shift` to undo
# fft related shifting and scaling as well as create lag coordinate.
= cc_patch.idft().correlate_shift("time") cc_out
correlate
correlate(
patch: Patch ,
samples = False,
lag = None,
**kwargs ,
)-> ‘PatchType’
Correlate source row/columns in a 2D patch with all other row/columns.
Correlations are done in the frequency domain. This function can accept a patch whose target dimension has already been transformed with the Patch.dft
method, otherwise the dft will be performed. If the input has already been transformed, Patch.correlation_shift
is useful to undo dft artefacts after the idft is applied.
While a 2D patch is required for input, a 3D patch is returned where the 3rd dimesion cooresponds to the source rows/columns. For the case of a single source, the Patch.squeeze
method can be helpful to remove length 1 dimensions.
Parameters
Parameter | Description |
---|---|
patch : PatchType |
The input data patch to be cross-correlated. Must be 2-dimensional. The patch can be in time or frequency domains. |
samples : bool, optional (default = False) |
If True, the argument specified in kwargs refers to the sample not value along that axis. See examples for details. |
lag | Deprecated, just use select on the output patch instead. |
**kwargs |
Specifies correlation dimension and the master source(s), to which we want to cross-correlate all other channels/time samples.If the master source is an array, the function will compute correlations for all the posible pairs. |
Examples
1 - The cross-correlation is performed in the frequency domain.
2 - The output dimension is opposite of the one specified in kwargs and shares a name with the original coord except the string “lag_” is prepended. For example, “lag_time”.