Skip to content

rapzyftminji/basic-fuzzy-implementation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Fuzzy Logic Stress Level Detection System

A Python-based fuzzy decision support system that determines stress levels based on physiological parameters: Heart Rate (HR) and Skin Conductance (SC).

Overview

This project implements a complete fuzzy logic inference system that analyzes physiological data to assess stress levels. The system uses Mamdani-style fuzzy inference with triangular membership functions, rule-based reasoning, and centroid defuzzification.

Features

  • Fuzzification: Converts crisp input values (HR and SC) into fuzzy membership degrees
  • Rule Evaluation: Applies 9 fuzzy inference rules to determine stress levels
  • Aggregation: Combines multiple rule outputs using maximum operator
  • Defuzzification: Converts fuzzy output to crisp stress level using Center of Gravity (COG) method
  • Visualization: Generates comprehensive plots showing membership functions and defuzzification process

System Architecture

Input Variables

  1. Heart Rate (HR) - Range: 0-100 bpm

    • Low HR: [20, 20, 40]
    • Normal HR: [30, 50, 70]
    • High HR: [60, 80, 80]
  2. Skin Conductance (SC) - Range: 0-50 ms

    • Small SC: [10, 10, 20]
    • Average SC: [15, 25, 35]
    • Above SC: [30, 40, 40]

Output Variable

Stress Level - Range: 0-3

  • Relaxed: [1, 1, 1.5]
  • Fatigue: [1, 1.5, 2]
  • Stressed: [1.5, 2, 2]

Fuzzy Rules

The system uses 9 IF-THEN rules to map input combinations to stress levels:

Rule IF HR is AND SC is THEN Stress is
1 Low Small Relaxed
2 Low Average Relaxed
3 Low Above Fatigue
4 Normal Small Relaxed
5 Normal Average Fatigue
6 Normal Above Stressed
7 High Small Fatigue
8 High Average Stressed
9 High Above Stressed

Requirements

pip install matplotlib numpy

Usage

Basic Usage

  1. Set Input Values: Modify the input parameters at the top of the script:
input_hr_val = 62.5  # Heart rate in bpm
input_sc_val = 22.5  # Skin conductance in ms
  1. Run the Script:
python fuzzy_dsc.py
  1. View Results: The script will output:
    • Fuzzification results (membership degrees)
    • Rule evaluation strengths
    • Aggregated consequent strengths
    • Final crisp stress level
    • Visualization plots

Example Output

Input Heart Rate: 62.5 bpm
Input Skin Conductance: 22.5 ms

1. Fuzzification:
  Heart Rate Memberships:
    μ(Low HR) = 0.00
    μ(Normal HR) = 0.62
    μ(High HR) = 0.12
  Skin Conductance Memberships:
    μ(Small SC) = 0.00
    μ(Average SC) = 0.75
    μ(Above SC) = 0.00

2. Rule Evaluation (Antecedent Firing Strengths):
  Rule 1: Strength = 0.00
  Rule 2: Strength = 0.00
  ...

3. Aggregation of Rule Outputs:
  Aggregated strength for 'Relaxed': 0.00
  Aggregated strength for 'Fatigue': 0.62
  Aggregated strength for 'Stressed': 0.12

4. Defuzzification (Centroid Method):
  Crisp Output Stress Level: 1.45

Methodology

1. Fuzzification

Converts crisp input values into fuzzy membership degrees using triangular membership functions:

  • Each input is evaluated against all membership functions
  • Returns degree of membership (0 to 1) for each linguistic term

2. Rule Evaluation

  • Applies MIN operator for AND conditions in rule antecedents
  • Generates firing strength for each rule

3. Aggregation

  • Uses MAX operator to combine outputs of multiple rules
  • Clips membership functions based on firing strengths

4. Defuzzification

  • Calculates Center of Gravity (Centroid method)
  • Formula: CoG = Σ(μ(x) × x) / Σμ(x)
  • Produces final crisp stress level value

Visualization

The system generates two main plots:

  1. Membership Functions Plot: Shows all input and output membership functions with current input values marked
  2. Defuzzification Plot: Displays the aggregated output and the centroid point used for defuzzification

Customization

Modifying Membership Functions

Edit the parameter arrays to adjust membership function shapes:

hr_mf_params = [[20, 20, 40], [30, 50, 70], [60, 80, 80]]
sc_mf_params = [[10, 10, 20], [15, 25, 35], [30, 40, 40]]
stress_mf_params = [[1, 1, 1.5], [1, 1.5, 2], [1.5, 2, 2]]

Format: [left_point, peak_point, right_point]

Adding New Rules

Extend the rules list with additional combinations:

rules = [
    (hr_index, sc_index, stress_index),
    # Add more rules...
]

Applications

  • Healthcare: Patient stress monitoring and assessment
  • Wellness Apps: Real-time stress tracking
  • Biofeedback Systems: Interactive stress management tools
  • Research: Psychophysiological studies
  • Wearable Devices: Integration with health monitoring equipment

Technical Details

  • Language: Python 3.x
  • Fuzzy Logic Type: Mamdani (Type-1)
  • Membership Functions: Triangular
  • T-norm: Minimum (MIN)
  • S-norm: Maximum (MAX)
  • Defuzzification: Centroid (Center of Gravity)

License

This project is available for educational and research purposes.

Contributing

Feel free to fork this repository and submit pull requests for:

  • Additional physiological parameters
  • Alternative defuzzification methods
  • Enhanced visualization features
  • Performance optimizations

Author

Created as part of a fuzzy logic decision support system implementation.

Contact

For questions or suggestions, please open an issue in the repository.

About

A Python-based fuzzy decision support system

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages