13 views (last 30 days)

QUESTION:

Hi everyone

Is it possible to calculate the covariance of two signals, not by the standard cov function, but by wcoherence instead? Below is a code example:

% make data

rng default;

t = 0:0.001:2;

x = cos(2*pi*10*t).*(t>=0.5 & t<1.1)+cos(2*pi*50*t).*(t>= 0.2 & t< 1.4)+0.25*randn(size(t));

y = sin(2*pi*10*t).*(t>=0.6 & t<1.2)+sin(2*pi*50*t).*(t>= 0.4 & t<1.6)+ 0.35*randn(size(t));

% calculate cross-spectrum

[~,wcs,f,coi] = wcoherence(x,y,1/0.001);

wcs=real(wcs);

% covariances

cc_cov=cov(x,y);cc_cov=cc_cov(2);

cc_wcoh=trapz(t,trapz(f,wcs)); %????!

% plot results

figure

h = pcolor(t,log10(f),wcs);

h.EdgeColor = 'none';

ax = gca;

hold on;

plot(ax,t,log10(coi),'k--','linewidth',2);

colorbar;

ax.XLabel.String='Time (s)';

ax.YLabel.String='Logarithmic frequency';

ax.Title.String = {'Wavelet cross spectrum';['cov(x,y)=',num2str(cc_cov),' | cov_{wavelet}=',num2str(cc_wcoh)]};

Now, obviously my understanding of how to calculate the covariance from the wcoherence output (cov_wav) is flawed.

Can anyone help me get this right?

Essentially what I am trying to do is to calculate the cross spectrum of two signals and determine which regions in the resulting scalogram to include in an estimate of a covariance, as opposed to including everything (as is the case with the cov function).

Thanks in advance!

Cheers

Jakob

Bjorn Gustavsson
on 19 Feb 2019

Well, to me it looks as if the wcoherence function returns a normalized cross-spectra, i.e. something like

WC = FT(A).*FT(B)/sqrd(|FT(A)|*|FT(B)|)

Since the covariance are not normalized like that (the correlation-matrix is), you cant go from WC - COV. Perhaps you can select periods to include in covariance clculations from time-periods with interesting coherence.

HTH

Bjorn Gustavsson
on 26 Feb 2019

Your choise of frequencies seems a bit odd to me. If you just take the inverse of the first few frequencies you get the corresponding period-times of the low-frequency Fourier-component:

2400 2375.9 2352 2328.4 2305

That is very long period-times compared to the time of your window of 200 samples (10 s). Your entire measurement time is 2400 s, if I got it rights. If you want to separate variations with such periods you'll have to have data from a longer observation-period. Would this be something that gives good enough cross-spectra?

Fin = linspace(0,10,400);

[SA,F,T,PA] = spectrogram(A,400,200,Fin,20);

[SB,F,T,PB] = spectrogram(B,400,200,Fin,20);

XAB = real(SA.*conj(SB));

subplot(3,1,1)

pcolor(T,F,asinh(XAB)),shading flat

colorbar

subplot(3,1,2)

pcolor(T,F(5:end),XAB(5:end,:)),shading flat

colorbar

subplot(3,1,3)

pcolor(T,F(1:15),XAB(1:15,:)),shading flat

colorbar

HTH

Bjorn Gustavsson
on 5 Mar 2019

Well dont jump to that conclusion, the power-spectra is the Fourier-transform of the autocorrelation function, likewise for the cross-covariance and cross-spectra:

x = 0:100;

K = exp(-(x-20).^2/5^2) + exp(-(x-70).^2/12^2);

a = randn(5000,1);

b = randn(5000,1);

A = conv2(a,K','full');

B = conv2(b,K','full');

clf

subplot(2,1,1)

plot(fftshift(ifft((fft(A).*conj(fft(A)))))/numel(A),'r')

hold on

plot(xcorr(A,numel(A)/2,'unbiased'))

subplot(2,1,2)

xcAB = xcorr([A,B],numel(A)/2,'unbiased');

plot(fftshift(ifft((fft(A).*conj(fft(B)))))/numel(A),'r')

hold on

plot(xcAB(:,2))

The "only" difference between this and what you get with the spectrogram/wavelet spectra is that those methods cuts the data into shorter (possibly overlapping segments) and applies the same functions to those data-segements. Thus making it possible to see temporal variations in spectral content/covariance.

Opportunities for recent engineering grads.

Apply Today
## 12 Comments

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_675276

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_675276

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_675283

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_675283

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_677247

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_677247

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_677255

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_677255

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_677263

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_677263

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_677265

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_677265

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_677704

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_677704

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_678055

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_678055

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_678100

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_678100

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_678136

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_678136

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_678190

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_678190

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_678585

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/445586-how-to-calculate-covariance-using-the-wcoherence-function#comment_678585

Sign in to comment.