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

Add N-order IIR Filter #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## Src
* Main
* Kotlin
* Audio Filters
* [IIRFilter](https://github.com/TheAlgorithms/Kotlin/blob/master/src/main/kotlin/audio_filters/IIRFilter.kt)
* Dynamic Programming
* [Palindromepartitioning](https://github.com/TheAlgorithms/Kotlin/blob/master/src/main/kotlin/dynamic_programming/PalindromePartitioning.kt)
* Dynamicprogramming
Expand Down Expand Up @@ -38,6 +40,8 @@
* [Quicksort](https://github.com/TheAlgorithms/Kotlin/blob/master/src/main/kotlin/sort/QuickSort.kt)
* [Selectionsort](https://github.com/TheAlgorithms/Kotlin/blob/master/src/main/kotlin/sort/SelectionSort.kt)
* Test
* Audio Filters
* [IIRFilterTest](https://github.com/TheAlgorithms/Kotlin/blob/master/src/test/kotlin/audio_filters/IIRFilterTest.kt)
* Dynamic Programming
* [Palindromepartitioningtest](https://github.com/TheAlgorithms/Kotlin/blob/master/src/test/kotlin/dynamic_programming/PalindromePartitioningTest.kt)
* Dynamicprogramming
Expand Down
62 changes: 62 additions & 0 deletions src/main/kotlin/audio_filters/IIRFilter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package audio_filters

/**
* Implement N-order IIR Filter using 1st form
* Assumes inputs are normalized to [-1, 1]
*
* @param order order of the IIR Filter
*/
class IIRFilter(val order: Int) {
private val coeffsA = MutableList(order+1) { if (it == 0) 1.0 else 0.0 }
private val coeffsB = MutableList(order+1) { if (it == 0) 1.0 else 0.0 }
private val historyX = MutableList(order) { 0.0 }
private val historyY = MutableList(order) { 0.0 }

/**
* Process a single sample
*
* @param sample Sample to process
* @return processed sample
*/
fun process(sample: Double): Double {
var result = 0.0

for (i in 1..order) {
result += coeffsB[i] * historyX[i-1] - coeffsA[i] * historyY[i-1]
}

result = (result + coeffsB[0] * sample) / coeffsA[0]

for (i in 1 until order) {
historyX[i] = historyX[i-1]
historyY[i] = historyY[i-1]
}

historyX[0] = sample
historyY[0] = result

return result
}

/**
* Set the A/B coefficients, where B is the numerator and A is the denominator
*/
fun setCoeffs(coeffsA: List<Double>, coeffsB: List<Double>) {
if (coeffsA.size != order+1) {
throw IllegalArgumentException("Expected coeffsA to have ${order+1} values for $order-order IIR filter, got ${coeffsA.size}")
}

if (coeffsA[0] == 0.0) {
throw IllegalArgumentException("coeffsA[0] cannot be 0.0!")
}

if (coeffsB.size != order+1) {
throw IllegalArgumentException("Expected coeffsA to have ${order+1} values for $order-order IIR filter, got ${coeffsB.size}")
}

for (i in 0..order) {
this.coeffsA[i] = coeffsA[i]
this.coeffsB[i] = coeffsB[i]
}
}
}
31 changes: 31 additions & 0 deletions src/test/kotlin/audio_filters/IIRFilterTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package audio_filters

import org.junit.Test

class IIRFilterTest {
@Test
fun iirFilterTestDefault() {
// Default behavior is to just return the sample
val filter = IIRFilter(10)
val sample = 0.5
assert(filter.process(sample) == sample)
}

@Test
fun iirFilterTestLowPass() {
// Butterworth low-pass filter with Fc=10_000
val filter = IIRFilter(2)
filter.setCoeffs(
listOf(1.0, -0.3075651627667613, 0.18834053593286307),
listOf(0.22019384329152544, 0.4403876865830509, 0.22019384329152544)
)

val samples = listOf(0.5, 1.0, -0.3)
val expected = listOf(0.11009692164576272, 0.47424966420914927, 0.6095534171786035)

for (i in samples.indices) {
val result = filter.process(samples[i])
assert(result == expected[i]) { "Expected ${expected[i]}, got $result" }
}
}
}