Skip to content

Commit

Permalink
generate kotlin config as data class
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-f committed Jan 24, 2024
1 parent f0e142e commit f57ddf8
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ yamnet.h5
*.tflite
examples/*.ogg
.env
*.kt
# openvpn server configuration
services/ansible_openvpn/docker/openvpn-data
config.json
143 changes: 143 additions & 0 deletions examples/generate_kotlin_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# BSD 3-Clause License
#
# Copyright (c) 2023, University Gustave Eiffel
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from noisesensor.filterdesign import FilterDesign

class_source = """
data class SpectrumChannelConfiguration (
val bandpass: List<Bandpass>,
val antiAliasing: AntiAliasing,
val aWeighting: DigitalFilterConfiguration,
val cWeighting: DigitalFilterConfiguration,
val configuration: GeneralConfiguration
)
data class DigitalFilterConfiguration(
val filterDenominator: DoubleArray,
val filterNumerator: DoubleArray
)
data class AntiAliasing(
val a1: DoubleArray,
val a2: DoubleArray,
val b0: DoubleArray,
val b1: DoubleArray,
val b2: DoubleArray,
val sampleRatio: Int
)
data class Bandpass(
val centerFrequency: Double,
val maxFrequency: Double,
val minFrequency: Double,
val nominalFrequency: Double,
val subsamplingDepth: Int,
val sos: Sos,
val subsamplingFilter: SubsamplingFilter
)
data class GeneralConfiguration(
val sampleRate: Int
)
data class Sos(
val a1: DoubleArray,
val a2: DoubleArray,
val b0: DoubleArray,
val b1: DoubleArray,
val b2: DoubleArray
)
data class SubsamplingFilter(
val centerFrequency: Double,
val maxFrequency: Double,
val minFrequency: Double,
val nominalFrequency: Double,
val sos: Sos
)
"""

f = FilterDesign(sample_rate=48000, first_frequency_band=50,
last_frequency_band=16000, window_samples=48000*0.125)

f.down_sampling = f.G2
f.band_division = 3

configuration = f.generate_configuration()


def write_array(fw, array_in):
for li, val in enumerate(array_in):
if li > 0:
fw.write(",%f" % val)
else:
fw.write("%f" % val)


def dump_sos(fw, sos):
fw.write("Sos(a1=doubleArrayOf(")
write_array(fw, sos["a1"])
fw.write("), a2=doubleArrayOf(")
write_array(fw, sos["a2"])
fw.write("), b0=doubleArrayOf(")
write_array(fw, sos["b0"])
fw.write("), b1=doubleArrayOf(")
write_array(fw, sos["b1"])
fw.write("), b2=doubleArrayOf(")
write_array(fw, sos["b2"])
fw.write("))")


def main():
with open("config_%d.kt" % f.sample_rate, "w") as fw:
fw.write("fun get%dHZ() {\n" % f.sample_rate)
fw.write(
" return SpectrumChannelConfiguration(\n bandpass=listOf(")
for bi, bandpass in enumerate(configuration["bandpass"]):
if bi > 0:
fw.write(",\n ")
else:
fw.write("\n ")
fw.write("Bandpass({center_frequency},{max_frequency},"
"{min_frequency},{nominal_frequency},{subsampling_depth},"
.format(**bandpass))
dump_sos(fw, bandpass["sos"])
fw.write(
",subsamplingFilter=SubsamplingFilter({center_frequency}"
"{max_frequency},{min_frequency},{nominal_frequency},sos="
.format(**bandpass["subsampling_filter"]))
dump_sos(fw, bandpass["subsampling_filter"]["sos"])
fw.write("))")
fw.write(" ))\n")
fw.write("}\n")


main()

0 comments on commit f57ddf8

Please sign in to comment.