Advertisement
Guest User

SAM_ADC_Animated.m

a guest
Apr 24th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 1.46 KB | None | 0 0
  1. %InnovateFPGA 2019
  2. %Team Number: EM002
  3. %Project Name: Successive-approximation-memory (SAM) analog-to-digital converter (ADC)
  4.  
  5. clear all;
  6.  
  7. function y = SquareWave(n,x)
  8.   y = 1.5 + sin(x * (4 + n));
  9.   %y = 1.5 + (sin(x * (4 + n)) > 0) * 2 - 1.0;
  10. endfunction
  11.  
  12. f_width = 500;
  13. f_height = 150;
  14. %f_width = 1500;
  15. %f_height = 550;
  16. h = figure('position',[100,100,f_width,f_height]);
  17. filename = 'SAM_ADC_Animated.gif';
  18.  
  19. xMAX = 10;
  20. xSTEP = 0.01;
  21. xNUM = (xMAX/xSTEP) + 1;
  22. iterNUM = 6;
  23.  
  24. x = 0:xSTEP:xMAX;
  25. SignalIN = SquareWave(0,x);
  26. ComparatorOUT = ones(1, xNUM);
  27. ApproximationLogicOUT = zeros(1, xNUM);
  28. DACOUT = zeros(1, xNUM);
  29.  
  30. for n = 1:1:iterNUM
  31.   DACOUT = ApproximationLogicOUT;
  32.   ComparatorOUT = (SignalIN - DACOUT > 0.0) * 2 - 1;
  33.   ApproximationLogicOUT = ApproximationLogicOUT + (ComparatorOUT * 3 / (2^n));
  34.   plot(x,SignalIN,"b;Signal input;",...
  35.        x,DACOUT,"g;DAC output;",...
  36.        x,ComparatorOUT * 0.3,"m;Comparator result;");
  37.   axis([0, 10, -0.4, 3]);
  38.   drawnow;
  39.   % Capture the plot as an image
  40.   frame = getframe(h,[1,1,f_width-1,f_height-3]);
  41.   im = frame2im(frame);
  42.   [imind,cm] = rgb2ind(im);
  43.   % Write to the GIF File
  44.   if n == 1
  45.     imwrite(imind,cm,filename,'gif','WriteMode','overwrite','Loopcount',inf,'DelayTime',1.5);
  46.   elseif n == iterNUM
  47.     imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',6.5);
  48.   else
  49.     imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',1.5);
  50.   endif
  51. end
  52. delete(h);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement