# Periodic Signals Tutorial

|In this post, we are going to see a practical study of periodic signals in Matlab. We are going to post the code and the results here so you can follow the exercise step by step. It’s very important for you to read the comments on the code, so that will help you to understand better what we are doing.

## Exercise 1: Create a periodic signal from one period

## Step 1: Function to create a periodic signal

function periodic=per(x, Nr) periodic=x; for i=1:Nr-1 periodic=[periodic x];%we add elements to signal;</span> %in this case, we add a repetition of the signal itself, %the number of repetitions is Nr -1 end;

## Step 2: Create a periodic signal using the previous function

t=[-20:1:20]; x=rectpuls(t,20); plot(t,x) pause %we need to use the command "pause" here if we want to show the rectangle plot, followed by its periodic form y=x; Nr=input('Number of repetition=');% you can enter manually the number of repetitions that you want in the command prompt when running the script periodic=per(y,Nr) plot(periodic)

## Exercise 2: Change the time origin

## Step 1: Function to delay a sinusoidal signal

function del=delay(x,n,start,L) pulse=deltas(start,L,n);%call the "deltas" function% pause del=conv(x,pulse);%convolve our signal with the delta% L2=length(del);%L2 represents the length of the convolved signal% positions_axis=[start:1:start+L2-1]; % the positions' axis goes from "start to start+L-1, taking each element sample by sample% plot(positions_axis,del)

## Step 2: Function to create a delta

As you can observe, the previous function call to the “deltas” function, which we have decided to write separately, so you can understand the process easily:

function delta=deltas(start,L,n) delta=zeros(1,L); %we create a vector of zeros with length L% positions_axis =[start:1:start+L-1]; position=n-start+1; %this is the position where we want to place the delta; it is a relative position in the positions_axis% delta(position)=1; %this is the position where we place the delta (with amplitude=1)% stem(positions_axis, delta) %we plot our delta%

The delta function is necessary to shift the signal, as the convolution of the signal and the delta gives, as an output, the original signal shifted to the delta position.

## Step 3: Script to create 2 delayed cosines

## Write the following code in a separated script:

frequency=10; time_axis=0:0.001:1; A=cos(2*pi*frequency*time_axis+ pi/4); plot(time_axis,A) pause del=delay(A,4,4,1) pause frequency=30; time_axis=0:0.001:1; B=cos(2*pi*frequency*time_axis+ pi/4); plot(time_axis,B) pause del=delay(B,4,4,1)

By running this script you are calling to the function created in the first step of this exercise, which at the same time, calls to the function created in the second step of this exercise, for each cosine: note that there are two cosines, named A and B, with the same delay but different frequencies. Therefore, the output of that script is:

## Exercise 3: Sinusoidal sampling

## Step 1: Function to create a cosine signal

function cosin=cosine(fs, f, phase) Tm=1/fs; n=[0:Tm:1]; <div>cosin=cos(2*pi*f*n + phase); stem(n, cosin)

## Step 2: Plot a cosine signal of frequency 10 Hz

cosen=coseno(400,10,0)

you will get the first 10 cycles of the signal. This is because fs/f gives 40 samples in one cycle and the sampling theorem (fs>f) is met. Then, for a frequency of 10 Hz we will obtain 10 cycles in 1 second:

## Step 3: Plot a cosine signal of frequency 40 Hz

## Step 3: Plot a cosine signal of frequency 100 Hz

In this case, the frequency is 100 Hz and the sampling frequency 400 Hz, so we have 4 samples per cycle, which shows greater discontinuities in the signal, although the Nyquist theorem is still met:

## Step 4: Plot a cosine signal of frequency 200 Hz

The reason for this behavior is that we are violating the Nyquist theorem and we are taking, approximately, a sample per cycle.