1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * RICE 4.0x Copyright (C) 1993 Rene' Jager *
5 * This toolbox is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation; either version 2 of *
8 * the License, or (at your option) any later version. *
10 * This toolbox 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. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this toolbox; if not, write to the: *
18 * Free Software Foundation, Inc. *
19 * 675 Mass Ave, Cambridge *
22 * See the RICE documentation for more information on the toolbox. *
23 * The file COPYING for the complete GNU General Public License. *
25 * You can reach me by (preferably e-mail): *
29 * Delft University of Technology *
30 * Department of Electrical Engineering *
31 * Control Laboratory *
40 * e-mail: R.Jager@ET.TUDelft.NL *
41 * phone: +31-15-78 51 14 *
42 * fax: +31-15-62 67 38 *
43 * telex: 38151 butud nl *
45 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
51 Info: utility source file, see file fuzzy.doc
62 #define MAX(a,b) ((a) > (b) ? (a) : (b))
63 #define MIN(a,b) ((a) < (b) ? (a) : (b))
64 #define SQR(a) ((a) * (a))
67 /* smooth membership function(s) */
69 float fpi(float val
, float p1
, float p2
, float p3
, float p4
)
71 if(p2
<= val
&& val
<= p3
)
73 if(val
<= p1
|| p4
<= val
)
75 if(p1
< val
&& val
<= (p1
+ p2
)/2.0)
76 return 2.0*SQR(val
- p1
)/SQR(p2
- p1
);
77 if((p1
+ p2
)/2.0 <= val
&& val
< p2
)
78 return 1.0 - 2.0*SQR(val
- p2
)/SQR(p1
- p2
);
79 if(p3
< val
&& val
<= (p3
+ p4
)/2.0)
80 return 1.0 - 2.0*SQR(val
- p3
)/SQR(p3
- p4
);
81 if((p3
+ p4
)/2.0 <= val
&& val
< p4
)
82 return 2.0*SQR(val
- p4
)/SQR(p3
- p4
);
88 /* hard membership function(s) */
90 float ftrapezium(float val
, float p1
, float p2
, float p3
, float p4
)
92 if(p2
<= val
&& val
<= p3
)
94 if(val
<= p1
|| p4
<= val
)
96 if(p1
< val
&& val
< p2
)
97 return (val
- p1
)/(p2
- p1
);
98 if(p3
< val
&& val
< p4
)
99 return (p4
- val
)/(p4
- p3
);
105 /* indexed-centre-of-gravity */
107 float ficog(int len
, float *set
, float lim
)
110 float up
= 0.0, low
= 0.0;
112 for(i
= 1; i
<= len
; set
++, i
++)
128 /* centre-of-gravity */
130 float fcog(int len
, float *set
)
133 float up
= 0.0, low
= 0.0;
135 for(i
= 1; i
<= len
; set
++, i
++)
150 /* mean-of-maximum */
152 float fmom(int len
, float *set
)
157 top
= fhgt(len
, set
);
180 /* general norm operators */
182 float *fnorm(int len
, float *dest
, float *src
, float (*norm
)(float, float))
189 *dest
= (*norm
)(*dest
, *src
);
195 *dest
= (*norm
)(*dest
, *src
);
204 float *fxnorm(int len
, float *dest
, float *src
,
205 float (*norm
)(float, float, float), float par
)
212 *dest
= (*norm
)(*dest
, *src
, par
);
218 *dest
= (*norm
)(*dest
, *src
, par
);
227 /* intersection operators */
229 float *fzand(int len
, float *dest
, float *src
)
236 *dest
= MIN(*dest
, *src
);
242 *dest
= MIN(*dest
, *src
);
251 float *fpand(int len
, float *dest
, float *src
)
258 *dest
= *dest
* *src
;
264 *dest
= *dest
* *src
;
273 float *fland(int len
, float *dest
, float *src
)
280 *dest
= MAX(*dest
+ *src
- 1.0, 0.0);
286 *dest
= MAX(*dest
+ *src
- 1.0, 0.0);
295 /* union operators */
297 float *fzor(int len
, float *dest
, float *src
)
304 *dest
= MAX(*dest
, *src
);
310 *dest
= MAX(*dest
, *src
);
319 float *fpor(int len
, float *dest
, float *src
)
326 *dest
= *dest
+ *src
- *dest
* *src
;
332 *dest
= *dest
+ *src
- *dest
* *src
;
341 float *flor(int len
, float *dest
, float *src
)
348 *dest
= MIN(*dest
+ *src
, 1.0);
354 *dest
= MIN(*dest
+ *src
, 1.0);
363 /* negation operator */
365 float *fnot(int len
, float *dest
)
381 float fhgt(int len
, float *set
)
387 value
= MAX(value
, *set
);
395 /* alpha-cut and strong alpha-cut */
397 float *fcut(int len
, float *dest
, float *src
, float cut
)
403 *dest
= (*src
>= cut
) ? *src
: 0.0;
412 float *fscut(int len
, float *dest
, float *src
, float cut
)
418 *dest
= (*src
> cut
) ? *src
: 0.0;
435 val
= fpi(0.1, 1.0, 2.0, 3.0, 4.0);
436 printf("val = %f\n", val
);
437 val
= fpi(1.1, 1.0, 2.0, 3.0, 4.0);
438 printf("val = %f\n", val
);
439 val
= fpi(1.6, 1.0, 2.0, 3.0, 4.0);
440 printf("val = %f\n", val
);
441 val
= fpi(2.1, 1.0, 2.0, 3.0, 4.0);
442 printf("val = %f\n", val
);
443 val
= fpi(3.1, 1.0, 2.0, 3.0, 4.0);
444 printf("val = %f\n", val
);
445 val
= fpi(3.6, 1.0, 2.0, 3.0, 4.0);
446 printf("val = %f\n", val
);
447 val
= fpi(4.1, 1.0, 2.0, 3.0, 4.0);
448 printf("val = %f\n", val
);