Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to plot square wave in Matlab?

Subject: How to plot square wave in Matlab?

From: Joey W

Date: 22 Dec, 2009 10:48:04

Message: 1 of 7

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

Subject: How to plot square wave in Matlab?

From: Toby

Date: 22 Dec, 2009 10:58:05

Message: 2 of 7


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" <joeywsy77@hotmail.com> wrote in message <hgq853$fs7$1@fred.mathworks.com>...
> 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

Subject: How to plot square wave in Matlab?

From: Wayne King

Date: 22 Dec, 2009 11:47:03

Message: 3 of 7

"Joey W" <joeywsy77@hotmail.com> wrote in message <hgq853$fs7$1@fred.mathworks.com>...
> 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

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

Subject: How to plot square wave in Matlab?

From: Joey W

Date: 22 Dec, 2009 15:04:04

Message: 4 of 7

"Wayne King" <wmkingty@gmail.com> wrote in message <hgqbjn$np7$1@fred.mathworks.com>...
> "Joey W" <joeywsy77@hotmail.com> wrote in message <hgq853$fs7$1@fred.mathworks.com>...
> > 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
>
> 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


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

Subject: How to plot square wave in Matlab?

From: Wayne King

Date: 22 Dec, 2009 15:48:05

Message: 5 of 7

"Joey W" <joeywsy77@hotmail.com> wrote in message <hgqn54$9q8$1@fred.mathworks.com>...
> "Wayne King" <wmkingty@gmail.com> wrote in message <hgqbjn$np7$1@fred.mathworks.com>...
> > "Joey W" <joeywsy77@hotmail.com> wrote in message <hgq853$fs7$1@fred.mathworks.com>...
> > > 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
> >
> > 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
>
>
> 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
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

Subject: How to plot square wave in Matlab?

From: Joey W

Date: 22 Dec, 2009 16:53:04

Message: 6 of 7

"Wayne King" <wmkingty@gmail.com> wrote in message <hgqpnl$q6d$1@fred.mathworks.com>...
> "Joey W" <joeywsy77@hotmail.com> wrote in message <hgqn54$9q8$1@fred.mathworks.com>...
> > "Wayne King" <wmkingty@gmail.com> wrote in message <hgqbjn$np7$1@fred.mathworks.com>...
> > > "Joey W" <joeywsy77@hotmail.com> wrote in message <hgq853$fs7$1@fred.mathworks.com>...
> > > > 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
> > >
> > > 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
> >
> >
> > 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
> 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


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');

Subject: How to plot square wave in Matlab?

From: Wayne King

Date: 22 Dec, 2009 17:03:06

Message: 7 of 7

"Joey W" <joeywsy77@hotmail.com> wrote in message <hgqthg$1l5$1@fred.mathworks.com>...
> "Wayne King" <wmkingty@gmail.com> wrote in message <hgqpnl$q6d$1@fred.mathworks.com>...
> > "Joey W" <joeywsy77@hotmail.com> wrote in message <hgqn54$9q8$1@fred.mathworks.com>...
> > > "Wayne King" <wmkingty@gmail.com> wrote in message <hgqbjn$np7$1@fred.mathworks.com>...
> > > > "Joey W" <joeywsy77@hotmail.com> wrote in message <hgq853$fs7$1@fred.mathworks.com>...
> > > > > 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
> > > >
> > > > 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
> > >
> > >
> > > 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
> > 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
>
>
> 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');

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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us