]> Creatis software - CreaPhase.git/blob - octave_packages/ocs-0.1.3/sbn/Mvoltagesources.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / ocs-0.1.3 / sbn / Mvoltagesources.m
1 ## Copyright (C) 2006,2007,2008  Carlo de Falco            
2 ##
3 ## This file is part of:
4 ## OCS - A Circuit Simulator for Octave
5 ##
6 ## OCS is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation.
9 ##
10 ## This program is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ## GNU General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with this program (see the file LICENSE); if not,
17 ## see <http://www.gnu.org/licenses/>.
18 ##
19 ## author: Carlo de Falco <cdf _AT_ users.sourceforge.net> 
20
21 ## -*- texinfo -*-
22 ##
23 ## @deftypefn{Function File} @
24 ## {[@var{a},@var{b},@var{c}]=} Mvoltagesources(@var{string},@var{parameters},@
25 ## @var{parameternames},@var{extvar},@var{intvar},@var{t})
26 ##
27 ## SBN file implementing models for voltage sources.
28 ##
29 ## @var{string} is used to select among models. Parameters are listed
30 ## as inner items. Possible models are:
31 ##
32 ## @enumerate
33 ## @item @var{string} = "DC" (Static indipendent voltage source)
34 ## @itemize @minus
35 ## @item V -> Current source value
36 ## @end itemize
37 ## @item @var{string} = "sinwave" (Sinusoidal indipendent voltage
38 ## source)
39 ## @itemize @minus
40 ## @item shift -> mean value of sinusoidal input
41 ## @item Ampl  -> amplitude of sinusoidal wave
42 ## @item f     -> frequency of sinusoidal wave
43 ## @item delay -> delay of sinusoidal wave
44 ## @end itemize
45 ## @item @var{string} = "pwl" (Piecewise linear voltage source)
46 ## @itemize @minus
47 ## @item takes as parameter times and values. For example @code{0 1 4 6}
48 ## means at time instant 0 value 1, at time instant 4 value 6, etc.
49 ## @end itemize
50 ## @item @var{string} = "squarewave" (Square wave)
51 ## @itemize @minus
52 ## @item low   -> low-state value
53 ## @item high  -> high-state value
54 ## @item tlow  -> duration of low-state
55 ## @item thigh -> duration of high-state
56 ## @item delay -> delay of square wave
57 ## @item start -> starting voltage value
58 ## @end itemize
59 ## @item @var{string} = "step" (Voltage step)
60 ## @itemize @minus
61 ## @item low   -> low-state value
62 ## @item high  -> high-state value
63 ## @item tstep -> time instant of step transition
64 ## @end itemize
65 ## @item @var{string} = "VCVS" (Voltage controlled voltage source)
66 ## @itemize @minus
67 ## @item K -> Control parameter
68 ## @end itemize
69 ## @end enumerate
70 ##
71 ## See the @cite{IFF file format specifications} for details about 
72 ## the output structures.
73 ##
74 ## @seealso{prs_iff,asm_initialize_system,asm_build_system}
75 ## @end deftypefn
76
77 function [a,b,c] = Mvoltagesources (string,parameters,parameternames,extvar,intvar,t)
78
79   if isempty(intvar)
80     intvar = 0;
81   endif
82
83   switch string 
84       ##LCR part
85     case "DC"
86       for ii=1:length(parameternames)
87         eval([parameternames{ii} "=" num2str(parameters(ii)) ";"])      
88       endfor
89       
90       j = intvar(1);
91       
92       a = zeros(3);
93       b = [0 0 1;0 0 -1;1 -1 0];
94       c = [0 0 -V];
95       break
96       ## NLC part
97     case "sinwave"
98       for ii=1:length(parameternames)
99         eval([parameternames{ii} "=" num2str(parameters(ii)) ";"])      
100       endfor
101      
102       DV = shift+Ampl * sin(2*pi*(t+delay)*f );
103       j = intvar(1);
104       
105       a = zeros(3);
106       b = [0 0 1;0 0 -1;1 -1 0];
107       c = [0 0 -DV]' + b * [extvar;intvar];
108       break
109     
110     case "pwl"
111
112       times = parameters(1:2:end-1);
113       values= parameters(2:2:end);
114      
115       DV = interp1(times,values,t,"linear","extrap");
116       j  = intvar(1);
117       
118       a = zeros(3);
119       b = [0 0 1;0 0 -1;1 -1 0];
120       c = [0 0 -DV]' + b * [extvar;intvar];
121       break
122       
123     case "squarewave"
124       for ii=1:length(parameternames)
125         eval([parameternames{ii} "=" num2str(parameters(ii)) ";"])      
126       endfor
127       if t<delay
128         if exist("start")
129           DV=start;
130         else
131           DV=low;
132         endif
133       else
134         T = tlow+thigh;
135         t = mod(t-delay,T);
136         if t<tlow
137           DV = low;
138         else
139           DV = high;
140         endif
141       endif
142       j = intvar(1);
143             
144       a = zeros(3);
145       b = [0 0 1;0 0 -1;1 -1 0];
146       c = [0 0 -DV]' + b * [extvar;intvar];
147       break
148
149     case "step"
150       for ii=1:length(parameternames)
151         eval([parameternames{ii} "=" num2str(parameters(ii)) ";"])      
152       endfor
153       
154       if t<tstep
155         DV=low;
156       else
157         DV = high;
158       endif
159
160       j = intvar(1);
161       
162       a = zeros(3);
163       b = [0 0 1;0 0 -1;1 -1 0];
164       c = [0 0 -DV]' + b * [extvar;intvar];
165       break
166       
167     case "VCVS"
168       K = 1;
169       for ii=1:length(parameternames)
170         eval([parameternames{ii} "=" num2str(parameters(ii)) ";"])      
171       endfor
172
173       j = intvar(1);
174       
175       a = zeros(5);
176       b = [0 0 0 0 1;0 0 0 0 -1;0 0 0 0 0;0 0 0 0 0;1 -1 -K K 0];
177       c = zeros(5,1);
178       break
179     otherwise
180       error (["unknown section:" string])
181   endswitch
182
183 endfunction