public void Start()
        {
            _queueStopped = false;
            Task.Run(async() => {
                bool queueEmpty      = false;
                int queueLastLength  = 0;
                int queueLengthSlope = 0;

                while (!(queueEmpty && _queueStopped))
                {
                    List <BciData> sample;
                    queueEmpty = !_queue.TryDequeue(out sample);

                    if (!queueEmpty)
                    {
                        Debug.WriteLine("Processing queue...");
                        // Update error and d_error
                        queueLengthSlope = _queue.Count - queueLastLength;
                        queueLastLength  = _queue.Count;
                        AdjustParameters(queueLastLength, queueLengthSlope);

                        // Analyse data and fire events
                        double[] xValues = new double[sample.Count];
                        for (int i = 0; i < sample.Count; ++i)
                        {
                            xValues[i] = i;
                        }

                        for (int channel = 0; channel < 8; ++channel)
                        {
                            double[] yValues = new double[sample.Count];
                            for (int i = 0; i < sample.Count; ++i)
                            {
                                yValues[i] = sample[i].ChannelData[channel] * ScaleFactor;
                            }

                            IImfDecomposition decomp = await Emd.EnsembleDecomposeAsync(xValues, yValues, 1000, _ensembleCount);
                            //var decomp = await Emd.DecomposeAsync(xValues, yValues);

                            IHilbertSpectrum spectrum = (decomp.ImfFunctions.Count == 0) ?
                                                        null : await Hsa.GetHilbertSpectrumAsync(decomp, 1.0);

                            SampleAnalysed?.Invoke(spectrum, yValues, channel);
                        }
                    }
                    else
                    {
                        await Task.Delay(100);
                    }
                }
            });
        }
        public async Task Initialize(int channelIndex)
        {
            BciData[] sampleData = DataManager.Current.LastSample;

            var channelData = new double[sampleData.Length];

            for (int i = 0; i < sampleData.Length; ++i)
            {
                channelData[i] = sampleData[i].ChannelData[channelIndex] * DataManager.ScaleFactor;
            }

            double[] xValues = new double[channelData.Length];
            for (int i = 0; i < channelData.Length; ++i)
            {
                xValues[i] = i;
            }

            Status = "Decomposing...";
            IImfDecomposition decomp = await Emd.EnsembleDecomposeAsync(xValues, channelData,
                                                                        1000, DataManager.Current.EnsembleCount);

            Status = "Analysing...";
            IHilbertSpectrum hs = await Hsa.GetHilbertSpectrumAsync(decomp, 1.0);

            Status = null;

            _xStep = hs.MaxFrequency / XLength;

            var marginalData = new ChartValues <double>();

            for (double w = 0; w <= hs.MaxFrequency; w += _xStep)
            {
                marginalData.Add(hs.ComputeMarginalAt(w));
            }

            double marginalMin = marginalData[0], marginalMax = marginalData[0], marginalMean = 0.0;

            foreach (double val in marginalData)
            {
                marginalMean += val;
                if (val < marginalMin)
                {
                    marginalMin = val;
                }
                else if (val > marginalMax)
                {
                    marginalMax = val;
                }
            }
            marginalMean /= marginalData.Count;

            Info = $"Channel = {channelIndex+1}\nMin value = {marginalMin}\nMax value = {marginalMax}\nAverage value = {marginalMean}";

            var mapper = new CartesianMapper <double>()
                         .X((value, index) => hs.MinFrequency + index * _xStep)
                         .Y((value, index) => value);

            _marginalSpectrum = new SeriesCollection(mapper)
            {
                new LineSeries {
                    Title           = "Marginal Hilbert Spectrum",
                    Values          = marginalData,
                    PointGeometry   = DefaultGeometries.None,
                    StrokeThickness = 0.5,
                    Stroke          = new SolidColorBrush(Colors.Red)
                }
            };
            OnPropertyChanged(nameof(MarginalSpectrum));
        }