plot square with a given average value
Show older comments
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
Answers (1)
Wayne King
on 24 Mar 2012
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 Comments
dilem
on 24 Mar 2012
Wayne King
on 24 Mar 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
on 24 Mar 2012
Categories
Find more on Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!