Skip to content

Commit

Permalink
Added band-pass and band-stop filter coefficients calculation to IIRF…
Browse files Browse the repository at this point in the history
…ilters.c
  • Loading branch information
Tellicious committed May 31, 2024
1 parent 63b0e33 commit 3bfc9d6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
7 changes: 5 additions & 2 deletions .github/releaseBody.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## Added assertions to verify correctness of parameters
## Added band-pass and band-stop filter coefficients calculation to IIRFilters.c

**Improvements:**
- Added assertions to `matrix.c` and `numMethods.c`
- Added band-pass and band-stop filter coefficients calculation to `IIRFilters.c` to simplify initialization

**Bugfix:**
- Fixed minor bug in `IIRFilters.c`

See [Changelog](Changelog.md)
9 changes: 8 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## v1.12.0

**Improvements:**
- Added band-pass and band-stop filter coefficients calculation to `IIRFilters.c` to simplify initialization

**Bugfix:**
- Fixed minor bug in `IIRFilters.c`

## v1.11.1

**Improvements:**
Expand Down Expand Up @@ -100,7 +108,6 @@
- Fixed snippets
- Renamed a few variables inside PID object


## v1.2.0

**Improvements:**
Expand Down
22 changes: 21 additions & 1 deletion inc/IIRFilters.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,31 @@ void IIRFilterInitLP(IIRFilterGeneric_t* filter, float lpFreq, float dT_ms);
* \brief Initialize second-order Butterworth high-pass IIR filter
*
* \param[in] filter: pointer to IIR filter structure
* \param[in] hpFreq: low-pass cutoff frequency in Hz
* \param[in] hpFreq: high-pass cutoff frequency in Hz
* \param[in] dT_ms: sampling time in ms
*/
void IIRFilterInitHP(IIRFilterGeneric_t* filter, float hpFreq, float dT_ms);

/**
* \brief Initialize second-order Butterworth band-pass IIR filter
*
* \param[in] filter: pointer to IIR filter structure
* \param[in] centerFreq: center frequency of pass-band in Hz
* \param[in] bandwidth: bandwidth of pass-band in Hz. Response will be symmetrical around centerFreq on a logarithmic scale
* \param[in] dT_ms: sampling time in ms
*/
void IIRFilterInitBP(IIRFilterGeneric_t* filter, float centerFreq, float bandwidth, float dT_ms);

/**
* \brief Initialize second-order Butterworth band-stop IIR filter
*
* \param[in] filter: pointer to IIR filter structure
* \param[in] centerFreq: center frequency of stop-band in Hz
* \param[in] bandwidth: bandwidth of stop-band in Hz. Response will be symmetrical around centerFreq on a logarithmic scale
* \param[in] dT_ms: sampling time in ms
*/
void IIRFilterInitBS(IIRFilterGeneric_t* filter, float centerFreq, float bandwidth, float dT_ms);

/**
* \brief Apply generic IIR filter to provided sample
*
Expand Down
25 changes: 24 additions & 1 deletion src/IIRFilters.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void IIRFilterInitLP(IIRFilterGeneric_t* filter, float lpFreq, float dT_ms) {
}

void IIRFilterInitHP(IIRFilterGeneric_t* filter, float hpFreq, float dT_ms) {
const float lambda = 1.f / TAN(3.141592653f * (hpFreq * dT_ms * 1e-3f));
const float lambda = 1.f / TAN(constPI * (hpFreq * dT_ms * 1e-3f));
const float q = SQRT(2.f);
float n0 = 1.f / (1.f + q * lambda + lambda * lambda);
float n1 = -2.f * n0 * lambda * lambda;
Expand All @@ -74,6 +74,29 @@ void IIRFilterInitHP(IIRFilterGeneric_t* filter, float hpFreq, float dT_ms) {
IIRFilterInit(filter, n0, n1, n0, 0, d1, d2, 0);
}

void IIRFilterInitBP(IIRFilterGeneric_t* filter, float centerFreq, float bandwidth, float dT_ms) {
const float Q = centerFreq / bandwidth; // Q factor
const float C = TAN(constPI * (centerFreq * dT_ms * 1e-3f));
const float D = 1.f / (1.f + C / Q + C * C);
float n0 = C / Q * D;
float n2 = -n0;
float d1 = 2.f * (C * C - 1.f) * D;
float d2 = (1.f - C / Q + C * C) * D;
IIRFilterInit(filter, n0, 0, n2, 0, d1, d2, 0);
}

void IIRFilterInitBS(IIRFilterGeneric_t* filter, float centerFreq, float bandwidth, float dT_ms) {
const float Q = centerFreq / bandwidth; // Q factor
const float C = TAN(constPI * (centerFreq * dT_ms * 1e-3f));
const float D = 1.f / (1.f + C / Q + C * C);
float n0 = (1.f + C * C) * D;
float n1 = 2.f * (C * C - 1.f) * D;
float n2 = n0;
float d1 = 2.f * (C * C - 1.f) * D;
float d2 = (1.f - C / Q + C * C) * D;
IIRFilterInit(filter, n0, n1, n2, 0, d1, d2, 0);
}

float IIRFilterProcess(IIRFilterGeneric_t* filter, float input) {
/* Apply the IIR filter equation */
float output;
Expand Down

0 comments on commit 3bfc9d6

Please sign in to comment.