function FFT_test
%% ********************************************
%% Basic example of FFT in MATLAB
%% provided by panic mode
%% ********************************************
clc; clear all; close all; % always start fresh,
% reset and close anything that may be open
L = 2000; % length of signal (number of sample values)
f1 = 10000; % set signal frequency
Duty = 10; % set duty of square wave
Fs = 512000; % recall Nyquist sampling frequency, it must be
% at least twice that of sampled signal
% but it is better if it is higher than that
for i=1:L % create pulse train with given duty cycle
if mod(i,100)<Duty
signal(i)=1;
else
signal(i)=0;
end;
end;
T = 1./Fs; % sampling period
t = [0:L-1]*T; % time scale for time domain
f = Fs/2*linspace(-1,1,L); % frequency scale for freq. domain
FFTsignal = fft(signal)/L; % fft of signal (freq domain)
figure(); % always create new figure for your plots
subplot(2,1,1),plot(t,signal); % top graph is time domain signal
axis([t(1) t(L) -0.25 1.25]); % set convenient axis limits for plot
title('Signal in time domain'); % indicate what the plot represents
subplot(2,1,2),plot(f,abs(fftshift(FFTsignal))); % bottom is freq. domain
title('Signal in frequency domain'); % indicate what the plot represents