在Android中修复跳过的帧

时间:2016-09-23 11:03:26

标签: java android multithreading android-studio signal-processing

我正在尝试开发一个Android应用程序,它从文本文件中读取输入并在其上运行算法。 我的代码如下:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alg);

    ////////////////////////////// main //////////////////////////////
    int i, j;          //loop counters
    int N;
    float productin2 = 2;
    double[] dly2 = new double[nh2];
    double sum2 = 0;
    double[] coef =  { 0, 0.01309854072, 0.06750108302, 0.1665802151, 0.252820164, 0.252820164, 0.1665802151, 0.06750108302, 0.01309854072, 0 };
    double[] hEnvs =  { 0.01579775570525054600, 0.01781403140084924000, 0.02007602712902524500, 0.02261170464938222500, 0.02545186237753081800, 0.02863036080004565900, 0.03218435360188533200, 0.03615452170070227600, 0.04058530654558931800, 0.04552513804246406700, 0.05102665129194776400, 0.05714688494379131100, 0.06394745235839295600, 0.07149467489305960700, 0.07985966446974707100, 0.08911834010325860000, 0.09935136024637487900, 0.11064394961563757000, 0.12308559557752539000, 0.13676958518580262000, 0.15179234956408211000, 0.16825257753603512000, 0.18625005525402527000, 0.20588418313000478000, 0.22725211573518173000, 0.25044646466527443000, 0.27555249889375555000, 0.30264477217428859000, 0.33178310303988118000, 0.36300783046335494000, 0.39633426806429845000, 0.43174628288460160000, 0.46918893252356092000, 0.50856010852041422000, 0.54970115646932416000, 0.59238647721601956000, 0.63631216212327169000, 0.68108378322949126000, 0.72620355172417450000, 0.77105718249523736000, 0.81490096727059447000, 0.85684977491951841000, 0.89586697826322947000, 0.93075766895485579000, 0.96016698625948826000, 0.98258597735805331000, 0.99636815747833529000, 0.99976088628954152000, 0.99095686993068455000, 0.96817259381709131000, 0.92976236102904553000, 0.87437894148283268000, 0.80119473425728838000, 0.71020093871482237000, 0.60260667694135717000, 0.48136550222324170000, 0.35186349880678341000, 0.22281151005596159000, 0.10739426873747662000, 0.02474176084283331200 };
    double sum = 0;

    double[] a_bp =  { 1.00000000000000000000, -10.74091648341686200000, 55.71763903406161900000, -184.23130974989306000000, 432.65788400377841000000, -762.10904937747364000000, 1037.22401660394640000000, -1107.35260286319680000000, 931.72411890039439000000, -614.95846638660942000000, 313.60709790142261000000, -119.95451911433545000000, 32.58814368922067200000, -5.64325562968512350000, 0.47198784101260066000, };
    double[] b_bp =  { 0.00113943092849382010, -0.01068561345428776700, 0.04726053423833388700, -0.12855488242290763000, 0.23410994143071942000, -0.28585601928705551000, 0.20343974889435235000, 0.00000000000000000000, -0.20343974889435235000, 0.28585601928705551000, -0.23410994143071942000, 0.12855488242290763000, -0.04726053423833388700, 0.01068561345428776700, -0.00113943092849382010, };

    double[] bina =  { 0.00000000000000000000, 0.58778524398803711000, 0.95105654001235962000, 0.95105654001235962000, 0.58778524398803711000, 0.000000000000000, -0.58778524398803711000, -0.95105654001235962000, -0.95105654001235962000, -0.58778524398803711000 };
    double[] nina =  { 1.00000000000000000000, 0.80901700258255005000, 0.30901700258255005000, -0.30901700258255005000, -0.80901700258255005000, -1.00000000000000000000, -0.80901700258255005000, -0.30901700258255005000, 0.30901700258255005000, 0.80901700258255005000 };
    filereader();
    j = 9;
    while (j < eFRI)
    {
        for (i = 0; i < 10; i++)
        {
            Blosi[j + i] = bina[i];
            Blosq[j + i] = nina[i];
        }
        j = j + 10;
    }

    outputiir[0] = b_bp[0] * savg1[0];
    N = M - 1;

    for (i = 1; i < N; i++)
    {
        sum = b_bp[0] * savg1[i];
        for (j = i; j >= 1; j--)
        {
            if (i - j < N && j < M)
                sum += b_bp[j] * savg1[i - j] - a_bp[j] * outputiir[i - j];
        }/*end of for*/
        outputiir[i] = sum;
        sum = 0;
    }/*end of for*/
     /* compute the outputs till nr-1*/

    for (i = 0; i < NR; i++)                //NR = 1000000-15
    {
        sum = b_bp[0] * savg1[M - 1 + i];
        for (j = 1; j <= M - 1; j++)        //M - 1 = 14
            sum += b_bp[j] * savg1[M - 1 + i - j] - a_bp[j] * outputiir[M - 1 + i - j];
        outputiir[M - 1 + i] = sum;
        sum = 0;
    }/*end for*/

    for (i = 0; i < nums; i++)
        savg1[i] = ((float)(outputiir[i]));

    for (i = 1; i < Math.floor((double)(nums / eFRI)); i++)
    {

        for (j = 0; j < eFRI; j++)
        {
            indx[j] = ((i * eFRI) - 1) + j;
            savg1[j] = savg1[j] + savg1[indx[j]];
        }
    }

    for (i = eFRI; i < nums; i++)/*atention think if you can change this num to eFRI*/
        savg1[i] = 0;

    /***********************product 1 & 2 **************************/
    for (i = 0; i < num; i++)
    {
        Blosq[i + nh1 - 1] = savg1[i] * Blosq[i + nh1 - 1];
        Blosi[i + nh1 - 1] = savg1[i] * Blosi[i + nh1 - 1];
    }/*end of for*/

    /********************FIR Filtering(lowpass) 1 & 2 **************/
    DSPF_sp_fir_gen(Blosi, coef, Blosi, nh1, num);/*lowpass FIR filter*/

    DSPF_sp_fir_gen(Blosq, coef, Blosq, nh1, num);

    for (j = 0; j < num; j++)
    {
        Blosi[j] = productin2 * Math.sqrt(Blosi[j] * Blosi[j] + Blosq[j] * Blosq[j]);
        outputiir[j] = Blosi[j]; /*env = outputiir*/
    }/*end of for*/

    /********************* match filtring ****************************/
    for (j = 0; j < (num / 5); j++)
    {
        dly2[0] = outputiir[5 * j];
        sum2 = dly2[0] * hEnvs[0];

        for (i = nh2 - 1; i > 0; i--)
        {
            sum2 += dly2[i] * hEnvs[i];
            dly2[i] = dly2[i - 1];
        }/*end of for*/

        outputiir[j] = sum2;

    }/*end of for*/

    /**************************************************************/
    int r = 0;
    sigenveval();
    r = sigfind(envpeakindx);/*output is sig_t: signals occurence time*/
    r = sigiden(sig_t, signaltlen, r);/*output is sigs: signals and their types*/
    r = sigfsync(r);/*out put is fsigs: the fine synchronization result*/
    r = sigtd(r);/*output is td : time difference between master and slaves*/
    latlong(r);
    r=1;
    TextView tv1=(TextView) findViewById(com.example.hanieh.mapapp.R.id.LngText);
    tv1.setText(Double.toString(pos[0]));

    TextView tv2=(TextView) findViewById(com.example.hanieh.mapapp.R.id.LatText);
    tv2.setText(Double.toString(pos[1]));

}

和logcat如下:

09-23 14:25:43.485 1921-1927/com.example.hanieh.mapapp W/art: Suspending all threads took: 19.306ms
09-23 14:25:55.031 1921-1927/com.example.hanieh.mapapp W/art: Suspending all threads took: 24.494ms
09-23 14:25:55.517 1921-1927/com.example.hanieh.mapapp W/art: Suspending all threads took: 9.116ms
09-23 14:26:03.621 1921-1921/com.example.hanieh.mapapp I/Choreographer: Skipped 1263 frames!  The application may be doing too much work on its main thread.

当我调试它时,我在logcat窗口中收到警告 I / Choreographer:跳过了1267帧!应用程序可能在其主线程上做了太多工作。但是当我在设备上运行应用程序时,我看不到任何问题。我的问题的答案在这里The application may be doing too much work on its main thread,我读了它,但我无法理解。任何人都可以指导我如何解决此警告并加快申请速度吗?

2 个答案:

答案 0 :(得分:1)

您在应用中使用了2种线程。第一个是UI线程,它负责用户界面。它基本上只应对用户做出反应并更新UI。

第二种是工人线程。它做繁重的工作,长时间的计算,网络下载等。如果您在 onCreate 内计算某些内容,而无需启动新主题,则用户可能会看不到该内容,因此您的应用会变得无法响应。

您有不同的选择:

  1. Java Threads,
  2. AsyncTasks,
  3. 服务,
  4. Reactive(RxJava)
  5. 对于您的具体情况,前2个可能是最好的。

答案 1 :(得分:1)

您应该使用AsyncTask进行这些类型的操作。 请通过以下链接:

http://www.compiletimeerror.com/2013/01/why-and-how-to-use-asynctask.html

在AsyncTask的doInBackground方法中执行所有文件和数据库操作。你不会得到例外。

异常的主要原因是android应用程序的所有进程都在一个主线程中完成。您正在执行的操作需要大量时间才能完成。这会阻止UI线程。因此,您将收到异常或错误。

通过使用异步任务,您的繁重操作将在单独的线程中完成。当您浏览AsyncTask的文档时,您将会发现AsyncTask的doInBackground方法在一个名为worker thread或后台线程的单独线程中运行。这使主UI线程保持活动和响应。因此,请在AsyncTask的doInBackground方法中执行所有文件,数据库,网络或任何其他时间的操作。你不会面对错误