clc;clear all;close all;
p=2;M=p+1;N=31;
n=0:N+M-2;
Nfft=1024;
wn=randn(1,N+M-1);
xn=exp(1i*2*pi*0.2*n)+5*exp(1i*2*pi*0.3*n)+wn;
%X=[xn(1:31).'xn(2:32).' xn(3:33).' ;(4:34).' ;(M:N+M-1).']
R=(1/N)*[X'*X];
[Q0,D]=eig(R);
[lambda,index]=sort(abs(diag(D)));
Lambda=lambda(M:-1:1);
Q=Q0(:,index(M:-1:1));
Qbar=zeros(Nfft,1);
for i=1:M-p
Qbar=Qbar+abs(fftshift(fft(Q(:,M-(i-1)),Nfft)));
end
Rbar=1./Qbar;
w=-0.5:0.000977:0.5;
RbardB=10*log10(Rbar);
Plot(w,RbardB);
grid on;
xlabel("frequency in pi units")
ylabel("MUSIC Spectrum (dB)");