**Laboratory #5 Clustering / Pattern Recognition**

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"

- Run Matlab
- Type "fuzzy"
- First we will create a new Fuzzy Inference System. Open a new Sugeno FIS under the "File" menu.
- Close the old FIS
- Save the new FIS as EMG.fis
- Now we will configure the first EMG input and the associated membership functions. Select "Edit/Add Inputs" and add 1 more input
- 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.
- Double click on the box now labeled "Flex"
- Select "Edit/Add MF's" and select 2 and click "OK"
- Make the input and display ranges [0 2048]
- Single click on the MF1 membership function
- Type in a new name: "Flex Large"
- Select a new Type "smf"
- 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.
- Single click on the MF2 membership function
- Type in a new name: "Flex Small"
- Select a new Type "zmf"
- 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.
- Click on "close"
- 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.
- Double click on th box now labeled "Extend"
- Select "Edit/Add MF's" and select 2 and click "OK"
- Make the input and display ranges [0 2048]
- Single click on the MF1 membership function
- Type in a new name: "Extend Large"
- Select a new Type "smf"
- 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.
- Single click on the MF2 membership function
- Type in a new name: "Extend Small"
- Select a new Type "zmf"
- 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.
- Click "close"
- 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.
- Single click on the output labeled "output1" and change its name to "bend".
- Double click on the box now labeled "bend".
- Select "Edit / Add MF's" and create three membership functions.
- Click "ok"
- Single click on "MF3" and change its name to "Bend Inward".
- 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].
- Single click on "MF2" and change its name to "No Bend".
- Select the membership function type as "linear" and set its parameters to [0 0 0.5].
- Single click on "MF1" and change its name to "Bend Outward".
- Select the membership function type as "linear" and set its parameters to [0 0 0].
- You have now finished setting up the output membership functions. Click on "close".
- For safety, go ahead and save the FIS.
- The last step in setting up an FIS is to set up the rules. Select "View/Edit Rules"
- We will start out with three rules. First type "If Flex is Flex_Small and Extend is Extend_Large then Bend is Bend_Outward"
- 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.
- Now type "If Flex is Flex_Small and Extend is Extend_Small then Bend is No_Bend"
- 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.
- Now type "If Flex is Flex_Large and Extend is Extend_Small then Bend is Bend_Inward"
- 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.
- Click on "close"
- Congratulations, you have just made your first fuzzy classifier!
- Make sure to save your FIS one more time.
- Quit the FIS editor
- To check your work, in Matlab type "EMGFIS = Readfis('EMG.fis')"
- Type "Plotfis(EMGFIS)" and Matlab will display the entire FIS you just created.
- Close the figure
- 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.
- Change the input to [0 0]. Notice with both inputs small you should get an output that is strongest at 0.5.
- 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.
- 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.
- 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.
- What happens if both inputs are large? Why does the Matlab command window display an error?
- Close the ruleview screen.

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

- Type "load forward;"
- 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.
- We need to scale the two inputs. Type "forward = 2048*forward;" and "back = 2048*back;"
- Type "Flex = evalfis(forward,EMGFIS);". This evaluates the FIS using the input "forward".
- Type "plot(Flex)"
- Is this what you expected? Why or why not?
- Type "Extend = evalfis(back,EMGFIS);". This evaluates the FIS using the input "back".
- Type "plot(Extend)"
- Is this what you expected? Why or why not?
- You can now type "fuzzy" again and edit your membership functions to improve the classification.
- Can you get 100% correct?
- What if you limit "forward" and "back" like you did in the previous pattern recognition experiment (Lab #2, Section 3.3, Step 11)?

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

- First we must set up the training matrix. Type "trainout = zeros(length(back),1)" This is your desired output.
- 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.
- type "NEWFIS = anfis(trainextend, EMGFIS)" This will create a new FIS from EMGFIS trained on your data.
- Examine the new FIS using "ruleview(NEWFIS)" and "ruleview(EMGFIS)"
- Have your membership functions changed?
- What if you limit the data set as discussed previously?
- How would we go about checking this new FIS?
- Close the two "rule view" windows
- 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.
- 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.
- type "NEWFIS2 = anfis(trainflex, NEWFIS)" This will create a new FIS from NEWFIS trained on your data.
- Examine the new FIS using "ruleview(NEWFIS2)" , "ruleview(NEWFIS)", and "ruleview(EMGFIS)"
- Have your membership functions changed?
- How could you train the FIS using all the input data at once instead of creating a new FIS each time you evaluate a new class of input data?