Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to plot square wave in Matlab?

1,996 views
Skip to first unread message

Joey W

unread,
Dec 22, 2009, 5:48:04 AM12/22/09
to
Hi,
i have done analysis on simple sine wave x[n]=sin(2*pi*fo*n*(Ts)) , and by perfoming fft(x) I can get the frequency content of the signal..Now, i need to do spectral analysis on some other waveforms, such as square/rectangular wave..thats why i need to know the programming code or the Function Equation for square wave.. I have no idea how to write coding to represent a square wave, hope you can help me..thankyou..

In Matlab Help-> Signal Processing toolbox->waveform generation only show
x=Square(t);
however i fail to generate a square plot with this 1 line of coding, actually how to define t? how can I input the frequency to this plot? and also sampling frequency?

can u show me a sample coding to help me understand it?

Regards,
Joey

Toby

unread,
Dec 22, 2009, 5:58:05 AM12/22/09
to

Is this what you are looking for? A definition of x and y.

x = [0 1 2 2 3 4 5 5 6 7 8]
y = [0 0 0 5 5 5 5 0 0 0 0]
plot(x,y,'-x')

"Joey W" <joey...@hotmail.com> wrote in message <hgq853$fs7$1...@fred.mathworks.com>...

Wayne King

unread,
Dec 22, 2009, 6:47:03 AM12/22/09
to
"Joey W" <joey...@hotmail.com> wrote in message <hgq853$fs7$1...@fred.mathworks.com>...

Hi Joey, did you read

>>doc square

?

The square wave works just like sin(t) and cos(t) except that it takes value +1 and -1.

a 100 Hz square wave

t = 0:0.001:0.1; % Sampling frequency 1 kHz
y = square(2*pi*100*t);
plot(t,y);
axis([0 0.1 -1.5 1.5]);
xlabel('Seconds');
ylabel('Amplitude');

Hope that helps,
Wayne

Joey W

unread,
Dec 22, 2009, 10:04:04 AM12/22/09
to
"Wayne King" <wmki...@gmail.com> wrote in message <hgqbjn$np7$1...@fred.mathworks.com>...


Dear Wayne,
Thanks so so so much!! it really helps.. i can understand already..
if there is a sum of signals, it will be y= square(2*pi*100*t)+ square(2*pi*2000*t); ?
is it correct? if i want to show combination of frequencies..
How to see the above example is taking 1KHz as sampling frequency?

thank you so much!

Regards,
Joey

Wayne King

unread,
Dec 22, 2009, 10:48:05 AM12/22/09
to
"Joey W" <joey...@hotmail.com> wrote in message <hgqn54$9q8$1...@fred.mathworks.com>...
Because the increment in the time vector is 0.001 (1 msec).
You can certainly create a superposition of square waves, but you should keep in mind:

1.) that square waves are not sine waves so you won't just be contributing a single frequency component per square wave. In fact, you should see that you get odd harmonics, e.g. a square wave of 100 Hz will have peaks at 100, 300, 500, Hz
2.) you have to increase your sampling rate, decrease your sampling interval, if you want a square wave at 2 kHz as you've written above. You can't do that if you keep your sampling interval at 0.001.

Hope that helps,
Wayne

Joey W

unread,
Dec 22, 2009, 11:53:04 AM12/22/09
to
"Wayne King" <wmki...@gmail.com> wrote in message <hgqpnl$q6d$1...@fred.mathworks.com>...


Dear Wayne,
Really thanks for your explanation..I have got better understanding on the square wave.
As u said a square wave of 100 Hz will have peaks at 100, 300, 500, Hz, so just now i tried to plot "amplitude versus frequency" to visualize it myself, but the coding i wrote ,cant generate the output as what we want, can you please correct my coding?

t = 0:0.001:0.1;

f= 100;
y = square(2*pi*f*t);


plot(t,y);
axis([0 0.1 -1.5 1.5]);
xlabel('Seconds');
ylabel('Amplitude');

X=fft(y);
subplot (2,1,2), plot (f,abs(X));
xlabel('Frequency');
ylabel('amplitude');

Wayne King

unread,
Dec 22, 2009, 12:03:06 PM12/22/09
to
"Joey W" <joey...@hotmail.com> wrote in message <hgqthg$1l5$1...@fred.mathworks.com>...

You need a proper frequency vector. You have declared f as a scalar and used it as the frequency of the square wave and then you try to use the same variable as a frequency vector in plotting the absolute value of the Fourier transform, but you need a vector of frequencies in the latter case.
Wayne

0 new messages