两组数据通过傅里叶变换生成互相关数据

一、获取数据
我们设定两组数据量差不多都是30w的数据的dat文件,地址(string)为dataAPath,dataBPath。通过地址读取数据byte[]
代码如下:
// 通过地址读取数据
public byte[] readFile(String fileAddress){
InputStream in = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] content = null;
try {
// 一次读多个字节
byte[] tempbytes = new byte[1024];
int byteread = 0;
in = new FileInputStream(fileAddress);
// 读入多个字节到字节数组中,byteread为一次读入的字节数
while ((byteread = in.read(tempbytes)) != -1) {
baos.write(tempbytes, 0, byteread);
}
content = baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
return content;
}
二、数据处理
byte 转 double(因为java里面的byte[-127,128],转完double也是16进制)代码如下:
public double[] byte2double(byte[] data){
double[] byte2double = new double[Ad_data_size];
for(int i = 0; i < Ad_data_size; i++){
if(data[i]<0){
byte2double[i]=256+ data[i];
}else{
byte2double[i] = data[i];
}
}
return byte2double;
}
再 16进制转10进制代码如下:
public double[] sixteen2ten(double[]data){
double[] byte2double = new double[length];
for (int i = 0; i < length; i++)
{
byte2double[i]=data[2*i]256+data[2i+1];
}
return byte2double;
}
归一化处理,不然出现的数据跳跃性比较大代码如下:
public double[] unitary(double[] data)
{
double data_avg=0;
//求出平均值
for (int i = 0; i < data.length; i++){
data_avg += data[i]/(data.length);
}
//归一处理
for (int i = 0; i < data.length; i++)
data[i] = (data[i] - data_avg) / data_avg;
return data;
}
三、傅里叶相关函数处理
代码如下:
public double[] Rxy(double[] y1, double[] y2)
{
int N = y1.length;
double[] y1_2N = new double[2 * N];
double[] y2_2N = new double[2 * N];
for (int i = 0; i < N; i++)
{
y1_2N[i] = y1[i];
y2_2N[i] = y2[i];
}
for (int i = N; i < 2 * N; i++)
{
y1_2N[i] = y2_2N[i] = 0;
}
RealToComplex rtc = new RealToComplex();
Complex[] y1_C = rtc.ToComplex(y1_2N);
Complex[] y2_C = rtc.ToComplex(y2_2N);
FFT fft = new FFT();
Complex[] y1_fft = fft.fft(y1_C, 2 * N);
Complex[] y2_fft = fft.fft(y2_C, 2 * N);
Complex[] y2_conj = new Complex[2 * N];
Complex[] Sxy = new Complex[2 * N];
for (int i = 0; i < 2 * N; i++)
{
y2_fft[i].Conjugate();
y2_conj[i] = y2_fft[i];
Sxy[i] = fft.multiplication(y1_fft[i],y2_conj[i]);
}
Complex[] Sxy_ifft = fft.ifft(Sxy, 2 * N);
double[] Sxy_ifft_real = new double[2 * N];
for (int i = 0; i < 2 * N; i++)
{
Sxy_ifft_real[i] = Sxy_ifft[i].real;
}
double[] Rxy = fft.fftshift(Sxy_ifft_real);
return Rxy;
}
实数转虚数代码
public class RealToComplex {
public Complex[] ToComplex(double[] inputData)
{
int DataLength = inputData.length;
Complex[] outputData = new Complex[DataLength];
for (int i = 0; i < DataLength; i++)
{
outputData[i] = new Complex(inputData[i], 0);
}
return outputData;
}
}
虚数定义代码
public class Complex {
public double real;
public double imag;
public Complex(double real, double imaginary)
{
this.real = real;
this.imag = imaginary;
}
public void Conjugate(){
imag = -imag;
}
}
傅里叶变换代码
public Complex[] fft(Complex[] Data, int N)
{
int r = (int)(Math.log(N) / Math.log(2));
Complex[] butt1 = new Complex[N];
Complex[] butt2 = new Complex[N];
for (int i = 0;i < N;i++)
butt1[i] = Data[i];
Complex[] w = new Complex[N / 2];
for (int k = 0; k < N / 2; k++)
{
double angle = -k * Math.PI * 2 / N;
w[k] = new Complex(Math.cos(angle), Math.sin(angle));
}
for (int i = 0; i < r; i++)
{
int m = 1 << i;
int n = 1 << (r - i);
for (int j = 0; j < m; j++)
{
int index = j * n;
for (int k = 0; k < n / 2; k++)
{
butt2[index + k] = add(butt1[index + k],butt1[index + k + n / 2]);
butt2[index + k + n / 2] = multiplication(subtraction(butt1[index + k], butt1[index + k + n / 2]) , w[k * m]);
}
}
for (int j = 0;j < N;j++)
butt1[j] = butt2[j];
}
for (int j = 0; j < N; j++)
{
int rev = 0;
int num = j;
for (int i = 0; i < r; i++)
{
rev <<= 1;
rev |= num & 1;
num >>= 1;
}
butt2[rev] = butt1[j];
}
return butt2;
}
虚数乘法运算代码
public Complex multiplication(Complex sum1,Complex sum2){
Complex summ = new Complex(0,0);
summ.real = sum1.real * sum2.real - sum1.imag * sum2.imag;
summ.imag = sum1.real * sum2.imag + sum1.imag * sum2.real;
return summ;
}
fft的逆运算代码
public Complex[] ifft(Complex[] Data, int N)
{
for (int i = 0; i < N; i++)
{
//取共轭
Data[i].Conjugate();
}
Complex[] butt = new Complex[N];
butt = fft(Data, N);
for (int i = 0; i < N; i++)
{
butt[i] = real_division(butt[i] ,N);
}
return butt;
}
傅里叶数据转移
public double[] fftshift(double[] Data)
{
int dataLength = Data.length;
int harfLength;
double[] OutPut = new double[dataLength];
if (dataLength % 2 == 0)
{
harfLength = dataLength / 2;
for (int k = 0; k < harfLength; k++)
{
OutPut[k] = Data[harfLength + k];
OutPut[k + harfLength] = Data[k];
}
}
else
{
harfLength = (dataLength + 1) / 2;
OutPut[harfLength - 1] = Data[0];
for (int k = 0; k < harfLength - 1; k++)
{
OutPut[k] = Data[harfLength + k];
OutPut[k + harfLength] = Data[k + 1];
}
}
return OutPut;
}
数据结果
在这里插入图片描述

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值