How to Generate Periodic and Aperiodic Sequence in MATLAB?


A periodic sequence is one which repeats its pattern at regular intervals, whereas an aperiodic sequence is another type of sequence in which there is no regularity in the pattern. We can use MATLAB to generate periodic and aperiodic sequences.

Before going to learn about generating periodic and aperiodic sequences using MATLAB, let us first get a brief overview of periodic and aperiodic sequences individually.

What is a Periodic Sequence?

A sequency of data points which has a regular pattern that repeats itself at a certain interval is called a periodic sequence. The interval over which the pattern of the periodic sequence repeats itself is known as a period.

Mathematically, a periodic sequence is expressed as follows −

$$\mathrm{x[n]=x[n+N]}$$

Hence, the periodic sequence can also be defined as a sequence whose value at a time "n" is same as the value at another time instant "n + N".

A sinusoidal sequence i.e., sine or cosine wave is a common example of a periodic sequence.

What is an Aperiodic Sequence?

An aperiodic sequence is one which does not have a definite or repeating pattern. Therefore, for an aperiodic sequence, there is no constant period, but it has an irregular period that can have any unpredictable value.

The white noise signal is an example of an aperiodic sequence.

Let us now learn to generate periodic and aperiodic sequences using MATLAB.

How to Generate a Periodic Sequence in MATLAB?

As mentioned above, a periodic sequence has a definite and regularly repeating pattern over a time interval. In MATLAB, we can generate different types of periodic sequences, such as "sin", "cosine", "square", and more.

Let us take some examples for generating periodic sequences in MATLAB.

Generate a Periodic Sine Wave Sequence

You can use the following code to generate a periodic sine wave sequence in MATLAB −

Example

% MATLAB code to generate a periodic sine wave sequence
% Define the sequence parameters
f = 2;		% Frequency in Hz
t = 0:0.01:5;	% Time vector from 0 to 5 seconds

% Generate a periodic sine wave sequence
periodic_seq = sin(2*pi*f*t);

% Display the periodic sequence
plot(t, periodic_seq);
xlabel('Time');
ylabel('Amplitude');
title('Periodic Sine Wave Sequence');

Output

When you run this code, it will produce the following output −

Generate a Periodic Cosine Wave Sequence

Take a look at this MATLAB code −

Example

% MATLAB code to generate a periodic cosine wave sequence
% Define the sequence parameters
f = 2;	% Frequency in Hz
t = 0:0.01:5;	% Time vector from 0 to 5 seconds

% Generate a periodic sine wave sequence
periodic_seq = cos(2*pi*f*t);

% Display the periodic sequence
plot(t, periodic_seq);
xlabel('Time');
ylabel('Amplitude');
title('Periodic Cosine Wave Sequence');

Output

When you run this code, it will produce the following output −

Generate a Periodic Square Wave Sequence

Example

% MATLAB code to generate a periodic square wave sequence
% Define the sequence parameters
f = 2;		% Frequency in Hz
t = 0:0.01:5;	% Time vector from 0 to 5 seconds

% Generate a periodic square wave sequence
periodic_seq = square(2*pi*f*t);

% Display the periodic square wave sequence
plot(t, periodic_seq);
xlabel('Time');
ylabel('Amplitude');
title('Periodic Square Wave Sequence');

Output

When you run this code, it will produce the following output −

Generate a Periodic Sawtooth Wave Sequence

Example

% MATLAB code to generate a periodic sawtooth wave sequence
% Define the sequence parameters
f = 2;		% Frequency in Hz
t = 0:0.01:5;	% Time vector from 0 to 5 seconds

% Generate a periodic sawtooth wave sequence
periodic_seq = sawtooth(2*pi*f*t);

% Display the periodic sequence
plot(t, periodic_seq);
xlabel('Time');
ylabel('Amplitude');
title('Periodic Sawtooth Wave Sequence');

Output

When you run this code, it will produce the following output −

How to Generate an Aperiodic Sequence in MATLAB?

As explained above, an aperiodic sequence is one which do not have a regular or predictable pattern that repeats itself over a certain interval. In MATLAB, we can generate an aperiodic sequence either by specifying values of the sequence or by using a random number generator i.e., "randn".

The following examples demonstrate the process of generating the aperiodic sequences using MATLAB.

Generate an Aperiodic Sequence by Specifying Values

Example

% MATLAB code to generate aperiodic sequence by specifying sequence values
% Specify the random values of the sequence
x = [1, 3, 4, 7, 2, 5, 3, 1, 7, 3];

% Specify the sequence length
n = 1:10;

% Display the aperiodic sequence for given values
stem(n, x);
xlabel('Sample');
ylabel('Value');
title('Aperiodic Random Sequence');

Output

When you run this code, it will produce the following output −

Generate an Aperiodic Sequence using Random Number Generator

Example

% MATLAB code to generate aperiodic sequence using "randn" function
% Specify the sequence length
n = 1:50;

% Generate random values of the sequence
x = randn(size(n));

% Display the aperiodic sequence
stem(n, x);
xlabel('Sample');
ylabel('Value');
title('Aperiodic Random Sequence');

Output

When you run this code, it will produce the following output −

Conclusion

This is all about generating periodic and aperiodic sequences using MATLAB. In conclusion, periodic and aperiodic sequences are elementary concepts in the field of mathematics and signal processing. In this tutorial, I explained how you can utilize MATLAB to generate the periodic and aperiodic sequences of different types.

Updated on: 05-Oct-2023

99 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements