A Python-based fuzzy decision support system that determines stress levels based on physiological parameters: Heart Rate (HR) and Skin Conductance (SC).
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.
- 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
-
Heart Rate (HR) - Range: 0-100 bpm
- Low HR: [20, 20, 40]
- Normal HR: [30, 50, 70]
- High HR: [60, 80, 80]
-
Skin Conductance (SC) - Range: 0-50 ms
- Small SC: [10, 10, 20]
- Average SC: [15, 25, 35]
- Above SC: [30, 40, 40]
Stress Level - Range: 0-3
- Relaxed: [1, 1, 1.5]
- Fatigue: [1, 1.5, 2]
- Stressed: [1.5, 2, 2]
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 |
pip install matplotlib numpy- 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- Run the Script:
python fuzzy_dsc.py- View Results: The script will output:
- Fuzzification results (membership degrees)
- Rule evaluation strengths
- Aggregated consequent strengths
- Final crisp stress level
- Visualization plots
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
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
- Applies MIN operator for AND conditions in rule antecedents
- Generates firing strength for each rule
- Uses MAX operator to combine outputs of multiple rules
- Clips membership functions based on firing strengths
- Calculates Center of Gravity (Centroid method)
- Formula: CoG = Σ(μ(x) × x) / Σμ(x)
- Produces final crisp stress level value
The system generates two main plots:
- Membership Functions Plot: Shows all input and output membership functions with current input values marked
- Defuzzification Plot: Displays the aggregated output and the centroid point used for defuzzification
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]
Extend the rules list with additional combinations:
rules = [
(hr_index, sc_index, stress_index),
# Add more rules...
]- 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
- 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)
This project is available for educational and research purposes.
Feel free to fork this repository and submit pull requests for:
- Additional physiological parameters
- Alternative defuzzification methods
- Enhanced visualization features
- Performance optimizations
Created as part of a fuzzy logic decision support system implementation.
For questions or suggestions, please open an issue in the repository.