To conclude, an uncomplicated utility to use Scipy. It has an outstanding module for digital signals, many things not easy to find
import sys
import json
import numpy as np
from scipy import signal
def butter_lowpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = signal.butter(order, normal_cutoff, btype='low', analog=False)
return b, a
def calculate_iir_coeffs(freqData, cutoff, fs, order=5):
b, a = butter_lowpass(cutoff, fs, order)
return b, a
if __name__ == "__main__":
input_json = sys.argv[1]
params = json.loads(input_json)
freqData = params["freqData"]
cutoff = params["cutoff"]
fs = params["fs"]
order = params["order"]
b, a = calculate_iir_coeffs(freqData, cutoff, fs, order)
result = {
"b": b.tolist(),
"a": a.tolist()
}
print(json.dumps(result))
(
var output;
var result;
var jsonString;
var freqData = [0.1, 0.2, 0.3, 0.4, 0.5]; // example frequency data
var cutoff = 0.3; // normalized cutoff frequency (0.0 to 1.0, 1.0 being Nyquist frequency)
var fs = 50; // sampling frequency
var order = 5; // filter order
var pythonScriptPath = "~/scwork/python/iir_filter_coeffs.py".standardizePath;
var params = (
freqData: freqData,
cutoff: cutoff,
fs: fs,
order: order
);
jsonString = params.asJSON;
"JSON string: ".postln;
jsonString.postln;
p = Pipe.new("python " ++ pythonScriptPath ++ " '" ++ jsonString ++ "'", "r");
if (p.isOpen) {
output = p.getLine;
while ({output.notNil}) {
output.postln;
output = p.getLine;
};
p.close;
} {
"Error: Could not open pipe".postln;
}
)
{ "cutoff": 0.3, "freqData": [ 0.1, 0.2, 0.3, 0.4, 0.5 ], "fs": 50, "order": 5 }
{"b": [2.240111257258062e-09, 1.120055628629031e-08, 2.240111257258062e-08, 2.240111257258062e-08, 1.120055628629031e-08, 2.240111257258062e-09], "a": [1.0, -4.878005218496593, 9.519429131062548, -9.289979563992423, 4.533698940535282, -0.8851432174252531]}