% This basic file allows to create a signal and compute the magnitude and
% phase of the transformed signal. The signal consists of two sinusoids at 
% different frequencies:

% Length(t)=10
t=0:1/100:10-1/100;

g=sin(2*pi*15*t)+sin(2*pi*40*t);

% Plot a short sample of the signal.

plot(t,g);
axis([1 2.5 -2.2 2.2]);
title('Extraction of a Sample of the Signal');
xlabel('Sample for 0\leq t \leq2.5 [total length(t)=10]')
grid on;

disp(' ')
disp('Press Any Key to Continue ...')

pause

% Use the FFT function to compute the DFT of the signal. Then compute the
% magnitude and phase of the transformed signal. The abs function is used
% to obtain the magnitude of the data, the angle function is used to obtain
% the phase information, and unwrap to remove phase jumps grater than pi to
% their 2*pi complement.

h=fft(g);
m=abs(h);
p=unwrap(angle(h));

% Plot the magnitude and phase.

f=(0:length(g)-1)'*100/length(h);
subplot(2,1,1), plot(f,m),
ylabel('Abs. Magnitude'), grid on
subplot(2,1,2), plot(f,p*180/pi), 
ylabel('Phase [Degrees]'),grid on
xlabel('Frequency [Hertz]')

% The magnitude plot is symmetrical about the Nyquist frequency of 50 hertz. 
% The useful information in the signal is found in the range 0 to 50 hertz.