%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PLOTS A 3D VERSION OF THE COMPLEX SPECTRUM
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function PlotSpectrum(x, N)

% fft(X,N) is the N-point fft, padded with zeros if X has less
% than N points and truncated if it has more. (From the manual).
y = fft(x,N);   % compute the FFT of x

% Shuffle the frequencies returned by the FFT
% to order them from negative to positive.
% The function swaps the left and the right part of the vector, e.g.,
% fftshift([0,1,2,3,4,5,6,7])  ->>  [4 5 6 7 0 1 2 3]
Y=fftshift(y);

% Plot the complex spectrum
figure
Yn = Y(1:N/2)/N;     % Negative frequencies
Yp = Y(N/2+1:N)/N;   % Positive frequencies
z  = zeros(1,N/2);

quiver3(-N/2:-1, z, z, z, real(Yn), imag(Yn), 0, 'Color','red',  'LineWidth', 2); % 0 - don't auto-scale the vectors 
hold on;
quiver3(0:N/2-1, z, z, z, real(Yp), imag(Yp), 0, 'Color','blue', 'LineWidth', 2);  

% draw the coordinate system
C =[0 0 0 0 0 0]; 
quiver3(C, C, C, [0 0 0 0 N/2 -N/2], [1 0 -1 0 0 0], [0 1 0 -1 0 0], 0, 'Color','black', 'LineWidth', 2, 'MaxHeadSize', 0.05);
axis equal
axis([-N/2, N/2, -1,  1, -1, 1])
xlabel('Frequency');
ylabel('Re');
zlabel('Im');
title('The Complex Spectrum of the Signal');
