Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First Order High-pass and Low-pass filters for Sensors #6695

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from

Conversation

latonita
Copy link
Contributor

@latonita latonita commented May 7, 2024

What does this implement/fix?

This implements simple but very useful first-order High-pass and Low-pass filters.

High-pass - removing static offset and reduce low-freq noise. y[i] := α × (y[i−1] + x[i] − x[i−1])
Low-pass - reducing high-freq noise on signals. y[i] := y[i-1] + α * (x[i] - y[i-1])

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other

Related issue or feature (if applicable): fixes esphome/feature-requests#2705

Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#3875

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx

Example entry for config.yaml:

# Example config.yaml

sensor:
  - platform: template
    id: sensor1
    name: Sensor1
    update_interval: 2s
    unit_of_measurement: s
    lambda: |- 
      const float abc[] =  {70, 65.9375, 66.1875, 67, 66, 66.25, 66.25, 66.3125, 66.1875, 66.1875, 66.6875, 66.375, 1, 66.3125, 66.375, 67.125, 78.8125, 66.875, 73.75, 68.625, 70, 73.5, 66.9375, 66.1875, 65.9375, 66, 66.125, 67.4375, 66, 66.375, 66.3125, 66.125, 66.1875, 67, 66.125, 66.375, 89, 66.5625, 66.25, 67.625, 66.125, 66.25, 1, 66.1875, 66.375, 66.375, 1, 66.1875, 67.125, 78.8125, 66.875, 73.75, 68.625, 70, 73.5, 66.9375, 66.1875, 65.9375, 66.375, 66.125, 67.4375, 66.25, 66.375, 66.3125, 66.125, 66.1875, 123, 123, 66.375, 89, 66.5625, 66.25, 67.625, 66.125, 66.25, 66.3125, 66.1875, 20, 66.375, 66.1875, 66.1875};
      static int num = 0;
      float ret = abc[num++];
      if (num >= sizeof(abc)/sizeof(float)) num = 0;
      return ret;
  - platform: template
    id: sensor1_high_pass
    name: Sensor High Pass
    update_interval: 2s
    lambda: return id(sensor1).state;
    unit_of_measurement: s
    filters:
      - high_pass: 0.75
  - platform: template
    id: sensor1_low_pass
    name: Sensor Low Pass
    update_interval: 2s
    lambda: return id(sensor1).state;
    unit_of_measurement: s
    filters:
      - low_pass: 0.25  
    

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

@codecov-commenter
Copy link

codecov-commenter commented May 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 53.90%. Comparing base (4d8b5ed) to head (92c2373).
Report is 702 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #6695      +/-   ##
==========================================
+ Coverage   53.70%   53.90%   +0.19%     
==========================================
  Files          50       50              
  Lines        9408     9623     +215     
  Branches     1654     1698      +44     
==========================================
+ Hits         5053     5187     +134     
- Misses       4056     4112      +56     
- Partials      299      324      +25     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@latonita latonita changed the title High Pass Filter for Sensors High-pass and Low-pass filters for Sensors May 8, 2024
@latonita latonita marked this pull request as ready for review May 27, 2024 10:21
@latonita latonita requested a review from a team as a code owner May 27, 2024 10:21
@probot-esphome
Copy link

Hey there @esphome/core, mind taking a look at this pull request as it has been labeled with an integration (sensor) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@clydebarrow
Copy link
Contributor

Had you considered generalising this to allow higher-order filters?

@latonita
Copy link
Contributor Author

latonita commented Jun 4, 2024

Had you considered generalising this to allow higher-order filters?

@clydebarrow When I made these simple 1st order HPF/LPF I actually thought it might be generalized and made configurable.
For example, there is a good library which I used many times (original is https://github.com/dimtass/DSP-Cpp-filters, a little bit more interesting fork is here https://github.com/crylent/DSP-Cpp-filters).
So potentially we can have a generic DSP Filter component, which can be configured - whether its a high pass or low pass , 1st/2nd order, and provide configuration params like cut-off frequencies, width and so on.
However, it might be hard for regular users to understand how to work with it and select proper parameters, especialy having in mind the fact that in esphome we work with quite low-frequency signals and might be tough to operate with 'frequency' concepts.

I might actually to come up with the updated generic dsp filter component so we can review. Its not the rocket science, question is usability. maybe several separate specific components might be easier to use... maybe not.

like 1st_hpf, 1st_lpf, 2nd_hpf, 1st_butterworth, etc.

Ideas, thoughts?

@latonita latonita changed the title High-pass and Low-pass filters for Sensors First Order High-pass and Low-pass filters for Sensors Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add high pass filter to sensor component
4 participants