Pwelch only returns a single value?

1 view (last 30 days)
Shayma Al Ali
Shayma Al Ali on 25 Feb 2022
Answered: Shushant on 25 Sep 2023
I am trying to use the pwelch method in order to calculate the coherence magnitude squared between two signals with confidence intervals. However, the pwelch function only returns a single value. What am I doing wrong?
I also attached the variables I use in my code.
My code:
%calculate pwelch
fs=1/i_ph1.spacing_l;
[pwelch_1,f1,pxx1]=pwelch(i_ph1.l,hanning(length(i_ph1.l)),(length(i_ph1.l))/2,fs,'ConfidenceLevel',0.95);
[pwelch_2,f2,pxx2]=pwelch(i_ph2.l,hanning(length(i_ph2.l)),(length(i_ph2.l))/2,fs,'ConfidenceLevel',0.95);
[pwelch_3,f3,pxx3]=pwelch(i_ph3.l,hanning(length(i_ph3.l)),(length(i_ph3.l))/2,fs,'ConfidenceLevel',0.95);
[pwelch_12,f12,pxx12]=pwelch(xcorr(i_ph1.l,i_ph2.l),hanning(length(i_ph1.l)),(length(i_ph1.l))/2,fs,'ConfidenceLevel',0.95);
[pwelch_23,f23,pxx23]=pwelch(xcorr(i_ph2.l,i_ph3.l),hanning(length(i_ph2.l)),(length(i_ph2.l))/2,fs,'ConfidenceLevel',0.95);
[pwelch_31,f13,pxx13]=pwelch(xcorr(i_ph3.l,i_ph1.l),hanning(length(i_ph3.l)),(length(i_ph3.l))/2,fs,'ConfidenceLevel',0.95);
%calculate CMS
%Pxy^2/Pxx*Pyy
CMS12=(pwelch_12).^2./(pwelch_1.*pwelch_2);
CMS23=(pwelch_23).^2./(pwelch_2.*pwelch_3);
CMS13=(pwelch_31).^2./(pwelch_3.*pwelch_1);
%calculate CMS of conf intervals
CMS_c13=(pxx12).^2./(pxx1.*pxx2);
CMS_c23=(pxx23).^2./(pxx2.*pxx3);
CMS_c13=(pxx13).^2./(pxx1.*pxx3);
  1 Comment
Mathieu NOE
Mathieu NOE on 8 Mar 2022
hello
have you tried mscohere ?
help mscohere
mscohere Magnitude Squared Coherence Estimate.
Cxy = mscohere(X,Y) estimates the magnitude squared coherence estimate
of the system with input X and output Y using Welch's averaged,
modified periodogram method. Coherence is a function of frequency with
values between 0 and 1 that indicate how well the input X corresponds
to the output Y at each frequency. The magnitude squared coherence,
Cxy, is given by Cxy = (abs(Pxy).^2)./(Pxx.*Pyy) where Pxx and Pyy are
the power spectral density (PSD) estimate of X and Y, respectively; and
Pxy is the Cross-PSD (CPSD) estimate of X and Y. See "help pwelch" and
"help cpsd" for complete details.

Sign in to comment.

Answers (1)

Shushant
Shushant on 25 Sep 2023
Hi Shayma Al Ali,
I understand that you are using the "pwelch" function to calculate the coherence magnitude squared between two signals with confidence intervals. But the function is only returning a single value. This is because the "pwelch" function is expecting "fs" to be the fifth argument.
To solve this issue, specify the fourth argument as empty "[]". Refer to the following code snippet for the modified function call:
[pwelch_1,f1,pxx1]=pwelch(i_ph1.l,hanning(length(i_ph1.l)),(length(i_ph1.l))/2,[],fs,'ConfidenceLevel',0.95);
Refer to the following documentation for information on why the fourth argument should be empty-
I hope this helped in solving the issue you were facing.
Thank you,
Shushant

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!