如何读取txt文件,并使用其中的double值?

时间:2016-12-08 14:36:55

标签: java android text-files

我有一个像这样的.txt文件:(它实际上更长)

  5.4167707e-02   7.4330113e-02   1.3307861e-01   1.1399230e-01   9.7865982e-02  -4.5091141e-02   5.4978066e-02   1.9342541e-01   1.3783929e-01 

是字符串(一行)。我需要读取文件并在计算中使用这些值。值应该像input [i]。

到目前为止,这是我的代码:

public class MainActivity extends AppCompatActivity {

   AudioAnalyzer aa = new AudioAnalyzer();
   BufferedReader brinput = null , brbandpass = null; 
   InputStream isinput = null, isbandpass = null;
   InputStreamReader isrinput = null, isrbandpass = null;

  @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
    }

  public void analyzer (View view) throws FileNotFoundException{
        try {

            isinput = new FileInputStream("/storage/sdcard/input.txt");
            isbandpass = new FileInputStream("/storage/sdcard/bandpass.txt");

            isrinput = new InputStreamReader(isinput);
            isrbandpass = new InputStreamReader(isbandpass);

            brinput = new BufferedReader(isrinput);
            brbandpass = new BufferedReader(isrbandpass);

            //reads to the end of the stream
            int i;
            int f;
            while ((i = brinput.read()) != -1) {
                f = brbandpass.read();
                double input = (double)i; // int to double
                double filtered = (double)f;

              String working = "The filter works!";
              String notworking = "Try again. The filter does not work!";
              TextView t = (TextView) findViewById(R.id.textView);
              if (aa.analyze(in, fil) = true) // Analyze is the methode I want to use the values for
                t.setText(working);
              else
                t.setText(notworking);
              } catch (IOException e) {
            e.printStackTrace();
        }

    }

由于红线错误,我无法运行程序:f(“f”可能尚未初始化)已编辑我想将文本文件中的每个值从int转换为double类型

double filtered = (double)f;

并且在输入下,过滤(analyze(double [],double [])不能应用于(double,double)。已编辑如何将其转换为数组,以适应方法?

 if (aa.analyze(input, filtered) = true)

被修改 这是我的分析方法:

public class AudioAnalyzer {
    private IIRFilter bandpass, lowpass;
    final double[] b1 = {0.0286, 0, -0.0859, 0, 0.0859, 0, -0.0286}; 
    final double[] a1 = {-3.9320, 6.8170, -6.7455, 4.0453, -1.3889, 0.2120};

public AudioAnalyzer() {
        bandpass = new IIRFilter(b1, a1);
    }

public boolean analyze(double[] inputFilter, double[] outputFilter) {
        int i;
        boolean work;
        double[] w;
        double g1[] = new double[inputFilter.length];
        for (i = 0; i < inputFilter.length; i++) {
            double f1 = inputFilter[i];
            g1[i] = outputFilter[i]-(bandpass.filter(f1));

          for ( i = 0;i < g1.length; i++) 
                w[i] = g1[i]-outputFilter[i];
            if (w[i] == 0)
                work = true;
  return work;
  }

我的过滤器类:

public class IIRFilter {

    private final double[] b;                   
    private final double[] a;                   
    private double[] g;                         //g(n) = output
    private double[] f;                         //f(n) = input

    public IIRFilter(double[] b, double[] a) {
        this.a = a;
        this.b = b;
        g = new double[a.length];
        f = new double[b.length];
        Arrays.fill(g, 0.0);
        Arrays.fill(f, 0.0);

    }

    public double filter(double f0) {
        // change the values' positions in the buffer f
        int s;
        for (s=f.length-1;s>0; s--){
                f[s] = f[s-1];
        }
        f[0] = f0;
        //filter equation
        int n;
        double gt;
        double ht;
        ht = 0.0;
        gt = 0.0;

        for (n = 0; n < b.length; n++) {
            ht += b[n] * f[n];
        }
        for (n = 0; n < a.length; n++) {
            gt -= a[n] * g[n];
        }

        // change the values' positions in the buffer g
        for ( s=g.length; s>0; s--){
                g[s] = g[s-1];
        }
        g[0] = ht+gt;
        return g[0];
    }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试使用该代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MainActivity extends AppCompatActivity {

    AudioAnalyzer aa = new AudioAnalyzer();
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView);
    }

    public void analyzer (View view) throws FileNotFoundException{
        try {
            double[] input = readFile("/storage/sdcard/input.txt");
            double[] analyzed = readFile("/storage/sdcard/bandpass.txt");

            isWorking(aa.analyze(input, analyzed));
        catch (IOException e) {
                e.printStackTrace();
        }

    }
    private void isWorking(boolean working){
        if(working)
            textView.setText("The filter works!");
        else
            textView.setText("Try again. The filter does not work!");           
    }
    public double[] readFile(String fname) throws FileNotFoundException {


    File file = new File(fname); 
        Scanner scanner = new Scanner(new FileInputStream(file));
        List<Double> collection = new ArrayList<Double>();

        while (scanner.hasNextDouble()){
            double number = scanner.nextDouble();
            collection.add(number);
        }

        scanner.close();
        return toPrimitive(collection.toArray(new Double[collection.size()]));
    }   
    public double[] toPrimitive(Double[] array) {
        if (array == null) {
            return null;
        } else if (array.length == 0) {
            return null;
        }
        final double[] result = new double[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i].doubleValue();
        }
        return result;
    }
}

分析仪

public class AudioAnalyzer {
    private IIRFilter bandpass, lowpass;
    final double[] b1 = {0.0286, 0, -0.0859, 0, 0.0859, 0, -0.0286}; 
    final double[] a1 = {-3.9320, 6.8170, -6.7455, 4.0453, -1.3889, 0.2120};

    public AudioAnalyzer() {
        bandpass = new IIRFilter(b1, a1);
    }

    public boolean analyze(double[] inputFilter, double[] outputFilter) {
        int i;
        double g1[] = new double[inputFilter.length];

        for (i = 0; i < inputFilter.length; i++) {
            double f1 = inputFilter[i];
            g1[i] = outputFilter[i]-(bandpass.filter(f1));

        for (i = 0; i < g1.length; i++){
            if (( g1[i]-outputFilter[i] != 0))
                return false;
        }


         return true;
     }
}