Laboratory #5 Clustering / Pattern Recognition


5. Fuzzy Pattern Recognition


5.1 Setting up a Sugeno Fuzzy Network


In this section of the lab, we will set up a Sugeno style Fuzzy Inference System to classify the EMG data you took last week. We will use both EMG inputs, one from the front of the forearm and one from the back of the forearm. There will be three output classes, "bend inward", "no bending", and "bend outward"

  1. Run Matlab
  2. Type "fuzzy"
  3. First we will create a new Fuzzy Inference System. Open a new Sugeno FIS under the "File" menu.
  4. Close the old FIS
  5. Save the new FIS as EMG.fis
  6. Now we will configure the first EMG input and the associated membership functions. Select "Edit/Add Inputs" and add 1 more input
  7. Single click on the box labeled "input 1" and name it "Flex". Flex will correspond to the EMG data coming from the front of the forearm.
  8. Double click on the box now labeled "Flex"
  9. Select "Edit/Add MF's" and select 2 and click "OK"
  10. Make the input and display ranges [0 2048]
  11. Single click on the MF1 membership function
  12. Type in a new name: "Flex Large"
  13. Select a new Type "smf"
  14. Make the new "Params" be [0 500]. This defines the membership function for the class "Flex Large". The first parameter defines when the curve begins to rise. The second parameter defines when the curve flattens out.
  15. Single click on the MF2 membership function
  16. Type in a new name: "Flex Small"
  17. Select a new Type "zmf"
  18. Make the new "Params" be [1000 1500 0 0]. This defines the membership function for the class "Flex Small". The first parameter defines when the curve begins to drop. The second parameter defines when the curve flattens out. The last two parameters are always zero.
  19. Click on "close"
  20. Now we will configure the second EMG input and the associated membership functions. Single click on the box labeled "input 2" and name it "Extend". Extend will correspond to the EMG data coming from the back of the forearm.
  21. Double click on th box now labeled "Extend"
  22. Select "Edit/Add MF's" and select 2 and click "OK"
  23. Make the input and display ranges [0 2048]
  24. Single click on the MF1 membership function
  25. Type in a new name: "Extend Large"
  26. Select a new Type "smf"
  27. Make the new "Params" be [0 500]. This defines the membership function for the class "Extend Large". The first parameter defines when the curve begins to rise. The second parameter defines when the curve flattens out.
  28. Single click on the MF2 membership function
  29. Type in a new name: "Extend Small"
  30. Select a new Type "zmf"
  31. Make the new "Params" be [1000 1500 0 0]. This defines the membership function for the class "Extend Small". The first parameter defines when the curve begins to drop. The second parameter defines when the curve flattens out. The last two parameters are always zero.
  32. Click "close"
  33. Now we will configure the output class. Since we are only interested in recognizing which way your wrist is flexed, we will have only one output.
  34. Single click on the output labeled "output1" and change its name to "bend".
  35. Double click on the box now labeled "bend".
  36. Select "Edit / Add MF's" and create three membership functions.
  37. Click "ok"
  38. Single click on "MF3" and change its name to "Bend Inward".
  39. Because this is a "Sugeno" FIS and not a "Mamdani" FIS, the output membership functions are either a constant or a line. The line has the equation "param1*input1 + param2*input + param3". Select the membership function type as "linear" and set its parameters to [0 0 1].
  40. Single click on "MF2" and change its name to "No Bend".
  41. Select the membership function type as "linear" and set its parameters to [0 0 0.5].
  42. Single click on "MF1" and change its name to "Bend Outward".
  43. Select the membership function type as "linear" and set its parameters to [0 0 0].
  44. You have now finished setting up the output membership functions. Click on "close".
  45. For safety, go ahead and save the FIS.
  46. The last step in setting up an FIS is to set up the rules. Select "View/Edit Rules"
  47. We will start out with three rules. First type "If Flex is Flex_Small and Extend is Extend_Large then Bend is Bend_Outward"
  48. Hit the "ctrl" key and the "return" key at the same time. If you have entered the rule correctly, the number 1 should appear before the rule you just typed. Otherwise an error will appear at the bottom of your screen.
  49. Now type "If Flex is Flex_Small and Extend is Extend_Small then Bend is No_Bend"
  50. Hit the "ctrl" key and the "return" key at the same time. If you have entered the rule correctly, the number 2 should appear before the rule you just typed. Otherwise an error will appear at the bottom of your screen.
  51. Now type "If Flex is Flex_Large and Extend is Extend_Small then Bend is Bend_Inward"
  52. Hit the "ctrl" key and the "return" key at the same time. If you have entered the rule correctly, the number 3 should appear before the rule you just typed. Otherwise an error will appear at the bottom of your screen.
  53. Click on "close"
  54. Congratulations, you have just made your first fuzzy classifier!
  55. Make sure to save your FIS one more time.
  56. Quit the FIS editor
  57. To check your work, in Matlab type "EMGFIS = Readfis('EMG.fis')"
  58. Type "Plotfis(EMGFIS)" and Matlab will display the entire FIS you just created.
  59. Close the figure
  60. Type "ruleview(EMGFIS)" and you will see the rules you have created. Notice there are three lines; one for each rule. If you click on the number on the left of the rule, the corresponding linguistic rule will be displayed below the figure. Try this for each rule.
  61. Change the input to [0 0]. Notice with both inputs small you should get an output that is strongest at 0.5.
  62. Change the input to [2048 0]. This means that "flex" is large and "extend" is small which means that the output should be "bend inward" which corresponds to an output of one.
  63. Change the input to [0 2048]. This means that "flex" is small and "extend" is large which means that the output should be "bend outward" which corresponds to an output of zero.
  64. You can change the inputs simply by clicking on one of the vertical input lines and dragging it left or right. Play around with various inputs.
  65. Close the ruleview screen.


5.2 Classifying Using a Sugeno Fuzzy Network


You are now ready to classify your emg data collected previously.

  1. Type "load forward;"
  2. Type "load back;" Check the previous part of the lab if you have forgotten how you recorded "forward" or "back" (Lab #2, Section 3.2, Steps 17 and 21). Remember, each has two columns; one for the EMG from the front of the arm; one for the EMG from the back of the arm.
  3. We need to scale the two inputs. Type "forward = 2048*forward;" and "back = 2048*back;"
  4. Type "Flex = evalfis(forward,EMGFIS);". This evaluates the FIS using the input "forward".
  5. Type "plot(Flex)"
  6. Type "Extend = evalfis(back,EMGFIS);". This evaluates the FIS using the input "back".
  7. Type "plot(Extend)"
  8. You can now type "fuzzy" again and edit your membership functions to improve the classification.


5.3 Adaptive Neuro-Fuzzy Inference System (ANFIS)


You can use the ANFIS algorithm as discussed in class to automatically modify your FIS to improve classification accuracy.

  1. First we must set up the training matrix. Type "trainout = zeros(length(back),1)" This is your desired output.
  2. Type "trainextend = [back trainout];" This sets up your training matrix with the first two columns being your two channels of EMG input data and the last column being your desired output.
  3. type "NEWFIS = anfis(trainextend, EMGFIS)" This will create a new FIS from EMGFIS trained on your data.
  4. Examine the new FIS using "ruleview(NEWFIS)" and "ruleview(EMGFIS)"
  5. Close the two "rule view" windows
  6. Lets go ahead and train on the flex data. First we must set up the training matrix. Type "trainout2 = ones(length(forward),1)" This is your desired output.
  7. Type "trainflex = [forward trainout2];" This sets up your training matrix with the first two columns being your two channels of EMG input data and the last column being your desired output.
  8. type "NEWFIS2 = anfis(trainflex, NEWFIS)" This will create a new FIS from NEWFIS trained on your data.
  9. Examine the new FIS using "ruleview(NEWFIS2)" , "ruleview(NEWFIS)", and "ruleview(EMGFIS)"