コード例 #1
0
        private void OnAnalyze(object sender, EventArgs e)
        {
            btnAnalyze.IsEnabled = false;
            if (filecontents != null)
            {
                // Test FFT here first
                Spectrum sp = new Spectrum();

                N        = Int32.Parse(entryFFTSize.Text);
                classval = new double[20, N];
                int subsampling = 4;        // Freq=44100Hz/subsampling
                int maxseg      = 10;
                frames = (int)(filecontents.Length / (subsampling * N));
                byte[] tmp = new byte[N];
                //double[,] sp_re = new double[frames, N];
                //double[,] sp_im = new double[frames, N];
                spectrum = new double[frames, N];
                double[] tmp_re = new double[N];
                double[] tmp_im = new double[N];
                //double[,] sumdre = new double[maxseg,N];
                //double[,] sumdim = new double[maxseg,N];
                double[,] sumd = new double[maxseg, N];
                double[,] maxd = new double[maxseg, N];
                int[] tmp_sp = new int[N];



                int subsampl;
                subsampl = Int32.Parse(entrySubsampling.Text);


                int trsamples;
                trsamples = Int32.Parse(entrySamples.Text);


                if (chkTraining.IsChecked == true)
                {
                    // assume it is lower than trsamples .Checked below
                    curr_train++;
                    string addtmp = "(" + curr_train.ToString() + ":" + trsamples.ToString() + ")";
                    l4.Text = tmptrain + addtmp;
                }

                if (trainval == null)
                {
                    trainval = new double[N];
                    for (int i = 0; i < N; i++)
                    {
                        trainval[i] = 0;
                    }
                }
                if (sampleval == null)
                {
                    sampleval = new double[N];
                    for (int i = 0; i < N; i++)
                    {
                        sampleval[i] = 0;
                    }
                }
                if (samplemax == null)
                {
                    samplemax = new double[N];
                    for (int i = 0; i < N; i++)
                    {
                        samplemax[i] = 0;
                    }
                }

                int      frame_no = 0;
                int      seg_no = 0;
                int      col, row;
                int      pos    = 0;
                double   abssum = 0;
                string[] newrow = new string[2];
                for (int k = 0; k < maxseg; k++)
                {
                    for (int j = 0; j < N; j++)
                    {
                        //sumdre[k,j] = sumdim[k,j]=0;
                        sumd[k, j] = 0;
                        maxd[k, j] = -9999;
                    }
                }
                while (frame_no < frames)
                {
                    newrow[0] = "";
                    // more frames to FFT!!
                    for (int i = 0; i < N; i++)
                    {      // copy N bytes to tmp
                        tmp[i] = filecontents[pos];
                        pos   += subsampl;
                    }
                    sp.DFT_realIn(tmp, N, ref tmp_re, ref tmp_im, ref tmp_sp);
                    abssum = sp.DFT_check(tmp_re, tmp_im, N);
                    if (abssum < Math.Pow(10, -20))
                    {
                        // almost zero ignore subsequent zero frames and increase seg_no
                        while (abssum < Math.Pow(10, -20))
                        {
                            for (int i = 0; i < N; i++)         // copy N bytes to tmp
                            {
                                tmp[i] = filecontents[pos++];
                            }
                            sp.DFT_realIn(tmp, N, ref tmp_re, ref tmp_im, ref tmp_sp);
                            abssum = sp.DFT_check(tmp_re, tmp_im, N);
                            frame_no++;
                        }
                        seg_no++;
                        if (seg_no >= maxseg)
                        {
                            break;                      // up to maxseg frames are checked
                        }
                    }
                    else
                    if (seg_no == 0)
                    {
                        seg_no = 1;
                    }

                    double[] pm_out = new double[N];
                    if ((pickerAnalysis.SelectedIndex == 2) || (pickerAnalysis.SelectedIndex == 3))
                    {
                        // PCA

                        int Seg   = Int32.Parse(entryParam1.Text);
                        int Comps = Int32.Parse(entryParam2.Text);
                        pca.pca_extract(tmp_sp, N, Seg, Comps, ref pm_out);
                    }
                    else if ((pickerAnalysis.SelectedIndex == 4) || (pickerAnalysis.SelectedIndex == 5))
                    {
                        // Mov. Average

                        int win = Int32.Parse(entryParam1.Text);
                        av_win(tmp_sp, N, win, ref pm_out);
                    }

                    if ((pickerAnalysis.SelectedIndex == 0) || (pickerAnalysis.SelectedIndex == 1))
                    {
                        for (int i = 0; i < N; i++)
                        {
                            //sp_re[frame_no, i] = tmp_re[i];
                            //sp_im[frame_no, i] = tmp_im[i];
                            //abssum += Math.Abs(tmp_re[i]) + Math.Abs(tmp_im[i]);

                            spectrum[frame_no, i] = tmp_sp[i];
                            sumd[seg_no - 1, i]  += tmp_sp[i];
                            if (maxd[seg_no - 1, i] < tmp_sp[i])
                            {
                                maxd[seg_no - 1, i] = tmp_sp[i];
                            }

                            //sumdim[seg_no-1,i] += tmp_im[i];
                            //sumdre[seg_no-1,i] += tmp_re[i];
                            //newrow[0] += tmp_re[i].ToString()+",";
                        }
                    }
                    else if (pickerAnalysis.SelectedIndex >= 2)
                    {
                        // PCA or Mov. Average
                        for (int i = 0; i < N; i++)
                        {
                            //sp_re[frame_no, i] = tmp_re[i];
                            //sp_im[frame_no, i] = tmp_im[i];
                            //abssum += Math.Abs(tmp_re[i]) + Math.Abs(tmp_im[i]);

                            spectrum[frame_no, i] = pm_out[i];
                            sumd[seg_no - 1, i]  += pm_out[i];
                            if (maxd[seg_no - 1, i] < pm_out[i])
                            {
                                maxd[seg_no - 1, i] = pm_out[i];
                            }

                            //sumdim[seg_no-1,i] += tmp_im[i];
                            //sumdre[seg_no-1,i] += tmp_re[i];
                            //newrow[0] += tmp_re[i].ToString()+",";
                        }
                    }

                    // NPETDEBUG
                    //File.AppendAllLines("C:\\users\\nikos\\source\\repos\\Coronario21\\Coronario2\\Coronario2.UWP\\bin\\x86\\Debug\\AppX\\FFT_magn.csv", newrow);
                    frame_no++;
                }


                // Here either use tmp_sp as the original output or apply PCA or moving average



                newrow[0] = "";
                newrow[1] = "";
                for (int k = 0; k < seg_no; k++)
                {
                    double sum = 0;
                    for (int j = 0; j < N; j++)
                    {
                        //newrow[0] += sumdre[k, j].ToString() + ",";
                        sampleval[j] = sumd[k, j] / frame_no;
                        samplemax[j] = maxd[k, j];
                        sum         += sampleval[j];
                        //newrow[0] += sampleval[j].ToString() + ",";
                        //newrow[1] += maxd[k, j].ToString() + ",";
                        //newrow[1] += sumdim[k, j].ToString() + ",";

                        if ((pickerAnalysis.SelectedIndex == 1) ||
                            (pickerAnalysis.SelectedIndex == 3) || (pickerAnalysis.SelectedIndex == 5))
                        {
                            newrow[0] += samplemax[j].ToString() + ",";
                            if (chkTraining.IsChecked == true)
                            {
                                trainval[j] += samplemax[j];
                            }
                        }
                        else
                        {
                            newrow[0] += sampleval[j].ToString() + ",";
                            if (chkTraining.IsChecked == true)
                            {
                                trainval[j] += sampleval[j];
                            }
                        }
                    }
                    ;
                    cur_mean = sum / N;

                    tottrain++;

                    newrow[0] += "$$$,";
                    //newrow[1] += "$$$,";
                }
                if (chkTraining.IsChecked == true)
                {
                    if (curr_train + 1 > trsamples)
                    {
                        // stop training. Show training results. Deactivate checkbox.k
                        chkTraining.IsChecked = false;

                        for (int j = 0; j < N; j++)
                        {
                            //newrow[0] += sumdre[k, j].ToString() + ",";
                            newrow[1] += (trainval[j] / tottrain).ToString() + ",";
                        }
                        editorResults.IsVisible = false;
                        editorResults.IsEnabled = false;

                        editorResults.IsEnabled = true;
                        editorResults.IsVisible = true;
                        editorResults.Text      = newrow[1];
                    }
                }
                else
                {
                    editorResults.IsVisible = false;
                    editorResults.IsEnabled = false;

                    editorResults.IsEnabled = true;
                    editorResults.IsVisible = true;
                    editorResults.Text      = newrow[0];
                }



                btnAnalyze.IsEnabled = true;
            }
        }