/// <summary>
        /// Генерирование гармонического сигнала
        /// </summary>
        /// <returns></returns>
        public static double[] GetHarmonicSignal(List <HarmonicSignal> harmonics, int samplingFrequency, int modelingTime, double manualSamplingPeriod = 0)
        {
            int size = modelingTime * samplingFrequency;

            double[] result = new double[size];
            double   t, dt;

            // если период дискретизации введен вручную
            if (manualSamplingPeriod != 0)
            {
                dt = manualSamplingPeriod;
            }
            else
            {
                dt = SignalUtilities.GetSamplingPeriod(samplingFrequency);
            }

            foreach (var harmonic in harmonics)
            {
                t = 0;
                for (int j = 0; j < size; j++)
                {
                    result[j] += harmonic.amplitude * Math.Cos(DoublePi * harmonic.frequency * t - harmonic.phase);
                    t         += dt;
                }
            }

            return(result);
        }
        /// <summary>
        /// Генерирование квазистаионарного сигнала
        /// </summary>
        /// <returns></returns>
        public static double[] GetQstSignal(List <HarmonicTemplate> templates, int samplingFrequency)
        {
            int size = templates.Select(h => h.Time).Sum() * samplingFrequency;
            int segmentSize, i = 0;

            double[] result = new double[size];
            double   t, dt;

            dt = SignalUtilities.GetSamplingPeriod(samplingFrequency);

            foreach (var segment in templates)
            {
                segmentSize = (int)segment.Time * samplingFrequency;

                foreach (var harmonic in segment.Harmonics)
                {
                    t = 0;
                    for (int j = 0; j < segmentSize; j++)
                    {
                        result[j + i] += harmonic.amplitude * Math.Cos(DigitalSignalsGenerator.DoublePi * harmonic.frequency * t - harmonic.phase);
                        t             += dt;
                    }
                }
                i += segmentSize;
            }

            return(result);
        }
Пример #3
0
        /// <summary>
        /// Инициализация генератора сигналов и значений гармоник
        /// </summary>
        public void InitHarmonics()
        {
            try
            {
                totalTime = int.Parse(textBoxTime.Text);
                totalSamplingFrequency = int.Parse(textBoxFrequency.Text);

                chartModlingResults.Series.Clear();

                var rows = dataGridViewHarmonics.Rows;

                DataGridViewRow row;
                double          fr, am, ph, maxFr = double.MinValue;
                if (checkBoxUseNoize.Checked)
                {
                    maxFr = double.Parse(textBoxNoizeFrequency.Text);
                }
                harmonics.Clear();

                foreach (var r in rows)
                {
                    row = r as DataGridViewRow;
                    if (row.Cells[1].Value == null)
                    {
                        break;
                    }

                    fr = Convert.ToDouble(row.Cells[1].Value);
                    am = Convert.ToDouble(row.Cells[2].Value);
                    ph = Convert.ToDouble(row.Cells[3].Value);

                    maxFr = maxFr < fr ? fr : maxFr;

                    harmonics.Add(new DigitalSignalsGenerator.HarmonicSignal(fr, am, ph));
                }

                if (totalSamplingFrequency < maxFr * 2)
                {
                    MessageBox.Show($"Частота дискретизации должна быть как минимум в 2 раза больше максимальной частоты ({maxFr})", "Ошибка!");
                    return;
                }

                dT = SignalUtilities.GetSamplingPeriod(double.Parse(textBoxFrequency.Text));
                textBoxManualPeriod.Text = dT.ToString();
                textBoxTimeSteps.Text    = (totalTime * totalSamplingFrequency).ToString();

                var chart = chartModlingResults.ChartAreas[0];
                chart.AxisY.LabelStyle.IsEndLabelVisible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"{ex.Message} [{ex.TargetSite.Name}]");
            }
        }
Пример #4
0
        /// <summary>
        /// Моделирование шаблона квазистационарного сигнала
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonDrawQstSignalSegment_Click(object sender, EventArgs e)
        {
            try
            {
                dT = SignalUtilities.GetSamplingPeriod(double.Parse(textBoxQstDiscFr.Text));

                DrawSegment();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"{ex.Message} [{ex.TargetSite.Name}]");
            }
        }
        /// <summary>
        /// Генерация шума
        /// </summary>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static double[] GetNoize(double max, int frequency, int time, int samplingFrequency)
        {
            if (frequency < 0)
            {
                return(null);
            }

            int size = time * samplingFrequency;

            double[] result = new double[size];
            double   t = 0, dt = SignalUtilities.GetSamplingPeriod(samplingFrequency);
            var      rnd = new Random();

            for (int j = 0; j < size; j++)
            {
                result[j] += max * Math.Cos(DoublePi * frequency * t - 0);
                t         += dt;
            }
            double d;

            for (int i = 0; i < size; i++)
            {
                d = result[i] % 1;

                if (result[i] > 0)
                {
                    result[i] = rnd.Next(0, (int)result[i] + 1);
                }
                else if (result[i] < 0)
                {
                    result[i] = rnd.Next((int)result[i], 1);
                }

                if (result[i] != 0 || Math.Abs(d) < 0.3)
                {
                    result[i] += d;
                }
            }

            return(result);
        }