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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
function [midcrossvalue, midreference, levels, t, tolerance, Tinput]= midcross(x, varargin)
// This function estimate midcross values of real vector X.
// Calling Sequence
// midcrossvalue=midcross(x)
// midcrossvalue=midcross(x, Fs)
// midcrossvalue=midcross(x, t)
// midcrossvalue=midcross(x, t, 'MidPercentReferenceLevels', N )
// midcrossvalue=midcross(x, t, 'Tolerance', M)
// midcrossvalue=midcross(x, t,'StateLevels', O)
// [midcrossvalue midreference]=midcross(x)
// [midcrossvalue midreference]=midcross(x, Fs)
// [midcrossvalue midreference]=midcross(x, t)
// [midcrossvalue midreference]= midcross(x, t, 'MidPercentReferenceLevel', N )
// [midcrossvalue midreference]= midcross(x, t, 'Tolerance', M)
// [midcrossvalue midreference]= midcross(x, t,'StateLevels', O)
// [midcrossvalue midreference]= midcross(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
// MidPercentReferenceLevels: specify the midpercentreferenceleves as a percentage, default value of N is 50.
// 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'.
// midcrossvalues: return the midcross values
// midreference: return the midrefence values.
// levels: return statelevels values.
// t: return the instant sample time.
// tolerance: retunr the tolerance value
// Tinput: return t value, which given as input parameter.
// Examples
// x=[1.2, 5, 10, -20, 12]
//t=1:length(x)
// midcrossvalue=midcross(x, t)
// See also
// Authors
// Jitendra Singh
if or(type(x)==10) then
error ('Input arguments must be double.')
end
if length(x) < 2 then // checking the length of input datasat
error('X must be a vector with more than one element.'); // if length of X is less 2, it will give error
end
if length(varargin) >9 then // checking the length of input datasat
error('Unexpected input/To many input'); // if length of X is less 2, it will give error
end
if length(varargin)==0 then
t=1:length(x);
Tinput=1:length(x);
[levels hist]=statelevels(x); // run statelevels function before running this function
midpercentval=50;
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))/varargin(1);
Tinput=varargin(1);
elseif length(varargin(1))==length(x) then
t=varargin(1);
Tinput=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
index=[];
if length(varargin)>=1 then
a=1;
for i=1:length(varargin)
if type(varargin(i))==10 then
index(a)=i;
a=a+1;
end
end
end
if length(index)>5 then
error('Unexpected argument.')
end
Nindex=[];
if length(varargin)>=1 then
b=1;
for i=1:length(varargin)
if type(varargin(i))==1 then
Nindex(b)=i;
b=b+1;
end
end
end
d=[];
if length(Nindex)>=2 then
c=1;
for k=1:(length(Nindex)-1)
d(c)=Nindex(k+1)-Nindex(k);
c=c+1;
end
end
if length(d)>=1 then
f_one=find(d==1);
if length(f_one)>0 then
error('Unexpected input.')
end
end
[levels hist]=statelevels(x);
midpercentval=50;
tolerance=2;
fig='OFF';
if (~isempty(index)) then
for j=1:length(index)
select convstr(varargin(index(j)),'u')
case {'STATELEVELS'}
//////
if length(varargin) <=index(j) then
error(strcat(['parameter StateLevels required a value']));
end
if type(varargin(index(j)+1))==1 then
levels=varargin(index(j)+1);
elseif type(varargin(index(j)+1))==10 & convstr(varargin(index(j)+1), 'u')=='MIDPERCENTREFERENCELEVEL' | convstr(varargin(index(j)+1),'u')== 'TOLERANCE' | convstr(varargin(index(j)+1), 'u')=='FIG' then
error('parameter StateLevels required a value.')
elseif type(varargin(index(j)+1))==10 then
error('Expected STATELEVELS to be one of these types: double, Instead its type was char.')
end
if length(levels)~=2 then
error ('Expected STATELEVELS to be of size 1x2')
end
if levels(2)<=levels(1) then
error('The state levels must be in increasing order.')
end
///////
case {'MIDPERCENTREFERENCELEVEL'}
if length(varargin) <=index(j) then
error(strcat(['parameter MidPercentRefernceLevel required a value.']));
end
if type(varargin(index(j)+1))==1 then
midpercentval= varargin(index(j)+1);
elseif type(varargin(index(j)+1))==10 & convstr(varargin(index(j)+1), 'u')=='STATELEVELS' | convstr(varargin(index(j)+1),'u')== 'TOLERANCE' | convstr(varargin(index(j)+1), 'u')=='FIG' then
error('parameter MidPercentRefernceLevel required a value.')
elseif type(varargin(index(j)+1))==10 then
error('Expected MidPercentRefernceLevel to be one of these types: double, Instead its type was char.')
end
if length( midpercentval)~=1 then
error ('Expected MidPercentRefernceLevel to be of size 1x1')
end
case {'FIG'}
if length(varargin) <=index(j) then
error(strcat(['parameter fig required a value.']));
end
if type(varargin(index(j)+1))==1 then
error ('Expected fig to match one of these strings: on or off');
elseif type(varargin(index(j)+1))==10 & convstr(varargin(index(j)+1), 'u')=='STATELEVELS' | convstr(varargin(index(j)+1), 'u')== 'TOLERANCE' | convstr(varargin(index(j)+1), 'u')=='MIDPERCENTREFERENCELEVEL' then
error('parameter fig required a value.')
else
fig= convstr(varargin(index(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'}
if length(varargin) == 1 then
error ('Unexpected input.')
elseif type(varargin(index(j)-1))==1 then
error ('Unexpected input.');
elseif convstr(varargin(index(j)-1), 'u')~='FIG' then
error('Unexpected input');
end
case{'OFF'}
if length(varargin) == 1 then
error ('Unexpected input.')
elseif type(varargin(index(j)-1))==1 then
error ('Unexpected input.');
elseif convstr(varargin(index(j)-1), 'u')~='FIG' then
error('Unexpected input');
end
//////
case {'TOLERANCE'}
if length(varargin) <=index(j) then
error(strcat(['parameter Tolerance required a value"]));
elseif type(varargin(index(j)+1))==1 then
tolerance= varargin(index(j)+1);
elseif type(varargin(index(j)+1))==10 & convstr(varargin(index(j)+1), 'u')== 'STATELEVELS' | convstr(varargin(index(j)+1), 'u')== 'MIDPERCENTREFERENCELEVEL' | convstr(varargin(index(j)+1), 'u')=='FIG' then
error('parameter Tolerance required a value.');
elseif type(varargin(index(j)+1))==10 then
error('Expected Tolerance to be one of these types: double, Instead its type was char.');
end
if length(tolerance)~=1 then
error ('Expected Tolerance to be of size 1x1');
end
else
error(strcat(['Invalid optional argument'," ", varargin(index(j))]));
end // switch
end // for
end // if
tolerance=tolerance;
if tolerance>=50 then
error('Expected Toleracne to be an array with all of the values < 50.')
end
if tolerance>= midpercentval then
error ('The percent state level tolerance must be less than the mid/lower percent reference level.')
end
if tolerance+midpercentval>=100 then
error('The sum of the percent state level Tolerance and the mid/upper percent reference level must be less than 100.')
end
midref=levels(1)+ (midpercentval/100)*(levels(2)-levels(1));
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));
istate = find(x<lowerbound | x>upperbound);
n=length(istate);
istatepre = istate(1:(n-1));
istatepost = istate(2:n);
itrans = find(x(istatepre) < lowerbound & upperbound < x(istatepost) | ...
x(istatepre) > upperbound & lowerbound > x(istatepost) );
ipre = istatepre(itrans);
ipost = istatepost(itrans);
polarity = 2 * (x(ipre) < lowerbound) - 1;
numtrans = length(itrans);
iRMid = zeros(numtrans, 1);
for i = 1:numtrans // define convenience indices for compactness
ia = ipre(i);
ib = ipost(i);
if polarity(i) > 0
// checking for first positive crossing of midrefence
iX = find((x(ia:ib-1) <= midref & midref < x(ia+1:ib)));
iRMid(i) = iX(1) + ia - 1;
else
// checking for negative crossing for midrefenrce
iX = find(x(ia:ib-1)>= midref & midref > x(ia+1:ib));
iRMid(i) = iX(1) + ia - 1;
end
end
x=x(:);
x=x';
t=t(:);
t=t';
if numtrans > 0
// interpolation to get instant values
midcrossvalue=t(iRMid)+(t(iRMid+1)-t(iRMid)).*(midref-x(iRMid))./( x(iRMid+1)-x(iRMid));
else
midcrossvalue = [];
end
midreference=midref;
if fig=='ON' then // if the defined output is only 1, the it will return the graphical representation of //levels
//////
if length(midcrossvalue)==0 then
plot(t,x, 'LineWidth',1, 'color', 'black' )
plot(t,midref * ones(1, length(t)),'-r', '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"; "mid reference"; "upper boundary"; "lower state"; "lower boundary"], [[1;1], [5;2], [1;2], [5;2], [5;1], [3;2],[1;2], [3;2]], opt='?')
else
plot(t,x, 'LineWidth',1, 'color', 'black')
plot(t,midref * ones(1, length(t)),'-r', 'LineWidth',0.5)
plot(midcrossvalue, midreference*ones(midcrossvalue), "r*", '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(["Signal"; "mid cross"; "upper boundary"; "upper state"; "lower boundary"; "mid reference"; "upper boundary"; "lower state"; "lower boundary"], [[1;1], [-10;5], [5;2], [1;2], [5;2], [5;1], [3;2],[1;2], [3;2]], opt='?')
//////////////
end
end
endfunction
|