Engine
Engine

Reputation: 5422

fill function in MATLAB

I'm having issues unterstanding the function fill in MATLAB , I have a PSD of a file the I want to change it background like :

[xPSD,f] = pwelch(x,hanning(4096),2048,4096*2 ,fs);
    plot(f,10*log10(xPSD));

x= f(100:150);
y= 10*log10(xPSD(100:150))
fill(x,y,'y')

the result is in the right direction but not what I need : image

I would like get the color tell x axis like : made with MS paint

is their a way to do this

Upvotes: 1

Views: 416

Answers (2)

LowPolyCorgi
LowPolyCorgi

Reputation: 5171

Yes, there is always a way ;) In your case, you simply need to add two points in x and y that go to the top boudary of the plot:

x = f(100:150);
y = 10*log10(xPSD(100:150))

% Add two points
Y = ylim;
x = [x(1) ; x(:) ; x(end)];
y = [Y(2) ; y(:) ; Y(2)];

% Display filled area
fill(x,y,'y')

Best,

Upvotes: 1

Xxxo
Xxxo

Reputation: 1931

A working solution is:

[xPSD,f] = pwelch(x,hanning(4096),2048,4096*2 ,fs);
plot(f,10*log10(xPSD));
hold on

x= f(100:150);
y= 10*log10(xPSD(100:150));

yMax = ylim;
yMax = yMax(2);
x = x'; % Use this line only in the case that the size(x, 1) > 1
X = [x fliplr(x)];
Y = [y' ones(1, length(y)) .* yMax];
fill(X, Y, 'y')

What you were missing is that fill method looks for an area to fill. In the above code the area is defined by pairs of points. That, for the first (i.e. lower part) of the area we have the x vector and the y points. The second area (i.e. the upper part) is defined by the reversed vector x (image your pencil to first starting drawing towards rights for the lower part and then for the upper going left) and the points of the upper limit of your axes.

Edit:

Minimal example with the handel data from MATLAB:

load handel;
x = y; % Just to be consistent with the OP
fs = Fs; % Just to be consistent with the OP

[xPSD,f] = pwelch(x,hanning(4096),2048,4096*2 ,fs);
plot(f,10*log10(xPSD));
hold on

x= f(100:150);
y= 10*log10(xPSD(100:150));

yMax = ylim;
yMax = yMax(2);
x = x'; % Use this line only in the case that the size(x, 1) > 1
X = [x fliplr(x)];
Y = [y' ones(1, length(y)) .* yMax];
fill(X, Y, 'y')
xlim([0 200]) % To focus on the result

The result is:

enter image description here

Upvotes: 2

Related Questions