Advertisement
Guest User

Untitled

a guest
May 6th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.66 KB | None | 0 0
  1. %clear all; close all; clc; format compact;
  2.  
  3. % Circuit component values (static)
  4. C1 = single(51.*10^(-12)); % 51pF
  5. C2 = single(0.047.*10^(-6)); % 0.047uF
  6. R1 = single(51.*10^3); % 51k Ohms
  7. R2 = single(4.7.*10^3); % 4.7k Ohms
  8. Is = single(2.5*10^-9); % Saturation current, ~2.5nA (play with if results are undesirable)
  9. Vt = single(0.026); % Thermal Vo_guessltage, 26mV@300K
  10.  
  11. %Drive control, variable betweek 0 and 500k Ohms
  12. Rd = 500.*10^3;
  13. R= single(R1+Rd);
  14.  
  15. % Signal parameters
  16. freq = single(1000);
  17. w = 2.*pi.*freq;
  18. samplerate = single(500*10^3);
  19. amp = single(.03);
  20. t0 = single(0);
  21. tf = single(.01);
  22. samples = (tf-t0).*(samplerate);
  23. h = (tf-t0)/samples;
  24. t = single(linspace(t0,tf,samples));
  25.  
  26. vi = amp.*sin(w.*t);
  27. vi_prime = w.*amp.*cos(w.*t);
  28.  
  29. %va = (amp.*C2.*R2.*w.*(C2.*R2.*w.*sin(w.*t)-e^(-t/(C2.*R2))+cos(w.*t)))./(C2^2.*R^2.*w^2+1);
  30. for n= 1:samples
  31.     va(n) = (amp.*C2.*R2.*w.*(C2.*R2.*w.*sin(w.*t(n))-exp((-t(n)/(C2.*R2)))+cos(w.*t(n))))./(C2^2.*R2^2.*w^2+1);
  32. end
  33.  
  34. maxiter = 10000; %Maximum # of iterations for any looping structure
  35. incr = single(.05); % used for derivative approximation
  36.  
  37. Vo = zeros(samples,1,'single');
  38. fprime_hold = zeros(samples,1,'single');
  39. Vo(1) = 0;
  40. test=eps('single');
  41. for n = 2:samples %Sample looping
  42.     Vo_guess_old = Vo(n-1);
  43.     for i = 1:maxiter
  44.         Vo_guess = Vo_guess_old;
  45.         Vo_guess_inc = Vo_guess+incr;
  46.         % F is the equation to be solved
  47.         f = (Vo_guess-vi(n))./R+Is.*exp((Vo_guess-vi(n))/Vt)-Is.*exp((vi(n)-Vo_guess)/Vt)+C1.*(((Vo_guess-vi(n))-(Vo(n-1)-vi(n-1)))./h)-C2.*(((vi(n)-va(n))-(vi(n-1)-va(n-1)))./h);
  48.         f2 = (Vo_guess_inc-vi(n))./R+Is.*exp((Vo_guess_inc-vi(n))/Vt)+Is.*exp((vi(n)-Vo_guess_inc)/Vt)+C1.*(((Vo_guess_inc-vi(n))-(Vo(n-1)-vi(n-1)))./h)-C2.*(((vi(n)-va(n))-(vi(n-1)-va(n-1)))./h);
  49.         fprime = (f2-f)/incr; %approximate the derivative
  50.        
  51.         Vo_guess_old = Vo_guess_old - f./fprime;
  52.         if(abs(Vo_guess_old - Vo_guess) < test)
  53.             Vo(n) = Vo_guess_old;
  54.             f_hold(n) = f;
  55.             f2_hold(n) = f2;
  56.             fprime_hold(n) = fprime;
  57.             break;
  58.         end
  59.        
  60.     end
  61. end
  62.  
  63. Fs = samplerate;
  64. T = 1/Fs;
  65. L = length(Vo);
  66. Y = fft(Vo);
  67. P2 = abs(Y/L);
  68. P1 = P2(1:L/2+1);
  69. P1(2:end-1) = 2*P1(2:end-1);
  70. f = Fs*(0:(L/2))/L;
  71.  
  72. % disp('Finished');
  73. %
  74. % title1 = sprintf('Signals Rd = %d A = %d f = %d' ,Rd,amp,freq);
  75. % title2 = sprintf('FFT Rd = %d A = %d f = %d', Rd,amp,freq);
  76. %
  77. % figure(1)
  78. % plot(t,vi,t,Vo)
  79. % legend('Vin','Vo')
  80. % title(title1);
  81. %
  82. % figure(2)
  83. % loglog(f,P1);
  84. % title('FFT of Vo');
  85. % xlabel('f(Hz)')
  86. % ylabel('|P1(f)|')
  87. % axis([0 20000 0 1])
  88. % title(title2);
  89. % grid on;
  90. %
  91. % clear all;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement