首页新闻找找看学习计划

C#代码转成java代码

0
悬赏园豆:200 [待解决问题]

public static List<Model.ZD> PinPu(string dataPath)
{
//D:﹠Storage Data﹠2016﹠04-25﹠11003﹠00-00-00.000~g~Scheduled~20~72000~6.26503E-4~-4.86564E-4~-8.47246E-8~1.18027E-4.bin
BLL.ZD manage = new BLL.ZD();
string b5 = dataPath.Split('﹠')[5].Replace(".bin", "").Split('~')[4];
string time = dataPath.Split('﹠')[2] + "-" + dataPath.Split('﹠')[3] + " " + dataPath.Split('﹠')[5].Replace(".bin", "").Split('~')[0];
string d = dataPath.Replace("﹠", "\");
string slqstr = "";// "SELECT [generateDatetime],[generateValue],[dataPath],a.[posCode],chushiValue FROM [SL] a,posinfo b where a.posCode=b.posCode and [generateDatetime]='" + timefrom + "' and a.[posCode]=" + posCode;

        FileStream fs = new FileStream(d, FileMode.Open);

        BinaryReader br = new BinaryReader(fs);
        int aaa = int.Parse(b5.ToString()) * 4;
        byte[] buffer = new byte[aaa];
        br.Read(buffer, 0, aaa);
        List<double> lists = clsReadData.ReadByteArray(buffer);
        clsReadData ReadData = new clsReadData();
        List<double> list1 = ReadData.KFftResult(lists, 1024, 4);
        double df = 50.0 / (1.0 * 1024.0);
        br.Close();
        DataTable dt = new DataTable();

        dt.Columns.Add("X");
        dt.Columns.Add("Y");
        for (int j = 0; j < list1.Count; j++)
        {
            DataRow dr = dt.NewRow();
            double X = 0, Y = 0;
            if ((j + 1) * df > 50.0 / 2) break;
            X = (j + 1) * df;
            Y = list1[j] / 1000;
            dr["X"] = X;
            dr["Y"] = Y;
            dt.Rows.Add(dr);
            //list2.Add((i + 1) * df, list1[i]);
        }

        List<Model.ZD> list = ConvertHelper<Model.ZD>.ConvertToList(dt);

        return list;
    }
问题补充:

代码其中clsReadData是以下代码,最好也转成java的

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

namespace PrjReadData
{

public struct Refilter_File_Head_T
{
    public Int32  filetype;
    public Int16 integrated;
    public Int16 version;
    public Int32 datanum;
    public Int32 filterdb;
    public Int32 resamplefre;
}
public struct RevFactor_AfterFile_Head_T
{
    public double conversionfactor;
    public double compensatingfactor;
    public double constant;
}
/// <summary>
/// 读取振动文件数据,并进行快速傅里叶分析
/// </summary>
public class clsReadData
{
    public string FileName;
    public List<double> ReadSmp()
    {
        FileStream fs = new FileStream(FileName, FileMode.Open,FileAccess.Read);
        fs.Seek(326, SeekOrigin.Begin);
        Single val1 = 0;
        BinaryReader r = new BinaryReader(fs);
        List<double> pr = new List<double>();
      
        while (fs.Position < fs.Length)
        {
            val1 = r.ReadSingle();
            pr.Add(val1);
        }
        return pr;
    }
    public static List<double> ReadByteArray(byte[] array)
    {
        List<double> list = new List<double>();
        MemoryStream stream = new MemoryStream(array);
        BinaryReader r = new BinaryReader(stream);
        int i = 0;
        Single rebuildy = 0;
        double rebuild = 0;
        while (i < stream.Length/4)//double 是八个字节
        {
            rebuildy = r.ReadSingle();
            rebuild = rebuildy;
            list.Add(rebuild);
            i++;
        }
        //FileStream dumpFile = new FileStream("Dump.dat", FileMode.Create, FileAccess.ReadWrite);
        //stream.WriteTo(dumpFile);
        //dumpFile.Close();
        stream.Dispose();
        return list;
    }

    public List<double> ReadOwner()
    {
        List<double> list = new List<double>();
        Refilter_File_Head_T head1;
        RevFactor_AfterFile_Head_T head2;
        Int32 savexstep ;//:longword;
        double rebuildy ;
        FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);

        //读文件Refilter_File_Head_T题头
        BinaryReader r = new BinaryReader(fs);
        int len = Marshal.SizeOf(typeof(Refilter_File_Head_T));
        byte[] b = r.ReadBytes(len);
        IntPtr intprt =GCHandle.Alloc(b, GCHandleType.Pinned).AddrOfPinnedObject();
        int isize = Marshal.SizeOf(typeof(Refilter_File_Head_T));
        head1 = (Refilter_File_Head_T)Marshal.PtrToStructure(intprt, typeof(Refilter_File_Head_T));
        //Marshal.FreeHGlobal(intprt);

        //读文件RevFactor_AfterFile_Head_T题头
        len = Marshal.SizeOf(typeof(RevFactor_AfterFile_Head_T));
        b = r.ReadBytes(len);
        intprt = GCHandle.Alloc(b, GCHandleType.Pinned).AddrOfPinnedObject();
        isize = Marshal.SizeOf(typeof(RevFactor_AfterFile_Head_T));
        head2 = (RevFactor_AfterFile_Head_T)Marshal.PtrToStructure(intprt, typeof(RevFactor_AfterFile_Head_T));
        //Marshal.FreeHGlobal(intprt);
        int i=0;
        while (i < head1.datanum)
        {
            savexstep = r.ReadInt32();
            rebuildy = r.ReadDouble();
            list.Add(rebuildy);
            i++;
        }
        return list;

    }
    public List<double> ReadTxt()
    {
        FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
        StreamReader r = new StreamReader(fs);
        r.BaseStream.Seek(0, SeekOrigin.Begin);
        string str = r.ReadLine();
        List<double> pr = new List<double>();
        while (str != null)
        {
            string[] str1 =str.Split(',');
            double d = Convert.ToDouble(str1[1]);//Convert.ToDouble(str);//
            if (d!=0)
                pr.Add(d);
            str = r.ReadLine();
        }
        return pr;
    }

    public List<double> HalfList(List<double> pr)
    {
        List<double> list = new List<double>();
        int HalfIndex = pr.Count / 2;
        for (int i = 0; i < HalfIndex; i++)
        {
            list.Add(pr[i]);
        }
        return list;
    }
    /// <summary>
    /// 通过快速傅里叶计算频谱
    /// </summary>
    /// <param name="pr">文件中取得数据数组</param>
    /// <param name="n">频域为2的次方,如1024,2048等</param>
    /// <param name="step">步长,就是每次移动的长度</param>
    /// <returns>返回频谱数据</returns>
    public List<Double> KFftResult(List<double> pr, int n,int step)
    {
        List<double>  pi, fr, fi;
        pi = new List<double>();
        fr = new List<double>();
        fi = new List<double>();
        int i = 0;

        List<double> prTemp = new List<double>();
        List<double> prCount = new List<double>();
        for (i = 0; i < n; i++)
        {
            prTemp.Add(0);
            prCount.Add(0);
            pi.Add(0);
            fr.Add(0);
            fi.Add(0);
        }

        int power1 = (int)Math.Truncate(Math.Log(n, 2));
        int index = 0;
        int num = 0;
        while (index + n <= pr.Count)
        {
            for (i = 0; i < n; i++)
            {
                prTemp[i] = pr[index + i];
                pi[i] = 0;
                fr[0] = 0;
                fi[0] = 0;
            }
            kfft(ref prTemp, ref pi, n, power1, ref fr, ref fi, 0, 1);

            for (i = 0; i < n; i++)
                prCount[i] = prCount[i] + 1.0 * prTemp[i];
            num++;
            index += step;
        }

        for (i = 0; i < n; i++)
        {
            prCount[i] = prCount[i] / (1.0 * num);
        }

        return HalfList(prCount);
    }

    /// <summary>
    /// 通过快速傅里叶中分裂基算法计算频谱
    /// </summary>
    /// <param name="pr">文件中取得数据数组</param>
    /// <param name="n">频域为2的次方,如1024,2048等</param>
    /// <param name="step">步长,就是每次移动的长度</param>
    /// <returns>返回频谱数据</returns>
    public List<Double> msplfftResult(List<double> pr, int n, int step)
    {
        int index = 0;
        List<complex> prTemp = new List<complex>();
        List<double> prCount = new List<double>();
        int i = 0;
        for (i = 0; i < n; i++)
        {
            prTemp.Add(new complex(0, 0));
            prCount.Add(0);
        }
        int num = 0;
        int power1 = (int)Math.Truncate(Math.Log(n, 2));
        while (index + n <= pr.Count)
        {
            for (i = 0; i < n; i++)
            {
                prTemp[i].real = pr[index + i];
                prTemp[i].imag = 0;
            }
            msplfft(ref prTemp, n, -1);


            for (i = 0; i < n; i++)
                prCount[i] = prCount[i] + 1.0 * Math.Sqrt(prTemp[i].real * prTemp[i].real + prTemp[i].imag * prTemp[i].imag);
            num++;
            index += step;
        }
        prCount[0] = prCount[n - 1];
        for (i = 0; i < n; i++)
        {
            prCount[i] = prCount[i] / (1.0 * num);
        }
        return HalfList(prCount);
    }

    public  void kfft(ref List<double> pr, ref List<double> pi, int n, int k,
                  ref List<double> fr, ref List<double> fi, int l, int il)
    {
        int it, m, is1, i, j, nv, l0;
        double p, q, s, vr, vi, poddr, poddi;
        for (it = 0; it <= n - 1; it++)
        {
            m = it; is1 = 0;
            for (i = 0; i <= k - 1; i++)
            { j = m / 2; is1 = 2 * is1 + (m - 2 * j); m = j; }
            fr[it] = pr[is1]; fi[it] = pi[is1];
        }
        pr[0] = 1.0; pi[0] = 0.0;
        p = 6.283185306 / (1.0 * n);
        pr[1] = Math.Cos(p); pi[1] = -Math.Sin(p);
        if (l != 0) pi[1] = -pi[1];
        for (i = 2; i <= n - 1; i++)
        {
            p = pr[i - 1] * pr[1]; q = pi[i - 1] * pi[1];
            s = (pr[i - 1] + pi[i - 1]) * (pr[1] + pi[1]);
            pr[i] = p - q; pi[i] = s - p - q;
        }
        for (it = 0; it <= n - 2; it = it + 2)
        {
            vr = fr[it]; vi = fi[it];
            fr[it] = vr + fr[it + 1]; fi[it] = vi + fi[it + 1];
            fr[it + 1] = vr - fr[it + 1]; fi[it + 1] = vi - fi[it + 1];
        }
        m = n / 2; nv = 2;
        for (l0 = k - 2; l0 >= 0; l0--)
        {
            m = m / 2; nv = 2 * nv;
            for (it = 0; it <= (m - 1) * nv; it = it + nv)
                for (j = 0; j <= (nv / 2) - 1; j++)
                {
                    p = pr[m * j] * fr[it + j + nv / 2];
                    q = pi[m * j] * fi[it + j + nv / 2];
                    s = pr[m * j] + pi[m * j];
                    s = s * (fr[it + j + nv / 2] + fi[it + j + nv / 2]);
                    poddr = p - q; poddi = s - p - q;
                    fr[it + j + nv / 2] = fr[it + j] - poddr;
                    fi[it + j + nv / 2] = fi[it + j] - poddi;
                    fr[it + j] = fr[it + j] + poddr;
                    fi[it + j] = fi[it + j] + poddi;
                }
        }
        if (l != 0)
            for (i = 0; i <= n - 1; i++)
            {
                fr[i] = fr[i] / (1.0 * n);
                fi[i] = fi[i] / (1.0 * n);
            }
        if (il != 0)
            for (i = 0; i <= n - 1; i++)
            {
                pr[i] = Math.Sqrt(fr[i] * fr[i] + fi[i] * fi[i]);
                if (Math.Abs(fr[i]) < 0.000001 * Math.Abs(fi[i]))
                {
                    if ((fi[i] * fr[i]) > 0) pi[i] = 90.0;
                    else pi[i] = -90.0;
                }
                else
                    pi[i] = Math.Atan(fi[i] / fr[i]) * 360.0 / 6.283185306;
            }
        pr[0] = pr[n - 1];
        return;
    }

    private void DelAVG(ref List<double> list)
    {
        double cnt = 0;
        foreach (double val in list)
            cnt += val;
        cnt = cnt / (1.0 * list.Count);
        for (int i = 0; i < list.Count; i++)
            list[i] = list[i] - cnt;

    }

    void msplfft(ref List<complex> x, int n, int isign)
    {
        /*----------------------------------------------------------------------
         Routine msplfft:to perform the split-radix DIF fft algorithm.
         input parameters:
         x : complex array.input signal is stored in x(0) to x(n-1).
         n : the dimension of x.
         isign:if isign=-1 For Forward Transform
               if isign=+1 For Inverse Transform.
         output parameters:
         x : complex array. DFT result is stored in x(0) to x(n-1).
         Notes:
             n must be power of 2.
                                                in chapter 5
        ----------------------------------------------------------------------*/
        complex xt;
        double es, e, a, a3, cc1, ss1, cc3, ss3, r1, r2, s1, s2, s3;
        int m, n2, k, n4, j, is1, id, i1, i2, i3, i0, n1, i, nn;
        xt = new complex(0, 0);
        for (m = 1; m <= 16; m++)
        {
            nn = (int)Math.Pow(2, m);
            if (n == nn) break;
        }
        if (m > 16)
        {
            //printf(" N is not a power of 2 ! \n");
            return;
        }
        n2 = n * 2;
        es = -isign * Math.Atan(1.0) * 8.0;
        for (k = 1; k < m; k++)
        {
            n2 = n2 / 2;
            n4 = n2 / 4;
            e = es / n2;
            a = 0.0;
            for (j = 0; j < n4; j++)
            {
                a3 = 3 * a;
                cc1 = Math.Cos(a);
                ss1 = Math.Sin(a);
                cc3 = Math.Cos(a3);
                ss3 = Math.Sin(a3);
                a = (j + 1) * e;
                is1 = j;
                id = 2 * n2;
                do
                {
                    for (i0 = is1; i0 < n; i0 += id)
                    {
                        i1 = i0 + n4;
                        i2 = i1 + n4;
                        i3 = i2 + n4;
                        r1 = x[i0].real - x[i2].real;
                        s1 = x[i0].imag - x[i2].imag;
                        r2 = x[i1].real - x[i3].real;
                        s2 = x[i1].imag - x[i3].imag;
                        x[i0].real += x[i2].real; x[i0].imag += x[i2].imag;
                        x[i1].real += x[i3].real; x[i1].imag += x[i3].imag;
                        if (isign != 1)
                        {
                            s3 = r1 - s2;
                            r1 += s2;
                            s2 = r2 - s1;
                            r2 += s1;
                        }
                        else
                        {
                            s3 = r1 + s2;
                            r1 = r1 - s2;
                            s2 = -r2 - s1;
                            r2 = -r2 + s1;
                        }
                        x[i2].real = r1 * cc1 - s2 * ss1;
                        x[i2].imag = -s2 * cc1 - r1 * ss1;
                        x[i3].real = s3 * cc3 + r2 * ss3;
                        x[i3].imag = r2 * cc3 - s3 * ss3;
                    }
                    is1 = 2 * id - n2 + j;
                    id = 4 * id;
                } while (is1 < n - 1);
            }
        }
        /*   ------------ special last stage -------------------------*/
        is1 = 0;
        id = 4;
        do
        {
            for (i0 = is1; i0 < n; i0 += id)
            {
                i1 = i0 + 1;
                xt.real = x[i0].real;
                xt.imag = x[i0].imag;
                x[i0].real = xt.real + x[i1].real;
                x[i0].imag = xt.imag + x[i1].imag;
                x[i1].real = xt.real - x[i1].real;
                x[i1].imag = xt.imag - x[i1].imag;
            }
            is1 = 2 * id - 2;
            id = 4 * id;
        } while (is1 < n - 1);
        j = 1;
        n1 = n - 1;
        for (i = 1; i <= n1; i++)
        {
            if (i < j)
            {
                xt.real = x[j - 1].real;
                xt.imag = x[j - 1].imag;
                x[j - 1].real = x[i - 1].real;
                x[j - 1].imag = x[i - 1].imag;
                x[i - 1].real = xt.real;
                x[i - 1].imag = xt.imag;
            }
            k = n / 2;
            do
            {
                if (k >= j)
                    break;
                j -= k;
                k /= 2;
            } while (true);
            j += k;
        }
        if (isign == -1)
            return;
        for (i = 0; i < n; i++)
        {
            x[i].real /= (float)n;
            x[i].imag /= (float)(n);
        }
    }


}

public class complex
{
    public double real;
    public double imag;
    public complex(double real, double imag)
    {
        this.real = real;
        this.imag = imag;
    }
}

}

McApp的主页 McApp | 初学一级 | 园豆:2
提问于:2019-08-08 11:19
< >
分享
所有回答(1)
0

写法都换过来了

public static List<Model.ZD> PinPu(String dataPath)
    {
//D:﹠Storage Data﹠2016﹠04-25﹠11003﹠00-00-00.000~g~Scheduled~20~72000~6.26503E-4~-4.86564E-4~-8.47246E-8~1.18027E-4.bin
        BLL.ZD manage = new ZD();
        String b5 = dataPath.split("﹠")[5].replace(".bin", "").split("~")[4];
        String time = dataPath.split("﹠")[2] + "-" + dataPath.split("﹠")[3] + " " + dataPath.split("﹠")[5].replace(".bin", "").split("~")[0];
        String d = dataPath.replace("﹠", "\\");
         String slqstr = "";// "SELECT [generateDatetime],[generateValue],[dataPath],a.[posCode],chushiValue FROM [SL] a,posinfo b where a.posCode=b.posCode and [generateDatetime]='" + timefrom + "' and a.[posCode]=" + posCode;
        FileStream fs = new FileStream(d, FileMode.Open);

        BinaryReader br = new BinaryReader(fs);
        int aaa = Integer.parseInt(b5) * 4;
        byte[] buffer = new byte[aaa];
        br.Read(buffer, 0, aaa);
        List<Double> lists = clsReadData.ReadByteArray(buffer);
        clsReadData ReadData = new clsReadData();
        List<Double> list1 = ReadData.KFftResult(lists, 1024, 4);
        double df = 50.0 / (1.0 * 1024.0);
        br.Close();
        DataTable dt = new DataTable();

        dt.Columns.Add("X");
        dt.Columns.Add("Y");
        for (int j = 0; j < list1.size(); j++)
        {
            DataRow dr = dt.NewRow();
            double X = 0, Y = 0;
            if ((j + 1) * df > 50.0 / 2) break;
            X = (j + 1) * df;
            Y = list1[j] / 1000;
            dr["X"] = X;
            dr["Y"] = Y;
            dt.Rows.Add(dr);
            //list2.Add((i + 1) * df, list1[i]);
        }

        List<Model.ZD> list = ConvertHelper<Model.ZD>.ConvertToList(dt);

        return list;
    }
小光 | 园豆:1830 (小虾三级) | 2019-08-08 14:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册