Updated July 6, 2023

## 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.

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:**

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:**

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:**

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.