From 1ab94a4beaf0440db6a7e11542d886167fd397c2 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Thu, 30 Aug 2018 18:06:49 +0000 Subject: [PATCH] added git-svn-id: http://moon:8086/svn/matlab/trunk@99 801c6759-fa7c-4059-a304-17956f83a07c --- common/adc_eval.m | 52 +++++++++++++++++++++ common/distort.m | 27 +++++++++++ common/distort_eval.m | 11 +++++ common/e_eval.m | 42 +++++++++++++++++ common/eval_gainControl.m | 23 +++++++++ common/gainControl.m | 67 +++++++++++++++++++++++++++ common/hmm_eval.m | 53 +++++++++++++++++++++ common/ip_eval.m | 50 ++++++++++++++++++++ common/jpid.m | 36 +++++++++++++++ common/lms_eval.m | 33 +++++++++++++ common/ltrack.m | 50 ++++++++++++++++++++ common/movingavg.m | 23 +++++++++ common/myPolyfit.m | 88 +++++++++++++++++++++++++++++++++++ common/octave-workspace | Bin 0 -> 11 bytes common/onPush.m | 7 +++ common/pf_eval.m | 16 +++++++ common/pid_eval.m | 95 ++++++++++++++++++++++++++++++++++++++ common/psample.m | 48 +++++++++++++++++++ common/pt_eval.m | 44 ++++++++++++++++++ common/sallenkey_eval.m | 55 ++++++++++++++++++++++ common/stdp_eval.m | 61 ++++++++++++++++++++++++ common/ui_eval.m | 36 +++++++++++++++ common/vanDusen_eval.m | 43 +++++++++++++++++ common/vga_eval.m | 28 +++++++++++ 24 files changed, 988 insertions(+) create mode 100755 common/adc_eval.m create mode 100755 common/distort.m create mode 100755 common/distort_eval.m create mode 100755 common/e_eval.m create mode 100755 common/eval_gainControl.m create mode 100755 common/gainControl.m create mode 100755 common/hmm_eval.m create mode 100755 common/ip_eval.m create mode 100644 common/jpid.m create mode 100755 common/lms_eval.m create mode 100755 common/ltrack.m create mode 100755 common/movingavg.m create mode 100755 common/myPolyfit.m create mode 100755 common/octave-workspace create mode 100755 common/onPush.m create mode 100755 common/pf_eval.m create mode 100755 common/pid_eval.m create mode 100755 common/psample.m create mode 100755 common/pt_eval.m create mode 100755 common/sallenkey_eval.m create mode 100755 common/stdp_eval.m create mode 100755 common/ui_eval.m create mode 100755 common/vanDusen_eval.m create mode 100755 common/vga_eval.m diff --git a/common/adc_eval.m b/common/adc_eval.m new file mode 100755 index 0000000..75532bc --- /dev/null +++ b/common/adc_eval.m @@ -0,0 +1,52 @@ +## Copyright (C) 2017 Jens Ahrensfeld +## +## This program is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{retval} =} adc_eval (@var{input1}, @var{input2}) +## +## @seealso{} +## @end deftypefn + +## Author: Jens Ahrensfeld +## Created: 2017-03-07 + +function [output] = adc_eval (nbits, input) + +Vref = 1.0; +%output = adc_pipelined(Vref, nbits, input) +output = adc_SAR(Vref, nbits, input) + + +function output = adc_pipelined(VRref, nbits, input) + output = zeros(1, nbits); + for n=1:nbits, + if input >= Vref/2 + output(n) = 1; + input = input - Vref/2 + end + input = input * 2 + end + +function output = adc_SAR(VRref, nbits, input) + Vsar = 0; + VStep = VRref/2 + output = zeros(1, nbits); + for n=1:nbits, + if input >= (Vsar + VStep) + output(n) = 1 + Vsar = Vsar + VStep; + end + VStep = VStep / 2; + end diff --git a/common/distort.m b/common/distort.m new file mode 100755 index 0000000..f3e1fc9 --- /dev/null +++ b/common/distort.m @@ -0,0 +1,27 @@ +## Copyright (C) 2016 Jens Ahrensfeld +## +## This program is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{retval} =} distort (@var{input1}, @var{input2}) +## +## @seealso{} +## @end deftypefn + +## Author: Jens Ahrensfeld +## Created: 2016-04-15 + +function [retval] = distort (input1, input2) + +endfunction diff --git a/common/distort_eval.m b/common/distort_eval.m new file mode 100755 index 0000000..4f976a9 --- /dev/null +++ b/common/distort_eval.m @@ -0,0 +1,11 @@ +function distort_eval(k) + +N = 100; + +x = (0:(N-1))/(N-1); + +b = 0.9 + +y = b + (1-b)*x.*exp(k*abs(x))./exp(k); + +plot(x, y); grid; diff --git a/common/e_eval.m b/common/e_eval.m new file mode 100755 index 0000000..55c69c1 --- /dev/null +++ b/common/e_eval.m @@ -0,0 +1,42 @@ +## Copyright (C) 2018 Jens Ahrensfeld +## +## This program is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{retval} =} e_eval (@var{input1}, @var{input2}) +## +## @seealso{} +## @end deftypefn + +## Author: Jens Ahrensfeld +## Created: 2018-02-09 + +function e_eval() + +N = 100; + +a = 0.25; +y1 = 0.0; +x = 1.0; +for n=1:N, + y1_(n) = y1; + y2_(n) = 1-exp(-(n-1)*a); + + y1 = (1-a)*y1 + a*x; +end + +close; +plot (0:N-1, y1_,0:N-1, y2_); grid; + +endfunction diff --git a/common/eval_gainControl.m b/common/eval_gainControl.m new file mode 100755 index 0000000..5df9028 --- /dev/null +++ b/common/eval_gainControl.m @@ -0,0 +1,23 @@ +function eval_gainControl() + +close all; + +% 1 +G_Target = [40*ones(1,200) 20*ones(1,200) 40*ones(1,200)]; +G_PA = 43+5*sin(1/400*2*pi*(1:600)); + +gainControl(G_Target, G_PA, 3, 0.2); + +% 2 +figure +G_Target = [40*ones(1,600)]; +G_PA = [43*ones(1,200) 46*ones(1,200) 23*ones(1,200)]; + +gainControl(G_Target, G_PA, 3, 0.2); + +% 2 +figure +G_Target = [40*ones(1,600)]; +G_PA = [43*ones(1,200) 40*ones(1,200) 83*ones(1,200)]; + +gainControl(G_Target, G_PA, 3, 0.2); diff --git a/common/gainControl.m b/common/gainControl.m new file mode 100755 index 0000000..0faa2a8 --- /dev/null +++ b/common/gainControl.m @@ -0,0 +1,67 @@ +function gainControl(G_TX_target, G_PA_dB, Nd, mu) + +% Constants +N=length(G_TX_target); % number of points +Ts = 1.0 % sample period in sec. + +atten_res_dB = 0.010; % attenuator resolution + +%mu = 0.1; % Averaging of measurement +knoise = 0.04; % measurement noise + +f_theta = 0.001; % Hz Temperature drift frequency +a_theta = 5; % dB Temperature drift level dB + +atten_dB=40; % initial value: Controller atten_ output + + +state = zeros(1, Nd-1); % filter state for delay + +error = 0; +enabled = 1; +LATENCY = 3; +latency_count = LATENCY; + +for i=1:N, + G_TX = G_PA_dB(i) - atten_dB + knoise*randn(); + [G_TX_est, state] = filter([zeros(1,Nd-1) 1], 1, G_TX, state); + error_last = error; + error = G_TX_est - G_TX_target(i); + derror = abs(error) - abs(error_last); + atten_dB = atten_dB + mu*error; + atten_dB = getAtten(atten_dB, atten_res_dB); + + if enabled == 0 + G_TX = -120; + end + error_v(i) = error; + derror_v(i) = derror; + gain_dB_v(i) = G_TX; + atten_v(i) = atten_dB; + + if enabled == 1 + if derror > -0.1 && abs(error) > 3 + if latency_count == 0 + enabled = 0; + else + latency_count = latency_count - 1; + end + else + latency_count = LATENCY; + end + end + +end + +subplot(4,1,1) +plot((0:N-1)*Ts, G_TX_target, (0:N-1)*Ts, G_PA_dB); grid; legend('G_{TX_target}', 'G_{PA}'); xlabel('t/s'); ylabel('dB') +subplot(4,1,2) +plot((0:N-1)*Ts, error_v, (0:N-1)*Ts, derror_v); grid; legend('error', 'derror'); xlabel('t/s'); ylabel('dB') +subplot(4,1,3) +plot((0:N-1)*Ts, gain_dB_v); grid; title('Gain_{TX}'); xlabel('t/s'); ylabel('dB') +subplot(4,1,4) +plot((0:N-1)*Ts, atten_v); grid; title('atten'); xlabel('t/s'); ylabel('dB') + +function atten = getAtten(gain_dB, res_dB) + atten = max(0, min(40, res_dB*fix(gain_dB/res_dB))); + diff --git a/common/hmm_eval.m b/common/hmm_eval.m new file mode 100755 index 0000000..a8bb3ab --- /dev/null +++ b/common/hmm_eval.m @@ -0,0 +1,53 @@ +## Copyright (C) 2017 Jens Ahrensfeld +## +## This program is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{retval} =} hmm_eval (@var{input1}, @var{input2}) +## +## @seealso{} +## @end deftypefn + +## Author: Jens Ahrensfeld +## Created: 2017-06-14 + +function sv = hmm_eval (A, numIter) + +numStates = length(A) +C = zeros(1,numStates); + +#A = rand(numStates, numStates) + +for s=1:numStates, + A(s,:) = A(s,:) ./ sum(A(s,:)); + [AS(s,:), Ai(s,:)] = sort(A(s,:)); + AC(s,:) = cumsum(AS(s,:)); +end + +A +AC +s=1; +for n=1:numIter, + sv(n) = s; + stateProbs = AC(s,:); + z = rand(); + c = find (z <= stateProbs); + s = Ai(s, c(1)); + C(s) = C(s) + 1; +end + +plot(1:numIter, sv, 'ro-'); grid; +C=C/numIter + +endfunction diff --git a/common/ip_eval.m b/common/ip_eval.m new file mode 100755 index 0000000..a6caff1 --- /dev/null +++ b/common/ip_eval.m @@ -0,0 +1,50 @@ +## Copyright (C) 2017 Jens Ahrensfeld +## +## This program is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{retval} =} ip_eval (@var{input1}, @var{input2}) +## +## @seealso{} +## @end deftypefn + +## Author: Jens Ahrensfeld +## Created: 2017-05-15 + +function [] = ip_eval () + +Nh = 100; +Np = 10; +omega = 0.4; + +close all; +x = -(Nh/2):(Nh/2-1); + +for p=1:Np, + d = (p-1)/Np + h(:, p) = sinc(omega*(x+d))'.*kaiser(length(x), 8); + plot(x, h); grid; + pause (0.1) +end + +figure +for p=1:Nh, + x = 0:Np-1; + y = h(p,:); + p = polyfit(x, y, 3); + plot(x, y, 'o', 0:0.1:Np-1, polyval(p, 0:0.1:Np-1), 'r'); grid; + pause (0.1) +end + +endfunction diff --git a/common/jpid.m b/common/jpid.m new file mode 100644 index 0000000..f50b257 --- /dev/null +++ b/common/jpid.m @@ -0,0 +1,36 @@ +function [y, yc, z] = jpid(zi, kp, ki, kd, alpha_i, e) + +if isempty(zi) + % Integrator + b_i = [1]; + a_i = [1 -alpha_i]; + % Differentiator + b_d = [1 -1]; + a_d = [1]; + i = zeros(1, max(length(a_i), length(b_i))-1); + d = zeros(1, max(length(a_d), length(b_d))-1); + + z = struct('b_i', b_i, 'a_i', a_i, 'b_d', b_d, 'a_d', a_d, 'i', i, 'd', d, 'y_min', 0.0, 'y_max', 1.0, 'diff_aw', 0.0); +else + z = zi; +end + +[yi, z.i] = filter(z.b_i, z.a_i, e + z.diff_aw, z.i); +[yd, z.d] = filter(z.b_d, z.a_d, e, z.d); + +z.yp = kp*e; +z.yi = ki*yi; +z.yd = kd*yd; + +y = z.yp + z.yi + z.yd; + +yaw = yi; +if (yaw < z.y_min) + z.diff_aw = abs(yaw - z.y_min); +end + +if (yaw > z.y_max) + z.diff_aw = -abs(yaw - z.y_max); +end + +yc = max(z.y_min, min(z.y_max, y)); \ No newline at end of file diff --git a/common/lms_eval.m b/common/lms_eval.m new file mode 100755 index 0000000..197f0d3 --- /dev/null +++ b/common/lms_eval.m @@ -0,0 +1,33 @@ +function lms_eval(N, mu) + +% lms_eval(N, mu) +% Example: lms_eval(1000, 0.01) + +h = [0 0 0 1 0 0 0 0]; +w = randn(1, length(h)); + +z_h = zeros(length(h)-1, 1); +z_w = zeros(length(h)-1, 1); + +x = zeros(1, length(h)); + +e_last = 0; +p = 1.0; + +for n=1:N, + xs = 0.1*randn(); + p = 0.99*p + 0.01*xs*xs; + x = [xs x(1:length(x)-1)]; + d = x*h'; + y = x*w'; + e = d - y; + + w = w + mu*e*x*(1/(0.001+p)); + + e_last = 0.5*e_last + 0.5*e*e; + _e(n) = e_last; + _p(n) = p; +end + +plot(1:N, _e, 1:N, _p); grid; +w=w' \ No newline at end of file diff --git a/common/ltrack.m b/common/ltrack.m new file mode 100755 index 0000000..d6e0598 --- /dev/null +++ b/common/ltrack.m @@ -0,0 +1,50 @@ +## Copyright (C) 2017 Jens Ahrensfeld +## +## This program is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{retval} =} ltrack (@var{input1}, @var{input2}) +## +## @seealso{} +## @end deftypefn + +## Author: Jens Ahrensfeld +## Created: 2017-05-29 + +function ltrack (d) + +N = 101 +phi = (-(N-1)/2:((N-1)/2))/(N-1)*pi/2; + +k=1; +for k=1:length(d) + err(:,k) = tan(phi)*d(k); +end + +close all; +plot(phi, err); grid; xlabel('error(phi)') + +err = (-(N-1)/2:((N-1)/2))/(N-1); +phi = []; +k=1; +for k=1:length(d) + phi(:,k) = atan(err/d(k)); +end + +figure; +plot(err, 180*phi/pi); grid; xlabel('phi(err)') + + +endfunction + diff --git a/common/movingavg.m b/common/movingavg.m new file mode 100755 index 0000000..6acebb4 --- /dev/null +++ b/common/movingavg.m @@ -0,0 +1,23 @@ +function [y, zf] = movingavg(x, zi) + if ~isstruct(zi) + N = zi; + zf = struct('N', N, 's', zeros(N+1, 1), 'ri', 2, 'wi', 1, 'y', 0); + else + zf = zi; + end + + xn = x/zf.N; + zf.y = zf.y + xn - zf.s(zf.ri); + y = zf.y; + + zf.ri = zf.ri + 1; + if zf.ri > length(zf.s) + zf.ri = 1; + end + + zf.s(zf.wi) = xn; + zf.wi = zf.wi + 1; + if zf.wi > length(zf.s) + zf.wi = 1; + end + diff --git a/common/myPolyfit.m b/common/myPolyfit.m new file mode 100755 index 0000000..d7ac38f --- /dev/null +++ b/common/myPolyfit.m @@ -0,0 +1,88 @@ +function [p,S,mu] = myPolyfit(x,y,n) +% POLYFIT Fit polynomial to data. +% POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) of +% degree N that fits the data, P(X(I))~=Y(I), in a least-squares sense. +% +% [P,S] = POLYFIT(X,Y,N) returns the polynomial coefficients P and a +% structure S for use with POLYVAL to obtain error estimates on +% predictions. If the errors in the data, Y, are independent normal +% with constant variance, POLYVAL will produce error bounds which +% contain at least 50% of the predictions. +% +% The structure S contains the Cholesky factor of the Vandermonde +% matrix (R), the degrees of freedom (df), and the norm of the +% residuals (normr) as fields. +% +% [P,S,MU] = POLYFIT(X,Y,N) finds the coefficients of a polynomial +% in XHAT = (X-MU(1))/MU(2) where MU(1) = mean(X) and MU(2) = std(X). +% This centering and scaling transformation improves the numerical +% properties of both the polynomial and the fitting algorithm. +% +% Warning messages result if N is >= length(X), if X has repeated, or +% nearly repeated, points, or if X might need centering and scaling. +% +% See also POLY, POLYVAL, ROOTS. + +% Copyright 1984-2002 The MathWorks, Inc. +% $Revision: 5.17 $ $Date: 2002/04/09 00:14:25 $ + +% The regression problem is formulated in matrix format as: +% +% y = V*p or +% +% 3 2 +% y = [x x x 1] [p3 +% p2 +% p1 +% p0] +% +% where the vector p contains the coefficients to be found. For a +% 7th order polynomial, matrix V would be: +% +% V = [x.^7 x.^6 x.^5 x.^4 x.^3 x.^2 x ones(size(x))]; + +if ~isequal(size(x),size(y)) + error('X and Y vectors must be the same size.') +end + +x = x(:); +y = y(:); + +if nargout > 2 + mu = [mean(x); std(x)]; + x = (x - mu(1))/mu(2); +end + +% Construct Vandermonde matrix. +V(:,n+1) = ones(length(x),1); +for j = n:-1:1 + V(:,j) = x.*V(:,j+1); +end + +% Solve least squares problem, and save the Cholesky factor. +[Q,R] = qr(V,0); +ws = warning('off','all'); +p = R\(Q'*y); % Same as p = V\y; +warning(ws); +if size(R,2) > size(R,1) + warning('MATLAB:polyfit:PolyNotUnique', ... + 'Polynomial is not unique; degree >= number of data points.') +elseif condest(R) > 1.0e10 + if nargout > 2 + warning('MATLAB:polyfit:RepeatedPoints', ... + 'Polynomial is badly conditioned. Remove repeated data points.') + else + warning('MATLAB:polyfit:RepeatedPointsOrRescale', ... + ['Polynomial is badly conditioned. Remove repeated data points\n' ... + ' or try centering and scaling as described in HELP POLYFIT.']) + end +end +r = y - V*p; +p = p.'; % Polynomial coefficients are row vectors by convention. + +% S is a structure containing three elements: the Cholesky factor of the +% Vandermonde matrix, the degrees of freedom and the norm of the residuals. + +S.R = R; +S.df = length(y) - (n+1); +S.normr = norm(r); diff --git a/common/octave-workspace b/common/octave-workspace new file mode 100755 index 0000000000000000000000000000000000000000..41ef164d111fcfa9cc9ec079bd8194241d90356e GIT binary patch literal 11 ScmeZIE=ep))iu=hVE_OY#sfM4 literal 0 HcmV?d00001 diff --git a/common/onPush.m b/common/onPush.m new file mode 100755 index 0000000..fc3181f --- /dev/null +++ b/common/onPush.m @@ -0,0 +1,7 @@ +function onPush(h) + disp('onPush') + close(h) + + system ("nautilus .") + +endfunction diff --git a/common/pf_eval.m b/common/pf_eval.m new file mode 100755 index 0000000..68f3027 --- /dev/null +++ b/common/pf_eval.m @@ -0,0 +1,16 @@ +function pf_eval(Np) +N = 100; +kn = 0.05; +R = 4; +x = (0:N-1)/N; +xr = x(1:R:N) +y = sin(0.25*2*pi*xr); + +Nr = length(xr); +yn = y + kn*(0.5-rand(1,Nr)); +[p,s] = myPolyfit(xr,yn,Np); + +yh = polyval(p,x); + +plot(xr, yn, 'ro', x, yh, 'b.'); grid; legend('y','y_h'); +s=s \ No newline at end of file diff --git a/common/pid_eval.m b/common/pid_eval.m new file mode 100755 index 0000000..d8f49dd --- /dev/null +++ b/common/pid_eval.m @@ -0,0 +1,95 @@ +function pid_eval(Nd, kp, ki, kd) + +% pid_eval(6, 0.002, 2.0e-6, 8.0) +% pid_eval(10, 0.0005, 1.0e-6, 4.0) + +% Constants +kNoise = 0.05; +aNoise = 0.5; +alpha_i = 0.999 + +% Init +x = [zeros(1, 40), 2*ones(1, 160), zeros(1, 40)]; +noise = kNoise*randn(length(x)); + +zfp = struct('velo', 0, 'dist', 0, 'x', 0); +zfc = [] + +ev = zeros(1, length(x)); +efv = zeros(1, length(x)); +y1v = zeros(1, length(x)); +y2v = zeros(1, length(x)); +ypv = zeros(1, length(x)); +yiv = zeros(1, length(x)); +ydv = zeros(1, length(x)); + +yvelo = 0; +ydist = 0; +state = 'Init'; +y_last = 0; + +b_f = [aNoise]; +a_f = [1 -(1-aNoise)]; +z_f = zeros(1, max(length(a_f), length(b_f))-1); + +for n=1:length(x), + e = x(n)-ydist + noise(n); + [ef, z_f] = filter(b_f, a_f, e, z_f); + + [ypid, yc, zfc] = jpid(zfc, kp, ki, kd, alpha_i, ef); + state_next = state; + if strcmp(state, 'Init') + state_next = 'Acquisition'; + elseif strcmp(state, 'Acquisition') + if abs(ef) < 0.001 + kp = kp * 1.0; + ki = ki * 1.0; + kd = kd * 1.0; + state_next = 'Track'; + end + elseif strcmp(state, 'Track') + if abs(ef) > 0.01 + kp = kp * 1.0; + ki = ki * 1.0; + kd = kd * 1.0; + state_next = 'Acquisition'; + end + end + if strcmp(state_next, state) == 0 + disp(state); + end + dy = ypid - y_last; + y_last = ypid; + [yvelo, ydist, zfp] = plant(zfp, Nd, dy); + ev(n) = e; + efv(n) = ef; + y1v(n) = ypid; + y2v(n) = ydist; + ypv(n) = zfc.yp; + yiv(n) = zfc.yi; + ydv(n) = zfc.yd; + state = state_next; +end + +if 1 +subplot(3, 1, 1) +plot(1:length(x), ev, 1:length(x), efv); grid; legend("Error", "Error_{Filtered}") +subplot(3, 1, 2) +plot(1:length(x), y1v, 1:length(x), ypv, 1:length(x), yiv, 1:length(x), ydv); grid; legend("PID", "P", "I", "D") +subplot(3, 1, 3) +plot(1:length(x), x, 1:length(x), y2v); grid; legend("Target", "Plant") +end + +function [yvelo, ydist, zf] = plant(zi, nd, x) +b = [zeros(1, nd-1) 0.01]; +a = [1 -1]; +if zi.velo == 0 + zi.velo = zeros(1, max(length(a), length(b))-1); +end +if zi.dist == 0 + zi.dist = zeros(1, 1); +end + +[yvelo, zf.velo] = filter(b, a, x, zi.velo); +[ydist, zf.dist] = filter([1], [1 -1], yvelo, zi.dist); + diff --git a/common/psample.m b/common/psample.m new file mode 100755 index 0000000..1bfb5b1 --- /dev/null +++ b/common/psample.m @@ -0,0 +1,48 @@ +## Copyright (C) 2017 Jens Ahrensfeld +## +## This program is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{retval} =} psample (@var{input1}, @var{input2}) +## +## @seealso{} +## @end deftypefn + +## Author: Jens Ahrensfeld +## Created: 2017-04-19 + +function [h] = psample (N) +P = [10 10 10 10]; +R = calcRange(P); + +h = zeros(1, length(P)); + +for k=1:N, + ii = getSample(R); + h(ii) = h(ii) + 1/N; +end + +function R = calcRange(P) +P = P / sum(P) +R0 = 0; +for k=1:length(P) + R0 = R0 + P(k); + R(k) = R0; +end + +function ii = getSample(R) +Z = rand(); +cand = find(Z <= R); +ii=cand(1); + diff --git a/common/pt_eval.m b/common/pt_eval.m new file mode 100755 index 0000000..a009e9c --- /dev/null +++ b/common/pt_eval.m @@ -0,0 +1,44 @@ +## Copyright (C) 2016 Jens Ahrensfeld +## +## This program is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{retval} =} pt_eval (@var{input1}, @var{input2}) +## +## @seealso{} +## @end deftypefn + +## Author: Jens Ahrensfeld +## Created: 2016-11-04 + +function pt_eval (vX, vY) + +if 0 + k = sqrt(2)/1.5; + v = sqrt(vX^2 + vY^2); + nx = k*abs(vX)/v; + ny = k*abs(vY)/v; +else + k = 1.0/1.5; + nx = k; + ny = k; +end + +mot1 = 0.5*vX*nx + vY*ny +mot2 = -0.5*vX*nx + vY*ny + +rotX = mot1 - mot2 +rotY = 0.5*(mot1 + mot2) +vrot = sqrt(rotX^2 + rotY^2) +endfunction diff --git a/common/sallenkey_eval.m b/common/sallenkey_eval.m new file mode 100755 index 0000000..fd939f7 --- /dev/null +++ b/common/sallenkey_eval.m @@ -0,0 +1,55 @@ +## Copyright (C) 2018 Jens Ahrensfeld +## +## This program is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{retval} =} sallenkey_eval (@var{input1}, @var{input2}) +## +## @seealso{} +## @end deftypefn + +## Author: Jens Ahrensfeld +## Created: 2018-05-28 + +function sallenkey_eval (f_cut_Hz, Q) + +w_cut = 2*pi*f_cut_Hz; + +m = 1.0; +n = Q*(m*m + 1)/m; + +R = 1.0e+3; +C = 1/(R*w_cut) + +R1 = m*R +R2 = R/m + +C1 = n*C +C2 = C/n + + +w0 = 0.1; +w1 = 2*pi*1e+6; +w_step = 10; + +s = j*(w0:w_step:w1); + +H_s = 1./(1 + C2*(R1 + R2)*s + C1*C2*R1*R2*s.^2); + +semilogx(abs(s)/(2*pi), abs(H_s)); grid; xlabel('f/Hz'); ylabel('|H(j*2*pi*f)|') + + + + +endfunction diff --git a/common/stdp_eval.m b/common/stdp_eval.m new file mode 100755 index 0000000..472050f --- /dev/null +++ b/common/stdp_eval.m @@ -0,0 +1,61 @@ +## Copyright (C) 2018 Jens Ahrensfeld +## +## This program is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{retval} =} stdp_eval (@var{input1}, @var{input2}) +## +## @seealso{} +## @end deftypefn + +## Author: Jens Ahrensfeld +## Created: 2018-01-17 + +function stdp_eval () + +N = 8; + +Vp = zeros(N,1); +W = rand(N, 1) + +K = 100; +alpha = 0.5; +beta = 0.5; +thresh = 2.0; +kn = 0.01; +mu = 0.01; + +Vf = 0; +for k=1:K, + + Vin = rand(N,1) > 0.8; + Vp = max(alpha*Vp, Vin); + + V = sum(W.*Vp); + n = kn*(2.0 * (0.5 - rand())); + fire = ((V+n) >= thresh); + Vf = max(beta*Vf, fire); + + W = W + mu*Vp*Vf; + + + _V(k) = V; + _Vin(:, k) = Vin; + _fire(k) = fire; + +end + +plot(1:K, _V, 1:K, _fire); grid; + +endfunction diff --git a/common/ui_eval.m b/common/ui_eval.m new file mode 100755 index 0000000..f72d9cc --- /dev/null +++ b/common/ui_eval.m @@ -0,0 +1,36 @@ +## Copyright (C) 2017 Jens Ahrensfeld +## +## This program is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{retval} =} ui_eval (@var{input1}, @var{input2}) +## +## @seealso{} +## @end deftypefn + +## Author: Jens Ahrensfeld +## Created: 2017-07-12 + +function ui_eval() + +% create figure and panel on it +f = figure; +p = uipanel("title", "Panel Title", "position", [.25 .25 .5 .5]); + +% add two buttons to the panel +b1 = uicontrol ("parent", p, "callback", "onPush(gcf)", "string", "A Button", "position",[18 10 150 36]); +b2 = uicontrol ("parent", p, "string", "Another Button", "position",[18 60 150 36]); + +endfunction + diff --git a/common/vanDusen_eval.m b/common/vanDusen_eval.m new file mode 100755 index 0000000..83bfe61 --- /dev/null +++ b/common/vanDusen_eval.m @@ -0,0 +1,43 @@ +## Copyright (C) 2018 Jens Ahrensfeld +## +## This program is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{retval} =} vanDusen_eval (@var{input1}, @var{input2}) +## +## @seealso{} +## @end deftypefn + +## Author: Jens Ahrensfeld +## Created: 2018-05-28 + +function vanDusen_eval (R0, Tmin, Tmax, dT) + +a = +3.90830e-03 +b = -5.77500e-07 +c = -4.18301e-12 + +T = Tmin:dT:Tmax; + +R1 = R0*(1 + a*T + b*T.^2); +R2 = c.*(T - 100).*T.^3; + + +a_IEC = 0.00385055; +a_SAMA = 0.00392; + +plot(T, R1, T, R0*(1+a_IEC*T), 'r', T, R0*(1+a_SAMA*T), 'g'); grid; xlabel('T/°C'); ylabel('RTD/ohms'); legend('van Dusen','a_{IEC}','a_{SAMA}'); + + +endfunction diff --git a/common/vga_eval.m b/common/vga_eval.m new file mode 100755 index 0000000..d02a141 --- /dev/null +++ b/common/vga_eval.m @@ -0,0 +1,28 @@ +function vga_eval(gain_dB) +% gain_dB : combined gain + +% Constants (gain contraints of HMC625) +gain_min_dB = -13.5; +gain_max_dB = +18.0; + +% Gains of individual VGA +vga_gain = zeros(1,2); + +if gain_dB > 0 + for k=1:length(vga_gain), + vga_gain(k) = min(gain_max_dB, gain_dB); + gain_dB = gain_dB - vga_gain(k); + end +else + for k=1:length(gain), + vga_gain(k) = max(gain_min_dB, gain_dB); + gain_dB = gain_dB - vga_gain(k); + end +end + +vga_gain = vga_gain +fprintf(stdout, "Gain = %f\n", vga_gain(1) + vga_gain(2)); +if gain_dB > 0 + fprintf("Gain not reached!\n"); +end +