MATLAB: How to mark 95% confidence interval with single bar line – example image

confidence interval 95% pwelch error bar

Hello
I'm using the pwelch method of power spectral density estimate, and would like to indicate the confidence interval like as one single bar – just like in the example image attached. At the moment I just plot the pxxc confidence interval array obtained when calling it in the pwelch function, and its looks very confusing (a lot of noise!). Would much prefer to have something like in the image shown…
Thanks!

Best Answer

Do you have the Statistics Toolbox? If so, you can use errorbar()
For example
Fs = 1000;
t = 0:1/Fs:.296;
x = cos(2*pi*t*200)+randn(size(t));
[Pxx,F,Pxxc] = pwelch(x,[],[],[],Fs,'ConfidenceLevel',0.95);
L = mean(10*log10(Pxx)-10*log10(Pxxc(:,1)));
U = mean(10*log10(Pxxc(:,2))-10*log10(Pxx));
plot(F,10*log10(Pxx)); hold on;
errorbar(350,-22,L,U,'ko');
xlabel('Hz'); ylabel('dB');
Note it is important that you plot the PSD estimate in dB because the logarithm is variance-stabilizing for PSD estimate (otherwise the width of the confidence interval is not independent of frequency).