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

## Step 1: Function to create a periodic signal

Make a script named periodic.m and write the following function:

```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

In a new script, write the following code to generate a rectangular pulse (you can create any other signal that you want to convert in periodic):
```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)
```
The output of this script will be the rectangle and its periodic version: Figure 1. Rectangle shape
Note that the rectangle is not exactly square. By looking to the code, can you guess why? If this topic is new for you, by the end of the post you will understand it better, but the reason is that we didn’t take enough number of sample to represent it. However, this is not an inconvenient for us to get its periodic version.
We have input Nr=5 which makes a total signal length of 200 samples: Figure 2. Periodic rectangle pulses

## Exercise 2: Change the time origin

In this exercise we are going to observe that for a sinusoidal signal, a delay (or shift) is equivalent to a change in its phase. In addition, for the same delay, the phase will be different if we change the frequency of the signal.
In the following example, we are going to see two sinusoidal waves with a delay of 4 samples, but different frequencies.

## Step 1: Function to delay a sinusoidal signal

Write the following function to generate a cosine and its delayed version:
```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.

## 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: Figure 3. Cosine A, frequency of 10 Hz Figure 4. Cosine A, shifted, frequency of 10 Hz Figure 5. Cosine B, frequency of 30 Hz Figure 6. Cosine B, shifted, frequency of 30 Hz
Note that the time axis for the non-shifted cosines is different from the shifted version one because when we call the function delay, we make this change.

## Exercise 3: Sinusoidal sampling

In this exercise our sampling frequency is going to be always 400 Hz.

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

If you do:
```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: Figure 7. Cosine, frequency of 10 Hz, sampling frequency of 400 Hz

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

Changing the frequency to 40 Hz gives us 40 cycles per second. As we are taking 10 samples per cycle, the signal will be less continuous but its representation is still congruent with the Nyquist theorem (no aliasing). Figure 8. Cosine, frequency of 40 Hz, sampling frequency of 400 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: Figure 9. Cosine, frequency of 100 Hz, sampling frequency of 400 Hz
Let’s zoom the signal to better appreciate the sampling effect: Figure 10. Cosine zoomed, frequency of 100 Hz, sampling frequency of 400 Hz

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

When the sampling frequency is exactly two times the signal frequency, we can observe that the signal looks even more sharpened than in the previous cases because we are working in the limit of the Nyquist theorem; therefore, if we keep increasing the signal frequency (by keeping constant the sampling frequency) the resultant signal will be incorrect and it will present aliasing.
Note that in this step we have 2 samples per cycle, so what we see is an approximation to the actual signal: Figure 11. Cosine, frequency of 200 Hz, sampling frequency of 400 Hz
Let’s have a look to this sharpening effect by zooming the signal: Figure 12. Cosine zoomed, frequency of 200 Hz, sampling frequency of 400 Hz

## Step 5: Plot a cosine signal of frequency 390 Hz

Surprise! For a signal frequency of 390 Hz and a sampling frequency of 400 Hz we obtain the same signal as in step 1 which was a 10 Hz cosine! Figure 13. Cosine, frequency of 390 Hz, sampling frequency of 400 Hz

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

## Step 6: Plot a cosine signal of frequency 33 Hz

In this exercise we are going to study what happens when the signal frequency is not a multiple of the sampling frequency. We will see that when sampling, if we want to measure, for example, a period we can’t make it in an accurate way because the period won’t be a multiple of the sampling frequency neither.
Therefore, for a signal frequency off 33 Hz and a sampling frequency of 400 Hz, we will get the following signal: Figure 14. Cosine, frequency of 33 Hz, sampling frequency of 400 Hz
We can appreciate this sharpening effect better if we plot the signal using stem and we zoom it: Figure 15. Zoomed cosine, frequency of 33 Hz, sampling frequency of 400 Hz
As we can observe, the Nyquist theorem is satisfied but not the sampling accuracy of the signal: note that the samples are not repeated in the same position for each cycle.
We hope you found useful this post and make any comment below!
Next topic in the signal processing section will be LTI Systems.