Continuous Square Pulse Matlab Without Using Square Function
Introduction to Square Wave MATLAB
MATLAB is a programming environment that is interactive and is used in scientific computing. It is extensively used in a lot of 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 in nature; each half is identical in shape for every cycle of a square wave.
Before we get into details of how square waves are created in MATLAB, let us refresh our understanding of Square waves.
Square waves are used widely in micro-electronics and electric circuits for signals requiring timing control. Square waves are symmetrical in nature, with every half of the cycle in a square shape. Mostly every digital circuit uses square waves as their 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 square wave looks like in MATLAB:
Example #1
First, we will 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 expected by us.
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 we can see in the output, the wave obtained has a period of 2 pi and is labeled as per the input passed 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
Square waves are used in the study of electronic circuits to get symmetric waves of square shape. These symmetric square waves find their utility in digital circuits. In MATLAB, we use the square function to create square waves and analyze them. A combination of the square wave and sine wave can also be created in MATLAB for comparison and analysis.
Recommended Articles
This is a guide to Square Wave Matlab. Here we discuss how the code for creating a square wave looks like in MATLAB through examples. You may also have a look at the following articles to learn more –
- Bisection Method Matlab
- Matlab plot title
- feval Matlab
- Matlab Double Integral
Source: https://www.educba.com/square-wave-matlab/
0 Response to "Continuous Square Pulse Matlab Without Using Square Function"
Postar um comentário