
% A simple analysis tool for windowing functions
%
%http://forums.parallax.com/discussion/169298/adc-sampling-breakthrough
% SaucySoliton at forums.parallax.com
% 11/21/2018
% Made for GNU Octave

n=4096;
t=n*3;


rec=zeros(t,1);
rec(1:n)=1;

ham=zeros(t,1);
ham(1:n)=hamming(n);

trap=rec;
trap(1:32)=(1:32)/32;
trap(n-31:n)=1-((1:32)/32);



seven=rec;
seven(1:586*7)=repmat([1 0 1 1 0 0 0]',586,1);
%seven(1:586*8)=repmat([1 0 1 0 1 0 1 0]',586,1);

%http://forums.parallax.com/discussion/comment/1454753/#Comment_1454753
tukeybase2464=[01 02 03 05 08 11 14 18 22 26 30 34 38 42 46 50 53 56 59 61 62 63 64]'/64; 
tukeybase1664=[1  4  7 12 17 23 29 35 41 47 52 57 60 63 64]'/64; 
tukey16=rec;
tukey16(1:length(tukeybase1664))=tukeybase1664;
tukey16(n-length(tukeybase1664)+1:n)=flipud(tukeybase1664);
tukey24=rec;
tukey24(1:length(tukeybase2464))=tukeybase2464;
tukey24(n-length(tukeybase2464)+1:n)=flipud(tukeybase2464);


signals=[seven rec ham trap tukey24 tukey16];

traces = [];
for c=1:size(signals,2)
	psd=abs(fft(signals(:,c)));
	psd=psd/max(psd); % normalize amplitude

	psd=max([ psd [0;psd(1:end-1)] [0;0;psd(1:end-2)] ],[],2); % neighborhood 3 maximum
	%psd=conv(psd,[1 1 1]'/3,'same'); % smoothing
	% without maximum or smoothing, plot oscillates between a value and near zero
	traces = [traces 20*log10(abs(psd)+1e-7) ];
end

plotrange=floor(t/2);

plot((1:plotrange')/(plotrange*2),traces(1:plotrange,:),'-')
ylim([-120 0]);

legend('Length 7 Sequence','Rectangular','Hamming','Trapezoid32','Tukey24/64','Tukey16/64')
ylabel('dB');
xlabel('relative frequency')
print('plot.png');
