采样 - java android - matlab。

时间:2016-01-07 01:10:14

标签: java android arrays matlab android-activity

所以这就是今天的伎俩。我正在读取字符串数字的文本文件数组。它大约有4000多个数字 - 基于我的压力传感器。我已经跟踪了我的代码很好,然后我做了简单的算法来找到所有的最大值。该阵列中的峰值。我在matlab中做了相同的算法,我跳过每5个元素 If(i%5 == 0){//当它大于0时找到峰值它运行得很好在matlab中,我确实找到了所有峰的局部最大值。 (但由于噪音不是不需要的峰值 - 我不需要它们)。我还通过说 if(dataOfArray> 170){//确实找到170以上的每个峰值来过滤数组。然而,我在matlab中有11个峰值而我在java android中有22个峰值。注意代码是相同的,如果有smthg错误我没有注意到。就像matlab索引从1开始,而java从0开始。我希望有人可以告诉我为什么我的java代码不像matlab那样表现。我必须得到同样的结果。希望。

 clear all
 close all
 clc
 x=load('string.txt');
 tmax=(length(x)-1)/100;
 t1_n=0:0.05:tmax;
 x4 = x(1:5:end);
 x1_n_ref=0;
 k=0;
 for i=1:length(t1_n)
     if x4(i)>170
        if x1_n_ref-x4(i)<0
           x1_n_ref=x4(i);
           alpha=1;
    elseif alpha==1 && x1_n_ref-x4(i)>0
           k=k+1;
           peak(k)=x1_n_ref;
           peak_time(k)=t1_n(i-1);
           alpha=2;
    else

       end
   else
    x1_n_ref=0;
   end

   end

  figure(1)
  hold on
  plot(t1_n,x4,'b');



 //Java code.



 //Read data.
    Recall = (Button) findViewById(R.id.Recall);
    Recall.setOnClickListener(new View.OnClickListener() {
       StringBuilder stringBuilder =  new StringBuilder();;
        boolean bTt = false;
        String line = null;
        @Override
        public void onClick(View v) {
            //readTextFile(this, R.raw.books);

            try {
                FileInputStream fIn = new FileInputStream(file);
                BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(fIn));
                while ((line = bufferedreader.readLine()) != null) {
                   stringBuilder.append(line).append(" ");
                 stringBuilder.length();
                   Toast.makeText(getApplicationContext(), "Data_Read_number:  " + stringBuilder.capacity(), Toast.LENGTH_SHORT).show();
                   // Log.i(TAG, "Capacity" + stringBuilder.capacity());
                }
                bufferedreader.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            String Adata = String.valueOf(stringBuilder);
            // find the peaks

            String dataArray[];
            dataArray = Adata.split("\\s+");
            List String_TO_List = new ArrayList<Float>();
            boolean bstart = false;
            int lastLength = dataArray.length -1;
            for(int i=0;i<lastLength;i++){
                float dataOfArray =  Float.parseFloat(dataArray[i]);
                if (dataOfArray>170){
                    bstart = true;
                }else {
                    bstart = false;
                }
                if (bstart){
                    //take String i from dataArray, convert it to a float with Float.parseFloat, then add it to your list
                    String_TO_List.add(dataOfArray);
                }

            }
            List<Integer> List_Of_Peaks = findPeaks(String_TO_List);
            // List a = findPeaks(aa);
            Log.i(TAG,"Peaks"+ List_Of_Peaks);
        //    DataAlert alert = new DataAlert();
          //  alert.show(getFragmentManager(), "DataAlert");
           // Peaks_value.setText(a.toString());
           // Toast.makeText(getApplicationContext(),
                          //  "Number of peaks:" + List_Of_Peaks, Toast.LENGTH_SHORT).show();
            Peaks_num.setText(String.valueOf(List_Of_Peaks));

        }

    });

}//end OnCreate().


public static ArrayList<Float> findPeaks(List<Float> points)
{
    if (points== null || points.size() < 1)
        return null;

    ArrayList<Float> peaks = new ArrayList<Float>();
    float x1_n_ref = 0;
    int alpha = 0;
    int size = points.size();
    for (int i = 0; i <size ; i++) {
        float IndexValues = points.get(i);
        if(i%5 == 0 ){
            if (x1_n_ref - IndexValues < 0) {
                x1_n_ref = points.get(i);
                alpha = 1;
            } else if (alpha == 1 && x1_n_ref - points.get(i) > 0) {
                peaks.add(x1_n_ref);
                alpha = 0;
            }else {
                x1_n_ref = 0;

            }
      }

    }

    return peaks;
}

0 个答案:

没有答案