EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Square Wave Matlab
 

Square Wave Matlab

Updated July 6, 2023

Square Wave Matlab

 

 

Introduction to Square Wave Matlab

Matlab is a programming environment that is interactive and is used in scientific computing. It is extensively used in many technical fields where problem-solving, data analysis, algorithm development, and experimentation are required. In addition, discipline-specific software is extensively written using Matlab. In this article, we will study Square waves in Matlab. Square waves are symmetric; each half is identical in shape for every square wave cycle.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Before we get into details of how square waves are created in Matlab, let us refresh our understanding of Square waves.

Microelectronics and electric circuits extensively utilize square waves for signals requiring precise timing control. Square waves are symmetrical, with every half of the cycle square. Mostly every digital circuit uses square waves as its input & output.

The square waves are different from sine waves because sine waves have round corners with smooth transitions during a rise & a fall. Whereas square waves have almost vertical rise & fall with flat tops & bottoms.

Syntax:

Let us understand the Syntax for creating square waves in Matlab

s = square (t);

This function is used in Matlab to create square waves.

Description of square function:

square (t) will generate a square wave of default period 2π. The square wave is created for all elements of the input time array.

Examples of Square Wave Matlab

Let us see how the code for creating a square wave looks like in Matlab:

Example #1

First, we need an input time array to create our square wave. For that, we will define an input array with equally spaced values between -2 pi to 2 pi

t = linspace (-2 * pi, 2 * pi)

[creating an array of values between -2 pi to 2 pi]

s = square (t)

[calling square function with our input array]

plot (s)

[plotting the square wave using plot function]

Input:

t = linspace (-2 * pi, 2 * pi)
s = square (t)
plot (s)

Output:

Square Wave Matlab output 1

As we can see in the output, the wave obtained has square-shaped halves with a flat top and a flat bottom. Also, the wave has a period of 2 pi, as we expected.

Example #2

In the next example, we will take our input array as values between 0 to 5 pi. Also, we will give a label to our square wave.

t = linspace (0, 5 * pi)

[creating an array of values between 0 to 5 pi]

s = square (t)

[calling square function with our input array]

plot (s)

[plotting the square wave using plot function]

xlabel (‘Learning Square Wave’)

[Defining the label for our square wave]

Input:

t = linspace (0,5 * pi)
s = square (t)
plot (s)
xlabel ('Learning Square Wave')

Output:

Square Wave Matlab output 2

As observed in the output, the obtained waveform exhibits a period of 2 pi and is appropriately labeled based on the input provided by us.

Example #3

While studying electronic circuits, there are situations when we need to analyze both sine waves and square waves together. In such cases, we normalize the waves and plot them together. Let us understand how to do this in Matlab.

t = linspace (0, 4 * pi);

[creating an array of values between 0 to 4 pi]

s = square (t);

[calling square function with our input array]

plot (t / 2 * pi, s, t / 2 * pi, sin (t))

[plotting the square wave and sine wave using plot function. Also, notice that we have normalized the time period by 2*pi]

xlabel (‘Combining Square and Sine Wave’)

[Defining the label for our square wave]

Input:

t = linspace (0,4 * pi)
s = square (t)
plot (t / 2 * pi, s, t / 2 * pi, sin (t))
xlabel (' Combining Square and Sine Wave ')

Output:

output 3

As we can see in the output, we have 2 waves, sine, and square, embedded in the same curve. This is very useful while analyzing waves in electronic signals

Conclusion

Researchers and engineers use square waves to obtain symmetric waveforms with a square shape in the study of electronic circuits. These symmetric square waves find their utility in digital circuits. In Matlab, we use the square function to create and analyze square waves. In Matlab, generating and analyzing a combination of square and sine waves is possible for comparison and analysis.

Recommended Articles

We hope that this EDUCBA information on “Square Wave Matlab” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Bisection Method Matlab
  2. Matlab plot title
  3. feval Matlab
  4. Matlab Double Integral

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW