예제 #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            UdpClient udpClient = null;
            IPAddress remoteIp  = IPAddress.Parse("112.112.16.144");

            try
            {
                udpClient = new UdpClient();
                udpClient.Connect(remoteIp, 26668);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            MessagePackSerializer serializer = MessagePackSerializer.Get <AccWave>();

            AccWave aw = new AccWave("123", "acc", new float[] { 12.1F, 12.5F, 44.6F });

            byte[] result = serializer.PackSingleObject(aw);
            udpClient.Send(result, result.Length);

            MessageBox.Show("Finished");
        }
예제 #2
0
        /// <summary>
        ///   This method will be called whenever there is a new audio
        ///   frame to be processed.
        /// </summary>
        ///
        void ProcessFrame(float[,] channels, string stamp)
        {
            // We can start by converting the audio frame to a complex signal

            //Signal realSignal = Signal.FromArray(channels,WindowSize,8, 50, SampleFormat.Format32BitIeeeFloat);
            //ComplexSignal signal = ComplexSignal.FromSignal(realSignal);
            ComplexSignal signal = ComplexSignal.FromArray(channels, 50);

            // If its needed,
            if (window != null)
            {
                // Apply the chosen audio window
                signal = window.Apply(signal, 0);
            }

            // Transform to the complex domain
            signal.ForwardFourierTransform();

            // Now we can get the power spectrum output and its
            // related frequency vector to plot our spectrometer.

            double[] freqv = Tools.GetFrequencyVector(signal.Length, signal.SampleRate);

            double[][] power = new double[signal.Channels][];

            //Complex[] channel = signal.GetChannel(0);

            //double[] g = Tools.GetPowerSpectrum(channel);

            int[][] peaksIndex1 = new int[signal.Channels][];
            int[][] peaksIndex2 = new int[signal.Channels][];

            int SearchLength = 7;

            string content = stamp + ",";

            for (int i = 0; i < signal.Channels; i++)
            {
                //complexChannels[i] = signal.GetChannel(i);
                power[i] = Tools.GetPowerSpectrum(signal.GetChannel(i));

                // zero DC
                power[i][0] = 0;

                double max      = power[i].Max();
                int    position = Array.IndexOf(power[i], max);

                //normalize amplitude
                for (int n = 0; n < power[i].Length; n++)
                {
                    power[i][n] = power[i][n] / max;
                }

                if (vibrateChannels.ContainsKey(i + 1))
                {
                    VibrateChannel vc        = vibrateChannels[i + 1];
                    float[]        floatList = power[i].Select(x => (float)x).ToArray();
                    AccWave        awObject  = new AccWave(vc.SensorId, "028", floatList);
                    //byte[] result = serializer.PackSingleObject(awObject);
                    //AppendLog(this.ip + " Frame Length: " + result.Length.ToString());
                    //udpClient.Send(result, result.Length, remoteEndPoint);
                    //udpClient.Close();
                }

                //if (!isCalculateCableForce)
                //{
                //    continue;
                //}

                double maxFrequency = freqv[position];


                peaksIndex1[i] = power[i].FindPeaks();

                if (peaksIndex1[i].Length < SearchLength)
                {
                    continue;
                }

                double[] peaks2 = new double[peaksIndex1[i].Length];
                for (int j = 0; j < peaksIndex1[i].Length; j++)
                {
                    peaks2[j] = power[i][peaksIndex1[i][j]];
                    //low pass
                    //if (freqv[peaksIndex1[i][j]] > 10)
                    //{
                    //    peaks2[j] = 0;
                    //}
                }

                peaksIndex2[i] = MaxSort(SearchLength, peaks2);

                Array.Sort(peaksIndex2[i]);
            }
            udpClient.Close();

            if (isUpdateChart)
            {
                if (chart1.InvokeRequired)
                {
                    chart1.BeginInvoke(new MethodInvoker(() =>
                    {
                        for (int j = 0; j < signal.Channels; j++)
                        {
                            chart1.Series[j + 16].Points.Clear();
                            for (int i = 0; i < freqv.Length; i++)
                            {
                                chart1.Series[j + 16].Points.AddXY(freqv[i], power[j][i]);
                            }

                            if (isCalculateCableForce)
                            {
                                for (int k = 0; k < peaksIndex2[j].Length; k++)
                                {
                                    chart1.Series[j + 16].Points[peaksIndex1[j][peaksIndex2[j][k]]].Label = freqv[peaksIndex1[j][peaksIndex2[j][k]]].ToString();
                                }
                            }
                        }
                        chart1.Invalidate();
                    }));
                }
                else
                {
                    for (int j = 0; j < signal.Channels; j++)
                    {
                        chart1.Series[j + 16].Points.Clear();
                        for (int i = 0; i < freqv.Length; i++)
                        {
                            chart1.Series[j + 16].Points.AddXY(freqv[i], power[j][i]);
                        }
                    }
                    chart1.Invalidate();
                }
            }
        }
예제 #3
0
        private void BackgroundWorkerUpdateUI_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bgWorker = sender as BackgroundWorker;

            int numberOfPointsInChart        = 200;
            MessagePackSerializer serializer = MessagePackSerializer.Get <AccWave>();
            UdpClient             udpClient  = null;
            IPAddress             remoteIp   = IPAddress.Parse("192.168.100.31");

            try
            {
                udpClient = new UdpClient();
                udpClient.Connect("192.168.100.31", 26660);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            while (true)
            {
                try
                {
                    float[] data;
                    bool    success = uiQueue.TryDequeue(out data);

                    if (success)
                    {
                        AccWave aw     = new AccWave("5600001715001", "016", data);
                        byte[]  result = serializer.PackSingleObject(aw);
                        udpClient.Send(result, result.Length);

                        chart1.BeginInvoke(new MethodInvoker(() => {
                            for (int i = 0; i < 8; i++)
                            {
                                chart1.Series[i].Points.AddY(data[i]);

                                if (chart1.Series[i].Points.Count > numberOfPointsInChart)
                                {
                                    chart1.Series[i].Points.RemoveAt(0);
                                }
                            }

                            // Adjust Y & X axis scale
                            chart1.ResetAutoValues();

                            // Invalidate chart
                            chart1.Invalidate();
                        }));
                    }
                    else
                    {
                        Thread.Sleep(10);
                    }

                    if (bgWorker.CancellationPending == true)
                    {
                        e.Cancel = true;
                        udpClient.Close();
                        break;
                    }
                }
                catch (Exception ex)
                {
                    //Console.WriteLine(ex.ToString());
                    if (bgWorker.CancellationPending == true)
                    {
                        e.Cancel = true;
                        udpClient.Close();
                        break;
                    }
                }
            }
        }
예제 #4
0
        private void BackgroundWorkerSendWave_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bgWorker = sender as BackgroundWorker;

            float[][] wave = new float[8][];

            for (int j = 0; j < 8; j++)
            {
                wave[j] = new float[50];
            }

            int data_length = 0;

            while (true)
            {
                try
                {
                    float[] data;
                    bool    success = waveQueue.TryDequeue(out data);

                    if (success)
                    {
                        for (int i = 0; i < 8; i++)
                        {
                            wave[i][data_length] = data[i];
                        }
                        data_length++;
                        if (data_length > 49)
                        {
                            data_length = 0;
                            IPAddress  remoteIp       = IPAddress.Parse(this.spectrumIP);
                            IPEndPoint remoteEndPoint = new IPEndPoint(remoteIp, spectrumPort);
                            UdpClient  udpClient      = new UdpClient();

                            for (int k = 0; k < 8; k++)
                            {
                                if (vibrateChannels.ContainsKey(k + 1))
                                {
                                    AccWave awObject = new AccWave(vibrateChannels[k + 1].SensorId, "016", wave[k]);
                                    //byte[] result = serializer.PackSingleObject(awObject);
                                    //udpClient.Send(result, result.Length, remoteEndPoint);
                                }
                            }
                            udpClient.Close();
                            //AppendLog(this.ip +" Send Wave length: " + data_length.ToString());
                            //AppendLog(this.ip + " Send Wave length: " + data_length.ToString());
                        }
                    }
                    else
                    {
                        Thread.Sleep(10);
                    }

                    if (bgWorker.CancellationPending == true)
                    {
                        e.Cancel = true;
                        break;
                    }
                }
                catch (Exception ex)
                {
                    log.Error(Tag, ex);
                    if (bgWorker.CancellationPending == true)
                    {
                        e.Cancel = true;
                        break;
                    }
                }

                if (bgWorker.CancellationPending == true)
                {
                    e.Cancel = true;
                    break;
                }
            }
        }