Java中用于替代scipy的Butterworth带通滤波器的方法:

huangapple go评论76阅读模式
英文:

Java alternative for scipy butterworth bandpass filter

问题

我正在尝试对一些音频频率进行带通滤波,我已经成功在Python的scipy.signal模块中实现并获得了正确的结果。但我的需求是在Java中实现,以便我可以在Android中进行一些滤波。

我尝试在Java中使用berndporr/iirj库,但它返回的数据与原始数据相同。以下是使用该库的Java代码。list是一个包含音频样本的双精度数组。

double[] filteredList = new double[list.length];
Butterworth butterworth = new Butterworth();
butterworth.bandPass(3,44100,110,90);
for(int i = 0; i < list.length; i++)
{
    filteredList[i] = butterworth.filter(list[i]);
}

filteredList与我的使用scipy编写的Python程序的输出不匹配。

Python代码如下:

import numpy as np
from scipy.signal import butter, lfilter

def butter_bandpass(lowcut, highcut, fs, order=5):
    nyq = 0.5 * fs
    low = lowcut / nyq
    high = highcut / nyq
    b, a = butter(order, [low, high], btype='band')
    return b, a

def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
    b, a = butter_bandpass(lowcut, highcut, fs, order=order)
    y = lfilter(b, a, data)
    return y

arr = np.array(list)
filteredList = butter_bandpass_filter(arr, 20, 200, 44100, order=3)

这里的filteredList包含了正确的输出。我正在将结果与应用20Hz-200Hz带通滤波器的FL Studio输出进行比较。

有人可以告诉我Java代码有什么问题,或者有人可以告诉我一个更好的用于音频滤波的库吗?

英文:

I am trying to bandpass some audio frequencies and I have successfully implemented and have got the correct results from scipy.signal module in python. But my necessity is to do that in java so that I can do some filtering in android.

I tried using berndporr/iirj library in java but it returns the data as it originally was. Here is the code in java using this library. The list is a double array containing the audio samples.

double[] filteredList = new double[list.length];
Butterworth butterworth = new Butterworth();
butterworth.bandPass(3,44100,110,90);
for(int i = 0; i &lt; list.length; i++)
{
    filteredList[i] = butterworth.filter(list[i]);
}

The filteredList does not match with the output of my python program which is using scipy.

The python code is as follows:

import numpy as np
from scipy.signal import butter, filter

def butter_bandpass(lowcut, highcut, fs, order=5):
    nyq = 0.5 * fs
    low = lowcut / nyq
    high = highcut / nyq
    b, a = butter(order, [low, high], btype=&#39;band&#39;)
    return b, a

def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
    b, a = butter_bandpass(lowcut, highcut, fs, order=order)
    y = lfilter(b, a, data)
    return y

arr = np.array(list)

filteredList = butter_bandpass_filter(arr, 20, 200, 44100, order=3)

Here the filteredList contains the correct output.
I am comparing the results with output from FL studio which is applying a 20hz-200hz bandpass filter.

Can anyone tell me whats wrong with my Java code or can anyone tell me a better library for audio filtering.

答案1

得分: 1

在Android中进行音频过滤,存在一个标准的均衡器AudioEffect库,看起来可用。

顺便提一下,Java是一个不太适合信号处理的痛苦语言。如果您需要超越Android的AudioEffects的某些功能,请考虑使用Android NDK并用C++编写。

英文:

For audio filtering in Android, there is a standard equalizer AudioEffect library that looks usable.

FWIW, Java is a painful language to do signal processing in. If you need something beyond Android's AudioEffects, consider using Android NDK and writing in C++.

huangapple
  • 本文由 发表于 2020年9月12日 18:27:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63859350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定