N=110;
M=5;
k= N/(2*M+1); % Because N= k(2*M + 1) 

tempX = randn(1, M) + i*randn(1, M);
X = [ randn(1, 1), tempX, zeros(1, N-2*M-1), fliplr(conj(tempX)) ];
x = ifft(X);
figure;
subplot(2,1,1);plot([-N/2:N/2-1], fftshift(abs(X))); title('Orignal spectrum magnitude');
subplot(2,1,2);plot([-N/2:N/2-1], fftshift(angle(X))); title('Original spectrum phase');



Ns=k/2; % Subsampling at N_nyquist /2 
xsampled=zeros(size(x)); %The subsampled signal
xsampled(1:Ns:N)=x(1:Ns:N);
Xsampled=fft(xsampled);
figure;
subplot(2,1,1);plot([-N/2:N/2-1], fftshift(abs(Xsampled))); title('Sampled spectrum magnitude, N_{Nyquist}/2');
subplot(2,1,2);plot([-N/2:N/2-1], fftshift(angle(Xsampled))); title('Sampled spectrum phase, N_{Nyquist}/2');


Xrec= Xsampled;
Xrec(k/2+2:length(Xrec)-(k/2))=0;  %Ideal low pass filtering 
xrec=Ns*ifft(Xrec);
figure; plot(real(x));
hold on;
plot(real(xrec),'ro-.');
legend('Original X','Reconstructed');
hold off;



Ns=k; % Subsampling at N_nyquist\ 
xsampled=zeros(size(x)); %The subsampled signal
xsampled(1:Ns:N)=x(1:Ns:N);
Xsampled=fft(xsampled);
figure;
subplot(2,1,1);plot([-N/2:N/2-1], fftshift(abs(Xsampled))); title('Sampled spectrum magnitude, N_{Nyquist}');
subplot(2,1,2);plot([-N/2:N/2-1], fftshift(angle(Xsampled))); title('Sampled spectrum phase, N_{Nyquist}');


Xrec= Xsampled;
Xrec(k/2+2:length(Xrec)-(k/2))=0;  %Ideal low pass filtering 
xrec=Ns*ifft(Xrec);
figure; plot(real(x));
hold on;
plot(real(xrec),'ro-.');
legend('Original X','Reconstructed');
hold off;


Ns=2*k; % Subsampling at N_nyquist\ 
xsampled=zeros(size(x)); %The subsampled signal
xsampled(1:Ns:N)=x(1:Ns:N);
Xsampled=fft(xsampled);
figure;
subplot(2,1,1);plot([-N/2:N/2-1], fftshift(abs(Xsampled))); title('Sampled spectrum magnitude, N_{Nyquist}');
subplot(2,1,2);plot([-N/2:N/2-1], fftshift(angle(Xsampled))); title('Sampled spectrum phase, N_{Nyquist}');


Xrec= Xsampled;
Xrec(k/2+2:length(Xrec)-(k/2))=0;  %Ideal low pass filtering 
xrec=Ns*ifft(Xrec);
figure; plot(real(x));
hold on;
plot(real(xrec),'ro-.');
legend('Original X','Reconstructed');
hold off;



