summaryrefslogtreecommitdiff
path: root/macros/slewrate.sci
blob: 8a5ac0496c1a96cbd0d56127a4fac7030fb22d14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
function [s, lowercrossvalue, uppercrossvalue, lowerreference, upperreference]=slewrate(x, varargin)


    // This function estimate slew rate of bilevel waveform transitions
    // Calling Sequence
    // s=slewrate(x)
    // s=slewrate(x, t)
     // s=slewrate(x, Fs)
    // s=slewrate(x, t, 'PercentReferenceLevels', N )
    // s=slewrate(x, t, 'Tolerance', M)
    // s=slewrate(x, t,'StateLevels', O)

    // [s lowercrossvalue uppercrossvalue lowerreference upperreference]=slewrate(x)
    // [s lowercrossvalue uppercrossvalue lowerreference upperreference]=slewrate(x, t)
    // // [s lowercrossvalue uppercrossvalue lowerreference upperreference]=slewrate(x, Fs)
    // [s lowercrossvalue uppercrossvalue lowerreference upperreference]=slewrate(x, t, 'PercentReferenceLevels', N )
    // [s lowercrossvalue uppercrossvalue lowerreference upperreference]= slewrate(x, t, 'Tolerance', M)
    // [s lowercrossvalue uppercrossvalue lowerreference upperreference]= slewrate(x, t,'StateLevels', O)
    // [s lowercrossvalue uppercrossvalue lowerreference upperreference]= slewrate(x, t,'StateLevels', O, 'fig', on or off)
    //
    // Parameters
    // x: real vector.
    // Fs: specifies the sample rate, Fs, as a positive   scalar, where the first sample instant corresponds to a time of zero.
    // t: defiene instant sample time t as vector with same length of x, or specifies the sample rate, t, as a positive scalar.
    // PercentReferenceLevels: specify the mid percent reference leves as a percentage, default value of N is [10 90].
    // Tolerance: define the tolerance value as real scaler value, where default value of M is 2.0.
    // StateLevels:  define the lower and upper state levels as two element real vector.
    // fig: specify the logical input value to display figure as one of 'on' or 'off', where the default input in 'off'.
    // s: returns a vector of the ratios of the level difference to the time duration between the points where each transition crosses the 10% and 90% reference levels.
    // lowercrossvalue: return the lowerc cross value of bilevel waveform transitions X
    // uppercrossvalue: return the upper cross value of bilevel waveform transitions X
    // lowerreference: return lower reference value corrosponding to lower percent reference value.
    // upperreference: return upper reference value corrosponding to upper percent reference value.

    // Example1
    // x=[1.2, 5, 10, -20, 12]
    //t=1:length(x)
    //s=slewrate(x, t)
    //EXPECTED OUTPUT:
    //s  = - 30.    32.
    //
    //Example2
    //x=[ones(1,25)  -ones(1,25)  ones(1,25) ]
    //t=1:length(x)
    //s=slewrate(x,t)
   //EXPECTED OUTPUT:
    //s  = - 2  2

    // See also
    // Authors
    // Jitendra Singh


  // run statelevels and midcross function before running risetime function.

         if or(type(x)==10) then
    error ('Input arguments must be double.')
end

if size(x,1)==1 | size (x,2)==1 then
    else
    error ('Argument X must be vector.')
end



   if  length(varargin)==0 then  // if the no of input is 0, then specify the default values to input parameter.
        [levels hist]=statelevels(x);
        Lvarargin=list(1:length(x), 'StateLevels', levels(1), 'MidPercentReferenceLevel', 10, 'Tolerance', 2, 'fig', 'off')
        Uvarargin=list(1:length(x), 'StateLevels', levels(2), 'MidPercentReferenceLevel', 90, 'Tolerance', 2, 'fig', 'off')
    end

if length(varargin)>=1 & type(varargin(1))==1 then
     if length(varargin(1))==1 then
        t=(0:(length(x)-1));

    elseif length(varargin(1))==length(x) then
        t=varargin(1);

    else
        error('T must be a same length as X.')
    end
else
    t=1:length(x);
end


if length(varargin)>=2 & type(varargin(1))==1 & type(varargin(2))==1 then
    error ("Too many leading numeric arguments (at most 2 expected).");
end




sindex=[];
if length(varargin)>=1 then
a=1;

for i=1:length(varargin)
    if type(varargin(i))==10 then
        sindex(a)=i;
        a=a+1;
        end
end
end


if length(sindex)>5 then
    error('Unexpected argument.')
end

if length(varargin)==1 & (isempty(sindex)) then

    [levels hist]=statelevels(x);
        Lvarargin=list(t, 'StateLevels', levels(1), 'MidPercentReferenceLevel', 10, 'Tolerance', 2, 'fig', 'off')
        Uvarargin=list(t, 'StateLevels', levels(2), 'MidPercentReferenceLevel', 90, 'Tolerance', 2, 'fig', 'off')

end

fig='OFF';
//////////////////////////////////

if (~isempty(sindex)) then
        for j=1:length(sindex)

            select convstr(varargin(sindex(j)),'u')

                case {'STATELEVELS'}
                   ////
                 if length(varargin) <=sindex(j) then
                      error(strcat(['parameter StateLevels required a value']));
                  end

                  if type(varargin(sindex(j)+1))==1 then
                      levels=varargin(sindex(j)+1);

                   elseif type(varargin(sindex(j)+1))==10 & convstr(varargin(sindex(j)+1), 'u')=='PERCENTREFERENCELEVELS' |  convstr(varargin(sindex(j)+1),'u')== 'TOLERANCE' | convstr(varargin(sindex(j)+1), 'u')=='FIG' then

                    error('parameter StateLevels required a value.')


                  elseif type(varargin(sindex(j)+1))==10  then

                    error('Expected STATELEVELS to be one of these types: double, Instead its type was char.')
                    end


                case {'PERCENTREFERENCELEVELS'}
            if length(varargin) <=sindex(j) then
                      error(strcat(['parameter MidPercentRefernceLevel required a value.']));
                  end

                  if  type(varargin(sindex(j)+1))==1 then
                      midpercentval= varargin(sindex(j)+1);
                        elseif type(varargin(sindex(j)+1))==10 & convstr(varargin(sindex(j)+1), 'u')=='STATELEVELS' | convstr(varargin(sindex(j)+1),'u')== 'TOLERANCE' | convstr(varargin(sindex(j)+1), 'u')=='FIG' then
                    error('parameter MidPercentRefernceLevel required a value.')

                  elseif type(varargin(sindex(j)+1))==10 then
                    error('Expected MidPercentRefernceLevel to be one of these types: double, Instead its type was char.')
                end

                if length( midpercentval)~=2 then
                    error ('Expected MidPercentRefernceLevel to be of size 1x2')
                end
  /////////////////////////////////

                    perval=varargin(sindex(j)+1);
                    disp(perval)
                   if perval(2)<= perval(1) then
                     error('The PercentReferenceLevels must be in increasing order.')
                     end

                       varargin(sindex(j))='MidPercentReferenceLevel';
                     varargin(sindex(j)+1)=perval(1);
                    Lvarargin= varargin;

                    varargin(sindex(j)+1)=perval(2);
                    Uvarargin=varargin;



            case {'FIG'}

                if length(varargin) <=sindex(j) then
                      error(strcat(['parameter fig required a value.']));
                  end

                  if type(varargin(sindex(j)+1))==1 then
                      error ('Expected fig to match one of these strings: on or off');

                 elseif type(varargin(sindex(j)+1))==10 & convstr(varargin(sindex(j)+1), 'u')=='STATELEVELS' | convstr(varargin(sindex(j)+1), 'u')== 'TOLERANCE' | convstr(varargin(sindex(j)+1), 'u')=='PERCENTREFERENCELEVELS' then
                    error('parameter fig required a value.')
                    else
                        fig=  convstr(varargin(sindex(j)+1), 'u');

                    end


                     if fig == 'OFF' | fig == 'ON' then
        else
     error('Expected fig to match one of these strings: on or off');
           end



        case{'ON'}



         case{'OFF'}


                case {'TOLERANCE'}

            if length(varargin) <=sindex(j) then
                      error(strcat(["parameter Tolerance required a value"]));

                  elseif type(varargin(sindex(j)+1))==1 then
                     tolerance= varargin(sindex(j)+1);

                  elseif type(varargin(sindex(j)+1))==10 & convstr(varargin(sindex(j)+1), 'u')== 'STATELEVELS' | convstr(varargin(sindex(j)+1), 'u')== 'PERCENTREFERENCELEVELs' | convstr(varargin(sindex(j)+1), 'u')=='FIG' then

                    error('parameter Tolerance required a value.');

                  elseif type(varargin(sindex(j)+1))==10  then

                    error('Expected Tolerance to be one of these types: double, Instead its type was char.');
                    end


            else
              error(strcat(['Invalid optional argument'," ", varargin(sindex(j))]));
            end // switch
        end // for
    end // if

/////////////////////////////////////////////



 indexx=[];
if length(sindex)>=1  then
a=1;
for i=1:length(sindex)

  indexx(a)=find(convstr(varargin(sindex(i)), 'u')=='MIDPERCENTREFERENCELEVEL')
    a=a+1;
end
end


if  sum(indexx)==0 then

     varargin(length(varargin)+1)='MIDPERCENTREFERENCELEVEL';
           varargin(length(varargin)+1)=10;
                    Lvarargin= varargin;

                    varargin(length(varargin))=90;
                    Uvarargin=varargin;
end

index_on=[];

if length(sindex)>=1  then
a=1;

for i=1:length(sindex)

  index_on(a)=find(convstr(varargin(sindex(i)), 'u')=='ON')

    a=a+1;

end
end

if sum(index_on)>0 then
    Lvarargin(sindex(find(index_on>0)))='OFF';
    Uvarargin(sindex(find(index_on>0)))='OFF';
end




   [lcrossval lref levels t tolerance]= midcross(x, Lvarargin(:));

    [ucrossval uref]=midcross(x, Uvarargin(:));


    if length(lcrossval)==length(ucrossval) then
        ss=ucrossval-lcrossval

    elseif length(lcrossval)>length(ucrossval)
        n=length(ucrossval);
       ss=ucrossval-lcrossval(1:n);

    else
        n=length(lcrossval);
        ss=ucrossval(1:n)-lcrossval;


 end


 s=(uref-lref)./ss;


 uppercrossvalue=ucrossval;
 lowercrossvalue=lcrossval;

 lowerreference=lref;
 upperreference=uref;

     upperbound= levels(2)- (tolerance/100)*(levels(2)-levels(1));
 mostupperbound=levels(2)+ (tolerance/100)*(levels(2)-levels(1));
  lowerbound= levels(1)+ (tolerance/100)*(levels(2)-levels(1));
  mostlowerbound=levels(1)- (tolerance/100)*(levels(2)-levels(1));





   if fig=='ON' then   // if the defined output is only 1, the it will provide the graphical representation of                          //levels

      if length(s)==0 then

        plot(t,x, 'LineWidth',1, 'color', 'black')

         plot(t,upperreference * ones(1, length(t)),'-r', 'LineWidth',0.5)

       plot(t,lowerreference * ones(1, length(t)),'-g', 'LineWidth',0.5)


      plot(t,mostupperbound * ones(1, length(t)),'--r', 'LineWidth',0.5)

      plot(t,levels(2) * ones(1, length(t)),'--k', 'LineWidth',0.5)

      plot(t,upperbound * ones(1, length(t)),'--r', 'LineWidth',0.5)



       plot(t,lowerbound *ones(1, length(t)),'--g', 'LineWidth',0.5)

       plot(t,levels(1) * ones(1, length(t)),'--k', 'LineWidth',0.5)

       plot(t,mostlowerbound * ones(1, length(t)),'--g', 'LineWidth',0.5)

       xlabel("Time (second)", "fontsize",3, "color", "black" )
     ylabel("Level (Volts)", "fontsize",3, "color", "black" )


     legends(["Signal";   "upper boundary"; "upper state"; "lower boundary"; "upper reference"; "lower reference"; "upper boundary"; "lower state"; "lower boundary"],  [[1;1], [5;2], [1;2], [5;2], [5;1], [3;1], [3;2], [1;2], [3;2]], opt='?')


      else


      plot(t,x, 'LineWidth',1, 'color', 'black')

       plot(t,upperreference * ones(1, length(t)),'-r', 'LineWidth',0.5)

      plot(t,lowerreference * ones(1, length(t)),'-g', 'LineWidth',0.5)

    rects=[lowercrossvalue; upperreference*ones(lowercrossvalue); ss; (upperreference-lowerreference)*ones(s)]

   col=-10*ones(s);

    xrects(rects, col);

     plot(uppercrossvalue, upperreference*ones(uppercrossvalue), "r*", 'MarkerSize',15);

      plot(lowercrossvalue, lowerreference*ones(lowercrossvalue), "g*", 'MarkerSize',15);

      plot(t,mostupperbound * ones(1, length(t)),'--r', 'LineWidth',0.5)

      plot(t,levels(2) * ones(1, length(t)),'--k', 'LineWidth',0.5)

      plot(t,upperbound * ones(1, length(t)),'--r', 'LineWidth',0.5)



       plot(t,lowerbound *ones(1, length(t)),'--g', 'LineWidth',0.5)

       plot(t,levels(1) * ones(1, length(t)),'--k', 'LineWidth',0.5)

       plot(t,mostlowerbound * ones(1, length(t)),'--g', 'LineWidth',0.5)
//
       xlabel("Time (second)", "fontsize",3, "color", "black" )
     ylabel("Level (Volts)", "fontsize",3, "color", "black" )


     legends(["slewrate"; "Signal"; "upper cross"; "lower cross"; "upper boundary"; "upper state"; "lower boundary"; "upper reference"; "lower reference"; "upper boundary"; "lower state"; "lower boundary"],  [[-11; 2] , [1;1], [-10;5], [-10;3], [5;2], [1;2], [5;2], [5;1], [3;1], [3;2],[1;2], [3;2]], opt='?')

   end
   end


endfunction