MATLAB: Determine whether or not the following signals are periodic

homework

clc
clear all
close all
t= -4 : 0.005 : 4;
a=1;
%FOR X(T)
subplot(2,1,1)
c1=2*pi;
c2=4*pi;
x2=a*cos(c1.*t);
x1=a*cos(c2.*t);
x=x1+x2
ac=xcorr(x,x);
[~,locs]=findpeaks(ac);
sol=mean(diff(locs)*0.1)
plot(t,x)
xlabel('time->')
ylabel('x(t)')
title('Plot For x(t)')
grid on
gtext('1841014009')
text(-1,-0.5, ['Period Of The Signal =', num2str(sol)])
%FOR Y(T)
subplot(2,1,2)
c1=2*pi;
c2=2;
y2=a*cos(c1.*t);
y1=a*cos(c2.*t);
y=y1+y2
ac=xcorr(y,y);
[~,locs]=findpeaks(ac);
sol=mean(diff(locs)*0.1)
plot(t,y)
xlabel('time->')
ylabel('y(t)')
title('Plot For y(t)')
grid on
gtext('1841014009')
text(-1,-0.5, ['Period Of The Signal =', num2str(sol)])
This is what i have done.
But i don't how to determine whether or not the following signals are periodic in MATLAB

Best Answer

plot(t,x)
That should be plot(t,y) the second time.
sol=mean(diff(locs)*0.1)
why 0.1 ?
Finding the average difference between peaks in the correlation matrix does not tell you whether the signal is periodic.
Consider for example a square wave signal that pulses on for one unit, and each time the distance between pulses gets one unit further apart:
|^|_|^|__|^|___|^|____|^|_____|^|
The correlation measurement will have peaks that are not a constant distance apart, but despite not being constant distance, the mean of the distance is going to be some number. Like mean([1,3,6,10,15,21]) -> 9+1/3 . mean() of a series of values is always defined unless the values are empty or nan -- but the fact it exists does not tell you that the signal is periodic.