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

回答 (1 件)

Wayne King
Wayne King 2012 年 3 月 24 日

0 投票

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 件のコメント

dilem
dilem 2012 年 3 月 24 日
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 2012 年 3 月 24 日
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 2012 年 3 月 24 日
thank you for your attention and time.

サインインしてコメントする。

質問済み:

2012 年 3 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by