Here is my code:
function [ T ] = FindPeriodicity2(x,Fs) ac=xcorr(x,x); [~,locs]=findpeaks(ac); T=mean(diff(locs)/Fs); end
and when I pass the signal
x with a Gaussian noise the period is 1.564 but as we know it equals $\pi$, so the answer is about half of the real period in this case. I know I should eliminate some of the peaks found in 'ac', but I have no idea how to choose from peaks such that it works for any signals.The plot of 'ac' is attached .
here is the signal x:
Fs=100; t=0:(1/Fs):30; n=length(t); noise=randn(1,n).*0.3; x=sin(2.*t+pi)+cos(4.*t)+noise; T=FindPeriodicity2(x,Fs); disp(T);
any hint would be appreciated.