plot square with a given average value

Hi all,
I want to ask a question about plotting a square waveform by using Matlab. I can plot the square waveform with square() function but i don't know how i can draw a square wave with a certain average value. i must plot a square waveform with an average value -2.
Best regards, Dilem

Antworten (1)

Wayne King
Wayne King am 24 Mär. 2012

0 Stimmen

t = 0:.0001:.0625;
y = square(2*pi*30*t)-2;
plot(t,y); set(gca,'ylim',[-3.5 -0.5]);
hold on; grid on;
plot(t,mean(y).*ones(length(y),1),'r');
If you want it exactly -2,
y = square(2*pi*30*t);
y = y-mean(y)-2;
plot(t,y); set(gca,'ylim',[-3.5 -0.5]);
hold on; grid on;
plot(t,mean(y).*ones(length(y),1),'r');

3 Kommentare

dilem
dilem am 24 Mär. 2012
Isn't average value equal to the area that is underneath positive one period of square wave? if so how can an area be negative? how can I calculate -2 as average value from your solution?
Wayne King
Wayne King am 24 Mär. 2012
no, look at the mean-value theorem for integrals if you want to think of the square wave as a continuous time function. If you want to think of it as a vector, you simply sum the elements of the vector and divide by the number of elements.
y = square(2*pi*30*t);
y = y-mean(y)-2;
mean(y)
you get -2. The mean of y is -2. You can also see from the plot that I showed you how to produce that the mean value is -2.
dilem
dilem am 24 Mär. 2012
thank you for your attention and time.

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 24 Mär. 2012

Community Treasure Hunt

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

Start Hunting!

Translated by