
% A simple analysis tool for windowing the Goertzel mode
%
%http://forums.parallax.com/discussion/169298/adc-sampling-breakthrough
% SaucySoliton at forums.parallax.com
% 12/6/2018
% Made for GNU Octave




% R is the number of clocks between processing the 
% accumulator output

R=256




n=34;
nfft=4096;

sumstart=floor(nfft/20);
sumend=floor(nfft/2);

if 0
% data file source:
% http://forums.parallax.com/discussion/comment/1455002/#Comment_1455002
gtext=textread('gio_bitstream_8ch_64000clocks.txt', '%s');
vtext=textread('vio_bitstream_8ch_64000clocks.txt', '%s');
gnum=hex2dec (gtext);
vnum=hex2dec (vtext);
gbit=[];
vbit=[];
for j=1:8
 gbit=[ gbit bitget(gnum,j)];
 vbit=[ vbit bitget(vnum,j)];
end
abit=[gbit vbit];
end

rec=resize(ones(n,1),[nfft 1]);

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

trap32=rec;
trap32(1:32)=(1:32)/32;
trap32(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);


% note: length(abit)=64000
glen=64000;
gfreq=1/7
gti=127*cos((1:glen)*gfreq*2*pi)';
gtq=127*sin((1:glen)*gfreq*2*pi)';
if 1
  gti=round(gti);   % not checked for symmetry 
  gtq=round(gtq);   % not checked for symmetry 
end



sig=resize(seven,size(gti));
addendi= sig.*(gti*2) - gti;
addendq= sig.*(gtq*2) - gtq;
bw=ceil(log2(R)*2)+8 % accumulator bits required
sumi=mod(cumsum(addendi),2^bw);
sumq=mod(cumsum(addendq),2^bw);
sqri=mod(cumsum(sumi),2^bw);
sqrq=mod(cumsum(sumq),2^bw);

sw=R; 

combq=         mod(  sqrq(sw+1:end)-sqrq(1:end-sw)     ,2^bw);
outq=          mod(  combq(sw+1:end)-combq(1:end-sw)    + 2^(bw-1),2^bw)-2^(bw-1);  % 2s compliment modulus 
singlediffoutq=mod(  sqrq(2*sw+1:end) - 2*sqrq(sw+1:end-sw) + 1*sqrq(1:end-2*sw)        + 2^(bw-1),2^bw)-2^(bw-1);

hold off
plot(outq)
hold on
directformi=conv(addendi, conv(ones(sw,1),ones(sw,1))  );
directformq=conv(addendq, conv(ones(sw,1),ones(sw,1))  );
plot( (1:length(directformq))-sw*2 , directformq  )
plot(singlediffoutq)

% all 3 graphs draw on top of each other, proving that the output is identical

adcchan=1
if 0
plot([adcrect(:,[0 8]+adcchan) adccma(:,[0 8]+adcchan)]*256)
title(['Filtered ADC values, filter length=' num2str(testlength) ])
legend('Gio Rectangular','Vio Rectangular','Gio Sinc','Vio Sinc')
print('plotadc1.png');
end



