DSP

Java调用MatLab进行FFT运算及滤波

2019-07-13 20:24发布

1、使用MatLab2015b生成Jar包时,需要使用JDK1.7,使用1.8会报错;
2、使用MatLab做FFT运算代码
Java代码:
public double[] FFT(double[] input) {
try {
MWNumericArray para = new MWNumericArray(input, MWClassID.DOUBLE);
Object[] result = tool.FFT(1, para);
MWNumericArray b = (MWNumericArray) result[0];
return b.getDoubleData();
} catch (Exception e) {
System.out.println("FFT失败!");
}
return input;
}

MatLab代码
function [ output ] = FFT( input )
N=length(input); %样点个数
output=abs(fft(input(1:N))/N*2);%真实的幅值
end
3、使用MatLab做滤波
/**
* 生成滤波器
* @param stop
* @param pass
* @return
*/
public Object getFilter(int stop, int pass) {
try {
MWNumericArray stopA = new MWNumericArray(Double.valueOf(stop), MWClassID.DOUBLE);
MWNumericArray passA = new MWNumericArray(Double.valueOf(pass), MWClassID.DOUBLE);
Object[] result = tool.GetFilter(1, stopA, passA);
return result[0];
} catch (Exception e) {
e.printStackTrace();
System.out.println("getFilter失败!");
}
return null;
}

/**
* 滤波
* @param input
* @return
*/
public double[] Filter(double[] input) {
try {
if (filter == null) {
filter = getFilter();
}
MWNumericArray para = new MWNumericArray(input, MWClassID.DOUBLE);
Object[] result = tool.Filter(1, filter, para);
MWNumericArray b = (MWNumericArray) result[0];
return b.getDoubleData();
} catch (Exception e) {
e.printStackTrace();
System.out.println("FilterHP10失败!");
}
return input;
}

MatLab代码:
function [ output ] = GetFilter( input1,input2 )
Fs = 15625;
Fstop = input1; % Stopband Frequency
Fpass = input2; % Passband Frequency
Dstop = 0.001; % Stopband Attenuation
Dpass = 0.057501127785; % Passband Ripple
dens = 20; % Density Factor
[N, Fo, Ao, W] = firpmord([Fstop, Fpass]/(Fs/2), [0 1], [Dstop, Dpass]);
b = firpm(N, Fo, Ao, W, {dens});
output = dfilt.dffir(b);
end

function [ output ] = Filter(filterimpl, input)
output=filter(filterimpl,input);
end