IvanNickSim
IvanNickSim

Reputation: 154

Octave - Generate Square Wave (Signal/Pulse)

I want to generate a periodic square wave (Signal/Pulse) in Octave/Matlab with the following properties:

  1. Amplitude of 0.5
  2. Period of 0.02 seconds
  3. Average (Mittelwert) of 0.5

So far I have this:

T = 1/10;
t = linspace(0,T,1001);
y = square(2*pi*50*t);
plot(t,y);
axis([0 0.1 -1.5 1.5]);

That produces this result:

enter image description here

As you may see the amplitude is not correct but since I am a total newbie, I have no idea how to fix it. The period is correct but I'm still not sure what is this average value in the graphical point of view...

Upvotes: 0

Views: 8467

Answers (1)

FangQ
FangQ

Reputation: 1544

this should be pretty fast and straightforward to write, checkout my implementation.

function y=square(t)
% License: public domain
t=t*(1/(pi));
y=ones(size(t));
y(find(bitand(abs(floor(t)),1)))=-1;

a test script:

t = -0.02:.001:.0625;
y = 0.5*square(2*pi*30*t);
plot(t,y,'-o')

Upvotes: 1

Related Questions