# Spectral Coherence in Julia

Hi everyone, I am trying to do the spectral coherence between two signals in julia. Much like mscohere in Matlab.

https://www.mathworks.com/help/signal/ug/measuring-signal-similarities.html

I guess DSP.jl must have it, but no example is provided.
Any help?

Hi @joa-quim, wow I did not think about GMT. I have used the coherence for gravity admittance. Do you think it will work for seismic data?

Well, the code just swallows numbers so why not?

In `DSP.jl`, there are cross power spectrum analyses.

1 Like

Hi @rafael.guerra! Thanks, I tried it a bit, but I cannot get it to run, any example around?

That is a great philosophy. Let me try and report back hahaha

What have you tried? Please provide a MWE.

I tried the basic

`````` mt_cross_power_spectra([data[1,:] data[2,:])
``````

A data matrix in:

data

Check out the docs that were linked; the input should be a matrix of size `n_channels` x `n_samples`.

You should write:
`mt_cross_power_spectra(data[1:2,:])`

3 Likes

That works great

Also,

``````A=[data[1,:] data[2,:]]'
B=mt_cross_power_spectra(A)
``````

But how do I get the spectra from this? I found the power and the frequencies, but not the â€śspectrumâ€ť :s

1 Like

Assuming `r` is the result above, the frequency is `r.freq` and for two input signals `x`, ` y`, the x-y off-diagonal entry of the CSD matrix is: `r.power[1,2,:]`

The r.freq is in sample units I guess. Can I pass a time vector, or sample rate so the freq are in hz?

it assumes the data is regularly sampled, but you can pass the keyword argument `fs` to set the sampling rate

HAHAH sorry for that dumb question. I found it in the doc after you said so!
Thanks a lot! @ericphanson , @rafael.guerra and @joa-quim.

Maybe an extra question. I am testing several ways to compare these signals. I have used the cross-correlation function, the pearson coeficient and now this spectral (if I understand it measures the frequencies where 2 signals are similar). Any other ideas to do this? I have several signals and I want to classify them, if I can group them in families.

You could try dynamic time warping as a time-domain approach. The README of GitHub - baggepinnen/DynamicAxisWarping.jl: Dynamic Time Warping (DTW) and related algorithms in Julia, at Julia speeds shows how to use that package for clustering for example.

2 Likes

WOW. I did not know about DAWâ€¦ I see maybe example 1 is close to what I like but I need to read about this. Any paper on the matter you can recommend?

I also thought about Matrix profile, see if I can cat all the signals and find motives. Thoughs?

Hey there

Iâ€™ve contributed to both DynamicAxisWarping.jl and MatrixProfile.jl, and similar to you, my motivation for this came from measuring distances and similarities between spectra.

I implemented several higher-level features in the package

maybe you could find something that would fit you there. The docs and the associated paper should have quite a few examples

3 Likes

Hi! Thanks for the input.

Just to clarify a bit, my particular problem. I have some signals like this ones (19 so far but more on the way):

Usually people use template matching to say â€śwell signal 1 4 and 7 look alike and 3, 8 and 14 look allike too, so this is group and and this is group Bâ€ť. I want something different to do this clasification. I was looking into MatrixProfile. What would you recommend? Maybe an example that leads me in the right direction :). Thanks in advance

1 Like