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