How to plot a shaded region in pwelch plot from top to bottom

1 visualizzazione (ultimi 30 giorni)
Dear Matlab,
I have a pwelch plot from matlab, for EEG data. I would like to shade in regions from 10-13 hz (on the x axis) from the bottom to the top of the plot, in the color grey.
So far, I can plot only a tiny sliver of grey along the zero line on the Y-axis. (see attached 'pwelchplot' PNG image).
My goal is to have a plot like the other one attached (see 'desiredPWELCHplot' PNG), with grey shaded stripes form top to bottom.
Please help. Code below...(I've deleted other code for simplification)
% load data
eegdata = mean(EEG.data([5,6,15,16],:));
srate = EEG.srate;
% create Hann window
winsize = 2*srate; % 2-second window
hannw = .5 - cos(2*pi*linspace(0,1,winsize))./2;
% number of FFT points (frequency resolution)
nfft = srate*100;
nfft2 = srate*100;
[powspect,hzp] = pwelch(eegdata,hannw,round(winsize/4),nfft,srate,'power');
%% now to extract range-specified power: SIGMA/MU
% boundaries in hz
stimbounds = [ 10 13 ];
% convert to indices
[~,fidx(1)] = min(abs( hzp-stimbounds(1) ));
[~,fidx(2)] = min(abs( hzp-stimbounds(2) ));
figure(5),clf
pwelch(eegdata,hannw,round(winsize/4),nfft,srate,'power');
% shade region in plot
ph = patch([hzp(fidx(1):fidx(2)); hzp(fidx(2)-1:-1:fidx(1))],[powspect(fidx(1):fidx(2)); zeros(diff(fidx),1)],'k');
set(ph,'edgecolor','none','FaceAlpha',.3)

Risposta accettata

Rohit
Rohit il 7 Ott 2022
Hi,
I understand that you are trying to create a grey highlighting strip between 10 Hz and 13 Hz on the x-axis of “pwelch” plot. It is not required to find out the corresponding y-axis indices and the limits before creating a patch since an entire grey column from top to bottom is required. Instead, you can simply set the y-limits. I have added a sample example below to show the same.
clear;
fs = 1000;
t = 0:1/fs:5-1/fs;
x = cos(2*pi*100*t) + randn(size(t));
[powspect,hzp] = pwelch(x,500,300,500,fs);
lowerlim=200;
higherlim=250;
pwelch(x,500,300,500,fs);
%%shade region in plot
hold on;
ylim([-50,40])
ph= patch([lowerlim higherlim higherlim lowerlim], [ [1 1]*min(ylim) [1 1]*max(ylim)],[ 0.5 0.5 0.5],'FaceAlpha',0.3, 'EdgeColor','none');
hold off;
In this example I have used dummy data to create “pwelch” plot. You can modify the “ylim,”” lowerlim” and “higherlim” based on your use case.
You can refer to the following documentation link for more information on the “pwelch” function:

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by